mirror of
https://github.com/johndoe6345789/SDL3CPlusPlus.git
synced 2026-04-24 13:44:58 +00:00
add lua debug mode
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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<float>(now - start).count();
|
||||
|
||||
10
src/main.cpp
10
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';
|
||||
|
||||
@@ -18,12 +18,14 @@ std::array<float, 16> 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");
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user