Просмотр исходного кода

Moving more stuff to font file

Vinicius Teshima 1 год назад
Родитель
Сommit
b88400f1ae
3 измененных файлов с 110 добавлено и 81 удалено
  1. 0 7
      src/app.h
  2. 25 67
      src/main.c
  3. 85 7
      src/tile_glyph.h

+ 0 - 7
src/app.h

@@ -24,13 +24,6 @@ struct app {
 		uint8_t tab_size;
 	} cfg;
 	bool show_fps;
-	struct {
-		int32_t time;
-		int32_t resolution;
-		int32_t scale;
-		int32_t cursor;
-		int32_t camera;
-	} uniforms;
 };
 
 struct ret_app_vec2 {

+ 25 - 67
src/main.c

@@ -32,10 +32,11 @@
 #include "config.h"
 #include "shader.h"
 #include "tile_glyph.h"
+#include "gl_util.h"
+
+struct app handle_events(struct app app, int32_t resolution_uniform);
 
-struct app handle_events(struct app app);
 
-int32_t get_uniform(uint32_t prog, const char *name);
 
 int32_t
 main(int32_t argc, char **argv)
@@ -60,45 +61,11 @@ main(int32_t argc, char **argv)
 		exit(EXIT_FAILURE);
 	}
 
-	struct tile_glyph_da glys = tile_glyph_da_create();
-
-	uint32_t vert_shader;
-	uint32_t frag_shader;
-	uint32_t prog;
-
-	/* 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);
-	}
-
-	glUseProgram(prog);
+	/* Getting the uniforms */
 
-	uint32_t font_tex = tile_glyph_load_texture_atlas(font_path);
-	(void) font_tex;
 
-	/* Getting the uniforms */
-	app.uniforms.time = get_uniform(prog, "time");
-	app.uniforms.resolution = get_uniform(prog, "resolution");
-	app.uniforms.scale = get_uniform(prog, "scale");
-	app.uniforms.camera = get_uniform(prog, "camera");
-	/* app.uniforms.cursor = get_uniform(prog, "cursor"); */
-
-	uint32_t vao = 0;
-	uint32_t vbo = 0;
-
-	/* Initialized array */
-	{
-		glGenVertexArrays(1, &vao);
-		glBindVertexArray(vao);
-		vbo = tile_glyph_gen_buffer(glys);
-		(void) vbo;
-	}
+	struct tile_glyph_render tgr = tile_glyph_render_create(font_path);
 
-	glUniform1f(app.uniforms.scale, (float)app.font.scale);
-	glUniform2f(app.uniforms.scale, 5.0, 5.0);
 
 	double dt = (1.0 / (double)app.target_fps) * 3;
 	uint32_t frame_target_dur = (uint32_t)(1000 / app.target_fps);
@@ -112,8 +79,10 @@ main(int32_t argc, char **argv)
 
 	while ( app.running ) {
 		fr_start = SDL_GetTicks();
+		glUniform1f(tgr.uniforms.scale,(float) app.font.scale);
+
 		struct buffer *buf = &app.buf;
-		app = handle_events(app);
+		app = handle_events(app, tgr.uniforms.resolution);
 
 		if ( app.show_fps == true ) {
 			fps_text_size = (size_t) snprintf(fps_text, 128,
@@ -133,17 +102,18 @@ main(int32_t argc, char **argv)
 			RET_UNWRAP2(app, cpos,
 				    struct ret_app_vec2,
 				    app_calc_buffer_cam(app, dt));
-			glUniform2f(app.uniforms.camera,
+			glUniform2f(tgr.uniforms.camera,
 				    (float)cpos.x,
 				    (float)cpos.y);
 		}
 
-		glys.size = 0;
-		glys = tile_glyph_da_calc_buffer(app, glys, vec2is(0));
-		tile_glyph_da_sync(app, glys);
+		tgr.glyphs.size = 0;
+		tgr.glyphs = tile_glyph_da_calc_buffer(app, tgr.glyphs,
+						       vec2is(0));
+		tile_glyph_da_sync(app, tgr.glyphs);
 		glDrawArraysInstanced(GL_TRIANGLE_STRIP,
 				      0, 4,
-				      (int32_t) glys.size);
+				      (int32_t) tgr.glyphs.size);
 
 		{
 			size_t col = 0;
@@ -153,21 +123,21 @@ main(int32_t argc, char **argv)
 				    buffer_calc_cur_pos(app.buf));
 
 			const char *c = buf->data.items + buf->cur;
-			glys.size = 0;
-			glys = tile_glyph_da_calc(app, glys,
-						  ( *c == '\n' ) ? " " : c, 1,
-						  vec2i((int32_t)col,
-							-(int32_t)row),
-						  vec4fs(0), vec4fs(1));
-			tile_glyph_da_sync(app, glys);
+			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);
 			glDrawArraysInstanced(GL_TRIANGLE_STRIP,
 					      0, 4,
-					      (int32_t) glys.size);
+					      (int32_t) tgr.glyphs.size);
 		}
 
 		SDL_GL_SwapWindow(app.win.ptr);
 
-		glUniform1f(app.uniforms.time,
+		glUniform1f(tgr.uniforms.time,
 			    (float) (SDL_GetTicks() / 1000));
 
 		fr_end = SDL_GetTicks();
@@ -181,20 +151,8 @@ main(int32_t argc, char **argv)
 	exit(EXIT_SUCCESS);
 }
 
-int32_t
-get_uniform(uint32_t prog, const char *name)
-{
-	int32_t uni = glGetUniformLocation(prog, name);
-	if ( uni == -1 ) {
-		fprintf(stderr, "Failed getting %s uniform location\n", name);
-		exit(EXIT_FAILURE);
-	}
-	printf("Got %s -> %d\n", name, uni);
-	return uni;
-}
-
 struct app
-handle_events(struct app app)
+handle_events(struct app app, int32_t resolution_uniform)
 {
 	SDL_Event e = {0};
 	bool done = false;
@@ -233,7 +191,7 @@ handle_events(struct app app)
 		} break;
 		case SDL_WINDOWEVENT: {
 			WINDOW_UP_SIZE(app.win);
-			glUniform2f(app.uniforms.resolution,
+			glUniform2f(resolution_uniform,
 				    (float) app.win.w,
 				    (float) app.win.h);
 			glViewport(0, 0 ,app.win.w, app.win.h);

+ 85 - 7
src/tile_glyph.h

@@ -20,11 +20,31 @@ enum tile_glyph_attr_enum {
 	TILE_GLYPH_ATTR_TOTAL
 };
 
+struct tile_glyph_render {
+	uint32_t font_tex;
+	struct {
+		uint32_t vert;
+		uint32_t frag;
+	} shaders;
+	uint32_t prog;
+	struct {
+		int32_t resolution;
+		int32_t scale;
+		int32_t time;
+		int32_t camera;
+	} uniforms;
+	uint32_t vao;
+	uint32_t vbo;
+	struct tile_glyph_da glyphs;
+};
+
 struct ret_uint32_t_uint32_t {
 	uint32_t f1;
 	uint32_t f2;
 };
 
+struct tile_glyph_render tile_glyph_render_create(const char *font_path);
+
 struct tile_glyph_da tile_glyph_da_create(void);
 const struct glyph_attr* tile_glyph_get_attrs(void);
 
@@ -38,7 +58,7 @@ 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);
-uint32_t tile_glyph_gen_buffer(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);
 
@@ -52,8 +72,58 @@ struct ret_uint32_t_uint32_t tile_glyph_compile_shaders_or_exit(void);
   #include "stb_image.h"
 #endif
 
+#include "gl_util.h"
 #include "unwrap.h"
 
+struct tile_glyph_render
+tile_glyph_render_create(const char *font_path)
+{
+	uint32_t font_tex = tile_glyph_load_texture_atlas(font_path);
+
+	struct tile_glyph_da glys = tile_glyph_da_create();
+
+	uint32_t vert_shader;
+	uint32_t frag_shader;
+	uint32_t prog;
+
+	/* 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);
+
+	glUseProgram(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");
+
+	glUniform1f(scale, 1.0f);
+
+	uint32_t vao, vbo;
+	RET_UNWRAP2(vao, vbo,
+		    struct ret_uint32_t_uint32_t, tile_glyph_gen_buffer(glys));
+
+	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,
+	};
+}
+
 struct tile_glyph_da
 tile_glyph_da_create(void)
 {
@@ -147,18 +217,26 @@ tile_glyph_da_sync(struct app app, struct tile_glyph_da glys)
 			glys.items);
 }
 
-uint32_t
+struct ret_uint32_t_uint32_t
 tile_glyph_gen_buffer(struct tile_glyph_da glys)
 {
-	uint32_t id = 0;
-	glGenBuffers(1, &id);
-	glBindBuffer(GL_ARRAY_BUFFER, id);
+	uint32_t vao = 0;
+
+	glGenVertexArrays(1, &vao);
+	glBindVertexArray(vao);
+
+	uint32_t vbo = 0;
+	glGenBuffers(1, &vbo);
+	glBindBuffer(GL_ARRAY_BUFFER, vbo);
 	glBufferData(GL_ARRAY_BUFFER,
 		     (ssize_t)(glys.cap * sizeof(struct tile_glyph)),
 		     glys.items,
 		     GL_DYNAMIC_DRAW);
 	glyph_attr_declare(tile_glyph_get_attrs(), TILE_GLYPH_ATTR_TOTAL);
-	return id;
+	return (struct ret_uint32_t_uint32_t) {
+		.f1 = vao,
+		.f2 = vbo,
+	};
 }
 
 uint32_t
@@ -191,7 +269,7 @@ tile_glyph_load_texture_atlas(const char *filepath)
 	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h,
 		     0, GL_RGBA, GL_UNSIGNED_BYTE,
 		     data);
-
+	stbi_image_free(data);
 	return id;
 }