]> git.mcshandy.xyz Git - picker/commitdiff
We'll call it 1.0 -- HSL-space navigation, common UI stuff, and quick text display...
authorrandy <randy@mcshandy.xyz>
Thu, 21 Sep 2023 20:47:58 +0000 (15:47 -0500)
committerrandy <randy@mcshandy.xyz>
Thu, 21 Sep 2023 20:47:58 +0000 (15:47 -0500)
Makefile
source/main.c
source/sdl/SDL_Utils.c
source/structs.h

index a947fbb018f9315fbe8c11a827ad54f2f5535415..8558858383dc455525a6a3a152e31b4098244508 100755 (executable)
--- 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/$@
index 6b989b3f8d622a1ce2716b140aaa02c31cedd3ee..f707486ee9202ff37f788858e42d50d104fe6ae5 100644 (file)
@@ -8,6 +8,7 @@
 #include <time.h>
 #include <stdio.h>
 #include <signal.h>
+#include <assert.h>
 
 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;
index f54d46eed0b73e0df063080e2573a7635723f799..974625273fe6dfcfc402069e6ee8b0e2834b2475 100644 (file)
@@ -1,4 +1,5 @@
 #include <SDL2/SDL2_gfxPrimitives.h>
+#include <SDL2/SDL_ttf.h>
 #include <sys/param.h>
 
 #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;
index 6a160861bc4a6435006da7be4d6678e8d273cdb7..9cfbcfc402f32a09292e8737f130886932c45c54 100644 (file)
@@ -3,6 +3,7 @@
 
 #if BUILD_SDL
 #include <SDL2/SDL.h>
+#include <SDL2/SDL_ttf.h>
 #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__