summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Stensgård <mastensg@mastensg.net>2025-07-06 21:07:10 +0200
committerMartin Stensgård <mastensg@mastensg.net>2025-07-06 21:07:10 +0200
commit18e51d63cc0da747df0968ca4c1fe8b3b1e2696b (patch)
treeee17e6b5493d175b68ff181dbbe7269992d36a73
parentb7e80be754df87c9190993338b973157cfa558b9 (diff)
opplysning: factorize
-rw-r--r--opplysning.c147
1 files changed, 82 insertions, 65 deletions
diff --git a/opplysning.c b/opplysning.c
index 8a466f2..1e2dd92 100644
--- a/opplysning.c
+++ b/opplysning.c
@@ -13,6 +13,11 @@
#include "sqlite/sqlite3.h"
+enum {
+ SCREEN_W = 1920,
+ SCREEN_H = 1080,
+};
+
static const char *DATABASE = "booking.db";
// Unfortunately, 0 = Sunday.
@@ -175,44 +180,83 @@ load_the_events(void)
const char *the_non_ascii = "ÄÅÉËÞÜÚÍÓÖÁÐFGHÏŒØÆŒ©®BÑΜ"
"äåéëþüúíóöáðfghïœøæœ©®bñµß";
-Shader the_shader;
-RenderTexture2D the_target;
-int the_shader_u_time;
+struct ray {
+ Shader shader;
+ RenderTexture2D target;
+ int shader_u_time;
+
+ Color bg;
+ Color fg;
+ Color hd;
+
+ Font font_h;
+ Font font_p;
+};
+struct ray R;
void
blur(int x, int y, Font f, Color c, char *s)
{
- const int sh = 1080;
Vector2 m = MeasureTextEx(f, s, f.baseSize, 0);
- BeginTextureMode(the_target);
+ BeginTextureMode(R.target);
ClearBackground((Color){0});
DrawTextEx(f, s, (Vector2){0, 0}, (float)f.baseSize, 0, c);
EndTextureMode();
- BeginShaderMode(the_shader);
+ BeginShaderMode(R.shader);
float t = GetTime();
- SetShaderValue(the_shader, the_shader_u_time, &t, SHADER_UNIFORM_FLOAT);
- DrawTextureRec(the_target.texture, (Rectangle){0, sh - m.y, m.x, -m.y},
+ SetShaderValue(R.shader, R.shader_u_time, &t, SHADER_UNIFORM_FLOAT);
+ DrawTextureRec(R.target.texture,
+ (Rectangle){0, SCREEN_H - m.y, m.x, -m.y},
(Vector2){x, y}, WHITE);
EndShaderMode();
}
void
-line(int x, int y, Font f, Color c, char *s)
+line(int x, int y, Font f, Color c, const char *s)
{
DrawTextEx(f, s, (Vector2){x, y}, (float)f.baseSize, 0, c);
}
-int
-main(void)
+void
+draw_one_event(int x, int y, const struct event *e)
{
- load_the_events();
+ char s[64] = {0};
+ snprintf(s, sizeof(s), "%02u:%02u - %02u:%02u", e->hour, e->minute,
+ e->end_hour, e->end_minute);
+ Vector2 m = MeasureTextEx(R.font_p, s, R.font_p.baseSize, 0);
+ line(x + 5.8 * R.font_p.baseSize - m.x, y, R.font_p, R.fg, s);
+ line(x + 6.5 * R.font_p.baseSize, y, R.font_p, R.fg, e->title);
+}
+void
+draw_events(int x, int y, size_t numevents,
+ const struct event events[static numevents])
- const int sw = 1920;
- const int sh = 1080;
+{
+ int year = 0, month = 0, day = 0;
+ for (size_t i = 0; i < numevents; ++i) {
+ const struct event *e = &events[i];
+ if (year != e->year || month != e->month || day != e->day) {
+ year = e->year;
+ month = e->month;
+ day = e->day;
+
+ char s[64] = {0};
+ snprintf(s, sizeof(s), "%s %u. %s", ukedag[e->wday],
+ e->day, maaned[e->month]);
+ line(x + 25, y += 50, R.font_h, R.hd, s);
+ y += 20;
+ }
+ y += 40;
+ draw_one_event(x, y, e);
+ }
+}
+void
+ray_init(void)
+{
SetTraceLogLevel(LOG_WARNING);
- InitWindow(sw, sh, "opplysning");
+ InitWindow(SCREEN_W, SCREEN_H, "opplysning");
SetTargetFPS(60);
char *codes = calloc(128 + strlen(the_non_ascii), 1);
@@ -223,22 +267,27 @@ main(void)
memcpy(codes + 128, the_non_ascii, strlen(the_non_ascii));
int ncp = 0;
int *cp = LoadCodepoints(codes + 1, &ncp);
- Font font_h = LoadFontEx("font/adventpro-bold.ttf", 60, cp, ncp);
- // Font font_p = LoadFontEx("font/adventpro-semibold.ttf", 40, cp, ncp);
- Font font_p =
- LoadFontEx("font/NHaasGroteskTXPro-55Rg.ttf", 40, cp, ncp);
+ R.font_h = LoadFontEx("font/adventpro-bold.ttf", 60, cp, ncp);
+ R.font_p = LoadFontEx("font/NHaasGroteskTXPro-55Rg.ttf", 40, cp, ncp);
UnloadCodepoints(cp);
free(codes);
- Color bg = RAYWHITE;
- Color fg = BLACK;
- Color hd = {0xf0, 0x4a, 0x00, 0xff};
+ R.bg = RAYWHITE;
+ R.fg = BLACK;
+ R.hd = (Color){0xf0, 0x4a, 0x00, 0xff};
- the_shader = LoadShader(0, "s.glsl");
- the_shader_u_time = GetShaderLocation(the_shader, "u_time");
- the_target = LoadRenderTexture(sw, sh);
- int fx = 0;
+ R.shader = LoadShader(0, "s.glsl");
+ R.shader_u_time = GetShaderLocation(R.shader, "u_time");
+ R.target = LoadRenderTexture(SCREEN_W, SCREEN_H);
+}
+int
+main(void)
+{
+ load_the_events();
+ ray_init();
+
+ int fx = 0;
while (!WindowShouldClose()) {
struct timespec now = {0};
clock_gettime(CLOCK_REALTIME, &now);
@@ -250,45 +299,13 @@ main(void)
fx = !fx;
BeginDrawing();
- ClearBackground(bg);
- Vector2 v2_ts = MeasureTextEx(font_h, ts, font_h.baseSize, 0);
- line(sw / 2 - v2_ts.x / 2, 0, font_h, fg, ts);
-
- int y = 0;
- int year = 0, month = 0, day = 0;
- for (int i = 0; i < num_events; ++i) {
- struct event *e = the_events + i;
- if (year != e->year || month != e->month ||
- day != e->day) {
- year = e->year;
- month = e->month;
- day = e->day;
-
- char s[64] = {0};
- snprintf(s, sizeof(s), "%s %u. %s",
- ukedag[e->wday], e->day,
- maaned[e->month]);
- if (fx)
- blur(25, y += 50, font_h, hd, s);
- else
- line(25, y += 50, font_h, hd, s);
- y += 20;
- }
- {
- y += 40;
- char s[64] = {0};
- snprintf(s, sizeof(s), "%02u:%02u - %02u:%02u",
- e->hour, e->minute, e->end_hour,
- e->end_minute);
- Vector2 m = MeasureTextEx(font_p, s,
- font_p.baseSize, 0);
- line(5.8 * font_p.baseSize - m.x, y, font_p, fg,
- s);
-
- line(6.5 * font_p.baseSize, y, font_p, fg,
- e->title);
- }
- }
+ ClearBackground(R.bg);
+ Vector2 v2_ts =
+ MeasureTextEx(R.font_h, ts, R.font_h.baseSize, 0);
+ line(SCREEN_W / 2 - v2_ts.x / 2, 0, R.font_h, R.fg, ts);
+
+ draw_events(0, 0, num_events, the_events);
+
EndDrawing();
}