colourpicker

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

Commit: 2886b729a546163d8c0fafbb82e40da26809e153
Parent: 92915ffca3bfa51a0449c97b6510b5f48f5842d8
Author: Randy Palamar
Date:   Thu,  8 May 2025 11:18:12 -0600

build: always use bundled raylib; don't use cmake

Diffstat:
M.gitignore | 3+--
MREADME.md | 7++-----
Mbuild.sh | 93+++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------
Mgen_incs.c | 2+-
Mmain.c | 3+--
5 files changed, 65 insertions(+), 43 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -2,6 +2,5 @@ *.so colourpicker config.h -external/include -external/lib gen_incs +out diff --git a/README.md b/README.md @@ -6,8 +6,6 @@ A basic colour picker written in C and [raylib][]. ## Requirements * C11 Compiler -* [raylib][] - Optional, a static version will be built if `USE_SYSTEM_RAYLIB` - is not set in the environment. ## Installation @@ -16,8 +14,7 @@ Run `build.sh` and copy `colourpicker` where you want it. ## Debug Hot Reloading If `DEBUG` is set in the environment then the hot reloading -version will be compiled. The program will load -`libcolourpicker.so` at runtime and reload it when it is updated. -In this case raylib **MUST** be dynamically linked. +version will be compiled. The program will load `colourpicker.so` +at runtime and reload it when it is updated. [raylib]: https://www.raylib.com/ diff --git a/build.sh b/build.sh @@ -2,61 +2,88 @@ version=1.0 -cflags=${CFLAGS:-"-march=native -O3 -Wall -Wextra"} -cflags="${cflags} -std=c11 -I./external/include -DVERSION=\"$version\"" +cflags=${CFLAGS:-"-march=native -O3"} +cflags="${cflags} -std=c11 " ldflags=${LDFLAGS:-"-flto"} -ldflags="$ldflags -lraylib -lm" +ldflags="$ldflags -lm" output="colourpicker" -debug=${DEBUG} - cc=${CC:-cc} -system_raylib=${USE_SYSTEM_RAYLIB:-$debug} + +for arg; do + case ${arg} in + debug) debug=1 + esac +done case $(uname -s) in MINGW64*) + w32=1 output="Colour Picker" - windres assets/colourpicker.rc assets/colourpicker.rc.o - ldflags="assets/colourpicker.rc.o $ldflags -mwindows -lgdi32 -lwinmm" + windres assets/colourpicker.rc out/colourpicker.rc.o + ldflags="out/colourpicker.rc.o ${ldflags} -mwindows -lgdi32 -lwinmm" ;; esac -# NOTE: clones and builds a static raylib if system lib is not requested -# NOTE: this requires cmake -if [ "$system_raylib" ]; then - ldflags="-L/usr/local/lib $ldflags" -else - if [ ! -f external/lib/libraylib.a ]; then - git submodule update --init --checkout --depth=1 external/raylib - cmake --install-prefix="${PWD}/external" \ - -G "Ninja" -B external/raylib/build -S external/raylib \ - -D CMAKE_INSTALL_LIBDIR=lib -D CMAKE_BUILD_TYPE="Release" \ - -D CUSTOMIZE_BUILD=ON -D WITH_PIC=ON -D BUILD_EXAMPLES=OFF - cmake --build external/raylib/build - cmake --install external/raylib/build +build_raylib() +{ + cp external/raylib/src/raylib.h external/raylib/src/rlgl.h out/ + src=external/raylib/src + srcs="rcore rglfw rshapes rtext rtextures utils" + + raylib_cmd="${cflags} -I./external/raylib/src -DPLATFORM_DESKTOP_GLFW" + raylib_cmd="${raylib_cmd} -Wno-unused-but-set-variable -Wno-unused-parameter" + [ ! ${w32} ] && raylib_cmd="${raylib_cmd} -D_GLFW_X11" + + if [ ${debug} ]; then + files="" + for n in ${srcs}; do + files="${files} ${src}/${n}.c" + done + raylib_cmd="${raylib_cmd} -DBUILD_LIBTYPE_SHARED -D_GLFW_BUILD_DLL" + raylib_cmd="${raylib_cmd} -fPIC -shared" + raylib_cmd="${raylib_cmd} ${files} -o ${raylib}" + [ ${w32} ] && raylib_cmd="${raylib_command} -L. -lgdi32 -lwinmm" + ${cc} ${raylib_cmd} + else + outs="" + for n in ${srcs}; do + ${cc} ${raylib_cmd} -c "${src}/${n}.c" -o "out/${n}.o" + outs="${outs} out/${n}.o" + done + + ar rc "${raylib}" ${outs} fi - ldflags="-L./external/lib $ldflags" +} + +if [ $(git diff-index --quiet HEAD -- external/raylib) ]; then + git submodule update --init --checkout --depth=1 external/raylib fi [ ! -s "config.h" ] && cp config.def.h config.h -if [ ! -e "external/include/shader_inc.h" ] || [ "slider_lerp.glsl" -nt "external/include/shader_inc.h" ]; then - ${cc} $cflags -o gen_incs gen_incs.c $ldflags +mkdir -p out +raylib=out/libraylib.a +[ ${debug} ] && raylib="libraylib.so" +[ "./build.sh" -nt ${raylib} ] || [ ! -f ${raylib} ] && build_raylib + +cflags="${cflags} -Wall -Wextra -Iout" + +if [ ! -e "out/shader_inc.h" ] || [ "slider_lerp.glsl" -nt "out/shader_inc.h" ]; then + ${cc} ${cflags} -o gen_incs gen_incs.c ${ldflags} ${raylib} ./gen_incs - mv lora_sb*.h external/include/ + mv lora_sb*.h out/ fi if [ "$debug" ]; then # Hot Reloading/Debugging - cflags="$cflags -O0 -ggdb -D_DEBUG -Wno-unused-function" - # NOTE: needed for sync(3p) - cflags="$cflags -D_XOPEN_SOURCE=700" + cflags="${cflags} -O0 -ggdb -D_DEBUG -Wno-unused-function" - libcflags="$cflags -fPIC" - libldflags="$ldflags -shared" - - ${cc} $libcflags colourpicker.c -o libcolourpicker.so $libldflags + ${cc} ${cflags} -fPIC -shared colourpicker.c -o colourpicker.so + ldflags="${ldflags} -Wl,-rpath,. ${raylib}" +else + ldflags="${raylib} ${ldflags}" fi -${cc} $cflags -o "$output" main.c $ldflags +${cc} ${cflags} -DVERSION="\"${version}\"" main.c -o "${output}" ${ldflags} diff --git a/gen_incs.c b/gen_incs.c @@ -159,7 +159,7 @@ main(void) export_font_as_code("assets/Lora-SemiBold.ttf", (char *)tmem.data, font_sizes[i], rmem); } - FILE *out_file = fopen("external/include/shader_inc.h", "w"); + FILE *out_file = fopen("out/shader_inc.h", "w"); if (!out_file) { fputs("Failed to open necessary files!\n", stdout); return 1; diff --git a/main.c b/main.c @@ -13,7 +13,7 @@ typedef struct timespec Filetime; -global const char *libname = "./libcolourpicker.so"; +global const char *libname = "./colourpicker.so"; global void *libhandle; typedef void (do_colour_picker_fn)(ColourPickerCtx *, f32 dt, Vector2 window_pos, Vector2 mouse); @@ -55,7 +55,6 @@ do_debug(void) local_persist Filetime updated_time; Filetime test_time = get_filetime(libname); if (compare_filetime(test_time, updated_time)) { - sync(); load_library(libname); updated_time = test_time; }