From 7c33107585ac3217adab850a13e7bdbad8791d0a Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Mon, 27 Jan 2025 05:05:43 +0300 Subject: [PATCH] game.background_color option in twn.toml --- src/rendering/twn_draw.c | 2 +- src/twn_engine_context_c.h | 2 ++ src/twn_loop.c | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/rendering/twn_draw.c b/src/rendering/twn_draw.c index 517fef0..3f528bb 100644 --- a/src/rendering/twn_draw.c +++ b/src/rendering/twn_draw.c @@ -500,7 +500,7 @@ void clear_draw_buffer(void) { .clear_color = true, .clear_depth = true, .clear_stencil = true, - .color = (Color) { 230, 230, 230, 1 } + .color = ctx.background_color, } }; diff --git a/src/twn_engine_context_c.h b/src/twn_engine_context_c.h index 4b89e1a..dac04b2 100644 --- a/src/twn_engine_context_c.h +++ b/src/twn_engine_context_c.h @@ -73,6 +73,8 @@ typedef struct EngineContext { SDL_Window *window; uint32_t window_id; + Color background_color; + bool is_running; bool window_size_has_changed; bool resync_flag; diff --git a/src/twn_loop.c b/src/twn_loop.c index c05b592..a769862 100644 --- a/src/twn_loop.c +++ b/src/twn_loop.c @@ -510,6 +510,41 @@ static bool initialize(void) { ctx.window_dims.x = (float)ctx.base_render_width; ctx.window_dims.y = (float)ctx.base_render_height; + ctx.background_color = (Color){ 230, 230, 230, 255 }; + + toml_array_t *datum_background_color = toml_array_in(game, "background_color"); + if (datum_background_color) do { + toml_datum_t datum_background_color_red = toml_int_at(datum_background_color, 0); + if (!datum_background_color_red.ok || datum_background_color_red.u.i < 0 || datum_background_color_red.u.i >= 256) { + log_warn("Invalid value for red channel of game.background_color"); + break; + } + toml_datum_t datum_background_color_green = toml_int_at(datum_background_color, 1); + if (!datum_background_color_green.ok || datum_background_color_green.u.i < 0 || datum_background_color_green.u.i >= 256) { + log_warn("Invalid value for green channel of game.background_color"); + break; + } + toml_datum_t datum_background_color_blue = toml_int_at(datum_background_color, 2); + if (!datum_background_color_blue.ok || datum_background_color_blue.u.i < 0 || datum_background_color_blue.u.i >= 256) { + log_warn("Invalid value for blue channel of game.background_color"); + break; + } + toml_datum_t datum_background_color_alpha = toml_int_at(datum_background_color, 3); + if (!datum_background_color_alpha.ok || datum_background_color_alpha.u.i < 0 || datum_background_color_alpha.u.i >= 256) { + log_warn("Invalid value for alpha channel of game.background_color"); + break; + } + ctx.background_color = (Color){ + (uint8_t)datum_background_color_red.u.i, + (uint8_t)datum_background_color_green.u.i, + (uint8_t)datum_background_color_blue.u.i, + (uint8_t)datum_background_color_alpha.u.i, + }; + } while (0); + + ctx.game.resolution.x = (float)ctx.base_render_width; + ctx.game.resolution.y = (float)ctx.base_render_height; + /* add a watcher for immediate updates on window size */ SDL_AddEventWatch(event_callback, NULL);