framer

Slide-show application for nerds ☝️🤓

Commit
d32329805004d7d9b2a35b990b05f1913419fed6
Parent
6c2ae8e740dbd5271de0d21aa5400bbbd96336d5
Author
Pablo <pablo-pie@riseup.net>
Date

Merged frames.h and text.h

Also made it so that the state of each frame is not re-initialized every time we pass frames

Diffstats

3 files changed, 72 insertions, 97 deletions

Status Name Changes Insertions Deletions
Modified src/frames.h 2 files changed 62 22
Modified src/plug.c 2 files changed 10 11
Deleted src/text.h 1 file changed 0 64
diff --git a/src/frames.h b/src/frames.h
@@ -10,6 +10,8 @@
 #define ARENA_IMPLEMENTATION
 #include "arena.h"
 
+// == Frames data-structure ===================================================
+
 typedef void (*FrameDraw)(float time, float scale);
 typedef void (*FrameTick)(float time, void *state);
 
@@ -18,7 +20,7 @@ typedef void (*FrameTick)(float time, void *state);
 
 typedef struct {
   FrameDraw draw;
-  FrameTick   tick;
+  FrameTick tick;
 
   void*  state;
   size_t state_size;
@@ -41,9 +43,6 @@ void frames_next(Frames *fs)
   if (fs->cursor == fs->count - 1) return;
 
   fs->cursor++;
-  Frame f = fs->items[fs->cursor];
-  if (f.state) memset(f.state, 0, f.state_size);
-
   fs->time = 0;
 }
 
@@ -53,9 +52,6 @@ void frames_prev(Frames *fs)
   if (fs->cursor == 0) return;
 
   fs->cursor--;
-  Frame f = fs->items[fs->cursor];
-  if (f.state) memset(f.state, 0, f.state_size);
-
   fs->time = 0;
 }
 
@@ -65,9 +61,6 @@ void frames_first(Frames *fs)
   if (fs->cursor == 0) return;
 
   fs->cursor = 0;
-  Frame f = fs->items[fs->cursor];
-  if (f.state) memset(f.state, 0, f.state_size);
-
   fs->time = 0;
 }
 
@@ -77,9 +70,6 @@ void frames_last(Frames *fs)
   if (fs->count == fs->count - 1) return;
 
   fs->cursor = fs->count - 1;
-  Frame f = fs->items[fs->cursor];
-  if (f.state) memset(f.state, 0, f.state_size);
-
   fs->time = 0;
 }
 
@@ -126,15 +116,9 @@ void frames_append(Frames *fs, FrameDraw draw, FrameTick tick,
   nob_da_append(fs, f);
 }
 
-/* 
- *
- * Loads new frames into `fs` using the `load` function
- *
- * This wrapper prevents us from potentially invalidating the state of the
- * frames when reloading the DLL
- *
- */
-void frames_load(Frames *fs, void (*load)(Frames*))
+typedef void (*FramesLoad)(Frames *fs);
+
+void frames_load(Frames *fs, FramesLoad load)
 {
   fs->count = 0;
   arena_reset(&fs->states);
@@ -144,4 +128,60 @@ void frames_load(Frames *fs, void (*load)(Frames*))
   if (fs->cursor >= fs->count) fs->cursor = fs->count - 1;
 }
 
+// == Text layout =============================================================
+
+typedef struct {
+  const char* text;
+  Color       color;
+
+  size_t width;
+  size_t x_offset;
+} Span;
+
+typedef struct {
+  Span*  items;
+  size_t count;
+  size_t capacity;
+
+  size_t width;
+  int    font_size;
+} Line;
+
+void line_append(Line *line, const char *text, Color color)
+{
+  size_t text_width = MeasureText(text, line->font_size);
+  Span le = {
+    .text     = text,
+    .color    = color,
+    .width    = text_width,
+    .x_offset = line->width,
+  };
+
+  nob_da_append(line, le);
+  line->width += text_width;
+}
+
+Line line(const char *text, Color color, int font_size)
+{
+  Line line = {0};
+  line.font_size = font_size;
+
+  line_append(&line, text, color);
+  return line;
+}
+
+void line_draw(Line *line, int x, int y)
+{
+  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);
+  }
+}
+
+void line_draw_centered(Line *line, int y)
+{
+  int x = (GetScreenWidth() - line->width)/2;
+  line_draw(line, x, y);
+}
+
 #endif // FRAMES_H_
diff --git a/src/plug.c b/src/plug.c
@@ -1,6 +1,5 @@
 #include "raylib.h"
 #include "frames.h"
-#include "text.h"
 
 #define BACKGROUND BLACK
 #define FOREGROUND WHITE
@@ -12,10 +11,10 @@ void title_frame(float time, float scale)
   int height = GetScreenHeight();
 
   int title_size = (int)(50.0 * scale);
-  LineBuilder title = lb_line("introdução a assemply", FOREGROUND, title_size);
+  Line title = line("introdução a assemply", FOREGROUND, title_size);
 
   int subtitle_size = (int)(30.0 * scale);
-  LineBuilder subtitle = lb_line("cripto goma 2020", ACCENT, subtitle_size);
+  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;
@@ -24,8 +23,8 @@ void title_frame(float time, float scale)
   int subtitle_y = title_y + title_size + title_spacing;
 
   ClearBackground(BACKGROUND);
-  lb_draw_centered(&title,    title_y);
-  lb_draw_centered(&subtitle, subtitle_y);
+  line_draw_centered(&title,    title_y);
+  line_draw_centered(&subtitle, subtitle_y);
 }
 
 void oq_frame(float time, float scale)
@@ -33,21 +32,21 @@ void oq_frame(float time, float scale)
   int height = GetScreenHeight();
   int font_size = (int)(50.0 * scale);
 
-  LineBuilder lb = {0};
-  lb.font_size = font_size;
+  Line line = {0};
+  line.font_size = font_size;
 
-  lb_append(&lb, "o quê é ", FOREGROUND);
-  lb_append(&lb, "assembly", ACCENT);
+  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;
-  lb_append(&lb, " ?", color);
+  line_append(&line, " ?", color);
 
   int y = (height - font_size)/2;
 
   ClearBackground(BACKGROUND);
-  lb_draw_centered(&lb, y);
+  line_draw_centered(&line, y);
 }
 
 void plug_load_frames(Frames *fs)
diff --git a/src/text.h /dev/null
@@ -1,64 +0,0 @@
-#ifndef TEXT_H_
-#define TEXT_H_
-
-#include <stddef.h>
-#include "raylib.h"
-
-#define NOB_IMPLEMENTATION
-#include "nob.h"
-
-typedef struct {
-  const char* text;
-  Color       color;
-
-  size_t width;
-  size_t x_offset;
-} Span;
-
-typedef struct {
-  Span*  items;
-  size_t count;
-  size_t capacity;
-
-  size_t width;
-  int    font_size;
-} LineBuilder;
-
-void lb_append(LineBuilder *lb, const char *text, Color color)
-{
-  size_t text_width = MeasureText(text, lb->font_size);
-  Span le = {
-    .text     = text,
-    .color    = color,
-    .width    = text_width,
-    .x_offset = lb->width,
-  };
-
-  nob_da_append(lb, le);
-  lb->width += text_width;
-}
-
-LineBuilder lb_line(const char *text, Color color, int font_size)
-{
-  LineBuilder lb = {0};
-  lb.font_size = font_size;
-
-  lb_append(&lb, text, color);
-  return lb;
-}
-
-void lb_draw(LineBuilder *lb, int x, int y)
-{
-  for (size_t i = 0; i < lb->count; i++) {
-    Span le = lb->items[i];
-    DrawText(le.text, x + le.x_offset, y, lb->font_size, le.color);
-  }
-}
-
-void lb_draw_centered(LineBuilder *lb, int y)
-{
-  int x = (GetScreenWidth() - lb->width)/2;
-  lb_draw(lb, x, y);
-}
-
-#endif // TEXT_H_