From 5cb5a95e36f2dc22a9d699b37f60e7c48dbc8db8 Mon Sep 17 00:00:00 2001 From: randy Date: Thu, 21 Sep 2023 15:47:58 -0500 Subject: [PATCH] We'll call it 1.0 -- HSL-space navigation, common UI stuff, and quick text display (holy shit it needs an overhaul though) --- Makefile | 2 +- source/main.c | 7 ++++- source/sdl/SDL_Utils.c | 59 +++++++++++++++++++++++++++++++++++++++++- source/structs.h | 19 +++++++++++--- 4 files changed, 81 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index a947fbb..8558858 100755 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ SDL_SOURCES := $(wildcard source/sdl/*.c) IMGUI_SOURCES = imgui/imgui.cpp imgui/imgui_impl_sdl.cpp imgui/imgui_impl_sdlrenderer.cpp imgui/imgui_tables.cpp imgui/imgui_widgets.cpp imgui/imgui_draw.cpp sdl: source/*.c source/*.h source/sdl/*.c source/sdl/*.h - gcc -DBUILD_SDL=1 $(SDL_SOURCES) $(SOURCES) $(LIBS) -lSDL2 -lSDL2_gfx -o bin/$@ + gcc -DBUILD_SDL=1 $(SDL_SOURCES) $(SOURCES) $(LIBS) -lSDL2 -lSDL2_gfx -lSDL2_ttf -o bin/$@ d_sdl: source/*.c source/*.h source/sdl/*.c source/sdl/*.h gcc -g -DBUILD_SDL=1 $(SDL_SOURCES) $(SOURCES) $(LIBS) -lSDL2 -lSDL2_gfx -o bin/$@ diff --git a/source/main.c b/source/main.c index 6b989b3..f707486 100644 --- a/source/main.c +++ b/source/main.c @@ -8,6 +8,7 @@ #include #include #include +#include Game_Info gi; void killterm_handler(int signum); @@ -18,7 +19,7 @@ int init() signal(SIGKILL, killterm_handler); signal(SIGTERM, killterm_handler); - float scale = 1.5f; + float scale = 1.0f; gi.window = (Rect){.x = 0.0f, .y = 0.0f, .w = scale*512.0f, .h = scale*512.0f}; // satisfying rel.[xy]*2 == .[wh] centers axis in parent container @@ -39,6 +40,10 @@ int init() gi.active_hsl = (HSL_Color){.h = 0, .s = 100, .l = 50}; gi.active_rgb = hsl_to_rgb(gi.active_hsl); + TTF_Init(); + gi.font = TTF_OpenFont("/usr/share/fonts/TTF/iosevka-fixed-regular.ttf", 24); + assert(gi.font != NULL); + init_renderer(&gi); gi.game_alive = 1; diff --git a/source/sdl/SDL_Utils.c b/source/sdl/SDL_Utils.c index f54d46e..9746252 100644 --- a/source/sdl/SDL_Utils.c +++ b/source/sdl/SDL_Utils.c @@ -1,4 +1,5 @@ #include +#include #include #include "SDL_Utils.h" @@ -112,6 +113,61 @@ int32_t render_color_preview(Game_Info* gi, SDL_FRect* container) return 0; } +// https://stackoverflow.com/questions/22886500/how-to-render-text-in-sdl2 +// This is horrible and textures should really be cached in some way +int32_t render_info_boxes(Game_Info* gi, 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", gi->active_rgb.r, gi->active_rgb.r); + sprintf(grn_string, "G:%d/%X", gi->active_rgb.g, gi->active_rgb.g); + sprintf(blu_string, "B:%d/%X", gi->active_rgb.b, gi->active_rgb.b); + sprintf(hue_string, "H:%d/%X", gi->active_hsl.h, gi->active_hsl.h); + sprintf(sat_string, "S:%d/%X", gi->active_hsl.s, gi->active_hsl.s); + sprintf(lum_string, "L:%d/%X", gi->active_hsl.l, gi->active_hsl.l); + + gi->red_component_text_surface = TTF_RenderText_Solid(gi->font, red_string, black); + gi->green_component_text_surface = TTF_RenderText_Solid(gi->font, grn_string, black); + gi->blue_component_text_surface = TTF_RenderText_Solid(gi->font, blu_string, black); + gi->hue_component_text_surface = TTF_RenderText_Solid(gi->font, hue_string, black); + gi->sat_component_text_surface = TTF_RenderText_Solid(gi->font, sat_string, black); + gi->lum_component_text_surface = TTF_RenderText_Solid(gi->font, lum_string, black); + + gi->red_component_text_tex = SDL_CreateTextureFromSurface(mgr.rend, gi->red_component_text_surface); + gi->green_component_text_tex = SDL_CreateTextureFromSurface(mgr.rend, gi->green_component_text_surface); + gi->blue_component_text_tex = SDL_CreateTextureFromSurface(mgr.rend, gi->blue_component_text_surface); + gi->hue_component_text_tex = SDL_CreateTextureFromSurface(mgr.rend, gi->hue_component_text_surface); + gi->sat_component_text_tex = SDL_CreateTextureFromSurface(mgr.rend, gi->sat_component_text_surface); + gi->lum_component_text_tex = SDL_CreateTextureFromSurface(mgr.rend, gi->lum_component_text_surface); + + // Now we can render + SDL_RenderCopyF(mgr.rend, gi->red_component_text_tex, NULL, &gi->red.real); + SDL_RenderCopyF(mgr.rend, gi->green_component_text_tex, NULL, &gi->green.real); + SDL_RenderCopyF(mgr.rend, gi->blue_component_text_tex, NULL, &gi->blue.real); + SDL_RenderCopyF(mgr.rend, gi->hue_component_text_tex, NULL, &gi->hue.real); + SDL_RenderCopyF(mgr.rend, gi->sat_component_text_tex, NULL, &gi->saturation.real); + SDL_RenderCopyF(mgr.rend, gi->lum_component_text_tex, NULL, &gi->luminence.real); + + // Cleanup + SDL_FreeSurface(gi->red_component_text_surface); + SDL_FreeSurface(gi->green_component_text_surface); + SDL_FreeSurface(gi->blue_component_text_surface); + SDL_FreeSurface(gi->hue_component_text_surface); + SDL_FreeSurface(gi->sat_component_text_surface); + SDL_FreeSurface(gi->lum_component_text_surface); + SDL_DestroyTexture(gi->red_component_text_tex); + SDL_DestroyTexture(gi->green_component_text_tex); + SDL_DestroyTexture(gi->blue_component_text_tex); + SDL_DestroyTexture(gi->hue_component_text_tex); + SDL_DestroyTexture(gi->sat_component_text_tex); + SDL_DestroyTexture(gi->lum_component_text_tex); +} + int32_t render_vertical_hue_spectrum(Game_Info* gi, SDL_FRect* container) { @@ -140,7 +196,7 @@ int32_t render_container(Game_Info* gi, SDL_FRect* parent, Layout_Rect* child, S { SDL_SetRenderDrawColor(mgr.rend, unroll_sdl_color(color)); child->real = fr_margin_adjust(*parent, child->rel); - SDL_RenderFillRectF(mgr.rend, &child->real); + //SDL_RenderFillRectF(mgr.rend, &child->real); return 0; } @@ -172,6 +228,7 @@ int32_t display(Game_Info* gi) render_color_preview(gi, &gi->final_sample.real); render_vertical_hue_spectrum(gi, &gi->hue_slider.real); render_rgb_square(gi, &gi->rgb_square.real); + render_info_boxes(gi, &gi->info_boxes.real); SDL_RenderPresent(mgr.rend); return 0; diff --git a/source/structs.h b/source/structs.h index 6a16086..9cfbcfc 100644 --- a/source/structs.h +++ b/source/structs.h @@ -3,6 +3,7 @@ #if BUILD_SDL #include +#include #define Point SDL_Point #define FPoint SDL_FPoint #define Rect SDL_FRect @@ -57,20 +58,32 @@ typedef struct Layout_Rect info_boxes; Layout_Rect rgb_info; Layout_Rect red; - Layout_Rect blue; + 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; 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; Color active_rgb; - - // Hue value should correspond to value on HSV slider HSL_Color active_hsl; int game_alive; + TTF_Font* font; + } Game_Info; #endif // STRUCTS__ -- 2.49.0