From 41729222431449a81535b28ad27ce2620cb5819b Mon Sep 17 00:00:00 2001 From: Federico Angelilli Date: Fri, 17 Nov 2023 13:30:38 +0100 Subject: Move all the state required for rendering in a struct --- comet.c | 19 ++++++++++++------- draw.c | 9 +++++---- draw.h | 5 ++--- state.c | 21 +++++++++++++++++++++ state.h | 18 ++++++++++++++++++ 5 files changed, 58 insertions(+), 14 deletions(-) create mode 100644 state.c create mode 100644 state.h diff --git a/comet.c b/comet.c index 4707337..5ad6dc1 100644 --- a/comet.c +++ b/comet.c @@ -6,19 +6,21 @@ #include "log.h" #include "draw.h" #include "connection.h" +#include "state.h" #define EVEN(n) ((int)(n) - ((int)(n) % 2 != 0)) static gboolean mainloop_quit(gpointer data) { - g_main_loop_quit(data); - return G_SOURCE_CONTINUE; + g_main_loop_quit(data); + return G_SOURCE_CONTINUE; } -static gboolean mainloop_do(gpointer data) +static gboolean mainloop_draw(gpointer data) { - draw_paint(data); - return G_SOURCE_REMOVE; + State *state = data; + draw_paint(state->draw, state->win); + return G_SOURCE_REMOVE; } int main(int argc, char **argv) @@ -40,12 +42,14 @@ int main(int argc, char **argv) log_debug("Calculated dimensions [height=%d, x_pad=%d, y_pad=%d]", height, x_padding, y_padding); - Drawable *draw = draw_create(win, "Hack 12", height, x_padding, x_padding, y_padding, 0.99); + Drawable *draw = draw_create("Hack 12", height, x_padding, x_padding, y_padding, 1); + + State *state = state_create(win, draw); guint source_term = g_unix_signal_add(SIGTERM, mainloop_quit, mainloop); guint source_int = g_unix_signal_add(SIGINT, mainloop_quit, mainloop); - guint id = g_timeout_add(100, mainloop_do, draw); + guint id = g_timeout_add(100, mainloop_draw, state); log_debug("Starting main loop"); g_main_loop_run(mainloop); @@ -56,6 +60,7 @@ int main(int argc, char **argv) g_source_remove(source_term); g_source_remove(source_int); + state_destroy(state); draw_destroy(draw); window_destroy(win); diff --git a/draw.c b/draw.c index b0c32ba..aed4da0 100644 --- a/draw.c +++ b/draw.c @@ -11,7 +11,7 @@ // (this will also work for animations in the future) // or use some flags to trigger drawing -Drawable *draw_create(Window *win, const char *font, int height, int left_pad, int right_pad, int top_pad, double alpha) +Drawable *draw_create(const char *font, int height, int left_pad, int right_pad, int top_pad, double alpha) { Drawable *draw = g_malloc(sizeof(Drawable)); g_assert_nonnull(draw); @@ -20,19 +20,20 @@ Drawable *draw_create(Window *win, const char *font, int height, int left_pad, i draw->desc = pango_font_description_from_string(font); log_debug("Pango found matching font '%s'", pango_font_description_get_family(draw->desc)); - draw->win = win; draw->height = height; draw->left_pad = left_pad; draw->right_pad = right_pad; draw->top_pad = top_pad; draw->alpha = alpha; + g_assert(alpha >= 0 && alpha <= 1); + + log_debug("Draw context created [height=%d, left_pad=%d, right_pad=%d, top_pad=%d, alpha=%.2lf]", height, left_pad, right_pad, top_pad, alpha); return draw; } -void draw_paint(Drawable *draw) +void draw_paint(Drawable *draw, Window *win) { - Window *win = draw->win; // FIXME: Does not work for scale != 1 //double scale = window_get_scale(win); double scale = 1; diff --git a/draw.h b/draw.h index 1d299ce..2bbb3c2 100644 --- a/draw.h +++ b/draw.h @@ -8,7 +8,6 @@ // TODO: Make opaque typedef struct { - Window *win; PangoFontDescription *desc; int height; int left_pad; @@ -17,9 +16,9 @@ typedef struct { double alpha; } Drawable; -Drawable *draw_create(Window *win, const char *font, int height, int left_pad, int right_pad, int top_pad, double alpha); +Drawable *draw_create(const char *font, int height, int left_pad, int right_pad, int top_pad, double alpha); -void draw_paint(Drawable *draw); +void draw_paint(Drawable *draw, Window *win); void draw_destroy(Drawable *draw); diff --git a/state.c b/state.c new file mode 100644 index 0000000..bb31fa6 --- /dev/null +++ b/state.c @@ -0,0 +1,21 @@ +#include + +#include "state.h" + +State *state_create(Window *win, Drawable *draw) +{ + State *state = g_malloc(sizeof(State)); + g_assert_nonnull(state); + + state->win = win; + state->draw = draw; + + return state; +} + +void state_destroy(State *state) +{ + g_free(state); +} + +// vim: ts=4 sw=4 et diff --git a/state.h b/state.h new file mode 100644 index 0000000..5c5528d --- /dev/null +++ b/state.h @@ -0,0 +1,18 @@ +#ifndef COMET_STATE_H +#define COMET_STATE_H + +#include "window.h" +#include "draw.h" + +typedef struct { + Window *win; + Drawable *draw; +} State; + +State *state_create(Window *win, Drawable *draw); + +void state_destroy(State *state); + +#endif + +// vim: ts=4 sw=4 et -- cgit v1.2.3