colourpicker

Simple Colour Picker written in C
git clone anongit@rnpnr.xyz:colourpicker.git
Log | Files | Refs | Feed | Submodules | README | LICENSE

Commit: d7811a9b010ea484dd651abb0fe97a33110e5047
Parent: b6ea33ae2b79f2516877ef74ea7bafd85f0b36ee
Author: Randy Palamar
Date:   Wed, 31 Jul 2024 19:51:20 -0600

generate static includes for needed runtime files

Diffstat:
M.gitignore | 8++++++--
Mbuild.sh | 7+++++++
Mcolourpicker.c | 7+++++--
Aconfig.def.h | 3+++
Agen_incs.c | 53+++++++++++++++++++++++++++++++++++++++++++++++++++++
Rpicker_shader.glsl -> hsv_lerp.glsl | 0
Mmain.c | 4++--
Mutil.c | 4++++
8 files changed, 80 insertions(+), 6 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,4 +1,8 @@ -colourpicker *.so -external/lib +colourpicker +config.h external/include +external/lib +font_inc.h +gen_incs +shader_inc.h diff --git a/build.sh b/build.sh @@ -29,6 +29,8 @@ else ldflags="-L./external/lib $ldflags" fi +[ ! -s "config.h" ] && cp config.def.h config.h + if [ "$debug" ]; then # Hot Reloading/Debugging cflags="$cflags -O0 -ggdb -D_DEBUG" @@ -41,4 +43,9 @@ if [ "$debug" ]; then ${cc} $libcflags colourpicker.c -o libcolourpicker.so $libldflags fi +if [ "hsv_lerp.glsl" -nt "shader_inc.h" ]; then + ${cc} $cflags -D_XOPEN_SOURCE=600 -o gen_incs gen_incs.c -s $ldflags + ./gen_incs +fi + ${cc} $cflags -o colourpicker main.c $ldflags diff --git a/colourpicker.c b/colourpicker.c @@ -870,8 +870,11 @@ static void do_picker_mode(ColourPickerCtx *ctx, v2 relative_origin) { if (!IsShaderReady(ctx->picker_shader)) { - /* TODO: LoadShaderFromMemory */ - ctx->picker_shader = LoadShader(0, "./picker_shader.glsl"); +#ifdef _DEBUG + ctx->picker_shader = LoadShader(0, HSV_LERP_SHADER_NAME); +#else + ctx->picker_shader = LoadShaderFromMemory(0, g_hsv_shader_text); +#endif ctx->mode_id = GetShaderLocation(ctx->picker_shader, "u_mode"); ctx->hsv_id = GetShaderLocation(ctx->picker_shader, "u_hsv"); ctx->size_id = GetShaderLocation(ctx->picker_shader, "u_size"); diff --git a/config.def.h b/config.def.h @@ -0,0 +1,3 @@ +#define FONT_SIZE 40u + +#define HSV_LERP_SHADER_NAME "hsv_lerp.glsl" diff --git a/gen_incs.c b/gen_incs.c @@ -0,0 +1,53 @@ +#include <raylib.h> + +#include <stdlib.h> +#include <stdio.h> + +#include "config.h" + +#define ISSPACE(a) ((a) == ' ' || (a) == '\t') + +int +main(void) +{ + SetConfigFlags(FLAG_WINDOW_HIDDEN); + SetTraceLogLevel(LOG_NONE); + InitWindow(640, 480, ""); + Font font = LoadFontEx("assets/Lora-SemiBold.ttf", FONT_SIZE, 0, 0); + ExportFontAsCode(font, "font_inc.h"); + CloseWindow(); + + FILE *shader_file, *out_file; + shader_file = fopen(HSV_LERP_SHADER_NAME, "r"); + out_file = fopen("shader_inc.h", "w"); + + if (!shader_file || !out_file) { + fputs("Failed to open necessary files!\n", stdout); + return 1; + } + + char *line = NULL; + size_t len = 0; + ssize_t read; + + /* NOTE: skip over license notice */ + getline(&line, &len, shader_file); + fputs("static char *g_hsv_shader_text =\n\t", out_file); + while ((read = getline(&line, &len, shader_file)) != -1) { + char *s; + for (s = line; *s; s++, read--) { + if (!ISSPACE(*s)) + break; + } + if (read > 1) { + fputc('"', out_file); + fwrite(s, read - 1, 1, out_file); + fputs("\\n\"\n\t", out_file); + } + } + fputs(";\n", out_file); + fclose(out_file); + fclose(shader_file); + + return 0; +} diff --git a/picker_shader.glsl b/hsv_lerp.glsl diff --git a/main.c b/main.c @@ -153,8 +153,8 @@ main(i32 argc, char *argv[]) SetConfigFlags(FLAG_VSYNC_HINT); InitWindow(ctx.window_size.w, ctx.window_size.h, "Colour Picker"); - ctx.font_size = 40; - ctx.font = LoadFontEx("assets/Lora-SemiBold.ttf", ctx.font_size, 0, 0); + ctx.font_size = FONT_SIZE; + ctx.font = LoadFont_FontInc(); while(!WindowShouldClose()) { do_debug(); diff --git a/util.c b/util.c @@ -7,6 +7,10 @@ #include <stddef.h> #include <stdint.h> +#include "shader_inc.h" +#include "font_inc.h" +#include "config.h" + #ifndef asm #define asm __asm__ #endif