Vinicius Teshima 1 vuosi sitten
vanhempi
sitoutus
5a5a7ae1ec
3 muutettua tiedostoa jossa 55 lisäystä ja 19 poistoa
  1. 2 1
      shaders/font.vert
  2. 1 1
      src/app.h
  3. 52 17
      src/main.c

+ 2 - 1
shaders/font.vert

@@ -33,7 +33,8 @@ void main()
 	    float((gl_VertexID >> 1) & 1));
 
   vec2 char_size = vec2(float(FONT_CH_W), float(FONT_CH_H));
-  vec2 pos = tile * char_size * scale;
+  vec2 shake = vec2(cos(time + tile.x), sin(time + tile.y)) * 0;
+  vec2 pos = tile * char_size * scale + shake;
   gl_Position = vec4(project_point(uv * char_size * scale + pos), 0.0, 1.0);
 
   glyph_c = c;

+ 1 - 1
src/app.h

@@ -334,7 +334,7 @@ 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)
+	double row_px = -(((double)row)
 			  * (app.font.ch_h * app.font.scale));
 	double col_px = (((double) col) *
 			 (app.font.ch_w * app.font.scale));

+ 52 - 17
src/main.c

@@ -90,6 +90,10 @@ static const struct glyph_attr glyph_attr[GLYPH_ATTR_TOTAL] = {
 	},
 };
 
+struct glyphs _gl_render_text(struct app app, struct glyphs glys,
+			      const char *cstr, size_t cstr_size,
+			      struct vec2i tile,
+			      struct vec4f fg_color, struct vec4f bg_color);
 struct glyphs gl_render_text(struct app app, struct glyphs glys,
 			     struct vec2i tile);
 void glyphs_sync(struct app app, struct glyphs glys);
@@ -258,14 +262,10 @@ main(int32_t argc, char **argv)
 			/* 		vec2s(0), 0xFF00FFFF, 5); */
 		}
 
-		glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
+		glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
 		glClear(GL_COLOR_BUFFER_BIT);
 
-		glDrawArraysInstanced(GL_TRIANGLE_STRIP,
-				      0, 4,
-				      (int32_t) glys.size);
-
-		/* glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); */
+		/* Calc Camera */
 		{
 			struct vec2 cur_pos = app_calc_cur_pos(app);
 			struct vec2 cpos = buf->cam.pos;
@@ -276,17 +276,40 @@ main(int32_t argc, char **argv)
 			buf->cam.vel = cvel;
 			glUniform2f(app.uniforms.camera,
 				    (float)cpos.x,
-				    (float)-cpos.y);
+				    (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); */
 		glys.size = 0;
 		glys = gl_render_text(app, glys, vec2is(0));
 		glyphs_sync(app, glys);
+		glDrawArraysInstanced(GL_TRIANGLE_STRIP,
+				      0, 4,
+				      (int32_t) glys.size);
+
+		{
+			size_t col = 0;
+			size_t row = 0;
+			RET_UNWRAP2(col, row,
+				    struct ret_size_t_size_t,
+				    buffer_calc_cur_pos(app.buf));
+
+			const char *c = buf->data.items + buf->cur;
+			glys.size = 0;
+			glys = _gl_render_text(app, glys,
+					       ( *c == '\n' ) ? " " : c, 1,
+					       vec2i((int32_t)col,
+						     -(int32_t)row),
+					       vec4fs(0), vec4fs(1));
+			glyphs_sync(app, glys);
+			glDrawArraysInstanced(GL_TRIANGLE_STRIP,
+					      0, 4,
+					      (int32_t) glys.size);
+		}
+
+		SDL_GL_SwapWindow(app.win.ptr);
+
+		glUniform1f(app.uniforms.time,
+			    (float) (SDL_GetTicks() / 1000));
 
 		fr_end = SDL_GetTicks();
 		fr_dur = (fr_end - fr_start);
@@ -300,12 +323,16 @@ main(int32_t argc, char **argv)
 }
 
 struct glyphs
-gl_render_text(struct app app, struct glyphs glys, struct vec2i tile)
+_gl_render_text(struct app app, struct glyphs glys,
+		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 < app.buf.data.size; ++i ) {
-		char c = app.buf.data.items[i];
+	for ( size_t i = 0; i < cstr_size; ++i ) {
+		char c = cstr[i];
 		if ( c == '\n' ) {
 			pen.y -= 1;
 			row = 0;
@@ -314,8 +341,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,
-			.fg_color = vec4fs(1),
-			.bg_color = vec4fs(0)
+			.fg_color = fg_color,
+			.bg_color = bg_color
 		};
 		DA_APPEND(glys, gly);
 		++row;
@@ -323,6 +350,14 @@ gl_render_text(struct app app, struct glyphs glys, struct vec2i tile)
 	return glys;
 }
 
+struct glyphs
+gl_render_text(struct app app, struct glyphs glys, struct vec2i tile)
+{
+	return _gl_render_text(app, glys,
+			       app.buf.data.items, app.buf.data.size,
+			       tile, vec4fs(1), vec4fs(0));
+}
+
 void
 glyphs_sync(struct app app, struct glyphs glys)
 {