]> git.mcshandy.xyz Git - picker/commitdiff
Initial improvements to text containerization and rendering, and a cleanup thing...
authorrandy <randy@mcshandy.xyz>
Wed, 27 Sep 2023 01:15:57 +0000 (20:15 -0500)
committerrandy <randy@mcshandy.xyz>
Wed, 27 Sep 2023 01:15:57 +0000 (20:15 -0500)
source/config.h
source/main.c
source/rendering.c
source/rendering.h
source/structs.h

index 56a08a7303c01b892aa17390374d2c5f685cb930..fd41e3c8707cb3a8c9b73188ced2da7940de81da 100644 (file)
@@ -21,19 +21,19 @@ Window_Layout config_layout = {
        .info_container.rel     =       (SDL_FRect){.x = 0.05, .y = 0.65, .w = 0.90, .h = 0.30},
        .info_boxes.rel                 =       (SDL_FRect){.x = 0.25, .y = 0.00, .w = 0.75, .h = 1.00},
        .rgb_info.rel                           =       (SDL_FRect){.x = 0.00, .y = 0.00, .w = 1.00, .h = 0.50},
-       .red.rel                                                =       (SDL_FRect){.x = 0.00, .y = 0.00, .w = 0.30, .h = 1.00},
-       .green.rel                                      =       (SDL_FRect){.x = 0.35, .y = 0.00, .w = 0.30, .h = 1.00},
-       .blue.rel                                               =       (SDL_FRect){.x = 0.70, .y = 0.00, .w = 0.30, .h = 1.00},
+       .red_component.body.rel                                         =       (SDL_FRect){.x = 0.00, .y = 0.00, .w = 0.30, .h = 1.00},
+       .green_component.body.rel                                       =       (SDL_FRect){.x = 0.35, .y = 0.00, .w = 0.30, .h = 1.00},
+       .blue_component.body.rel                                                =       (SDL_FRect){.x = 0.70, .y = 0.00, .w = 0.30, .h = 1.00},
        .hsl_info.rel                           =       (SDL_FRect){.x = 0.00, .y = 0.50, .w = 1.00, .h = 0.50},
-       .hue.rel                                                =       (SDL_FRect){.x = 0.00, .y = 0.00, .w = 0.30, .h = 1.00},
-       .saturation.rel                 =       (SDL_FRect){.x = 0.35, .y = 0.00, .w = 0.30, .h = 1.00},
-       .luminence.rel                  =       (SDL_FRect){.x = 0.70, .y = 0.00, .w = 0.30, .h = 1.00}
+       .hue_component.body.rel                                         =       (SDL_FRect){.x = 0.00, .y = 0.00, .w = 0.30, .h = 1.00},
+       .sat_component.body.rel                 =       (SDL_FRect){.x = 0.35, .y = 0.00, .w = 0.30, .h = 1.00},
+       .lum_component.body.rel                 =       (SDL_FRect){.x = 0.70, .y = 0.00, .w = 0.30, .h = 1.00}
 };
 
 // For now we need a full path, maybe OpenFont can do automatic lookup
 const char config_font_path[] = "/usr/share/fonts/TTF/iosevka-fixed-regular.ttf";
 // This currently acts more like a static resolution scale rather than font size
-const int config_font_size = 10;
+const int config_font_size = 128;
 const int config_framerate = 30;
 
 #endif //__PICKER_CONFIG__
index 024b8edc516f3617fe74f91eaf679a37469c4951..4ec7f849954048d7429f004784abc734e46aed47 100644 (file)
@@ -53,12 +53,15 @@ int main(void)
                delay(time_step - frameproc_ms);
        }
 
+       TTF_Quit();
+
        return 0;
 }
 
 void killterm_handler(int signum)
 {
-       shutdown_renderer();
+       shutdown_renderer(&runtime);
+       TTF_Quit();
        exit(0);
 }
 
index 4ec7ef64cd65887509210b50e1a038c621660e9b..e9324db649834c3058c04952a818abb265826ab2 100644 (file)
@@ -14,6 +14,24 @@ SDL_Color magenta = {255, 0, 255, 255};
 
 sdl_group mgr;
 
+int32_t refresh_ui(Runtime_Info* runtime)
+{
+       // this would be really cool to turn into some stack-based type of thing
+       render_container(&runtime->layout.window, &runtime->layout.hsl_square);
+       render_container(&runtime->layout.window, &runtime->layout.hue_slider);
+       render_container(&runtime->layout.window, &runtime->layout.info_container);
+       render_container(&runtime->layout.window, &runtime->layout.final_sample);
+       render_container(&runtime->layout.info_container.real, &runtime->layout.info_boxes);
+       render_container(&runtime->layout.info_boxes.real, &runtime->layout.rgb_info);
+       render_container(&runtime->layout.rgb_info.real, &runtime->layout.red_component.body);
+       render_container(&runtime->layout.rgb_info.real, &runtime->layout.green_component.body);
+       render_container(&runtime->layout.rgb_info.real, &runtime->layout.blue_component.body);
+       render_container(&runtime->layout.info_boxes.real, &runtime->layout.hsl_info);
+       render_container(&runtime->layout.hsl_info.real, &runtime->layout.hue_component.body);
+       render_container(&runtime->layout.hsl_info.real, &runtime->layout.sat_component.body);
+       render_container(&runtime->layout.hsl_info.real, &runtime->layout.lum_component.body);
+}
+
 int32_t init_renderer(Runtime_Info* runtime)
 {
        SDL_SetMainReady();
@@ -39,26 +57,25 @@ int32_t init_renderer(Runtime_Info* runtime)
 
        SDL_SetRenderDrawBlendMode(mgr.rend, SDL_BLENDMODE_BLEND);
 
-       // 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.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);
-       render_container(runtime, &runtime->layout.info_container.real, &runtime->layout.info_boxes, green);
-       render_container(runtime, &runtime->layout.info_boxes.real, &runtime->layout.rgb_info, black);
-       render_container(runtime, &runtime->layout.rgb_info.real, &runtime->layout.red, red);
-       render_container(runtime, &runtime->layout.rgb_info.real, &runtime->layout.green, green);
-       render_container(runtime, &runtime->layout.rgb_info.real, &runtime->layout.blue, blue);
-       render_container(runtime, &runtime->layout.info_boxes.real, &runtime->layout.hsl_info, white);
-       render_container(runtime, &runtime->layout.hsl_info.real, &runtime->layout.hue, green);
-       render_container(runtime, &runtime->layout.hsl_info.real, &runtime->layout.saturation, blue);
-       render_container(runtime, &runtime->layout.hsl_info.real, &runtime->layout.luminence, red);
+       init_text_container(&runtime->layout.red_component, 64);
+       init_text_container(&runtime->layout.green_component, 64);
+       init_text_container(&runtime->layout.blue_component, 64);
+       init_text_container(&runtime->layout.hue_component, 64);
+       init_text_container(&runtime->layout.sat_component, 64);
+       init_text_container(&runtime->layout.lum_component, 64);
+
+       refresh_ui(runtime);
 }
 
-int32_t shutdown_renderer()
+int32_t shutdown_renderer(Runtime_Info* runtime)
 {
+       free_text_container(&runtime->layout.red_component);
+       free_text_container(&runtime->layout.green_component);
+       free_text_container(&runtime->layout.blue_component);
+       free_text_container(&runtime->layout.hue_component);
+       free_text_container(&runtime->layout.sat_component);
+       free_text_container(&runtime->layout.lum_component);
+
        SDL_DestroyRenderer(mgr.rend);
        SDL_DestroyWindow(mgr.win);
        SDL_Quit();
@@ -122,61 +139,64 @@ int32_t render_color_preview(Runtime_Info* runtime, SDL_FRect* container)
        return 0;
 }
 
+// I'd love if this pre-alloc'd surface and texture, but it looks like
+// SDL_TTF just doesn't work that way
+int32_t init_text_container(Text_Container* tc, size_t text_size)
+{
+       tc->text = (char*)malloc(sizeof(char) * text_size);
+       tc->text_len = text_size;
+       return 0;
+}
+
+// Inter-frame SDL cleanup until I figure out a good solution that doesn't
+// involve creating a whole new surface and texture every single frame
+int32_t release_text_container(Text_Container* tc)
+{
+       if (tc->surface)
+               SDL_FreeSurface(tc->surface);
+       if (tc->texture)
+               SDL_DestroyTexture(tc->texture);
+
+       tc->surface = NULL;
+       tc->texture = NULL;
+}
+
+// Clean up the whole thing, when we're done with it for good
+int32_t free_text_container(Text_Container* tc)
+{
+       release_text_container(tc);
+       free(tc->text);
+}
+
+int32_t render_text_container(Runtime_Info* runtime, Text_Container* tc)
+{
+       tc->surface = TTF_RenderText_Solid(runtime->font, tc->text, black);
+       tc->texture = SDL_CreateTextureFromSurface(mgr.rend, tc->surface);
+       SDL_RenderCopyF(mgr.rend, tc->texture, NULL, &tc->body.real);
+
+       // we'll keep this implicit to rendering for now to keep
+       // the bigger rendering picture a bit cleaner
+       release_text_container(&runtime->layout.red_component);
+       return 0;
+}
+
 // https://stackoverflow.com/questions/22886500/how-to-render-text-in-sdl2
-// This is horrible horrible horrible
-// Look away
 int32_t render_info_boxes(Runtime_Info* runtime, SDL_FRect* container)
 {
-       char red_string[32];
-       char blu_string[32];
-       char grn_string[32];
-       char hue_string[32];
-       char sat_string[32];
-       char lum_string[32];
-
-       sprintf(red_string, "R:%d/%X", runtime->active_rgb.r, runtime->active_rgb.r);
-       sprintf(grn_string, "G:%d/%X", runtime->active_rgb.g, runtime->active_rgb.g);
-       sprintf(blu_string, "B:%d/%X", runtime->active_rgb.b, runtime->active_rgb.b);
-       sprintf(hue_string, "H:%d/%X", runtime->active_hsl.h, runtime->active_hsl.h);
-       sprintf(sat_string, "S:%d/%X", runtime->active_hsl.s, runtime->active_hsl.s);
-       sprintf(lum_string, "L:%d/%X", runtime->active_hsl.l, runtime->active_hsl.l);
-
-       runtime->layout.red_component_text_surface              = TTF_RenderText_Solid(runtime->font, red_string, black);
-       runtime->layout.green_component_text_surface    = TTF_RenderText_Solid(runtime->font, grn_string, black);
-       runtime->layout.blue_component_text_surface             = TTF_RenderText_Solid(runtime->font, blu_string, black);
-       runtime->layout.hue_component_text_surface              = TTF_RenderText_Solid(runtime->font, hue_string, black);
-       runtime->layout.sat_component_text_surface              = TTF_RenderText_Solid(runtime->font, sat_string, black);
-       runtime->layout.lum_component_text_surface              = TTF_RenderText_Solid(runtime->font, lum_string, black);
-
-       runtime->layout.red_component_text_tex                          = SDL_CreateTextureFromSurface(mgr.rend, runtime->layout.red_component_text_surface);
-       runtime->layout.green_component_text_tex                        = SDL_CreateTextureFromSurface(mgr.rend, runtime->layout.green_component_text_surface);
-       runtime->layout.blue_component_text_tex                         = SDL_CreateTextureFromSurface(mgr.rend, runtime->layout.blue_component_text_surface);
-       runtime->layout.hue_component_text_tex                          = SDL_CreateTextureFromSurface(mgr.rend, runtime->layout.hue_component_text_surface);
-       runtime->layout.sat_component_text_tex                          = SDL_CreateTextureFromSurface(mgr.rend, runtime->layout.sat_component_text_surface);
-       runtime->layout.lum_component_text_tex                          = SDL_CreateTextureFromSurface(mgr.rend, runtime->layout.lum_component_text_surface);
-
-       // Now we can render
-       SDL_RenderCopyF(mgr.rend, runtime->layout.red_component_text_tex,               NULL, &runtime->layout.red.real);
-       SDL_RenderCopyF(mgr.rend, runtime->layout.green_component_text_tex,     NULL, &runtime->layout.green.real);
-       SDL_RenderCopyF(mgr.rend, runtime->layout.blue_component_text_tex,      NULL, &runtime->layout.blue.real);
-       SDL_RenderCopyF(mgr.rend, runtime->layout.hue_component_text_tex,               NULL, &runtime->layout.hue.real);
-       SDL_RenderCopyF(mgr.rend, runtime->layout.sat_component_text_tex,               NULL, &runtime->layout.saturation.real);
-       SDL_RenderCopyF(mgr.rend, runtime->layout.lum_component_text_tex,               NULL, &runtime->layout.luminence.real);
-
-       // Cleanup
-       SDL_FreeSurface(runtime->layout.red_component_text_surface);
-       SDL_FreeSurface(runtime->layout.green_component_text_surface);
-       SDL_FreeSurface(runtime->layout.blue_component_text_surface);
-       SDL_FreeSurface(runtime->layout.hue_component_text_surface);
-       SDL_FreeSurface(runtime->layout.sat_component_text_surface);
-       SDL_FreeSurface(runtime->layout.lum_component_text_surface);
-
-       SDL_DestroyTexture(runtime->layout.red_component_text_tex);
-       SDL_DestroyTexture(runtime->layout.green_component_text_tex);
-       SDL_DestroyTexture(runtime->layout.blue_component_text_tex);
-       SDL_DestroyTexture(runtime->layout.hue_component_text_tex);
-       SDL_DestroyTexture(runtime->layout.sat_component_text_tex);
-       SDL_DestroyTexture(runtime->layout.lum_component_text_tex);
+       sprintf(runtime->layout.red_component.text, "R:%d/%X\0", runtime->active_rgb.r, runtime->active_rgb.r);
+       sprintf(runtime->layout.green_component.text, "G:%d/%X\0", runtime->active_rgb.g, runtime->active_rgb.g);
+       sprintf(runtime->layout.blue_component.text, "B:%d/%X\0", runtime->active_rgb.b, runtime->active_rgb.b);
+       sprintf(runtime->layout.hue_component.text, "H:%d/%X\0", runtime->active_hsl.h, runtime->active_hsl.h);
+       sprintf(runtime->layout.sat_component.text, "S:%d/%X\0", runtime->active_hsl.s, runtime->active_hsl.s);
+       sprintf(runtime->layout.lum_component.text, "L:%d/%X\0", runtime->active_hsl.l, runtime->active_hsl.l);
+
+       render_text_container(runtime, &runtime->layout.red_component);
+       render_text_container(runtime, &runtime->layout.green_component);
+       render_text_container(runtime, &runtime->layout.blue_component);
+       render_text_container(runtime, &runtime->layout.hue_component);
+       render_text_container(runtime, &runtime->layout.sat_component);
+       render_text_container(runtime, &runtime->layout.lum_component);
+       return 0;
 }
 
 int32_t render_vertical_hue_spectrum(Runtime_Info* runtime, SDL_FRect* container)
@@ -202,7 +222,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(SDL_FRect* parent, Layout_Rect* child)
 {
        child->real = fr_margin_adjust(*parent, child->rel);
        return 0;
index 6f34ccea966be977882839001b36edaf05fed675..720d942e45c2726844d4ef74d9a0a57f4b303023 100644 (file)
@@ -11,12 +11,13 @@ typedef struct sdl_group
 sdl_group;
 
 int32_t init_renderer(Runtime_Info* runtime);
-int32_t shutdown_renderer();
+int32_t shutdown_renderer(Runtime_Info* runtime);
 int32_t delay(int32_t delay_time);
 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(SDL_FRect* parent, Layout_Rect* child);
+int32_t init_text_container(Text_Container* tc, size_t text_size);
+int32_t free_text_container(Text_Container* tc);
 
 SDL_FRect fr_margin_adjust(const SDL_FRect parent, const Relative_Rect child);
 SDL_Color hsl_to_rgb (const HSL_Color hsl);
index 5d4188e9a64e44f37cdede932a15275437da457f..3f87fbd2e7ab766f8de7867759d26698560168a9 100644 (file)
@@ -40,6 +40,16 @@ typedef struct
 
 } Circle;
 
+typedef struct
+{
+       Layout_Rect body;
+       SDL_Surface* surface;
+       SDL_Texture* texture;
+       size_t text_len;
+       char* text;
+
+} Text_Container;
+
 typedef struct
 {
        // Tabbing these to visually indicate layout
@@ -50,25 +60,13 @@ typedef struct
                        Layout_Rect final_sample; // small square showing full selected color
                        Layout_Rect info_boxes;
                                Layout_Rect rgb_info;
-                                       Layout_Rect red;
-                                       SDL_Surface* red_component_text_surface;
-                                       SDL_Texture* red_component_text_tex;
-                                       Layout_Rect green;
-                                       SDL_Surface* green_component_text_surface;
-                                       SDL_Texture* green_component_text_tex;
-                                       Layout_Rect blue;
-                                       SDL_Surface* blue_component_text_surface;
-                                       SDL_Texture* blue_component_text_tex;
+                                       Text_Container red_component;
+                                       Text_Container green_component;
+                                       Text_Container blue_component;
                                Layout_Rect hsl_info;
-                                       Layout_Rect hue;
-                                       SDL_Surface* hue_component_text_surface;
-                                       SDL_Texture* hue_component_text_tex;
-                                       Layout_Rect saturation;
-                                       SDL_Surface* sat_component_text_surface;
-                                       SDL_Texture* sat_component_text_tex;
-                                       Layout_Rect luminence;
-                                       SDL_Surface* lum_component_text_surface;
-                                       SDL_Texture* lum_component_text_tex;
+                                       Text_Container hue_component;
+                                       Text_Container sat_component;
+                                       Text_Container lum_component;
 
 } Window_Layout;