Skip to content

Commit

Permalink
update lua build files on run
Browse files Browse the repository at this point in the history
  • Loading branch information
arozx committed Jan 24, 2025
1 parent 16ecd38 commit 4b1b674
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 10 deletions.
46 changes: 37 additions & 9 deletions sandbox/assets/scripts/init.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
-- Initialize Lua environment
if not engine then
error("Engine API not available")
--- Loads the new lua files into the build directory

local function copyScript(name)
-- Define relative paths
local sourcePath = "sandbox/assets/scripts/" .. name
local destPath = "build/assets/scripts/" .. name

-- Read source script
local source = io.open(sourcePath, "rb")
if not source then
engine.error("Failed to open source script: " .. sourcePath)
return false
end

local content = source:read("*all")
source:close()

-- Write to build directory
local dest = io.open(destPath, "wb")
if not dest then
engine.error("Failed to create build script: " .. destPath)
return false
end

local success = dest:write(content)
dest:close()

if success then
engine.trace("Successfully copied: " .. name)
return true
else
engine.error("Failed to write: " .. destPath)
return false
end
end

-- Set default terrain parameters
engine.setTerrainHeight(10.0)
engine.trace("Lua environment initialized")
engine.log("Lua environment initialized")
engine.warn("Lua environment initialized")
engine.error("Lua environment initialized")
-- Copy main script
copyScript("main.lua")

20 changes: 20 additions & 0 deletions sandbox/assets/scripts/main.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- Initialize Lua environment
if not engine then
error("Engine API not available")
end

local debuggingEnabled = true

-- Set default terrain parameters
engine.setTerrainHeight(10.0)

if debuggingEnabled then
engine.log("debuggingEnabled")
else
engine.showFPSCounter(false)
engine.showEventDebugger(false)
engine.showRendererSettings(false)
engine.showTerrainControls(false)
engine.showProfiler(false)
end

19 changes: 19 additions & 0 deletions sandbox/src/SandboxApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,25 @@ SandboxApp::SandboxApp() : Application() {
} catch (const std::exception& e) {
LOG_ERROR("Script initialization error: {", e.what(), "}");
}

// Run main.lua script
try {
std::string scriptPath = "build/assets/scripts/main.lua";
std::ifstream scriptFile(scriptPath);
if (!scriptFile.is_open()) {
LOG_ERROR_CONCAT("Failed to open main.lua at path: ", scriptPath);
return;
}

std::stringstream scriptBuffer;
scriptBuffer << scriptFile.rdbuf();

if (!scriptSystem->ExecuteScript(scriptBuffer.str())) {
LOG_ERROR("Failed to execute main script");
}
} catch (const std::exception& e) {
LOG_ERROR("Script initialization error: {", e.what(), "}");
}
}

void SandboxApp::OnImGuiRender() {
Expand Down
2 changes: 2 additions & 0 deletions src/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ namespace Engine {

const std::string& GetAssetPath() const { return m_AssetPath; }

ImGuiOverlay* GetImGuiOverlay() { return m_ImGuiOverlay.get(); }

protected:
/**
* @brief Initialize the application window
Expand Down
36 changes: 35 additions & 1 deletion src/Scripting/LuaScriptSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,41 @@ namespace Engine {
});

// ImGui Overlay controls

engine.set_function("showTransformControls", [](bool show) {
if (auto* overlay = Application::Get().GetImGuiOverlay()) {
overlay->ShowTransformControls(show);
}
});

engine.set_function("showProfiler", [](bool show) {
if (auto* overlay = Application::Get().GetImGuiOverlay()) {
overlay->ShowProfiler(show);
}
});

engine.set_function("showRendererSettings", [](bool show) {
if (auto* overlay = Application::Get().GetImGuiOverlay()) {
overlay->ShowRendererSettings(show);
}
});

engine.set_function("showEventDebugger", [](bool show) {
if (auto* overlay = Application::Get().GetImGuiOverlay()) {
overlay->ShowEventDebugger(show);
}
});

engine.set_function("showTerrainControls", [](bool show) {
if (auto* overlay = Application::Get().GetImGuiOverlay()) {
overlay->ShowTerrainControls(show);
}
});

engine.set_function("showFPSCounter", [](bool show) {
if (auto* overlay = Application::Get().GetImGuiOverlay()) {
overlay->ShowFPSCounter(show);
}
});

// Constants
auto keyCodes = m_LuaState->create_named_table("KeyCode");
Expand Down
6 changes: 6 additions & 0 deletions src/UI/ImGuiOverlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace Engine {
float currentFPS, float averageFPS, float frameTime,
float fps1PercentLow, float fps1PercentHigh)
{
if (!m_ShowFPSCounter) return; // Early return if FPS counter is disabled
m_FPSCounter.Update(frameTime);

if (showFPSCounter) {
Expand Down Expand Up @@ -121,6 +122,7 @@ namespace Engine {
* @param renderObject Object whose transform is being controlled
*/
void ImGuiOverlay::RenderTransformControls(RenderObject& renderObject) {
if (!m_ShowTransformControls) return;
if (ImGui::Begin("Transform Controls")) {
auto& transform = renderObject.GetTransform();

Expand All @@ -135,6 +137,7 @@ namespace Engine {
* @brief Render profiler window showing performance metrics
*/
void ImGuiOverlay::RenderProfiler() {
if (!m_ShowProfiler) return;
ImGui::Begin("Profiler", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
bool enabled = Profiler::Get().IsEnabled();
if (ImGui::Checkbox("Enable Profiling", &enabled)) {
Expand Down Expand Up @@ -193,6 +196,7 @@ namespace Engine {
* @details Controls settings like back-face culling
*/
void ImGuiOverlay::RenderRendererSettings() {
if (!m_ShowRendererSettings) return;
ImGui::Begin("Renderer");
bool cullingEnabled = glIsEnabled(GL_CULL_FACE);
if (ImGui::Checkbox("Enable Back-face Culling", &cullingEnabled)) {
Expand All @@ -212,6 +216,7 @@ namespace Engine {
* @details Shows recent events and their details
*/
void ImGuiOverlay::RenderEventDebugger() {
if (!m_ShowEventDebugger) return;
if (ImGui::Begin("Recent Events", &m_ShowEventDebugger)) {
ImGui::Text("Last 5 Events:");
ImGui::Separator();
Expand Down Expand Up @@ -248,6 +253,7 @@ namespace Engine {
* @details Controls terrain parameters like chunk range, seed, and height settings
*/
void ImGuiOverlay::RenderTerrainControls(TerrainSystem& terrainSystem) {
if (!m_ShowTerrainControls) return;
if (ImGui::Begin("Terrain Controls")) {
// Chunk range control
int chunkRange = terrainSystem.GetChunkRange();
Expand Down

0 comments on commit 4b1b674

Please sign in to comment.