game.background_color option in twn.toml

This commit is contained in:
veclavtalica 2025-01-27 05:05:43 +03:00
parent f625dde8d1
commit 7c33107585
3 changed files with 38 additions and 1 deletions

View File

@ -500,7 +500,7 @@ void clear_draw_buffer(void) {
.clear_color = true, .clear_color = true,
.clear_depth = true, .clear_depth = true,
.clear_stencil = true, .clear_stencil = true,
.color = (Color) { 230, 230, 230, 1 } .color = ctx.background_color,
} }
}; };

View File

@ -73,6 +73,8 @@ typedef struct EngineContext {
SDL_Window *window; SDL_Window *window;
uint32_t window_id; uint32_t window_id;
Color background_color;
bool is_running; bool is_running;
bool window_size_has_changed; bool window_size_has_changed;
bool resync_flag; bool resync_flag;

View File

@ -510,6 +510,41 @@ static bool initialize(void) {
ctx.window_dims.x = (float)ctx.base_render_width; ctx.window_dims.x = (float)ctx.base_render_width;
ctx.window_dims.y = (float)ctx.base_render_height; 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 */ /* add a watcher for immediate updates on window size */
SDL_AddEventWatch(event_callback, NULL); SDL_AddEventWatch(event_callback, NULL);