Commit c7d3bc3b33699b6ea94f22f5579ea51f8adf1f34

Thomas de Grivel 2023-12-02T22:28:49

fps

diff --git a/libc3/types.h b/libc3/types.h
index 47b3bae..79ce8f9 100644
--- a/libc3/types.h
+++ b/libc3/types.h
@@ -462,6 +462,7 @@ struct sequence {
   s_tag tag;
   f64 dt;
   f64 duration;
+  u64 frame;
   f_sequence_load load;
   f_sequence_render render;
   f64 t;
diff --git a/libc3/window/cairo/demo/window_cairo_demo.c b/libc3/window/cairo/demo/window_cairo_demo.c
index 91d02ac..4881ede 100644
--- a/libc3/window/cairo/demo/window_cairo_demo.c
+++ b/libc3/window/cairo/demo/window_cairo_demo.c
@@ -106,6 +106,32 @@ bool window_cairo_demo_load (s_window_cairo *window)
   return true;
 }
 
+static void render_text (cairo_t *cr, double x, double y,
+                         const s8 *p)
+{
+  cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
+  cairo_move_to(cr, x - 1.0, y - 1.0);
+  cairo_show_text(cr, p);
+  cairo_move_to(cr, x - 1.0, y);
+  cairo_show_text(cr, p);
+  cairo_move_to(cr, x - 1.0, y + 1.0);
+  cairo_show_text(cr, p);
+  cairo_move_to(cr, x, y - 1.0);
+  cairo_show_text(cr, p);
+  cairo_move_to(cr, x, y + 1.0);
+  cairo_show_text(cr, p);
+  cairo_move_to(cr, x + 1.0, y - 1.0);
+  cairo_show_text(cr, p);
+  cairo_move_to(cr, x + 1.0, y);
+  cairo_show_text(cr, p);
+  cairo_move_to(cr, x + 1.0, y + 1.0);
+  cairo_show_text(cr, p);
+  /* text */
+  cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
+  cairo_move_to(cr, x, y);
+  cairo_show_text(cr, p);
+}
+
 bool window_cairo_demo_render (s_window_cairo *window,
                                cairo_t *cr)
 {
@@ -121,30 +147,11 @@ bool window_cairo_demo_render (s_window_cairo *window,
   /* text */
   cairo_set_font_size(cr, 20);
   cairo_select_font_face(cr, "Courier New",
-                          CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
+                         CAIRO_FONT_SLANT_NORMAL,
+                         CAIRO_FONT_WEIGHT_NORMAL);
   cairo_text_extents(cr, seq->title, &te);
-  /* text bg */
-  cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
-  cairo_move_to(cr, 19, window->h - te.height - te.y_bearing - 21);
-  cairo_show_text(cr, seq->title);
-  cairo_move_to(cr, 19, window->h - te.height - te.y_bearing - 20);
-  cairo_show_text(cr, seq->title);
-  cairo_move_to(cr, 19, window->h - te.height - te.y_bearing - 19);
-  cairo_show_text(cr, seq->title);
-  cairo_move_to(cr, 20, window->h - te.height - te.y_bearing - 21);
-  cairo_show_text(cr, seq->title);
-  cairo_move_to(cr, 20, window->h - te.height - te.y_bearing - 19);
-  cairo_show_text(cr, seq->title);
-  cairo_move_to(cr, 21, window->h - te.height - te.y_bearing - 19);
-  cairo_show_text(cr, seq->title);
-  cairo_move_to(cr, 21, window->h - te.height - te.y_bearing - 20);
-  cairo_show_text(cr, seq->title);
-  cairo_move_to(cr, 21, window->h - te.height - te.y_bearing - 21);
-  cairo_show_text(cr, seq->title);
-  /* text */
-  cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
-  cairo_move_to(cr, 20, window->h - te.height - te.y_bearing - 20);
-  cairo_show_text(cr, seq->title);
+  render_text(cr, 20.0, window->h - te.height - te.y_bearing - 20,
+              seq->title);
   /* progress bar */
   cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
   cairo_rectangle(cr, 19, window->h - 12,
@@ -156,6 +163,11 @@ bool window_cairo_demo_render (s_window_cairo *window,
                   (window->w - 40.0) * seq->t / seq->duration,
                   2);
   cairo_fill(cr);
+  /* fps */
+  s8 fps[32];
+  snprintf(fps, sizeof(fps), "%f", (f64) seq->frame / seq->t);
+  cairo_text_extents(cr, fps, &te);
+  render_text(cr, 20.0, 20.0 + te.height, fps);
   return true;
 }
 
diff --git a/libc3/window/sdl2/demo/window_sdl2_demo.c b/libc3/window/sdl2/demo/window_sdl2_demo.c
index 3235f1c..f87caaa 100644
--- a/libc3/window/sdl2/demo/window_sdl2_demo.c
+++ b/libc3/window/sdl2/demo/window_sdl2_demo.c
@@ -142,6 +142,37 @@ bool window_sdl2_demo_load (s_window_sdl2 *window)
   return true;
 }
 
+static void render_text (s_sdl2_font *font, double x, double y,
+                         const s8 *p)
+{
+  glPushMatrix();
+  glTranslated(x, y, 0.0);
+  glPushMatrix();
+  glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
+  glTranslatef(-1.0f, -1.0f, 0.0f);
+  sdl2_font_render_text(font, p);
+  glTranslatef( 1.0f,  0.0f, 0.0f);
+  sdl2_font_render_text(font, p);
+  glTranslatef( 1.0f,  0.0f, 0.0f);
+  sdl2_font_render_text(font, p);
+  glTranslatef( 0.0f,  1.0f, 0.0f);
+  sdl2_font_render_text(font, p);
+  glTranslatef(-1.0f,  0.0f, 0.0f);
+  sdl2_font_render_text(font, p);
+  glTranslatef(-1.0f,  0.0f, 0.0f);
+  sdl2_font_render_text(font, p);
+  glTranslatef( 0.0f,  1.0f, 0.0f);
+  sdl2_font_render_text(font, p);
+  glTranslatef( 1.0f,  0.0f, 0.0f);
+  sdl2_font_render_text(font, p);
+  glTranslatef( 1.0f,  0.0f, 0.0f);
+  sdl2_font_render_text(font, p);
+  glPopMatrix();
+  glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
+  sdl2_font_render_text(font, p);
+  glPopMatrix();
+}
+
 bool window_sdl2_demo_render (s_window_sdl2 *window,
                               void *context)
 {
@@ -160,22 +191,7 @@ bool window_sdl2_demo_render (s_window_sdl2 *window,
   glLoadIdentity();
   glDisable(GL_DEPTH_TEST);
   sdl2_font_set_size(&g_font_courier_new, 20);
-  glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
-  glPushMatrix();
-  glTranslatef(19.0f, 29.0f, 0.0f);
-  sdl2_font_render_text(&g_font_courier_new, seq->title);
-  glTranslatef( 2.0f, 0.0f, 0.0f);
-  sdl2_font_render_text(&g_font_courier_new, seq->title);
-  glTranslatef( 0.0f, 2.0f, 0.0f);
-  sdl2_font_render_text(&g_font_courier_new, seq->title);
-  glTranslatef(-2.0f, 0.0f, 0.0f);
-  sdl2_font_render_text(&g_font_courier_new, seq->title);
-  glPopMatrix();
-  glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
-  glPushMatrix();
-  glTranslatef(20.0f, 30.0f, 0.0f);
-  sdl2_font_render_text(&g_font_courier_new, seq->title);
-  glPopMatrix();
+  render_text(&g_font_courier_new, 20.0f, 30.0f, seq->title);
   /* progress bar */
   glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
   glRectd(19, 11,
@@ -185,6 +201,10 @@ bool window_sdl2_demo_render (s_window_sdl2 *window,
   glRectd(20, 12,
           20 + (window->w - 40.0) * seq->t / seq->duration,
           12 + 2);
+  /* fps */
+  s8 fps[32];
+  snprintf(fps, sizeof(fps), "%f", (f64) seq->frame / seq->t);
+  render_text(&g_font_courier_new, 20, window->h - 30, fps);
   return true;
 }
 
diff --git a/libc3/window/sdl2/types.h b/libc3/window/sdl2/types.h
index 553def1..8eca1b2 100644
--- a/libc3/window/sdl2/types.h
+++ b/libc3/window/sdl2/types.h
@@ -93,13 +93,12 @@ struct window_sdl2 {
   f_window_sdl2_load   load;
   f_window_sdl2_motion motion;
   f_window_sdl2_render render;
-  void                *context;
+  SDL_Window          *sdl_window;
   f_window_sdl2_resize resize;
   s_sequence          *sequence;
   uw                   sequence_count;
   uw                   sequence_pos;
   const s8            *title;
-  SDL_Window          *sdl_window;
 };
 
 #endif /* LIBC3_WINDOW_SDL2_TYPES_H */
diff --git a/libc3/window/sdl2/window_sdl2.c b/libc3/window/sdl2/window_sdl2.c
index dfa5611..f363298 100644
--- a/libc3/window/sdl2/window_sdl2.c
+++ b/libc3/window/sdl2/window_sdl2.c
@@ -35,7 +35,6 @@ s_window_sdl2 * window_sdl2_init (s_window_sdl2 *window,
   window->motion = window_sdl2_motion_default;
   window->render = window_sdl2_render_default;
   window->resize = window_sdl2_resize_default;
-  window->context = NULL;
   window->sdl_window = NULL;
   window->sequence = calloc(sequence_count, sizeof(s_sequence));
   window->sequence_count = sequence_count;
@@ -88,10 +87,10 @@ bool window_sdl2_motion_default (s_window_sdl2 *window, sw x, sw y)
 }
 
 bool window_sdl2_render_default (s_window_sdl2 *window,
-                                 void *context)
+                                 void *sdl_window)
 {
   (void) window;
-  (void) context;
+  (void) sdl_window;
   assert(window);
   printf("window_sdl2_render_default\n");
   return true;
@@ -226,6 +225,7 @@ s_sequence * window_sdl2_sequence_init
   assert(seq);
   seq->t = 0.0;
   seq->duration = duration;
+  seq->frame = 0;
   seq->title = title;
   seq->load = (f_sequence_load) load;
   seq->render = (f_sequence_render) render;
diff --git a/libc3/window/window.c b/libc3/window/window.c
index 1b06bec..331e6d3 100644
--- a/libc3/window/window.c
+++ b/libc3/window/window.c
@@ -34,6 +34,7 @@ bool window_animate (s_window *window)
   time_sub(&clock_monotonic, &seq->t0, &delta);
   time_to_f64(&delta, &t);
   seq->dt = t - seq->t;
+  seq->frame++;
   seq->t = t;
   /* printf("window_animate: %f\n", t); */
   if (t > seq->duration &&
@@ -51,6 +52,8 @@ bool window_set_sequence_pos (s_window *window, uw sequence_pos)
   if (sequence_pos >= window->sequence_count)
     return false;
   seq = window->sequence + sequence_pos;
+  seq->dt = 0.0;
+  seq->frame = 0;
   seq->t = 0.0;
   if (clock_gettime(CLOCK_MONOTONIC, &seq->t0)) {
     warn("window_set_sequence_pos");