Vinicius Teshima 1 рік тому
батько
коміт
2cded863d4
5 змінених файлів з 112 додано та 50 видалено
  1. 11 5
      shaders/font.frag
  2. 7 10
      shaders/font.vert
  3. 1 1
      src/app.h
  4. 21 16
      src/keybind.h
  5. 72 18
      src/main.c

+ 11 - 5
shaders/font.frag

@@ -17,6 +17,12 @@ in vec4 glyph_fg_color;
 in vec4 glyph_bg_color;
 in vec2 uv;
 
+float
+map01(float x)
+{
+  return (x + 1) / 2;
+}
+
 void main() {
   int c = glyph_c;
   if ( ! (c >= 32 && c <= 126) ) {
@@ -32,9 +38,9 @@ void main() {
   vec2 uv_ = vec2(uv.x, uv.y);
 
   vec4 t = texture(font, pos + ch_size * uv_);
-  gl_FragColor = glyph_bg_color * (1.0 - t.x) + t.x * glyph_fg_color;
-    // * vec4((sin(uv.x + time) + 1.0) / 2.0,
-    // 	   (cos(uv.y + time) + 1.0) / 2.0,
-    // 	   (sin(uv.x + uv.y + time) + 1.0) / 2.0,
-    // 	   1);
+  gl_FragColor = glyph_bg_color * (1.0 - t.x) + t.x * glyph_fg_color
+    * vec4(map01(sin(time)),
+	   map01(cos(time)),
+	   map01(sin(time)),
+	   1);
 }

+ 7 - 10
shaders/font.vert

@@ -9,11 +9,13 @@
 
 uniform float scale;
 uniform vec2 resolution;
-uniform ivec2 cursor;
+uniform float time;
+uniform vec2 camera;
 
 layout(location = 0) in ivec2 tile;
 layout(location = 1) in int c;
-layout(location = 2) in vec4 color;
+layout(location = 2) in vec4 fg_color;
+layout(location = 3) in vec4 bg_color;
 
 out vec2 uv;
 flat out int glyph_c;
@@ -22,7 +24,7 @@ out vec4 glyph_bg_color;
 
 vec2 project_point(vec2 point)
 {
-  return (2.0 * point) / resolution;
+  return (2.0 * (point - camera)) / resolution;
 }
 
 void main()
@@ -35,11 +37,6 @@ void main()
   gl_Position = vec4(project_point(uv * char_size * scale + pos), 0.0, 1.0);
 
   glyph_c = c;
-  glyph_fg_color = color;
-  glyph_bg_color = vec4(0);
-
-  if ( tile == cursor ) {
-    glyph_fg_color = vec4(0);
-    glyph_bg_color = vec4(1);
-  }
+  glyph_fg_color = fg_color;
+  glyph_bg_color = bg_color;
 }

+ 1 - 1
src/app.h

@@ -29,6 +29,7 @@ struct app {
 		int32_t resolution;
 		int32_t scale;
 		int32_t cursor;
+		int32_t camera;
 	} uniforms;
 };
 
@@ -333,7 +334,6 @@ app_calc_cur_pos(struct app app)
 		    buffer_count_char_between(app.buf, '\t',
 					      start_line, app.buf.cur));
 	col += ( tabs_n > 0 ) * ((tabs_n * app.cfg.tab_size) - tabs_n);
-
 	double row_px = (((double)row)
 			  * (app.font.ch_h * app.font.scale));
 	double col_px = (((double) col) *

+ 21 - 16
src/keybind.h

@@ -117,8 +117,10 @@ struct app
 keybind_delete_word(struct app app, union keybind_args args)
 {
 	size_t index, n_rm;
+	size_t start, end ;
 	enum buffer_err err;
-	if ( args.dir == DIR_BACKWARD ) {
+	switch ( args.dir ) {
+	case DIR_BACKWARD: {
 		RET_UNWRAP2(index, err,
 			    struct ret_size_t_err,
 			    buffer_index_bw_word(app.buf));
@@ -128,15 +130,10 @@ keybind_delete_word(struct app app, union keybind_args args)
 			}
 			return app;
 		}
-		RET_UNWRAP3(app.buf, n_rm, err,
-			    struct ret_buffer_size_t_err,
-			    buffer_remove_between(app.buf, index, app.buf.cur));
-		if ( err != BUFFER_ERR_OK ) {
-			return app;
-		}
-		app.buf.cur -= n_rm;
-	}
-	if ( args.dir == DIR_FORWARD ) {
+		start = index;
+		end = app.buf.cur;
+	} break;
+	case DIR_FORWARD: {
 		RET_UNWRAP2(index, err,
 			    struct ret_size_t_err,
 			    buffer_index_fw_word(app.buf));
@@ -146,13 +143,21 @@ keybind_delete_word(struct app app, union keybind_args args)
 			}
 			return app;
 		}
-		RET_UNWRAP3(app.buf, n_rm, err,
-			    struct ret_buffer_size_t_err,
-			    buffer_remove_between(app.buf, app.buf.cur, index));
-		if ( err != BUFFER_ERR_OK ) {
-			return app;
-		}
+		start = app.buf.cur;
+		end = index;
+	} break;
+	default:
+		fprintf(stderr, "Got invalid direction in %s: %d",
+			__func__, args.dir);
+		exit(EXIT_FAILURE);
+	}
+	RET_UNWRAP3(app.buf, n_rm, err,
+		    struct ret_buffer_size_t_err,
+		    buffer_remove_between(app.buf, start, end));
+	if ( err != BUFFER_ERR_OK ) {
+		return app;
 	}
+	app.buf.cur -= n_rm;
 	return app;
 }
 

+ 72 - 18
src/main.c

@@ -37,7 +37,8 @@ struct app handle_events(struct app app);
 struct glyph {
 	struct vec2i tile;
 	int32_t c;
-	struct vec4f color;
+	struct vec4f fg_color;
+	struct vec4f bg_color;
 };
 
 DA_DEF_STRUCT(struct glyph, glyphs);
@@ -53,7 +54,8 @@ struct glyph_attr {
 enum glyph_attr_enum {
 	GLYPH_ATTR_TILE = 0,
 	GLYPH_ATTR_C,
-	GLYPH_ATTR_COLOR,
+	GLYPH_ATTR_FG_COLOR,
+	GLYPH_ATTR_BG_COLOR,
 	GLYPH_ATTR_TOTAL
 };
 
@@ -72,12 +74,19 @@ static const struct glyph_attr glyph_attr[GLYPH_ATTR_TOTAL] = {
 		.stride = sizeof(struct glyph),
 		.offset = offsetof(struct glyph, c)
 	},
-	[GLYPH_ATTR_COLOR] = {
+	[GLYPH_ATTR_FG_COLOR] = {
 		.size = 4,
 		.type = GL_FLOAT,
 		.norm = GL_FALSE,
 		.stride = sizeof(struct glyph),
-		.offset = offsetof(struct glyph, color)
+		.offset = offsetof(struct glyph, fg_color)
+	},
+	[GLYPH_ATTR_BG_COLOR] = {
+		.size = 4,
+		.type = GL_FLOAT,
+		.norm = GL_FALSE,
+		.stride = sizeof(struct glyph),
+		.offset = offsetof(struct glyph, bg_color)
 	},
 };
 
@@ -95,10 +104,10 @@ main(int32_t argc, char **argv)
 	struct app app = app_create("ged");
 	const char *font_path = "./charmap-oldschool_white.png";
 	app.font = font_create(app.rdr, font_path, 7, 18);
-	app.font.scale = 2.0;
+	app.font.scale = 4.0;
 
 	struct glyphs glys;
-	glys.cap = 1024;
+	glys.cap = 1024 * 1024;
 	glys.size = 0;
 	glys.items = calloc(glys.cap, sizeof(struct glyph));
 
@@ -168,10 +177,11 @@ main(int32_t argc, char **argv)
 	}
 
 	/* Getting the uniforms */
-	/* app.uniforms.time = get_uniform(prog, "time"); */
+	app.uniforms.time = get_uniform(prog, "time");
 	app.uniforms.resolution = get_uniform(prog, "resolution");
 	app.uniforms.scale = get_uniform(prog, "scale");
-	app.uniforms.cursor = get_uniform(prog, "cursor");
+	app.uniforms.camera = get_uniform(prog, "camera");
+	/* app.uniforms.cursor = get_uniform(prog, "cursor"); */
 
 	uint32_t vao = 0;
 	uint32_t vbo = 0;
@@ -213,20 +223,41 @@ main(int32_t argc, char **argv)
 	enum buffer_err err_;
 	RET_UNWRAP2(app.buf, err_,
 		    struct ret_buffer_err,
-		    buffer_load_from_file(app.buf, "./m.c"));
+		    buffer_load_from_file(app.buf, "./src/main.c"));
 	if ( err_ != BUFFER_ERR_OK ) {
 		fprintf(stderr, "Failed to read load buffer: %d\n", err_);
 		exit(EXIT_FAILURE);
 	}
 
-	glys = gl_render_text(app, glys, vec2is(0));
-	glyphs_sync(app, glys);
+	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;
+	(void) dt;
+	uint32_t frame_target_dur = (uint32_t)(1000 / app.target_fps);
+
+	char *fps_text = calloc(1, 128);
+	size_t fps_text_size = 0;
+
+	uint32_t fr_end = 0;
+	uint32_t fr_start = 0;
+	uint32_t fr_dur = 1;
 
-	struct vec2i cur = app_calc_cursor(app);
-	glUniform1f(app.uniforms.scale, 5.0);
+	/* struct vec2i cur = app_calc_cursor(app); */
 	while ( app.running ) {
+		fr_start = SDL_GetTicks();
+		struct buffer *buf = &app.buf;
 		app = handle_events(app);
 
+		if ( app.show_fps == true ) {
+			fps_text_size = (size_t) snprintf(fps_text, 128,
+							  "FPS: %.2f",
+							  1000.0 / fr_dur);
+			(void) fps_text_size;
+			/* app_render_text(app, fps_text, fps_text_size, */
+			/* 		vec2s(0), 0xFF00FFFF, 5); */
+		}
+
 		glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
 		glClear(GL_COLOR_BUFFER_BIT);
 
@@ -235,12 +266,33 @@ main(int32_t argc, char **argv)
 				      (int32_t) glys.size);
 
 		/* glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); */
+		{
+			struct vec2 cur_pos = app_calc_cur_pos(app);
+			struct vec2 cpos = buf->cam.pos;
+			struct vec2 cvel = buf->cam.vel;
+			cvel = vec2_sub(cur_pos, cpos);
+			cpos = vec2_add(cpos, vec2_mul(cvel, vec2s(dt)));
+			buf->cam.pos = cpos;
+			buf->cam.vel = cvel;
+			glUniform2f(app.uniforms.camera,
+				    (float)cpos.x,
+				    (float)-cpos.y);
+		}
 
 		SDL_GL_SwapWindow(app.win.ptr);
-		/* glUniform1f(app.uniforms.time, */
-		/* 	    (float) (SDL_GetTicks() / 1000)); */
-		cur = app_calc_cursor(app);
-		glUniform2i(app.uniforms.cursor, cur.x, cur.y);
+		glUniform1f(app.uniforms.time,
+			    (float) (SDL_GetTicks() / 1000));
+		/* cur = app_calc_cursor(app); */
+		/* glUniform2i(app.uniforms.cursor, cur.x, cur.y); */
+		glys.size = 0;
+		glys = gl_render_text(app, glys, vec2is(0));
+		glyphs_sync(app, glys);
+
+		fr_end = SDL_GetTicks();
+		fr_dur = (fr_end - fr_start);
+		if ( fr_dur < frame_target_dur ) {
+			SDL_Delay(frame_target_dur - fr_dur);
+		}
 	}
 
 	app_destroy(app);
@@ -262,7 +314,8 @@ gl_render_text(struct app app, struct glyphs glys, struct vec2i tile)
 		struct glyph gly = {
 			.tile = vec2i_add(pen, vec2i(row, 0)),
 			.c = c,
-			.color = vec4fs(1)
+			.fg_color = vec4fs(1),
+			.bg_color = vec4fs(0)
 		};
 		DA_APPEND(glys, gly);
 		++row;
@@ -288,6 +341,7 @@ get_uniform(uint32_t prog, const char *name)
 		fprintf(stderr, "Failed getting %s uniform location\n", name);
 		exit(EXIT_FAILURE);
 	}
+	printf("Got %s -> %d\n", name, uni);
 	return uni;
 }