Skip to content

Commit

Permalink
move objects into scenes
Browse files Browse the repository at this point in the history
  • Loading branch information
arozx committed Jan 28, 2025
1 parent 5302416 commit f8c383a
Show file tree
Hide file tree
Showing 21 changed files with 809 additions and 699 deletions.
469 changes: 248 additions & 221 deletions sandbox/assets/scripts/engine.lua

Large diffs are not rendered by default.

43 changes: 25 additions & 18 deletions sandbox/assets/scripts/init.lua
Original file line number Diff line number Diff line change
@@ -1,39 +1,46 @@
--- Loads the new lua files into the build directory
engine.trace("Starting init.lua execution")

-- Create build directory structure first
local buildDir = "build/assets/scripts"
local scriptDir = "sandbox/assets/scripts"

engine.trace("Creating build directory: " .. buildDir)
os.execute("mkdir -p " .. buildDir)

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

-- Read source script
-- Try to read source file directly
local sourcePath = scriptDir .. "/" .. name
local source = io.open(sourcePath, "rb")
if not source then
engine.error("Failed to open source script: " .. sourcePath)
engine.error("Could not find source script: " .. sourcePath)
return false
end

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

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

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

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

-- Only copy scripts, don't execute them
local scripts = { "main.lua", "engine.lua" }
for _, script in ipairs(scripts) do
if not copyScript(script) then
engine.error("Failed to copy " .. script)
return
end
end

-- Copy main script
copyScript("main.lua")
engine.trace("init.lua execution complete")

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

local debuggingEnabled = true

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

-- Set up UI and debug options
if debuggingEnabled then
engine.log("debuggingEnabled")
else
engine.showFPSCounter(false)
engine.showEventDebugger(false)
engine.showRendererSettings(false)
engine.showTerrainControls(false)
engine.showProfiler(false)
engine.log("Debug mode enabled")
engine.showFPSCounter(true)
engine.showRendererSettings(true)
engine.showTerrainControls(true)
end

-- Initialize scene system
local function initializeScenes()
-- Create and set up main scene
if not engine.createScene("MainScene") then
engine.error("Failed to create main scene")
return false
end

if not engine.setActiveScene("MainScene") then
engine.error("Failed to set main scene as active")
return false
end

-- Set up camera and terrain
engine.setCameraType("perspective")
engine.setCameraPosition(0, 10, 10)
engine.setCameraRotation(-45, 0)
engine.setClearColor(0.2, 0.3, 0.3, 1.0)

return true
end

-- Initialize game
if not initializeScenes() then
engine.error("Scene initialization failed")
end
118 changes: 43 additions & 75 deletions sandbox/src/SandboxApp.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
#include <pch.h>
#include "SandboxApp.h"

#include <imgui.h>
#include <pch.h>

#include "../src/Scene/SceneManager.h"
#include "../src/Scripting/LuaScriptSystem.h"

SandboxApp::SandboxApp() : Application() {
// Check script system first
auto* scriptSystem = GetScriptSystem();
Engine::LuaScriptSystem* scriptSystem = GetScriptSystem();
if (!scriptSystem) {
LOG_ERROR("Script system not available!");
return;
}

// Initialize terrain after script system is ready
m_TerrainSystem = std::make_unique<Engine::TerrainSystem>();
auto activeScene = Engine::SceneManager::Get().GetActiveScene();
if (!activeScene) {
LOG_ERROR("No active scene available!");
return;
}

// Get terrain system from active scene
m_TerrainSystem = activeScene->GetTerrainSystem();
if (!m_TerrainSystem) {
LOG_ERROR("Failed to create terrain system!");
LOG_ERROR("Failed to get terrain system from active scene!");
return;
}

Expand All @@ -28,44 +36,6 @@ SandboxApp::SandboxApp() : Application() {
}
});
}

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

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

if (!scriptSystem->ExecuteScript(scriptBuffer.str())) {
LOG_ERROR("Failed to execute initialization script");
}
} 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 All @@ -78,9 +48,10 @@ void SandboxApp::OnImGuiRender() {
for (const auto& cmd : m_CommandHistory) {
consoleOutput += cmd + "\n";
}
ImGui::InputTextMultiline("##ConsoleOutput", &consoleOutput[0], consoleOutput.size() + 1, ImVec2(-FLT_MIN, ImGui::GetTextLineHeight() * 16), ImGuiInputTextFlags_ReadOnly);
if (ImGui::GetScrollY() >= ImGui::GetScrollMaxY())
ImGui::SetScrollHereY(1.0f);
ImGui::InputTextMultiline("##ConsoleOutput", &consoleOutput[0], consoleOutput.size() + 1,
ImVec2(-FLT_MIN, ImGui::GetTextLineHeight() * 16),
ImGuiInputTextFlags_ReadOnly);
if (ImGui::GetScrollY() >= ImGui::GetScrollMaxY()) ImGui::SetScrollHereY(1.0f);
ImGui::EndChild();

// Command input
Expand All @@ -93,50 +64,50 @@ void SandboxApp::OnImGuiRender() {
if (ImGui::IsKeyPressed(ImGuiKey_UpArrow) && !m_CommandHistory.empty()) {
if (m_HistoryIndex == -1) {
m_HistoryIndex = static_cast<int>(m_CommandHistory.size()) - 1;
}
else if (m_HistoryIndex > 0) {
} else if (m_HistoryIndex > 0) {
m_HistoryIndex--;
}
if (m_CommandHistory[m_HistoryIndex].find("Error") == std::string::npos) {
std::string command = m_CommandHistory[m_HistoryIndex].substr(2); // Strip the "> " prefix
std::string command =
m_CommandHistory[m_HistoryIndex].substr(2); // Strip the "> " prefix
strncpy(inputBuffer, command.c_str(), sizeof(inputBuffer));
inputBuffer[sizeof(inputBuffer) - 1] = '\0'; // Ensure null-termination
}
else {
inputBuffer[0] = '\0'; // Clear the buffer if the command contains "Error"
inputBuffer[sizeof(inputBuffer) - 1] = '\0'; // Ensure null-termination
} else {
inputBuffer[0] = '\0'; // Clear the buffer if the command contains "Error"
}
}

// Handle down arrow key for command history navigation
if (ImGui::IsKeyPressed(ImGuiKey_DownArrow) && !m_CommandHistory.empty() && inputBuffer[0] != '\0') {
if (ImGui::IsKeyPressed(ImGuiKey_DownArrow) && !m_CommandHistory.empty() &&
inputBuffer[0] != '\0') {
if (m_HistoryIndex == -1) {
m_HistoryIndex = static_cast<int>(m_CommandHistory.size()) - 1;
}
else if (m_HistoryIndex < static_cast<int>(m_CommandHistory.size()) - 1) {
} else if (m_HistoryIndex < static_cast<int>(m_CommandHistory.size()) - 1) {
m_HistoryIndex++;
}
else {
} else {
m_HistoryIndex = -1;
inputBuffer[0] = '\0'; // Clear the buffer if at the end of history
inputBuffer[0] = '\0'; // Clear the buffer if at the end of history
}
if (m_HistoryIndex != -1 && m_CommandHistory[m_HistoryIndex].find("Error") == std::string::npos) {
std::string command = m_CommandHistory[m_HistoryIndex].substr(2); // Strip the "> " prefix
if (m_HistoryIndex != -1 &&
m_CommandHistory[m_HistoryIndex].find("Error") == std::string::npos) {
std::string command =
m_CommandHistory[m_HistoryIndex].substr(2); // Strip the "> " prefix
strncpy(inputBuffer, command.c_str(), sizeof(inputBuffer));
inputBuffer[sizeof(inputBuffer) - 1] = '\0'; // Ensure null-termination
}
else {
inputBuffer[0] = '\0'; // Clear the buffer if the command contains "Error" or at the end of history
inputBuffer[sizeof(inputBuffer) - 1] = '\0'; // Ensure null-termination
} else {
inputBuffer[0] = '\0'; // Clear the buffer if the command contains "Error" or at
// the end of history
}
}

if (ImGui::InputText("Command", inputBuffer, sizeof(inputBuffer), inputFlags)) {
std::string command = inputBuffer;
m_CommandBuffer = inputBuffer;
ExecuteCommand(m_CommandBuffer);
inputBuffer[0] = '\0'; // Clear the buffer
inputBuffer[0] = '\0'; // Clear the buffer
m_CommandBuffer.clear();
reclaimFocus = true;
m_HistoryIndex = -1; // Reset history index after executing a command
m_HistoryIndex = -1; // Reset history index after executing a command
}

if (reclaimFocus) {
Expand All @@ -151,7 +122,7 @@ void SandboxApp::ExecuteCommand(const std::string& command) {

m_CommandHistory.push_back("> " + command);

auto* scriptSystem = GetScriptSystem();
Engine::LuaScriptSystem* scriptSystem = GetScriptSystem();
if (!scriptSystem) {
m_CommandHistory.push_back("Error: Script system not available");
return;
Expand All @@ -162,16 +133,13 @@ void SandboxApp::ExecuteCommand(const std::string& command) {
if (!success) {
m_CommandHistory.push_back("Error: command execution failed");
}
}
catch (const std::exception& e) {
} catch (const std::exception& e) {
m_CommandHistory.push_back(std::string("Error: ") + e.what());
}

m_HistoryIndex = -1;
}

namespace Engine {
Application* CreateApplication() {
return new SandboxApp();
}
}
Application* CreateApplication() { return new SandboxApp(); }
} // namespace Engine
16 changes: 8 additions & 8 deletions sandbox/src/SandboxApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ class SandboxApp : public Engine::Application {
virtual void OnImGuiRender() override;

private:
std::unique_ptr<Engine::TerrainSystem> m_TerrainSystem;
bool m_ShowConsole = false;
std::string m_CommandBuffer;
std::vector<std::string> m_CommandHistory;
int m_HistoryIndex = -1;
void ExecuteCommand(const std::string& command);
void HandleConsoleInput();
Engine::TerrainSystem* m_TerrainSystem = nullptr;
bool m_ShowConsole = false;
std::string m_CommandBuffer;
std::vector<std::string> m_CommandHistory;
int m_HistoryIndex = -1;

void ExecuteCommand(const std::string& command);
void HandleConsoleInput();
};
Loading

0 comments on commit f8c383a

Please sign in to comment.