framer

Slide-show application for nerds ☝️🤓

Commit
835ecc87cfd6d2bd712dd11bba2a61831f139580
Parent
d32329805004d7d9b2a35b990b05f1913419fed6
Author
Pablo <pablo-pie@riseup.net>
Date

Refactored the code for handling lines

Diffstats

3 files changed, 95 insertions, 58 deletions

Status Name Changes Insertions Deletions
Modified src/frames.h 2 files changed 77 35
Modified src/main.c 2 files changed 5 5
Modified src/plug.c 2 files changed 13 18
diff --git a/src/frames.h b/src/frames.h
@@ -13,7 +13,7 @@
 // == Frames data-structure ===================================================
 
 typedef void (*FrameDraw)(float time, float scale);
-typedef void (*FrameTick)(float time, void *state);
+typedef void (*FrameTick)(float time, void* state);
 
 #define FRAMES_EMPTY_TICK  NULL
 #define FRAMES_EMPTY_STATE 0
@@ -37,7 +37,7 @@ typedef struct {
   Arena states;
 } Frames;
 
-void frames_next(Frames *fs)
+void frames_next(Frames* fs)
 {
   assert(fs->cursor < fs->count);
   if (fs->cursor == fs->count - 1) return;
@@ -46,7 +46,7 @@ void frames_next(Frames *fs)
   fs->time = 0;
 }
 
-void frames_prev(Frames *fs)
+void frames_prev(Frames* fs)
 {
   assert(fs->cursor < fs->count);
   if (fs->cursor == 0) return;
@@ -55,7 +55,7 @@ void frames_prev(Frames *fs)
   fs->time = 0;
 }
 
-void frames_first(Frames *fs)
+void frames_first(Frames* fs)
 {
   assert(fs->cursor < fs->count);
   if (fs->cursor == 0) return;
@@ -64,7 +64,7 @@ void frames_first(Frames *fs)
   fs->time = 0;
 }
 
-void frames_last(Frames *fs)
+void frames_last(Frames* fs)
 {
   assert(fs->cursor < fs->count);
   if (fs->count == fs->count - 1) return;
@@ -73,7 +73,7 @@ void frames_last(Frames *fs)
   fs->time = 0;
 }
 
-void frames_draw(Frames *fs, float scale)
+void frames_draw(Frames* fs, float scale)
 {
   assert(fs->cursor < fs->count);
   if (fs->count == 0) return;
@@ -86,7 +86,7 @@ void frames_draw(Frames *fs, float scale)
   EndDrawing();
 }
 
-void frames_tick(Frames *fs, float dt)
+void frames_tick(Frames* fs, float dt)
 {
   assert(fs->cursor < fs->count);
 
@@ -98,10 +98,10 @@ void frames_tick(Frames *fs, float dt)
   if (f.tick != FRAMES_EMPTY_TICK) f.tick(fs->time, f.state);
 }
 
-void frames_append(Frames *fs, FrameDraw draw, FrameTick tick,
+void frames_append(Frames* fs, FrameDraw draw, FrameTick tick,
                    size_t state_size)
 {
-  void *state = NULL;
+  void* state = NULL;
   if (state_size > 0) {
     state = arena_alloc(&fs->states, state_size);
     memset(state, 0, state_size);
@@ -116,72 +116,114 @@ void frames_append(Frames *fs, FrameDraw draw, FrameTick tick,
   nob_da_append(fs, f);
 }
 
-typedef void (*FramesLoad)(Frames *fs);
+typedef void (*FramesLoad)(Frames* fs);
 
-void frames_load(Frames *fs, FramesLoad load)
+void frames_load(Frames* fs, FramesLoad load)
 {
   fs->count = 0;
   arena_reset(&fs->states);
 
   load(fs);
 
+  assert(fs->count > 0);
+  assert(fs->items != NULL);
   if (fs->cursor >= fs->count) fs->cursor = fs->count - 1;
 }
 
 // == Text layout =============================================================
 
+typedef enum {
+  FRAMER_CENTERED,
+  FRAMER_XY,
+} FramerAlignment;
+
 typedef struct {
   const char* text;
   Color       color;
 
   size_t width;
   size_t x_offset;
-} Span;
+} FramerLineSpan;
 
 typedef struct {
-  Span*  items;
-  size_t count;
-  size_t capacity;
+  FramerLineSpan*  items;
+  size_t           count, capacity;
 
   size_t width;
   int    font_size;
-} Line;
 
-void line_append(Line *line, const char *text, Color color)
+  FramerAlignment align;
+  int             y, x;
+} FramerLine;
+
+static FramerLine framer__line = {0};
+
+void line_begin(int x, int y, int font_size)
+{
+  framer__line.count = 0;
+  framer__line.width = 0;
+
+  framer__line.align = FRAMER_XY;
+  framer__line.x = x;
+  framer__line.y = y;
+  framer__line.font_size = font_size;
+}
+
+void line_begin_centered(int y, int font_size)
+{
+  framer__line.count = 0;
+  framer__line.width = 0;
+
+  framer__line.align = FRAMER_CENTERED;
+  framer__line.y = y;
+  framer__line.font_size = font_size;
+}
+
+void line_add(const char* text, Color color)
 {
-  size_t text_width = MeasureText(text, line->font_size);
-  Span le = {
+  size_t text_width = MeasureText(text, framer__line.font_size);
+  FramerLineSpan s = {
     .text     = text,
     .color    = color,
     .width    = text_width,
-    .x_offset = line->width,
+    .x_offset = framer__line.width,
   };
 
-  nob_da_append(line, le);
-  line->width += text_width;
+  nob_da_append(&framer__line, s);
+  framer__line.width += text_width;
 }
 
-Line line(const char *text, Color color, int font_size)
+void line_end(void)
 {
-  Line line = {0};
-  line.font_size = font_size;
+  int x, y = framer__line.y;
+
+  switch (framer__line.align) {
+    case FRAMER_CENTERED:
+      x = (GetScreenWidth() - framer__line.width)/2;
+      break;
+    case FRAMER_XY:
+      x = framer__line.x;
+      break;
+  }
 
-  line_append(&line, text, color);
-  return line;
+  for (size_t i = 0; i < framer__line.count; i++) {
+    FramerLineSpan s = framer__line.items[i];
+    DrawText(s.text, x + s.x_offset, y, framer__line.font_size, s.color);
+  }
 }
 
-void line_draw(Line *line, int x, int y)
+void line(const char* text, Color color, int x, int y, int font_size)
 {
-  for (size_t i = 0; i < line->count; i++) {
-    Span le = line->items[i];
-    DrawText(le.text, x + le.x_offset, y, line->font_size, le.color);
-  }
+  line_begin(x, y, font_size);
+  line_add(text, color);
+  line_end();
 }
 
-void line_draw_centered(Line *line, int y)
+void line_centered(const char* text, Color color, int y, int font_size)
 {
-  int x = (GetScreenWidth() - line->width)/2;
-  line_draw(line, x, y);
+  line_begin_centered(y, font_size);
+  line_add(text, color);
+  line_end();
 }
 
 #endif // FRAMES_H_
diff --git a/src/main.c b/src/main.c
@@ -8,10 +8,10 @@
 #define MONITOR_WIDTH  1366
 #define MONITOR_HEIGHT 768
 
-static void *libplug = NULL;
-void (*plug_load_frames)(Frames *fs);
+static void* libplug = NULL;
+void (*plug_load_frames)(Frames* fs);
 
-bool reload_libplug(char *libplug_path)
+bool reload_libplug(char* libplug_path)
 {
   if (libplug != NULL) dlclose(libplug);
 
@@ -22,7 +22,7 @@ bool reload_libplug(char *libplug_path)
     return false;
   }
 
-  const char *symbol = "plug_load_frames";
+  const char* symbol = "plug_load_frames";
   plug_load_frames = dlsym(libplug, symbol);
   if (plug_load_frames == NULL) {
     TraceLog(LOG_ERROR, "HOTRELOAD: could not find %s symbol in %s: %s",
@@ -43,7 +43,7 @@ int main(int argc, char **argv)
     return EXIT_FAILURE;
   }
 
-  char *libplug_path = nob_shift(argv, argc);
+  char* libplug_path = nob_shift(argv, argc);
   if (!reload_libplug(libplug_path)) return EXIT_FAILURE;
 
   int width  = 800;
diff --git a/src/plug.c b/src/plug.c
@@ -10,46 +10,41 @@ void title_frame(float time, float scale)
   (void)time;
   int height = GetScreenHeight();
 
-  int title_size = (int)(50.0 * scale);
-  Line title = line("introdução a assemply", FOREGROUND, title_size);
-
+  int title_size    = (int)(50.0 * scale);
   int subtitle_size = (int)(30.0 * scale);
-  Line subtitle = line("cripto goma 2020", ACCENT, subtitle_size);
 
   int title_spacing = (int)(10.0 * scale);
-  int title_height = title_size + title_spacing + subtitle_size;
+  int title_height  = title_size + title_spacing + subtitle_size;
 
-  int title_y = (height - title_height)/2;
+  int title_y    = (height - title_height)/2;
   int subtitle_y = title_y + title_size + title_spacing;
 
   ClearBackground(BACKGROUND);
-  line_draw_centered(&title,    title_y);
-  line_draw_centered(&subtitle, subtitle_y);
+
+  line_centered("introdução a assemply", FOREGROUND, title_y, title_size);
+  line_centered("cripto goma 2020",      ACCENT,  subtitle_y, subtitle_size);
 }
 
 void oq_frame(float time, float scale)
 {
-  int height = GetScreenHeight();
+  int height    = GetScreenHeight();
   int font_size = (int)(50.0 * scale);
 
-  Line line = {0};
-  line.font_size = font_size;
-
-  line_append(&line, "o quê é ", FOREGROUND);
-  line_append(&line, "assembly", ACCENT);
-
   const float animation_speed = .5;
   Color color = FOREGROUND;
   if ((int)(time / animation_speed) % 2 == 0) color = BACKGROUND;
-  line_append(&line, " ?", color);
 
   int y = (height - font_size)/2;
 
   ClearBackground(BACKGROUND);
-  line_draw_centered(&line, y);
+  line_begin_centered(y, font_size);
+    line_add("o quê é ", FOREGROUND);
+    line_add("assembly", ACCENT);
+    line_add(" ?",       color);
+  line_end();
 }
 
-void plug_load_frames(Frames *fs)
+void plug_load_frames(Frames* fs)
 {
   frames_append(fs, title_frame, FRAMES_EMPTY_TICK, FRAMES_EMPTY_STATE);
   frames_append(fs, oq_frame,    FRAMES_EMPTY_TICK, FRAMES_EMPTY_STATE);