From 9e85763f9e454e6f048aa51f2e4243f3facc2713 Mon Sep 17 00:00:00 2001 From: Daanish Date: Sun, 3 May 2026 23:00:41 +0100 Subject: [PATCH] Rearrange & setup --- .gitignore | 4 ++- .gitmodules | 1 + Makefile | 16 +++++++++--- include/app/app.h | 25 +++++++++++++++++++ include/binding/ruby.h | 13 ++++++++++ include/pch.h | 40 ++++++++++++++++++++++++++++++ include/window/window.h | 4 +-- samples/button.rb | 6 ++--- samples/main.rb | 25 ++++++++----------- setup.rb | 14 +++++++++++ src/{scene/scene.cc => app/app.cc} | 0 src/main.cc | 5 +--- 12 files changed, 125 insertions(+), 28 deletions(-) create mode 100644 include/app/app.h create mode 100644 include/binding/ruby.h create mode 100644 setup.rb rename src/{scene/scene.cc => app/app.cc} (100%) diff --git a/.gitignore b/.gitignore index c50c3c5..8d6cd86 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ **old** build/* -bin/* \ No newline at end of file +bin/* + +.clangd \ No newline at end of file diff --git a/.gitmodules b/.gitmodules index 4eebb7f..6cbca6f 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,4 @@ [submodule "libs/mruby"] path = libs/mruby url = https://github.com/mruby/mruby.git + ignore = dirty diff --git a/Makefile b/Makefile index b61f350..378f041 100644 --- a/Makefile +++ b/Makefile @@ -38,6 +38,10 @@ CFLAGS_RELEASE := \ PCH_CFLAGS_DEBUG := $(CFLAGS_DEBUG) -x c++-header PCH_CFLAGS_RELEASE := $(CFLAGS_RELEASE) -x c++-header +# ---------------- LIBS ---------------- + +LIBS := ./libs/mruby/build/host/lib/libmruby.a + # ---------------- SOURCES ---------------- SRC := $(wildcard $(SRC_DIR)/*.cc) $(wildcard $(SRC_DIR)/**/*.cc) @@ -50,7 +54,7 @@ DEP_RELEASE := $(OBJ_RELEASE:.o=.d) # ---------------- TARGETS ---------------- -.PHONY: all debug release clean +.PHONY: all debug release clean setup all: debug @@ -58,6 +62,12 @@ debug: $(TARGET_DEBUG) release: $(TARGET_RELEASE) +# ---------------- CLANGD ---------------- + +setup: setup.rb + @echo "Setting up clangd configuration..." + ruby setup.rb + # ---------------- PCH ---------------- $(PCH_DEBUG): $(INCLUDE_DIR)/pch.h @@ -72,11 +82,11 @@ $(PCH_RELEASE): $(INCLUDE_DIR)/pch.h $(TARGET_DEBUG): $(PCH_DEBUG) $(OBJ_DEBUG) mkdir -p $(BIN_DIR) - $(CXX) $(CFLAGS_DEBUG) -o $@ $(OBJ_DEBUG) $(SDL_LIBS_DEBUG) + $(CXX) $(CFLAGS_DEBUG) -o $@ $(OBJ_DEBUG) $(SDL_LIBS_DEBUG) $(LIBS) $(TARGET_RELEASE): $(PCH_RELEASE) $(OBJ_RELEASE) mkdir -p $(BIN_DIR) - $(CXX) $(CFLAGS_RELEASE) -o $@ $(OBJ_RELEASE) $(SDL_LIBS_RELEASE) + $(CXX) $(CFLAGS_RELEASE) -o $@ $(OBJ_RELEASE) $(SDL_LIBS_RELEASE) $(LIBS) # ---------------- COMPILE ---------------- diff --git a/include/app/app.h b/include/app/app.h new file mode 100644 index 0000000..c5f459a --- /dev/null +++ b/include/app/app.h @@ -0,0 +1,25 @@ +#include "binding/ruby.h" +#include "utils.h" +#include "window/window.h" + +struct Element { + Vec2 position; + float rotation; + Vec2 scale; + float z; + Ruby::Block on_click; + Ruby::Block on_update; + Ruby::Block on_hover; + + virtual void render(Window &window) {} +}; + +struct Scene { + std::vector elements; +}; + +struct App { + Window window; + + App(Vec2 size, const char *title) : window(size, title) {} +}; \ No newline at end of file diff --git a/include/binding/ruby.h b/include/binding/ruby.h new file mode 100644 index 0000000..bb7de47 --- /dev/null +++ b/include/binding/ruby.h @@ -0,0 +1,13 @@ +#include "pch.h" +#include "utils.h" + +namespace Ruby { +extern mrb_state *mrb; +struct Block { + mrb_value proc; + + Block(mrb_value proc) : proc(proc) { + mrb_gc_protect(mrb, proc); + } +}; +}; // namespace Ruby \ No newline at end of file diff --git a/include/pch.h b/include/pch.h index 2e86570..84842b8 100644 --- a/include/pch.h +++ b/include/pch.h @@ -1,15 +1,55 @@ #ifndef PCH_H #define PCH_H +#include "mruby.h" +#include "mruby/array.h" +#include "mruby/compile.h" +#include "mruby/hash.h" +#include "mruby/irep.h" +#include "mruby/string.h" + #include #include #include #include #include +#include +#include +#include #include +#include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include #endif \ No newline at end of file diff --git a/include/window/window.h b/include/window/window.h index 518bc1c..8eb0b0b 100644 --- a/include/window/window.h +++ b/include/window/window.h @@ -1,8 +1,8 @@ #ifndef WINDOW_H #define WINDOW_H -#include "../pch.h" -#include "../utils.h" +#include "pch.h" +#include "utils.h" using ImageID = uint32_t; using FontID = uint32_t; diff --git a/samples/button.rb b/samples/button.rb index 6a9f713..23ed3e4 100644 --- a/samples/button.rb +++ b/samples/button.rb @@ -1,4 +1,4 @@ -window = Window.new 400, 400, title: "Button Test" +app = App.new 400, 400, title: "Button Test" main_scene = Scene.new @@ -18,6 +18,4 @@ end main_scene << text main_scene << rect -window.scenes[:main_scene] = main_scene - -window.start :main_scene +app.start main_scene \ No newline at end of file diff --git a/samples/main.rb b/samples/main.rb index f692146..fee3521 100644 --- a/samples/main.rb +++ b/samples/main.rb @@ -5,15 +5,15 @@ width, height = 720, 480 image = Image.new "assets/loading.png" -window = Window.new width, height, title: "My Game", loading_bg: image +app = App.new width, height, title: "My Game", loading_bg: image -window.map_key ?w, :forward -window.map_key ?s, :backward -window.map_key ?a, :left -window.map_key ?d, :right +app.map_key ?w, :forward +app.map_key ?s, :backward +app.map_key ?a, :left +app.map_key ?d, :right -window.map_key ?h, :heal -window.map_key ?t, :place +app.map_key ?h, :heal +app.map_key ?t, :place menu_scene = Scene.new @@ -21,14 +21,14 @@ default_font = Font.new "assets/DejaVuSans.ttf", 12, italic: true start_button = ElementText.new "Start Game", font: default_font, position: {x: 100, y: 100} +game_scene = Scene.new + start_button.on_click do - window.start :game_scene + app.start game_scene end menu_scene << start_button -game_scene = Scene.new - world = ElementWorld.new # world.cam # camera object to control the view of the world, has position, zoom, rotation @@ -183,7 +183,4 @@ world.shader :fog do |shader| z = 100 # shaders always appear above normal sprites but z is used to sort them relative to each other, so a shader with z 100 will appear above a shader with z 50, but both will appear above normal sprites end -window.scenes[:menu_scene] = menu_scene -window.scenes[:game_scene] = game_scene - -window.start :menu_scene # first scene to show when the game starts, stops the loading bg +app.start menu_scene # first scene to show when the game starts, stops the loading bg diff --git a/setup.rb b/setup.rb new file mode 100644 index 0000000..f144455 --- /dev/null +++ b/setup.rb @@ -0,0 +1,14 @@ +dir = File.expand_path(__dir__) + +clangd = <