somthing, i forgoted

This commit is contained in:
OusmBlueNinja 2025-04-18 11:50:25 -05:00
parent fba1465991
commit e409bed53f
11 changed files with 245 additions and 107 deletions

View File

@ -10,24 +10,24 @@ Collapsed=1
[Window][WindowOverViewport_11111111] [Window][WindowOverViewport_11111111]
Pos=0,19 Pos=0,19
Size=1280,701 Size=1920,1158
Collapsed=0 Collapsed=0
[Window][Inspector] [Window][Inspector]
Pos=890,19 Pos=1530,19
Size=390,505 Size=390,835
Collapsed=0 Collapsed=0
DockId=0x00000005,0 DockId=0x00000005,0
[Window][Scene Tree] [Window][Scene Tree]
Pos=0,19 Pos=0,19
Size=263,701 Size=263,1158
Collapsed=0 Collapsed=0
DockId=0x00000001,0 DockId=0x00000001,0
[Window][Viewport] [Window][Viewport]
Pos=265,19 Pos=265,19
Size=623,329 Size=1263,786
Collapsed=0 Collapsed=0
DockId=0x00000007,0 DockId=0x00000007,0
@ -36,14 +36,14 @@ Size=1280,19
Collapsed=0 Collapsed=0
[Window][Performance Info] [Window][Performance Info]
Pos=890,526 Pos=1530,856
Size=390,194 Size=390,321
Collapsed=0 Collapsed=0
DockId=0x00000006,0 DockId=0x00000006,0
[Window][Console] [Window][Console]
Pos=265,350 Pos=265,807
Size=623,370 Size=1263,370
Collapsed=0 Collapsed=0
DockId=0x00000008,0 DockId=0x00000008,0
@ -54,7 +54,7 @@ Collapsed=0
DockId=0x00000007,1 DockId=0x00000007,1
[Docking][Data] [Docking][Data]
DockSpace ID=0x11111111 Window=0x1BBC0F80 Pos=0,19 Size=1280,701 Split=X DockSpace ID=0x11111111 Window=0x1BBC0F80 Pos=0,19 Size=1920,1158 Split=X
DockNode ID=0x00000003 Parent=0x11111111 SizeRef=888,1158 Split=X DockNode ID=0x00000003 Parent=0x11111111 SizeRef=888,1158 Split=X
DockNode ID=0x00000001 Parent=0x00000003 SizeRef=263,701 HiddenTabBar=1 Selected=0x12EF0F59 DockNode ID=0x00000001 Parent=0x00000003 SizeRef=263,701 HiddenTabBar=1 Selected=0x12EF0F59
DockNode ID=0x00000002 Parent=0x00000003 SizeRef=623,701 Split=Y Selected=0xC450F867 DockNode ID=0x00000002 Parent=0x00000003 SizeRef=623,701 Split=Y Selected=0xC450F867

View File

@ -1 +1,3 @@
[COMPILE] g++ -std=c++20 -Wall -Isrc/include -Isrc/include/lua -Isrc/vendor -Isrc/vendor/imgui -IC:/msys64/mingw64/include -IC:\msys64\mingw64\lib\libyaml-cpp.a -Isrc\vendor\imgui -MMD -MP -c src\src\Components\ScriptComponent.cpp -o src\build\Components\ScriptComponent.o
[LINK] g++ src\build\Engine.o src\build\main.o src\build\Renderer.o src\build\Components\CameraComponent.o src\build\Components\LightComponent.o src\build\Components\ScriptComponent.o src\build\Components\SpriteComponent.o src\build\Components\TextComonent.o src\build\Components\TilemapComponent.o src\build\Entitys\Object.o src\build\utils\EngineConfig.o src\build\utils\ExceptionHandler.o src\build\utils\FileDialog.o src\build\utils\GameObjectsList.o src\build\utils\Logging.o src\build\utils\Shader.o src\build\utils\UID.o src\build\utils\utils.o src\build\imgui\imgui.o src\build\imgui\imgui_demo.o src\build\imgui\imgui_draw.o src\build\imgui\imgui_impl_glfw.o src\build\imgui\imgui_impl_opengl3.o src\build\imgui\imgui_tables.o src\build\imgui\imgui_widgets.o src\build\lapi.o src\build\lauxlib.o src\build\lbaselib.o src\build\lcode.o src\build\lcorolib.o src\build\lctype.o src\build\ldblib.o src\build\ldebug.o src\build\ldo.o src\build\ldump.o src\build\lfunc.o src\build\lgc.o src\build\linit.o src\build\liolib.o src\build\llex.o src\build\lmathlib.o src\build\lmem.o src\build\loadlib.o src\build\lobject.o src\build\lopcodes.o src\build\loslib.o src\build\lparser.o src\build\lstate.o src\build\lstring.o src\build\lstrlib.o src\build\ltable.o src\build\ltablib.o src\build\ltm.o src\build\lua.o src\build\luac.o src\build\lundump.o src\build\lutf8lib.o src\build\lvm.o src\build\lzio.o -o src\build\app.exe -LC:\msys64\mingw64\lib -lglfw3 -lglew32 -lopengl32 -lgdi32 -lyaml-cpp -lcomdlg32 -lssl -lcrypto [LINK] g++ src\build\Engine.o src\build\main.o src\build\Renderer.o src\build\Components\CameraComponent.o src\build\Components\LightComponent.o src\build\Components\ScriptComponent.o src\build\Components\SpriteComponent.o src\build\Components\TextComonent.o src\build\Components\TilemapComponent.o src\build\Entitys\Object.o src\build\utils\EngineConfig.o src\build\utils\ExceptionHandler.o src\build\utils\FileDialog.o src\build\utils\GameObjectsList.o src\build\utils\Logging.o src\build\utils\Shader.o src\build\utils\UID.o src\build\utils\utils.o src\build\imgui\imgui.o src\build\imgui\imgui_demo.o src\build\imgui\imgui_draw.o src\build\imgui\imgui_impl_glfw.o src\build\imgui\imgui_impl_opengl3.o src\build\imgui\imgui_tables.o src\build\imgui\imgui_widgets.o src\build\lapi.o src\build\lauxlib.o src\build\lbaselib.o src\build\lcode.o src\build\lcorolib.o src\build\lctype.o src\build\ldblib.o src\build\ldebug.o src\build\ldo.o src\build\ldump.o src\build\lfunc.o src\build\lgc.o src\build\linit.o src\build\liolib.o src\build\llex.o src\build\lmathlib.o src\build\lmem.o src\build\loadlib.o src\build\lobject.o src\build\lopcodes.o src\build\loslib.o src\build\lparser.o src\build\lstate.o src\build\lstring.o src\build\lstrlib.o src\build\ltable.o src\build\ltablib.o src\build\ltm.o src\build\lua.o src\build\luac.o src\build\lundump.o src\build\lutf8lib.o src\build\lvm.o src\build\lzio.o -o src\build\app.exe -LC:\msys64\mingw64\lib -lglfw3 -lglew32 -lopengl32 -lgdi32 -lyaml-cpp -lcomdlg32 -lssl -lcrypto
[RUN] Executed app.exe successfully.

View File

@ -18,6 +18,7 @@ public:
virtual std::string GetName() const = 0; virtual std::string GetName() const = 0;
virtual void Save(YAML::Emitter& out) const = 0; virtual void Save(YAML::Emitter& out) const = 0;
virtual void Load(const YAML::Node& node) = 0; virtual void Load(const YAML::Node& node) = 0;

View File

@ -15,69 +15,97 @@
#include <cstring> #include <cstring>
static bool luaDebugEnabled = false; static bool luaDebugEnabled = false;
static bool old_state = false;
struct LuaObjectWrapper { struct LuaObjectWrapper
{
Object *obj; Object *obj;
}; };
#define LUA_OBJECT_MT "LuaObjectMeta" #define LUA_OBJECT_MT "LuaObjectMeta"
struct LuaVector2 { struct LuaVector2
{
float x, y; float x, y;
}; };
#define LUA_VECTOR2_MT "LuaVector2Meta" #define LUA_VECTOR2_MT "LuaVector2Meta"
ScriptComponent::ScriptComponent(Object *owner) : Component(owner), L(nullptr) {} ScriptComponent::ScriptComponent(Object *owner) : Component(owner), L(nullptr) {}
ScriptComponent::~ScriptComponent() { if (L) lua_close(L); } ScriptComponent::~ScriptComponent()
{
if (L)
lua_close(L);
}
void ScriptComponent::SetScriptPath(const std::string& path) { void ScriptComponent::SetScriptPath(const std::string &path)
{
scriptPath = path; scriptPath = path;
ReloadScript(); ReloadScript();
} }
const std::string &ScriptComponent::GetScriptPath() const { return scriptPath; } const std::string &ScriptComponent::GetScriptPath() const { return scriptPath; }
// Logging bindings // Logging bindings
static int Lua_LogInfo(lua_State* L) { static int Lua_LogInfo(lua_State *L)
{
Logger::LogInfo("[Lua] %s", lua_tostring(L, 1)); Logger::LogInfo("[Lua] %s", lua_tostring(L, 1));
return 0; return 0;
} }
static int Lua_LogError(lua_State* L) { static int Lua_LogError(lua_State *L)
{
Logger::LogError("[Lua] %s", lua_tostring(L, 1)); Logger::LogError("[Lua] %s", lua_tostring(L, 1));
return 0; return 0;
} }
static int Lua_LogDebug(lua_State* L) { static int Lua_LogDebug(lua_State *L)
{
Logger::LogDebug("[Lua] %s", lua_tostring(L, 1)); Logger::LogDebug("[Lua] %s", lua_tostring(L, 1));
return 0; return 0;
} }
static int Lua_DebugLua(lua_State* L) { static int Lua_DebugLua(lua_State *L)
{
luaDebugEnabled = lua_toboolean(L, 1); luaDebugEnabled = lua_toboolean(L, 1);
Logger::LogInfo("[Lua] DebugLua = %s", luaDebugEnabled ? "true" : "false"); if (old_state != luaDebugEnabled)
{
Logger::LogInfo("[Lua] DebugLua(%s)", luaDebugEnabled ? "true" : "false");
}
old_state = luaDebugEnabled;
return 0; return 0;
} }
// Component resolver // Component resolver
static Component* GetComponentByName(Object* obj, const std::string& type) { static Component *GetComponentByName(Object *obj, const std::string &type)
if (type == "SpriteComponent") return obj->GetComponent<SpriteComponent>().get(); {
if (type == "CameraComponent") return obj->GetComponent<CameraComponent>().get(); if (type == "SpriteComponent")
if (type == "LightComponent") return obj->GetComponent<LightComponent>().get(); return obj->GetComponent<SpriteComponent>().get();
if (type == "TilemapComponent") return obj->GetComponent<TilemapComponent>().get(); if (type == "CameraComponent")
if (type == "TextComponent") return obj->GetComponent<TextComponent>().get(); return obj->GetComponent<CameraComponent>().get();
if (type == "ScriptComponent") return obj->GetComponent<ScriptComponent>().get(); if (type == "LightComponent")
return obj->GetComponent<LightComponent>().get();
if (type == "TilemapComponent")
return obj->GetComponent<TilemapComponent>().get();
if (type == "TextComponent")
return obj->GetComponent<TextComponent>().get();
if (type == "ScriptComponent")
return obj->GetComponent<ScriptComponent>().get();
return nullptr; return nullptr;
} }
// Object:GetComponent("Type") // Object:GetComponent("Type")
static int Lua_Object_GetComponent(lua_State* L) { static int Lua_Object_GetComponent(lua_State *L)
{
auto *wrapper = (LuaObjectWrapper *)luaL_checkudata(L, 1, LUA_OBJECT_MT); auto *wrapper = (LuaObjectWrapper *)luaL_checkudata(L, 1, LUA_OBJECT_MT);
const char *type = luaL_checkstring(L, 2); const char *type = luaL_checkstring(L, 2);
Component *comp = GetComponentByName(wrapper->obj, type); Component *comp = GetComponentByName(wrapper->obj, type);
if (comp) lua_pushlightuserdata(L, comp); if (comp)
else lua_pushnil(L); lua_pushlightuserdata(L, comp);
else
lua_pushnil(L);
return 1; return 1;
} }
// Object:GetPosition() // Object:GetPosition()
static int Lua_Object_GetPosition(lua_State* L) { static int Lua_Object_GetPosition(lua_State *L)
{
auto *wrapper = (LuaObjectWrapper *)luaL_checkudata(L, 1, LUA_OBJECT_MT); auto *wrapper = (LuaObjectWrapper *)luaL_checkudata(L, 1, LUA_OBJECT_MT);
glm::vec2 pos = wrapper->obj->GetLocalPosition(); glm::vec2 pos = wrapper->obj->GetLocalPosition();
@ -90,7 +118,8 @@ static int Lua_Object_GetPosition(lua_State* L) {
} }
// Object:SetPosition(Vector2) // Object:SetPosition(Vector2)
static int Lua_Object_SetPosition(lua_State* L) { static int Lua_Object_SetPosition(lua_State *L)
{
auto *wrapper = (LuaObjectWrapper *)luaL_checkudata(L, 1, LUA_OBJECT_MT); auto *wrapper = (LuaObjectWrapper *)luaL_checkudata(L, 1, LUA_OBJECT_MT);
auto *vec = (LuaVector2 *)luaL_checkudata(L, 2, LUA_VECTOR2_MT); auto *vec = (LuaVector2 *)luaL_checkudata(L, 2, LUA_VECTOR2_MT);
wrapper->obj->SetLocalPosition({vec->x, vec->y}); wrapper->obj->SetLocalPosition({vec->x, vec->y});
@ -98,19 +127,24 @@ static int Lua_Object_SetPosition(lua_State* L) {
} }
// __index for Object // __index for Object
static int Lua_Object_Index(lua_State* L) { static int Lua_Object_Index(lua_State *L)
{
const char *key = luaL_checkstring(L, 2); const char *key = luaL_checkstring(L, 2);
lua_getfield(L, lua_upvalueindex(1), key); lua_getfield(L, lua_upvalueindex(1), key);
return 1; return 1;
} }
void RegisterObjectType(lua_State* L) { void RegisterObjectType(lua_State *L)
{
luaL_newmetatable(L, LUA_OBJECT_MT); luaL_newmetatable(L, LUA_OBJECT_MT);
lua_newtable(L); // method table lua_newtable(L); // method table
lua_pushcfunction(L, Lua_Object_GetComponent); lua_setfield(L, -2, "GetComponent"); lua_pushcfunction(L, Lua_Object_GetComponent);
lua_pushcfunction(L, Lua_Object_GetPosition); lua_setfield(L, -2, "GetPosition"); lua_setfield(L, -2, "GetComponent");
lua_pushcfunction(L, Lua_Object_SetPosition); lua_setfield(L, -2, "SetPosition"); lua_pushcfunction(L, Lua_Object_GetPosition);
lua_setfield(L, -2, "GetPosition");
lua_pushcfunction(L, Lua_Object_SetPosition);
lua_setfield(L, -2, "SetPosition");
lua_pushcclosure(L, Lua_Object_Index, 1); lua_pushcclosure(L, Lua_Object_Index, 1);
lua_setfield(L, -2, "__index"); lua_setfield(L, -2, "__index");
@ -118,7 +152,8 @@ void RegisterObjectType(lua_State* L) {
lua_pop(L, 1); lua_pop(L, 1);
} }
static void PushObject(lua_State* L, Object* obj) { static void PushObject(lua_State *L, Object *obj)
{
auto *wrapper = (LuaObjectWrapper *)lua_newuserdata(L, sizeof(LuaObjectWrapper)); auto *wrapper = (LuaObjectWrapper *)lua_newuserdata(L, sizeof(LuaObjectWrapper));
wrapper->obj = obj; wrapper->obj = obj;
luaL_getmetatable(L, LUA_OBJECT_MT); luaL_getmetatable(L, LUA_OBJECT_MT);
@ -126,11 +161,14 @@ static void PushObject(lua_State* L, Object* obj) {
} }
// Engine.GetObjectByTag(name) // Engine.GetObjectByTag(name)
static int Lua_GetObjectByTag(lua_State* L) { static int Lua_GetObjectByTag(lua_State *L)
{
const char *name = luaL_checkstring(L, 1); const char *name = luaL_checkstring(L, 1);
for (const auto& obj : objects) { for (const auto &obj : objects)
{
auto found = FindByTagRecursive(obj, name); auto found = FindByTagRecursive(obj, name);
if (found) { if (found)
{
PushObject(L, found.get()); PushObject(L, found.get());
return 1; return 1;
} }
@ -140,7 +178,8 @@ static int Lua_GetObjectByTag(lua_State* L) {
} }
// Vector2(x, y) // Vector2(x, y)
static int Lua_Vector2_New(lua_State* L) { static int Lua_Vector2_New(lua_State *L)
{
LuaVector2 *vec = (LuaVector2 *)lua_newuserdata(L, sizeof(LuaVector2)); LuaVector2 *vec = (LuaVector2 *)lua_newuserdata(L, sizeof(LuaVector2));
vec->x = (float)luaL_optnumber(L, 1, 0); vec->x = (float)luaL_optnumber(L, 1, 0);
vec->y = (float)luaL_optnumber(L, 2, 0); vec->y = (float)luaL_optnumber(L, 2, 0);
@ -149,48 +188,67 @@ static int Lua_Vector2_New(lua_State* L) {
return 1; return 1;
} }
static int Lua_Vector2_Index(lua_State* L) { static int Lua_Vector2_Index(lua_State *L)
{
auto *vec = (LuaVector2 *)luaL_checkudata(L, 1, LUA_VECTOR2_MT); auto *vec = (LuaVector2 *)luaL_checkudata(L, 1, LUA_VECTOR2_MT);
const char *key = luaL_checkstring(L, 2); const char *key = luaL_checkstring(L, 2);
if (strcmp(key, "x") == 0) lua_pushnumber(L, vec->x); if (strcmp(key, "x") == 0)
else if (strcmp(key, "y") == 0) lua_pushnumber(L, vec->y); lua_pushnumber(L, vec->x);
else lua_pushnil(L); else if (strcmp(key, "y") == 0)
lua_pushnumber(L, vec->y);
else
lua_pushnil(L);
return 1; return 1;
} }
static int Lua_Vector2_NewIndex(lua_State* L) { static int Lua_Vector2_NewIndex(lua_State *L)
{
auto *vec = (LuaVector2 *)luaL_checkudata(L, 1, LUA_VECTOR2_MT); auto *vec = (LuaVector2 *)luaL_checkudata(L, 1, LUA_VECTOR2_MT);
const char *key = luaL_checkstring(L, 2); const char *key = luaL_checkstring(L, 2);
float value = (float)luaL_checknumber(L, 3); float value = (float)luaL_checknumber(L, 3);
if (strcmp(key, "x") == 0) vec->x = value; if (strcmp(key, "x") == 0)
else if (strcmp(key, "y") == 0) vec->y = value; vec->x = value;
else if (strcmp(key, "y") == 0)
vec->y = value;
return 0; return 0;
} }
void RegisterVector2Type(lua_State* L) { void RegisterVector2Type(lua_State *L)
{
luaL_newmetatable(L, LUA_VECTOR2_MT); luaL_newmetatable(L, LUA_VECTOR2_MT);
lua_pushcfunction(L, Lua_Vector2_Index); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, Lua_Vector2_Index);
lua_pushcfunction(L, Lua_Vector2_NewIndex); lua_setfield(L, -2, "__newindex"); lua_setfield(L, -2, "__index");
lua_pushcfunction(L, Lua_Vector2_NewIndex);
lua_setfield(L, -2, "__newindex");
lua_pop(L, 1); lua_pop(L, 1);
lua_pushcfunction(L, Lua_Vector2_New); lua_pushcfunction(L, Lua_Vector2_New);
lua_setglobal(L, "Vector2"); lua_setglobal(L, "Vector2");
} }
void ScriptComponent::RegisterEngineBindings() { void ScriptComponent::RegisterEngineBindings()
{
lua_newtable(L); lua_newtable(L);
lua_pushcfunction(L, Lua_LogInfo); lua_setfield(L, -2, "LogInfo"); lua_pushcfunction(L, Lua_LogInfo);
lua_pushcfunction(L, Lua_LogError); lua_setfield(L, -2, "LogError"); lua_setfield(L, -2, "LogInfo");
lua_pushcfunction(L, Lua_LogDebug); lua_setfield(L, -2, "LogDebug"); lua_pushcfunction(L, Lua_LogError);
lua_pushcfunction(L, Lua_GetObjectByTag); lua_setfield(L, -2, "GetObjectByTag"); lua_setfield(L, -2, "LogError");
lua_pushcfunction(L, Lua_DebugLua); lua_setfield(L, -2, "DebugLua"); lua_pushcfunction(L, Lua_LogDebug);
lua_setfield(L, -2, "LogDebug");
lua_pushcfunction(L, Lua_GetObjectByTag);
lua_setfield(L, -2, "GetObjectByTag");
lua_pushcfunction(L, Lua_DebugLua);
lua_setfield(L, -2, "DebugLua");
lua_setglobal(L, "Engine"); lua_setglobal(L, "Engine");
} }
void ScriptComponent::ReloadScript() { void ScriptComponent::ReloadScript()
if (scriptPath.empty()) return; {
if (scriptPath.empty())
return;
if (L) lua_close(L); if (L)
lua_close(L);
L = luaL_newstate(); L = luaL_newstate();
luaL_openlibs(L); luaL_openlibs(L);
@ -198,54 +256,77 @@ void ScriptComponent::ReloadScript() {
RegisterVector2Type(L); RegisterVector2Type(L);
RegisterEngineBindings(); RegisterEngineBindings();
if (luaL_dofile(L, scriptPath.c_str())) { Logger::LogVerbose("[Lua] Loading Script from file.");
if (luaL_dofile(L, scriptPath.c_str()))
{
Logger::LogError("[Lua] %s", lua_tostring(L, -1)); Logger::LogError("[Lua] %s", lua_tostring(L, -1));
RecoverableError("Failed to load Lua script: " + scriptPath, Create::Exceptions::ComponentLoad).Handle(); RecoverableError("Failed to load Lua script: " + scriptPath, Create::Exceptions::ComponentLoad).Handle();
return; return;
} }
if (luaDebugEnabled)
{
Logger::LogVerbose("[Lua][call] OnInit()");
}
lua_getglobal(L, "OnInit"); lua_getglobal(L, "OnInit");
if (lua_isfunction(L, -1)) { if (lua_isfunction(L, -1))
if (lua_pcall(L, 0, 0, 0) != LUA_OK) { {
if (lua_pcall(L, 0, 0, 0) != LUA_OK)
{
Logger::LogError("[Lua] %s", lua_tostring(L, -1)); Logger::LogError("[Lua] %s", lua_tostring(L, -1));
RecoverableError("OnInit failed: " + scriptPath, Create::Exceptions::ComponentLoad).Handle(); RecoverableError("OnInit failed: " + scriptPath, Create::Exceptions::ComponentLoad).Handle();
} }
} else { }
else
{
lua_pop(L, 1); lua_pop(L, 1);
} }
} }
void ScriptComponent::OnUpdate(float dt) { void ScriptComponent::OnUpdate(float dt)
if (!L) return; {
if (!L)
return;
lua_getglobal(L, "OnUpdate"); lua_getglobal(L, "OnUpdate");
if (lua_isfunction(L, -1)) { if (lua_isfunction(L, -1))
{
lua_pushnumber(L, dt); lua_pushnumber(L, dt);
if (lua_pcall(L, 1, 0, 0) != LUA_OK) { if (lua_pcall(L, 1, 0, 0) != LUA_OK)
{
Logger::LogError("[Lua] %s", lua_tostring(L, -1)); Logger::LogError("[Lua] %s", lua_tostring(L, -1));
RecoverableError("OnUpdate failed in: " + scriptPath, Create::Exceptions::ComponentLoad).Handle(); RecoverableError("OnUpdate failed in: " + scriptPath, Create::Exceptions::ComponentLoad).Handle();
} }
} else { }
else
{
lua_pop(L, 1); lua_pop(L, 1);
} }
} }
void ScriptComponent::Save(YAML::Emitter& out) const { void ScriptComponent::Save(YAML::Emitter &out) const
{
out << YAML::BeginMap; out << YAML::BeginMap;
out << YAML::Key << "type" << YAML::Value << "ScriptComponent"; out << YAML::Key << "type" << YAML::Value << "ScriptComponent";
out << YAML::Key << "scriptPath" << YAML::Value << scriptPath; out << YAML::Key << "scriptPath" << YAML::Value << scriptPath;
out << YAML::EndMap; out << YAML::EndMap;
} }
void ScriptComponent::Load(const YAML::Node& node) { void ScriptComponent::Load(const YAML::Node &node)
try { {
if (!node["scriptPath"]) { try
{
if (!node["scriptPath"])
{
RecoverableError("Missing 'scriptPath' in ScriptComponent", Create::Exceptions::MissingField).Handle(); RecoverableError("Missing 'scriptPath' in ScriptComponent", Create::Exceptions::MissingField).Handle();
return; return;
} }
scriptPath = node["scriptPath"].as<std::string>(); scriptPath = node["scriptPath"].as<std::string>();
ReloadScript(); ReloadScript();
} catch (const YAML::Exception& e) { }
catch (const YAML::Exception &e)
{
RecoverableError("YAML error in ScriptComponent::Load: " + std::string(e.what()), Create::Exceptions::ComponentLoad).Handle(); RecoverableError("YAML error in ScriptComponent::Load: " + std::string(e.what()), Create::Exceptions::ComponentLoad).Handle();
} }
} }

View File

@ -369,12 +369,12 @@ void Engine::Run()
if (!playing) if (!playing)
{ {
Logger::LogVerbose("[RestoreScene] Saving original scene"); Logger::LogVerbose("[RestoreScene] Saving original scene");
SaveScene(tempScenePath); SaveState();
} }
else else
{ {
Logger::LogVerbose("[RestoreScene] Reloading original scene"); Logger::LogVerbose("[RestoreScene] Reloading original scene");
LoadScene(tempScenePath); LoadState();
} }
selected = nullptr; selected = nullptr;
playing = !playing; playing = !playing;
@ -813,6 +813,54 @@ void Engine::LoadScene(const std::string &path)
Logger::LogInfo("[LoadScene] Loaded scene: %s", root["scene_name"].as<std::string>().c_str()); Logger::LogInfo("[LoadScene] Loaded scene: %s", root["scene_name"].as<std::string>().c_str());
} }
void Engine::LoadState()
{
if (savedStateYAML.empty())
{
Logger::LogWarning("[LoadState] No scene state in memory.");
return;
}
YAML::Node objectArray;
try
{
objectArray = YAML::Load(savedStateYAML);
}
catch (const std::exception& e)
{
Logger::LogError("[LoadState] Failed to parse saved scene: %s", e.what());
return;
}
objects.clear();
for (const auto& node : objectArray)
{
auto obj = std::make_shared<Object>("[DefaultObject]");
obj->Load(node);
objects.push_back(obj);
}
Logger::LogVerbose("[LoadState] Scene restored");
}
void Engine::SaveState()
{
YAML::Emitter sceneData;
sceneData << YAML::BeginSeq;
for (const auto& obj : objects)
obj->Save(sceneData);
sceneData << YAML::EndSeq;
savedStateYAML = sceneData.c_str();
Logger::LogVerbose("[SaveState] Scene serialized (%zu bytes)", savedStateYAML.size());
}
void Engine::Shutdown() void Engine::Shutdown()
{ {
ImGui_ImplOpenGL3_Shutdown(); ImGui_ImplOpenGL3_Shutdown();

View File

@ -3,19 +3,16 @@
#include <fstream> #include <fstream>
#include <string> #include <string>
class Object; class Object;
class Engine { class Engine
{
public: public:
Engine(); Engine();
~Engine(); ~Engine();
void Run(); void Run();
std::shared_ptr<Object> GetObjectByTag(const std::string &tag); std::shared_ptr<Object> GetObjectByTag(const std::string &tag);
private: private:
void Init(); void Init();
void Shutdown(); void Shutdown();
@ -24,7 +21,8 @@ private:
void LoadScene(const std::string &path); void LoadScene(const std::string &path);
void ShowDebugOverlay(float deltaTime); void ShowDebugOverlay(float deltaTime);
void SaveState();
void LoadState();
int m_Reserved_draws; int m_Reserved_draws;
}; };

View File

@ -13,7 +13,7 @@
#include <algorithm> #include <algorithm>
Object::Object(const std::string &name) Object::Object(const std::string &name)
: name(name), localPosition(0.0f, 0.0f), uid() {} : name(name), localPosition(0.0f, 0.0f), uid(), visable(true) {}
Object::~Object() {} Object::~Object() {}
@ -68,6 +68,8 @@ glm::vec2 Object::GetWorldPosition() const
return localPosition; return localPosition;
} }
const std::string &Object::GetName() const { return name; } const std::string &Object::GetName() const { return name; }
void Object::SetName(const std::string &n) { name = n; } void Object::SetName(const std::string &n) { name = n; }
std::vector<std::shared_ptr<Object>> &Object::GetChildren() { return children; } std::vector<std::shared_ptr<Object>> &Object::GetChildren() { return children; }

View File

@ -32,6 +32,8 @@ public:
void SetVisable(bool state); void SetVisable(bool state);
template <typename T> template <typename T>
std::shared_ptr<T> GetComponent() const; std::shared_ptr<T> GetComponent() const;
template <typename T> template <typename T>

View File

@ -2,6 +2,8 @@
#include "../utils/Logging.h" #include "../utils/Logging.h"
std::vector<std::shared_ptr<Object>> objects; std::vector<std::shared_ptr<Object>> objects;
std::string savedStateYAML;
std::shared_ptr<Object> FindByTagRecursive(const std::shared_ptr<Object>& obj, const std::string& tag) { std::shared_ptr<Object> FindByTagRecursive(const std::shared_ptr<Object>& obj, const std::string& tag) {
if (obj->GetName() == tag) if (obj->GetName() == tag)

View File

@ -5,4 +5,6 @@
#include "../Entitys/Object.h" #include "../Entitys/Object.h"
extern std::vector<std::shared_ptr<Object>> objects; extern std::vector<std::shared_ptr<Object>> objects;
extern std::string savedStateYAML;
std::shared_ptr<Object> FindByTagRecursive(const std::shared_ptr<Object>& obj, const std::string& tag); std::shared_ptr<Object> FindByTagRecursive(const std::shared_ptr<Object>& obj, const std::string& tag);

View File

@ -67,7 +67,7 @@ void Logger::LogVA(Level level, const char *fmt, va_list args)
std::cout << GetAnsiColor(level) std::cout << GetAnsiColor(level)
<< "[Logger][" << ToString(level) << "] " << "[Logger][" << ToString(level) << "] "
<< buffer << "\033[0m" << std::endl; << buffer << "\033[0m" << "\n";
} }
void Logger::LogInfo(const char *fmt, ...) void Logger::LogInfo(const char *fmt, ...)