Commit 26fbcdf6fd1524580bdf199500d8293ce3bf6230

Thomas de Grivel 2024-01-15T19:45:00

wip

diff --git a/libc3/window/sdl2/demo/window_sdl2_demo.c b/libc3/window/sdl2/demo/window_sdl2_demo.c
index 5934648..2e92cce 100644
--- a/libc3/window/sdl2/demo/window_sdl2_demo.c
+++ b/libc3/window/sdl2/demo/window_sdl2_demo.c
@@ -204,6 +204,7 @@ bool window_sdl2_demo_load (s_window_sdl2 *window)
 
 static void render_text (s_gl_text *text, f64 x, f64 y)
 {
+  s_gl_matrix_4f matrix;
   assert(glGetError() == GL_NO_ERROR);
   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                   GL_LINEAR);
@@ -211,7 +212,7 @@ static void render_text (s_gl_text *text, f64 x, f64 y)
   glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
                       GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
   assert(glGetError() == GL_NO_ERROR);
-  gl_matrix_4f_init_identity(&g_ortho.model_matrix);
+  matrix = g_ortho.model_matrix;
   gl_matrix_4f_translate(&g_ortho.model_matrix, x - 1.0, y - 1.0, 0.0);
   gl_ortho_update_model_matrix(&g_ortho);
   glBlendColor(1.0f, 1.0f, 1.0f, 1.0f);
@@ -239,14 +240,19 @@ static void render_text (s_gl_text *text, f64 x, f64 y)
   gl_matrix_4f_translate(&g_ortho.model_matrix, 1.0, 0.0, 0.0);
   gl_ortho_update_model_matrix(&g_ortho);
   gl_ortho_text_render(&g_ortho, text);
+  assert(glGetError() == GL_NO_ERROR);
   gl_matrix_4f_translate(&g_ortho.model_matrix, 1.0, 0.0, 0.0);
   gl_ortho_update_model_matrix(&g_ortho);
   gl_ortho_text_render(&g_ortho, text);
+  assert(glGetError() == GL_NO_ERROR);
   glBlendColor(0.0f, 0.0f, 0.0f, 1.0f);
+  assert(glGetError() == GL_NO_ERROR);
   gl_matrix_4f_translate(&g_ortho.model_matrix, -1.0, -1.0, 0.0);
   gl_ortho_update_model_matrix(&g_ortho);
+  assert(glGetError() == GL_NO_ERROR);
   gl_ortho_text_render(&g_ortho, text);
   assert(glGetError() == GL_NO_ERROR);
+  g_ortho.model_matrix = matrix;
 }
 
 bool window_sdl2_demo_render (s_window_sdl2 *window)
@@ -276,6 +282,7 @@ bool window_sdl2_demo_render (s_window_sdl2 *window)
   gl_font_set_size(&g_font_courier_new, 20,
                    (f64) window->gl_h / window->h);
   gl_text_update_1(&g_text_seq_title, seq->title);
+  gl_matrix_4f_init_identity(&g_ortho.model_matrix);
   render_text(&g_text_seq_title, 20.0f, 30.0f);
   /* progress bar */
   gl_matrix_4f_init_identity(&g_ortho.model_matrix);
@@ -294,6 +301,7 @@ bool window_sdl2_demo_render (s_window_sdl2 *window)
   /* fps */
   char fps[32];
   snprintf(fps, sizeof(fps), "%.1f", (f64) seq->frame / seq->t);
+  gl_matrix_4f_init_identity(&g_ortho.model_matrix);
   gl_text_update_1(&g_text_fps, fps);
   glEnable(GL_BLEND);
   render_text(&g_text_fps, 20, window->h - 30);
diff --git a/libc3/window/sdl2/gl_ortho.c b/libc3/window/sdl2/gl_ortho.c
index ac5e2a7..e89d0be 100644
--- a/libc3/window/sdl2/gl_ortho.c
+++ b/libc3/window/sdl2/gl_ortho.c
@@ -42,10 +42,10 @@ static const char * g_gl_ortho_fragment_shader_src =
   "in vec2 TexCoord;\n"
   "out vec4 FragColor;\n"
   "uniform bool enable_texture;\n"
-  "uniform sampler2D texture2D;\n"
+  "uniform sampler2D tex;\n"
   "void main() {\n"
   "  if (enable_texture) {\n"
-  "    vec4 textureColor = texture(texture2D, TexCoord);\n"
+  "    vec4 textureColor = texture(tex, TexCoord);\n"
   "    FragColor = textureColor;\n"
   "  }\n"
   "  else\n"
@@ -174,8 +174,8 @@ void gl_ortho_rect (s_gl_ortho *ortho, f32 x, f32 y, f32 w, f32 h)
   assert(ortho);
   assert(glGetError() == GL_NO_ERROR);
   matrix = ortho->model_matrix;
-  gl_matrix_4f_translate(&ortho->model_matrix, x, y, 0.0f);
-  gl_matrix_4f_scale(&ortho->model_matrix, w, h, 1.0f);
+  gl_matrix_4f_translate(&ortho->model_matrix, x - 0.5, y - 0.5, 0.0f);
+  gl_matrix_4f_scale(&ortho->model_matrix, w + 1.0f, h + 1.0f, 1.0f);
   gl_ortho_update_model_matrix(ortho);
   assert(glGetError() == GL_NO_ERROR);
   gl_square_render(&ortho->square);
diff --git a/libc3/window/sdl2/gl_text.c b/libc3/window/sdl2/gl_text.c
index b0ee82d..4b2b2b3 100644
--- a/libc3/window/sdl2/gl_text.c
+++ b/libc3/window/sdl2/gl_text.c
@@ -101,7 +101,7 @@ bool gl_text_render_to_texture (s_gl_text *text)
     if (prev_glyph_index && glyph_index) {
       FT_Get_Kerning(font->ft_face, prev_glyph_index, glyph_index,
                      FT_KERNING_DEFAULT, &delta);
-      total_width += ceil((f32) delta.x / (1 << 6));
+      total_width += delta.x >> 6;
     }
     if (FT_Load_Glyph(font->ft_face, glyph_index, FT_LOAD_RENDER)) {
       err_write_1("gl_font_render_to_texture: failed to load glyph: ");
@@ -111,9 +111,8 @@ bool gl_text_render_to_texture (s_gl_text *text)
     }
     glyph = font->ft_face->glyph;
     total_width += glyph->bitmap.width;
-    max_height =
-      (glyph->bitmap.rows + glyph->bitmap_top > max_height) ?
-      (glyph->bitmap.rows + glyph->bitmap_top) : max_height;
+    if (glyph->bitmap.rows + glyph->bitmap_top > max_height)
+      max_height = glyph->bitmap.rows + glyph->bitmap_top;
     prev_glyph_index = glyph_index;
   }
   data_w = ceil(total_width);
diff --git a/libc3/window/sdl2/types.h b/libc3/window/sdl2/types.h
index 918be94..1c824ed 100644
--- a/libc3/window/sdl2/types.h
+++ b/libc3/window/sdl2/types.h
@@ -191,12 +191,28 @@ struct gl_point_3f {
   f32 z;
 };
 
+struct gl_sprite {
+  s_str path;
+  s_str real_path;
+  uw total_w;
+  uw total_h;
+  uw dim_x;
+  uw dim_y;
+  uw frame_count;
+  f32 pt_w;
+  f32 pt_h;
+  uw tex_w;
+  uw tex_h;
+  GLuint *texture;
+};
+
 struct gl_text {
-  s_gl_object object;
   const s_gl_font *font;
   s_str str;
-  uw w;
-  uw h;
+  uw tex_w;
+  uw tex_h;
+  f32 pt_w;
+  f32 pt_h;
   GLuint texture;
 };
 
@@ -273,22 +289,6 @@ struct gl_sphere {
   uw segments_v;
 };
 
-struct gl_sprite {
-  s_gl_object object;
-  s_str path;
-  s_str real_path;
-  uw total_w;
-  uw total_h;
-  uw dim_x;
-  uw dim_y;
-  uw frame_count;
-  uw w;
-  uw h;
-  uw tex_w;
-  uw tex_h;
-  GLuint *texture;
-};
-
 struct gl_square {
   s_gl_object object;
   uw segments_u;