From 16acd7ec2790e08f8157a3391756d84f86ba1489 Mon Sep 17 00:00:00 2001 From: Un1q32 Date: Fri, 31 Jul 2026 18:23:44 -0400 Subject: [PATCH 01/15] refactor yippe --- CMakeLists.txt | 2 +- Makefile | 14 +- src/{desktop => }/backends/glfw2.c | 2 +- src/{desktop => }/backends/glfw3.c | 2 +- src/{desktop => }/backends/sdl1.c | 2 +- src/{desktop => }/backends/sdl2.c | 2 +- src/{desktop => }/backends/sdl3.c | 2 +- src/cli/main.c | 498 ++++++++++++++++++++++ src/{desktop => cli}/stb_impl.c | 0 src/{desktop/main.c => loop.c} | 640 +---------------------------- src/loop.h | 104 +++++ src/{desktop => }/platformdefs.h | 0 12 files changed, 619 insertions(+), 649 deletions(-) rename src/{desktop => }/backends/glfw2.c (99%) rename src/{desktop => }/backends/glfw3.c (99%) rename src/{desktop => }/backends/sdl1.c (99%) rename src/{desktop => }/backends/sdl2.c (99%) rename src/{desktop => }/backends/sdl3.c (99%) create mode 100644 src/cli/main.c rename src/{desktop => cli}/stb_impl.c (100%) rename src/{desktop/main.c => loop.c} (67%) create mode 100644 src/loop.h rename src/{desktop => }/platformdefs.h (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index e344daea5..e4994dad0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -323,7 +323,7 @@ if(PLATFORM STREQUAL "desktop") set(BACKEND_LIBRARIES ${SDL3_LIBRARIES}) endif() target_link_libraries(butterscotch PRIVATE ${BACKEND_LIBRARIES} ${AUDIO_LIBRARIES} ${PLATFORM_LIBRARIES}) - target_sources(butterscotch PRIVATE src/desktop/backends/${DESKTOP_BACKEND}.c) + target_sources(butterscotch PRIVATE src/backends/${BACKEND}.c) # Enable AddressSanitizer by default in Debug builds if(CMAKE_BUILD_TYPE STREQUAL "Debug") diff --git a/Makefile b/Makefile index c270d0e51..4f195d39c 100644 --- a/Makefile +++ b/Makefile @@ -43,7 +43,7 @@ INCLUDES += $(INCLUDE). \ HEADERS += $(wildcard src/*.h) $(shell find vendor -name '*.h') SRCS += $(wildcard src/*.c) $(wildcard src/image/*.c) $(wildcard vendor/bzip2/*.c) vendor/md5/md5.c vendor/sha1/sha1.c vendor/base64/base64.c -DESKTOP_BACKEND := glfw3 +BACKEND := glfw3 AUDIO_BACKEND := miniaudio ifdef BUTTERSCOTCH_COMMIT_DATE @@ -70,34 +70,34 @@ DEFINES += $(DEFINE)ENABLE_WAD17 endif # TODO: add support for non-desktop backends -SRCS += $(wildcard src/desktop/*.c) src/desktop/backends/$(DESKTOP_BACKEND).c +SRCS += $(wildcard src/desktop/*.c) src/backends/$(BACKEND).c ifeq ($(OS),Windows) PKG_CONFIG_FLAGS := --static endif INCLUDES += $(INCLUDE)src/desktop -ifeq ($(DESKTOP_BACKEND),glfw3) +ifeq ($(BACKEND),glfw3) GLFW3_LIBS += $(shell $(PKG_CONFIG) $(PKG_CONFIG_FLAGS) --libs glfw3) LIBS += $(GLFW3_LIBS) DEFINES += $(DEFINE)USE_GLFW3 ENABLE_GLAD := 1 endif -ifeq ($(DESKTOP_BACKEND),glfw2) +ifeq ($(BACKEND),glfw2) GLFW2_LIBS += $(shell $(PKG_CONFIG) $(PKG_CONFIG_FLAGS) --libs libglfw) LIBS += $(GLFW2_LIBS) DEFINES += $(DEFINE)USE_GLFW2 ENABLE_GLAD := 1 endif -ifeq ($(DESKTOP_BACKEND),sdl1) +ifeq ($(BACKEND),sdl1) SDL1_LIBS += $(shell $(PKG_CONFIG) $(PKG_CONFIG_FLAGS) --libs sdl) LIBS += $(SDL1_LIBS) DEFINES += $(DEFINE)USE_SDL1 endif -ifeq ($(DESKTOP_BACKEND),sdl2) +ifeq ($(BACKEND),sdl2) SDL2_LIBS += $(shell $(PKG_CONFIG) $(PKG_CONFIG_FLAGS) --libs sdl2) LIBS += $(SDL2_LIBS) DEFINES += $(DEFINE)USE_SDL2 endif -ifeq ($(DESKTOP_BACKEND),sdl3) +ifeq ($(BACKEND),sdl3) SDL3_LIBS += $(shell $(PKG_CONFIG) $(PKG_CONFIG_FLAGS) --libs sdl3) LIBS += $(SDL3_LIBS) DEFINES += $(DEFINE)USE_SDL3 diff --git a/src/desktop/backends/glfw2.c b/src/backends/glfw2.c similarity index 99% rename from src/desktop/backends/glfw2.c rename to src/backends/glfw2.c index 0a3001089..3f07ef37e 100644 --- a/src/desktop/backends/glfw2.c +++ b/src/backends/glfw2.c @@ -12,7 +12,7 @@ #include "common.h" #include "input_recording.h" -#include "desktop/platformdefs.h" +#include "platformdefs.h" #include "gettime.h" #include "runner_mouse.h" diff --git a/src/desktop/backends/glfw3.c b/src/backends/glfw3.c similarity index 99% rename from src/desktop/backends/glfw3.c rename to src/backends/glfw3.c index 6abfccf1a..23af551ee 100644 --- a/src/desktop/backends/glfw3.c +++ b/src/backends/glfw3.c @@ -14,7 +14,7 @@ #include "common.h" #include "input_recording.h" -#include "desktop/platformdefs.h" +#include "platformdefs.h" #include "gettime.h" #include "runner_mouse.h" diff --git a/src/desktop/backends/sdl1.c b/src/backends/sdl1.c similarity index 99% rename from src/desktop/backends/sdl1.c rename to src/backends/sdl1.c index e1917d80c..cc65997c0 100644 --- a/src/desktop/backends/sdl1.c +++ b/src/backends/sdl1.c @@ -8,7 +8,7 @@ #include "common.h" #include "input_recording.h" -#include "desktop/platformdefs.h" +#include "platformdefs.h" #include "gettime.h" #ifndef SDL_BUTTON_WHEELUP diff --git a/src/desktop/backends/sdl2.c b/src/backends/sdl2.c similarity index 99% rename from src/desktop/backends/sdl2.c rename to src/backends/sdl2.c index 7b13a7294..da31683bb 100644 --- a/src/desktop/backends/sdl2.c +++ b/src/backends/sdl2.c @@ -5,7 +5,7 @@ #include "common.h" #include "input_recording.h" -#include "desktop/platformdefs.h" +#include "platformdefs.h" #include "gettime.h" #include "runner_mouse.h" diff --git a/src/desktop/backends/sdl3.c b/src/backends/sdl3.c similarity index 99% rename from src/desktop/backends/sdl3.c rename to src/backends/sdl3.c index 4c3c8c237..7c9f6bee2 100644 --- a/src/desktop/backends/sdl3.c +++ b/src/backends/sdl3.c @@ -5,7 +5,7 @@ #include "common.h" #include "input_recording.h" -#include "desktop/platformdefs.h" +#include "platformdefs.h" #include "gettime.h" #include #include "runner_mouse.h" diff --git a/src/cli/main.c b/src/cli/main.c new file mode 100644 index 000000000..402083558 --- /dev/null +++ b/src/cli/main.c @@ -0,0 +1,498 @@ +#include +#include + +/* For SDL_main */ +#if defined(USE_SDL1) +#include +#elif defined(USE_SDL2) +#include +#elif defined(USE_SDL3) +#include +#endif + +static bool parseOsTypeArg(const char* s, YoYoOperatingSystem* out) { + forEach(const OsTypeNameEntry, entry, OS_TYPE_NAMES, OS_TYPE_NAMES_COUNT) { + if (strcmp(s, entry->name) == 0) { + *out = entry->value; + return true; + } + } + return false; +} + +static void printOsTypeNames(FILE* out) { + forEachIndexed(const OsTypeNameEntry, entry, i, OS_TYPE_NAMES, OS_TYPE_NAMES_COUNT) { + fprintf(out, "%s%s", i > 0 ? ", " : "", entry->name); + } +} + +static void printUsage(const char *argv0) { + fprintf( + stderr, + "Usage: %s \n" + " --help - Show this message\n" + " --screenshot - Specify the filename for screenshots\n" + " --screenshot-at-frame - Take a screenshot at the specified frame\n" + " --screenshot-surfaces - Take a screenshot of all surfaces at the specified frame\n" + " --screenshot-surfaces-at-frame - Specify the filename for surface screenshots\n" +#ifndef USE_GLFW2 + " --headless - Launch without a window\n" +#endif + " --print-rooms - Print all rooms in the game and exit\n" + " --print-objects - Print all objects in the game and exit\n" + " --print-shaders - Print all shaders in the game and exit\n" + " --print-declared-functions - Print all declared functions in the game and exit\n" + " --print-unknown-functions - Print all unknown functions used by the game and exit\n" + " --trace-variable-reads - Trace variable reads\n" + " --trace-variable-writes - Trace variable writes\n" + " --trace-function-calls - Trace function calls\n" + " --trace-alarms - Trace alarms\n" + " --trace-instance-lifecycles - Trace instance creations and deletions\n" + " --trace-events - Trace events\n" + " --trace-collisions - Trace collisions between instances\n" + " --trace-event-inherited - Trace event inherited calls\n" + " --trace-tiles - Trace drawn tiles\n" + " --trace-opcodes - Trace opcodes\n" + " --trace-stack - Trace stack\n" + " --trace-frames - Log frametimes\n" + " --always-log-unknown-functions - Always log unknown function calls instead of once per script\n" + " --always-log-stubbed-functions - Always log stubbed function calls instead of once per script\n" + " --exit-at-frame - Exit at the specified frame\n" + " --trace-bytecode-after-frame - Delay stack and opcode tracing until the specified frame\n" + " --dump-frame - Dump the runner state at the specified frame\n" + " --dump-frame-json - Dump the runner state in json at the specified frame\n" + " --dump-frame-json-file - Specify an output file for runner state dumps\n" + " --speed - Set a normal speed multiplier\n" + " --fast-forward-speed - Set a fast-forward speed multiplier\n" + " --seed - Seed for the random number generator\n" + " --debug - Enable debug mode\n" + " --disassemble