From 91d85e162995d44e673c1c3240eacd686718980c Mon Sep 17 00:00:00 2001 From: randy Date: Sat, 23 Sep 2023 12:17:33 -0500 Subject: [PATCH] Introduce configuration header --- source/main.c | 31 ++++--------------------------- source/rendering.c | 22 +++++++++++----------- source/rendering.h | 8 ++++---- source/structs.h | 5 ++--- 4 files changed, 21 insertions(+), 45 deletions(-) diff --git a/source/main.c b/source/main.c index b63e25e..024b8ed 100644 --- a/source/main.c +++ b/source/main.c @@ -6,8 +6,8 @@ #include "rendering.h" #include "structs.h" +#include "config.h" -runtime_info runtime; void killterm_handler(int signum); int init() @@ -16,30 +16,12 @@ int init() signal(SIGKILL, killterm_handler); signal(SIGTERM, killterm_handler); - float scale = 1.0f; - runtime.layout.window = (SDL_FRect){.x = 0.0f, .y = 0.0f, .w = scale*512.0f, .h = scale*512.0f}; - - // satisfying rel.[xy]*2 == .[wh] centers axis in parent container - runtime.layout.rgb_square.rel = (SDL_FRect){.x = 0.05, .y = 0.05, .w = 0.5, .h = 0.5}; - runtime.layout.hue_slider.rel = (SDL_FRect){.x = 0.70, .y = 0.05, .w = .08, .h = 0.5}; - runtime.layout.final_sample.rel = (SDL_FRect){.x = 0.05, .y = .65, .w = 0.20, .h = 0.20}; - runtime.layout.info_container.rel = (SDL_FRect){.x = 0.05, .y = .65, .w = .9, .h = .30}; - runtime.layout.info_boxes.rel = (SDL_FRect){.x = .25, .y = 0.00, .w = 0.75, .h = 1.00}; - runtime.layout.rgb_info.rel = (SDL_FRect){.x = 0.00, .y = 0.00, .w = 1.00, .h = 0.50}; - runtime.layout.red.rel = (SDL_FRect){.x = 0.00, .y = 0.00, .w = 0.30, .h = 1.00}; - runtime.layout.green.rel = (SDL_FRect){.x = 0.35, .y = 0.00, .w = 0.30, .h = 1.00}; - runtime.layout.blue.rel = (SDL_FRect){.x = 0.70, .y = 0.00, .w = 0.30, .h = 1.00}; - runtime.layout.hsl_info.rel = (SDL_FRect){.x = 0.00, .y = 0.50, .w = 1.00, .h = 0.50}; - runtime.layout.hue.rel = (SDL_FRect){.x = 0.00, .y = 0.00, .w = 0.30, .h = 1.00}; - runtime.layout.saturation.rel = (SDL_FRect){.x = 0.35, .y = 0.00, .w = 0.30, .h = 1.00}; - runtime.layout.luminence.rel = (SDL_FRect){.x = 0.70, .y = 0.00, .w = 0.30, .h = 1.00}; - + runtime.layout = config_layout; runtime.active_hsl = (HSL_Color){.h = 0, .s = 100, .l = 50}; runtime.active_rgb = hsl_to_rgb(runtime.active_hsl); TTF_Init(); - // Probably figure out how to get reliable fonts - runtime.font = TTF_OpenFont("/usr/share/fonts/TTF/iosevka-fixed-regular.ttf", 64); + runtime.font = TTF_OpenFont(config_font_path, config_font_size); assert(runtime.font != NULL); init_renderer(&runtime); @@ -51,10 +33,9 @@ int init() int main(void) { - int quit = 0; struct timespec ts_start; struct timespec ts_end; - float time_step = 1000.0f/30; + float time_step = 1000.0f/config_framerate; if(init() != 0) { @@ -72,16 +53,12 @@ int main(void) delay(time_step - frameproc_ms); } - killterm_handler(15); - return 0; } void killterm_handler(int signum) { - printf("handling sig %d, bye bye\n", signum); shutdown_renderer(); - exit(0); } diff --git a/source/rendering.c b/source/rendering.c index a5dc989..925dbc6 100644 --- a/source/rendering.c +++ b/source/rendering.c @@ -14,7 +14,7 @@ SDL_Color magenta = {255, 0, 255, 255}; sdl_group mgr; -int32_t init_renderer(runtime_info* runtime) +int32_t init_renderer(Runtime_Info* runtime) { SDL_SetMainReady(); if (SDL_Init(SDL_INIT_VIDEO)) @@ -42,7 +42,7 @@ int32_t init_renderer(runtime_info* runtime) // this would be really cool to turn into some stack-based type of thing // Also, if resizing is disabled, this can be moved to a static initialization section // orrrr, maybe we can finally do callbacks - render_container(runtime, &runtime->layout.window, &runtime->layout.rgb_square, green); + render_container(runtime, &runtime->layout.window, &runtime->layout.hsl_square, green); render_container(runtime, &runtime->layout.window, &runtime->layout.hue_slider, green); render_container(runtime, &runtime->layout.window, &runtime->layout.info_container, blue); render_container(runtime, &runtime->layout.window, &runtime->layout.final_sample, green); @@ -73,7 +73,7 @@ int32_t delay(int32_t delay_time) // Rename this eventually // It's actually HSL -int32_t render_rgb_square(runtime_info* runtime, SDL_FRect* container) +int32_t render_hsl_square(Runtime_Info* runtime, SDL_FRect* container) { HSL_Color active_hsl = runtime->active_hsl; SDL_Color hsl_pixel; @@ -83,7 +83,6 @@ int32_t render_rgb_square(runtime_info* runtime, SDL_FRect* container) // I guess not much other way than good ol' n^2 // hmmm we'll fix this later - active_hsl.s = 0; active_hsl.l = 0; for (int r = 0; r < container->w; r++) @@ -114,7 +113,7 @@ int32_t render_rgb_square(runtime_info* runtime, SDL_FRect* container) return 0; } -int32_t render_color_preview(runtime_info* runtime, SDL_FRect* container) +int32_t render_color_preview(Runtime_Info* runtime, SDL_FRect* container) { runtime->active_rgb = hsl_to_rgb(runtime->active_hsl); SDL_SetRenderDrawColor(mgr.rend, unroll_sdl_color(runtime->active_rgb)); @@ -125,7 +124,8 @@ int32_t render_color_preview(runtime_info* runtime, SDL_FRect* container) // https://stackoverflow.com/questions/22886500/how-to-render-text-in-sdl2 // This is horrible horrible horrible -int32_t render_info_boxes(runtime_info* runtime, SDL_FRect* container) +// Look away +int32_t render_info_boxes(Runtime_Info* runtime, SDL_FRect* container) { char red_string[32]; char blu_string[32]; @@ -179,7 +179,7 @@ int32_t render_info_boxes(runtime_info* runtime, SDL_FRect* container) SDL_DestroyTexture(runtime->layout.lum_component_text_tex); } -int32_t render_vertical_hue_spectrum(runtime_info* runtime, SDL_FRect* container) +int32_t render_vertical_hue_spectrum(Runtime_Info* runtime, SDL_FRect* container) { int hue_slice_scale = container->h; @@ -202,7 +202,7 @@ int32_t render_vertical_hue_spectrum(runtime_info* runtime, SDL_FRect* container } // REALLY this should be "generate layout", and not a true rendering step -int32_t render_container(runtime_info* runtime, SDL_FRect* parent, Layout_Rect* child, SDL_Color color) +int32_t render_container(Runtime_Info* runtime, SDL_FRect* parent, Layout_Rect* child, SDL_Color color) { SDL_SetRenderDrawColor(mgr.rend, unroll_sdl_color(color)); child->real = fr_margin_adjust(*parent, child->rel); @@ -210,7 +210,7 @@ int32_t render_container(runtime_info* runtime, SDL_FRect* parent, Layout_Rect* return 0; } -int32_t display(runtime_info* runtime) +int32_t display(Runtime_Info* runtime) { SDL_SetRenderDrawColor(mgr.rend, 0xCB, 0xCB, 0xCB, 0xCB); SDL_RenderClear(mgr.rend); @@ -219,14 +219,14 @@ int32_t display(runtime_info* runtime) render_color_preview(runtime, &runtime->layout.final_sample.real); render_vertical_hue_spectrum(runtime, &runtime->layout.hue_slider.real); - render_rgb_square(runtime, &runtime->layout.rgb_square.real); + render_hsl_square(runtime, &runtime->layout.hsl_square.real); render_info_boxes(runtime, &runtime->layout.info_boxes.real); SDL_RenderPresent(mgr.rend); return 0; } -int32_t check_inputs(runtime_info* runtime) +int32_t check_inputs(Runtime_Info* runtime) { while(SDL_PollEvent(&(mgr.event))) { diff --git a/source/rendering.h b/source/rendering.h index 64ebe1c..19b58ce 100644 --- a/source/rendering.h +++ b/source/rendering.h @@ -10,13 +10,13 @@ typedef struct sdl_group } sdl_group; -int32_t init_renderer(runtime_info* runtime); +int32_t init_renderer(Runtime_Info* runtime); int32_t shutdown_renderer(); int32_t delay(int32_t delay_time); -int32_t display(runtime_info* runtime); -int32_t check_inputs(runtime_info* runtime); +int32_t display(Runtime_Info* runtime); +int32_t check_inputs(Runtime_Info* runtime); -int32_t render_container(runtime_info* runtime, SDL_FRect* parent, Layout_Rect* child, SDL_Color color); +int32_t render_container(Runtime_Info* runtime, SDL_FRect* parent, Layout_Rect* child, SDL_Color color); SDL_FRect fr_add(const SDL_FRect left, const SDL_FRect right); SDL_FRect fr_subtract(const SDL_FRect left, const SDL_FRect right); diff --git a/source/structs.h b/source/structs.h index ab712b2..5d4188e 100644 --- a/source/structs.h +++ b/source/structs.h @@ -4,7 +4,6 @@ #include #include #define Relative_Rect SDL_FRect - #include // A placeable rect, with relative components to its parent, @@ -45,7 +44,7 @@ typedef struct { // Tabbing these to visually indicate layout SDL_FRect window; - Layout_Rect rgb_square; // big clicky draggy square + Layout_Rect hsl_square; // big clicky draggy square Layout_Rect hue_slider; // HSV slider bar Layout_Rect info_container; Layout_Rect final_sample; // small square showing full selected color @@ -83,7 +82,7 @@ typedef struct TTF_Font* font; -} runtime_info; +} Runtime_Info; #endif // STRUCTS__ -- 2.49.0