From 845aea848b6ee9fbf6fc7cc897c17d8db9482e31 Mon Sep 17 00:00:00 2001 From: Richard Ward Date: Fri, 19 Dec 2025 12:13:22 +0000 Subject: [PATCH] add lua debug mode --- scripts/cube_logic.lua | 24 +++++++++++++++++++----- src/app/sdl3_app.hpp | 2 +- src/app/sdl3_app_core.cpp | 30 ++++++++++++++---------------- src/main.cpp | 10 +++++++++- src/script/cube_script.cpp | 6 ++++-- src/script/cube_script.hpp | 3 ++- 6 files changed, 49 insertions(+), 26 deletions(-) diff --git a/scripts/cube_logic.lua b/scripts/cube_logic.lua index a36e1d1..b1b7293 100644 --- a/scripts/cube_logic.lua +++ b/scripts/cube_logic.lua @@ -36,6 +36,8 @@ local pyramid_indices = { } local math3d = require("math3d") +local string_format = string.format +local table_concat = table.concat local InputState = {} InputState.__index = InputState @@ -79,6 +81,13 @@ end gui_input = InputState:new() +local function log_debug(fmt, ...) + if not lua_debug or not fmt then + return + end + print(string_format(fmt, ...)) +end + local rotation_speeds = {x = 0.5, y = 0.7} local shader_variants = { @@ -140,6 +149,7 @@ local function update_camera_zoom(delta) camera.eye[1] = camera.center[1] + normalizedX * targetDistance camera.eye[2] = camera.center[2] + normalizedY * targetDistance camera.eye[3] = camera.center[3] + normalizedZ * targetDistance + log_debug("zoom delta=%.2f -> distance=%.2f", delta, targetDistance) end local function build_model(time) @@ -179,22 +189,26 @@ local function create_pyramid(position, shader_key) end function get_scene_objects() - return { + local objects = { create_cube({0.0, 0.0, 0.0}, 1.0, "cube"), create_cube({3.0, 0.0, 0.0}, 0.8, "cube"), create_cube({-3.0, 0.0, 0.0}, 1.2, "cube"), create_pyramid({0.0, -0.5, -4.0}, "pyramid"), } + if lua_debug then + local labels = {} + for idx, obj in ipairs(objects) do + table.insert(labels, string_format("[%d:%s]", idx, obj.shader_key)) + end + log_debug("get_scene_objects -> %d entries: %s", #objects, table_concat(labels, ", ")) + end + return objects end function get_shader_paths() return shader_variants end -function get_gui_commands() - return {} -end - function get_view_projection(aspect) if gui_input then update_camera_zoom(gui_input.wheel) diff --git a/src/app/sdl3_app.hpp b/src/app/sdl3_app.hpp index 3f3fbc3..d314b3c 100644 --- a/src/app/sdl3_app.hpp +++ b/src/app/sdl3_app.hpp @@ -52,7 +52,7 @@ struct SwapChainSupportDetails { class Sdl3App { public: - explicit Sdl3App(const std::filesystem::path& scriptPath); + explicit Sdl3App(const std::filesystem::path& scriptPath, bool luaDebug = false); void Run(); private: diff --git a/src/app/sdl3_app_core.cpp b/src/app/sdl3_app_core.cpp index 19f4153..1d0f22d 100644 --- a/src/app/sdl3_app_core.cpp +++ b/src/app/sdl3_app_core.cpp @@ -81,7 +81,8 @@ void ThrowSdlErrorIfFailed(bool success, const char* context) { } // namespace -Sdl3App::Sdl3App(const std::filesystem::path& scriptPath) : cubeScript_(scriptPath) { +Sdl3App::Sdl3App(const std::filesystem::path& scriptPath, bool luaDebug) + : cubeScript_(scriptPath, luaDebug) { TRACE_FUNCTION(); TRACE_VAR(scriptPath); } @@ -140,25 +141,22 @@ void Sdl3App::MainLoop() { running = false; } else if (event.type == SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED) { framebufferResized_ = true; - } else if (guiHasCommands_) { - ProcessGuiEvent(event); } + ProcessGuiEvent(event); } - if (guiHasCommands_) { - float mouseX = 0.0f; - float mouseY = 0.0f; - SDL_GetMouseState(&mouseX, &mouseY); - guiInputSnapshot_.mouseX = mouseX; - guiInputSnapshot_.mouseY = mouseY; - cubeScript_.UpdateGuiInput(guiInputSnapshot_); - if (guiRenderer_) { - guiCommands_ = cubeScript_.LoadGuiCommands(); - guiRenderer_->Prepare(guiCommands_, swapChainExtent_.width, swapChainExtent_.height); - } - guiInputSnapshot_.wheel = 0.0f; - guiInputSnapshot_.textInput.clear(); + float mouseX = 0.0f; + float mouseY = 0.0f; + SDL_GetMouseState(&mouseX, &mouseY); + guiInputSnapshot_.mouseX = mouseX; + guiInputSnapshot_.mouseY = mouseY; + cubeScript_.UpdateGuiInput(guiInputSnapshot_); + if (guiHasCommands_ && guiRenderer_) { + guiCommands_ = cubeScript_.LoadGuiCommands(); + guiRenderer_->Prepare(guiCommands_, swapChainExtent_.width, swapChainExtent_.height); } + guiInputSnapshot_.wheel = 0.0f; + guiInputSnapshot_.textInput.clear(); auto now = std::chrono::steady_clock::now(); float time = std::chrono::duration(now - start).count(); diff --git a/src/main.cpp b/src/main.cpp index 2c63e26..a932f72 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -42,6 +42,7 @@ struct RuntimeConfig { uint32_t width = sdl3cpp::app::kWidth; uint32_t height = sdl3cpp::app::kHeight; std::filesystem::path scriptPath; + bool luaDebug = false; }; RuntimeConfig GenerateDefaultRuntimeConfig(const char* argv0) { @@ -126,6 +127,13 @@ RuntimeConfig LoadRuntimeConfigFromJson(const std::filesystem::path& configPath, config.width = parseDimension("window_width", config.width); config.height = parseDimension("window_height", config.height); + if (document.HasMember("lua_debug")) { + const auto& value = document["lua_debug"]; + if (!value.IsBool()) { + throw std::runtime_error("JSON member 'lua_debug' must be a boolean"); + } + config.luaDebug = value.GetBool(); + } return config; } @@ -300,7 +308,7 @@ int main(int argc, char** argv) { throw std::runtime_error("Unable to determine platform config directory"); } } - sdl3cpp::app::Sdl3App app(options.runtimeConfig.scriptPath); + sdl3cpp::app::Sdl3App app(options.runtimeConfig.scriptPath, options.runtimeConfig.luaDebug); app.Run(); } catch (const std::exception& e) { std::cerr << "ERROR: " << e.what() << '\n'; diff --git a/src/script/cube_script.cpp b/src/script/cube_script.cpp index 58ddb28..2524d0f 100644 --- a/src/script/cube_script.cpp +++ b/src/script/cube_script.cpp @@ -18,12 +18,14 @@ std::array IdentityMatrix() { } // namespace -CubeScript::CubeScript(const std::filesystem::path& scriptPath) - : L_(luaL_newstate()), scriptDirectory_(scriptPath.parent_path()) { +CubeScript::CubeScript(const std::filesystem::path& scriptPath, bool debugEnabled) + : L_(luaL_newstate()), scriptDirectory_(scriptPath.parent_path()), debugEnabled_(debugEnabled) { if (!L_) { throw std::runtime_error("Failed to create Lua state"); } luaL_openlibs(L_); + lua_pushboolean(L_, debugEnabled_); + lua_setglobal(L_, "lua_debug"); auto scriptDir = scriptPath.parent_path(); if (!scriptDir.empty()) { lua_getglobal(L_, "package"); diff --git a/src/script/cube_script.hpp b/src/script/cube_script.hpp index b178336..d98235d 100644 --- a/src/script/cube_script.hpp +++ b/src/script/cube_script.hpp @@ -68,7 +68,7 @@ public: using GuiColor = ::sdl3cpp::script::GuiColor; public: - explicit CubeScript(const std::filesystem::path& scriptPath); + explicit CubeScript(const std::filesystem::path& scriptPath, bool debugEnabled = false); ~CubeScript(); struct ShaderPaths { @@ -107,6 +107,7 @@ private: int guiInputRef_ = LUA_REFNIL; int guiCommandsFnRef_ = LUA_REFNIL; std::filesystem::path scriptDirectory_; + bool debugEnabled_ = false; }; } // namespace sdl3cpp::script