فهرست منبع

Bettering tile_glyph_render

Vinicius Teshima 1 سال پیش
والد
کامیت
d2de79be4f
4فایلهای تغییر یافته به همراه94 افزوده شده و 115 حذف شده
  1. 1 1
      src/app.h
  2. 0 1
      src/buffer.h
  3. 16 17
      src/main.c
  4. 77 96
      src/tile_glyph.h

+ 1 - 1
src/app.h

@@ -142,7 +142,7 @@ app_destroy(struct app app)
 {
 	SDL_DestroyWindow(app.win.ptr);
 	SDL_GL_DeleteContext(app.glctx);
-	DA_DESTROY(app.buf.data);
+	free(app.buf.data.items);
 	SDL_Quit();
 }
 

+ 0 - 1
src/buffer.h

@@ -115,7 +115,6 @@ buffer_destroy(struct buffer buf)
 struct ret_buffer_err
 buffer_load_from_file(struct buffer buf, const char *filepath)
 {
-
 	void *ptr;
 	size_t file_size;
 	enum file_err err;

+ 16 - 17
src/main.c

@@ -36,8 +36,6 @@
 
 struct app handle_events(struct app app, int32_t resolution_uniform);
 
-
-
 int32_t
 main(int32_t argc, char **argv)
 {
@@ -61,11 +59,10 @@ main(int32_t argc, char **argv)
 		exit(EXIT_FAILURE);
 	}
 
-	/* Getting the uniforms */
-
-
-	struct tile_glyph_render tgr = tile_glyph_render_create(font_path);
-
+	struct tile_glyph_render tgr = tile_glyph_render_create(
+		font_path,
+		"./shaders/tile_font.vert",
+		"./shaders/tile_font.frag");
 
 	double dt = (1.0 / (double)app.target_fps) * 3;
 	uint32_t frame_target_dur = (uint32_t)(1000 / app.target_fps);
@@ -108,9 +105,8 @@ main(int32_t argc, char **argv)
 		}
 
 		tgr.glyphs.size = 0;
-		tgr.glyphs = tile_glyph_da_calc_buffer(app, tgr.glyphs,
-						       vec2is(0));
-		tile_glyph_da_sync(app, tgr.glyphs);
+		tgr = tile_glyph_render_calc_buffer(tgr, app.buf, vec2is(0));
+		tile_glyph_render_sync(tgr);
 		glDrawArraysInstanced(GL_TRIANGLE_STRIP,
 				      0, 4,
 				      (int32_t) tgr.glyphs.size);
@@ -124,12 +120,12 @@ main(int32_t argc, char **argv)
 
 			const char *c = buf->data.items + buf->cur;
 			tgr.glyphs.size = 0;
-			tgr.glyphs = tile_glyph_da_calc(app, tgr.glyphs,
-							( *c == '\n' ) ? " " : c, 1,
-							vec2i((int32_t)col,
-							      -(int32_t)row),
-							vec4fs(0), vec4fs(1));
-			tile_glyph_da_sync(app, tgr.glyphs);
+			tgr = tile_glyph_render_calc(tgr,
+						     ( *c == '\n' ) ? " " : c, 1,
+						     vec2i((int32_t)col,
+							   -(int32_t)row),
+						     vec4fs(0), vec4fs(1));
+			tile_glyph_render_sync(tgr);
 			glDrawArraysInstanced(GL_TRIANGLE_STRIP,
 					      0, 4,
 					      (int32_t) tgr.glyphs.size);
@@ -147,7 +143,7 @@ main(int32_t argc, char **argv)
 		}
 	}
 
-	app_destroy(app);
+	/* app_destroy(app); */
 	exit(EXIT_SUCCESS);
 }
 
@@ -198,6 +194,9 @@ handle_events(struct app app, int32_t resolution_uniform)
 		} break;
 		}
 	}
+	if ( app.buf.cur > app.buf.data.size ) {
+		app.buf.cur = app.buf.data.size - 1;
+	}
 
 	return app;
 }

+ 77 - 96
src/tile_glyph.h

@@ -1,6 +1,9 @@
 #ifndef TILE_GLYPH_H
 #define TILE_GLYPH_H
 
+#include "app.h"
+#include "buffer.h"
+
 #include "glyph_attr.h"
 
 struct tile_glyph {
@@ -43,24 +46,31 @@ struct ret_uint32_t_uint32_t {
 	uint32_t f2;
 };
 
-struct tile_glyph_render tile_glyph_render_create(const char *font_path);
+struct tile_glyph_render tile_glyph_render_create(const char *font_path,
+						  const char *vert_path,
+						  const char *frag_path);
 
 struct tile_glyph_da tile_glyph_da_create(void);
 const struct glyph_attr* tile_glyph_get_attrs(void);
 
-struct tile_glyph_da tile_glyph_da_calc(struct app app,
-					struct tile_glyph_da glys,
-					const char *cstr, size_t cstr_size,
-					struct vec2i tile,
-					struct vec4f fg_color,
-					struct vec4f bg_color);
-struct tile_glyph_da tile_glyph_da_calc_buffer(struct app app,
-					       struct tile_glyph_da glys,
-					       struct vec2i tile);
-void tile_glyph_da_sync(struct app app, struct tile_glyph_da glys);
-struct ret_uint32_t_uint32_t tile_glyph_gen_buffer(struct tile_glyph_da glys);
-uint32_t tile_glyph_load_texture_atlas(const char *filepath);
-struct ret_uint32_t_uint32_t tile_glyph_compile_shaders_or_exit(void);
+struct tile_glyph_render tile_glyph_render_calc(
+	struct tile_glyph_render tgr, const char *cstr, size_t cstr_size,
+	struct vec2i tile, struct vec4f fg_color, struct vec4f bg_color);
+
+struct tile_glyph_render  tile_glyph_render_calc_buffer(
+	struct tile_glyph_render tgr, struct buffer buf, struct vec2i tile);
+
+struct tile_glyph_render tile_glyph_render_sync(struct tile_glyph_render tgr);
+struct tile_glyph_render tile_glyph_render_gen_buffer(
+	struct tile_glyph_render tgr);
+
+struct tile_glyph_render tile_glyph_render_load_texture_atlas_or_exit(
+	struct tile_glyph_render tgr,
+	const char *filepath);
+
+struct tile_glyph_render tile_glyph_compile_shaders_or_exit(
+	struct tile_glyph_render tgr,
+	const char *vert_path, const char *frag_path);
 
 #if defined(TILE_GLYPH_IMP) || defined(IMP)
 
@@ -76,52 +86,30 @@ struct ret_uint32_t_uint32_t tile_glyph_compile_shaders_or_exit(void);
 #include "unwrap.h"
 
 struct tile_glyph_render
-tile_glyph_render_create(const char *font_path)
+tile_glyph_render_create(const char *font_path, const char *vert_path,
+			 const char *frag_path)
 {
-	uint32_t font_tex = tile_glyph_load_texture_atlas(font_path);
+	struct tile_glyph_render tgr = {0};
+	tgr = tile_glyph_render_load_texture_atlas_or_exit(tgr, font_path);
 
-	struct tile_glyph_da glys = tile_glyph_da_create();
-
-	uint32_t vert_shader;
-	uint32_t frag_shader;
-	uint32_t prog;
+	tgr.glyphs = tile_glyph_da_create();
 
 	/* Loading shaders and linking prog */
-	RET_UNWRAP2(vert_shader, frag_shader,
-		    struct ret_uint32_t_uint32_t,
-		    tile_glyph_compile_shaders_or_exit());
-	prog = program_link_or_exit(vert_shader, frag_shader);
+	tgr = tile_glyph_compile_shaders_or_exit(tgr, vert_path, frag_path);
+	tgr.prog = program_link_or_exit(tgr.shaders.vert, tgr.shaders.frag);
 
-	glUseProgram(prog);
+	glUseProgram(tgr.prog);
 
-	int32_t resolution = get_uniform_or_exit(prog, "resolution");
-	int32_t scale = get_uniform_or_exit(prog, "scale");
-	int32_t time = get_uniform_or_exit(prog, "time");
-	int32_t camera = get_uniform_or_exit(prog, "camera");
+	tgr.uniforms.resolution = get_uniform_or_exit(tgr.prog, "resolution");
+	tgr.uniforms.scale = get_uniform_or_exit(tgr.prog, "scale");
+	tgr.uniforms.time = get_uniform_or_exit(tgr.prog, "time");
+	tgr.uniforms.camera = get_uniform_or_exit(tgr.prog, "camera");
 
-	glUniform1f(scale, 1.0f);
+	glUniform1f(tgr.uniforms.scale, 1.0f);
 
-	uint32_t vao, vbo;
-	RET_UNWRAP2(vao, vbo,
-		    struct ret_uint32_t_uint32_t, tile_glyph_gen_buffer(glys));
+	tgr = tile_glyph_render_gen_buffer(tgr);
 
-	return (struct tile_glyph_render) {
-		.font_tex = font_tex,
-		.shaders = {
-			.vert = vert_shader,
-			.frag = frag_shader,
-		},
-		.prog = prog,
-		.uniforms = {
-			.resolution = resolution,
-			.scale = scale,
-			.time = time,
-			.camera = camera,
-		},
-		.vao = vao,
-		.vbo = vbo,
-		.glyphs = glys,
-	};
+	return tgr;
 }
 
 struct tile_glyph_da
@@ -172,12 +160,11 @@ tile_glyph_get_attrs(void)
 	return tile_glyph_attr;
 }
 
-struct tile_glyph_da
-tile_glyph_da_calc(struct app app, struct tile_glyph_da glys,
+struct tile_glyph_render
+tile_glyph_render_calc(struct tile_glyph_render tgr,
 		   const char *cstr, size_t cstr_size, struct vec2i tile,
 		   struct vec4f fg_color, struct vec4f bg_color)
 {
-	(void) app;
 	struct vec2i pen = tile;
 	int32_t row = 0;
 	for ( size_t i = 0; i < cstr_size; ++i ) {
@@ -193,56 +180,50 @@ tile_glyph_da_calc(struct app app, struct tile_glyph_da glys,
 			.fg_color = fg_color,
 			.bg_color = bg_color
 		};
-		DA_APPEND(glys, gly);
+		DA_APPEND(tgr.glyphs, gly);
 		++row;
 	}
-	return glys;
+	return tgr;
 }
 
-struct tile_glyph_da
-tile_glyph_da_calc_buffer(struct app app, struct tile_glyph_da glys,
-			  struct vec2i tile)
+struct tile_glyph_render
+tile_glyph_render_calc_buffer(struct tile_glyph_render tgr, struct buffer buf,
+			      struct vec2i tile)
 {
-	return tile_glyph_da_calc(app, glys,
-			     app.buf.data.items, app.buf.data.size,
-			     tile, vec4fs(1), vec4fs(0));
+	return tile_glyph_render_calc(tgr, buf.data.items, buf.data.size,
+				      tile, vec4fs(1), vec4fs(0));
 }
 
-void
-tile_glyph_da_sync(struct app app, struct tile_glyph_da glys)
+struct tile_glyph_render
+tile_glyph_render_sync(struct tile_glyph_render tgr)
 {
-	(void) app;
 	glBufferSubData(GL_ARRAY_BUFFER, 0,
-			(ssize_t) (glys.size * sizeof(struct tile_glyph)),
-			glys.items);
+			(ssize_t) (tgr.glyphs.size
+				   * sizeof(struct tile_glyph)),
+			tgr.glyphs.items);
+	return tgr;
 }
 
-struct ret_uint32_t_uint32_t
-tile_glyph_gen_buffer(struct tile_glyph_da glys)
+struct tile_glyph_render
+tile_glyph_render_gen_buffer(struct tile_glyph_render tgr)
 {
-	uint32_t vao = 0;
+	glGenVertexArrays(1, &tgr.vao);
+	glBindVertexArray(tgr.vao);
 
-	glGenVertexArrays(1, &vao);
-	glBindVertexArray(vao);
-
-	uint32_t vbo = 0;
-	glGenBuffers(1, &vbo);
-	glBindBuffer(GL_ARRAY_BUFFER, vbo);
+	glGenBuffers(1, &tgr.vbo);
+	glBindBuffer(GL_ARRAY_BUFFER, tgr.vbo);
 	glBufferData(GL_ARRAY_BUFFER,
-		     (ssize_t)(glys.cap * sizeof(struct tile_glyph)),
-		     glys.items,
+		     (ssize_t)(tgr.glyphs.cap * sizeof(struct tile_glyph)),
+		     tgr.glyphs.items,
 		     GL_DYNAMIC_DRAW);
 	glyph_attr_declare(tile_glyph_get_attrs(), TILE_GLYPH_ATTR_TOTAL);
-	return (struct ret_uint32_t_uint32_t) {
-		.f1 = vao,
-		.f2 = vbo,
-	};
+	return tgr;
 }
 
-uint32_t
-tile_glyph_load_texture_atlas(const char *filepath)
+struct tile_glyph_render
+tile_glyph_render_load_texture_atlas_or_exit(struct tile_glyph_render tgr,
+					     const char *filepath)
 {
-	uint32_t id;
 	int32_t w, h, n;
 	uint8_t *data = stbi_load(filepath,
 				  &w, &h, &n,
@@ -255,8 +236,8 @@ tile_glyph_load_texture_atlas(const char *filepath)
 	}
 
 	glActiveTexture(GL_TEXTURE0);
-	glGenTextures(1, &id);
-	glBindTexture(GL_TEXTURE_2D, id);
+	glGenTextures(1, &tgr.font_tex);
+	glBindTexture(GL_TEXTURE_2D, tgr.font_tex);
 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
 			GL_NEAREST);
 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
@@ -270,18 +251,18 @@ tile_glyph_load_texture_atlas(const char *filepath)
 		     0, GL_RGBA, GL_UNSIGNED_BYTE,
 		     data);
 	stbi_image_free(data);
-	return id;
+	return tgr;
 }
 
-struct ret_uint32_t_uint32_t
-tile_glyph_compile_shaders_or_exit(void)
+struct tile_glyph_render
+tile_glyph_compile_shaders_or_exit(struct tile_glyph_render tgr,
+				   const char *vert_path, const char *frag_path)
 {
-	return (struct ret_uint32_t_uint32_t) {
-		.f1 = shader_compile_file_or_exit("./shaders/tile_font.vert",
-						  GL_VERTEX_SHADER),
-		.f2 = shader_compile_file_or_exit("./shaders/tile_font.frag",
-						  GL_FRAGMENT_SHADER),
-	};
+	tgr.shaders.vert = shader_compile_file_or_exit(vert_path,
+						       GL_VERTEX_SHADER);
+	tgr.shaders.frag = shader_compile_file_or_exit(frag_path,
+						       GL_FRAGMENT_SHADER);
+	return tgr;
 }
 
 #endif /* defined(TILE_GLYPH_IMP) || defined(IMP) */