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]
Pos=0,19
Size=1280,701
Size=1920,1158
Collapsed=0
[Window][Inspector]
Pos=890,19
Size=390,505
Pos=1530,19
Size=390,835
Collapsed=0
DockId=0x00000005,0
[Window][Scene Tree]
Pos=0,19
Size=263,701
Size=263,1158
Collapsed=0
DockId=0x00000001,0
[Window][Viewport]
Pos=265,19
Size=623,329
Size=1263,786
Collapsed=0
DockId=0x00000007,0
@ -36,14 +36,14 @@ Size=1280,19
Collapsed=0
[Window][Performance Info]
Pos=890,526
Size=390,194
Pos=1530,856
Size=390,321
Collapsed=0
DockId=0x00000006,0
[Window][Console]
Pos=265,350
Size=623,370
Pos=265,807
Size=1263,370
Collapsed=0
DockId=0x00000008,0
@ -54,7 +54,7 @@ Collapsed=0
DockId=0x00000007,1
[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=0x00000001 Parent=0x00000003 SizeRef=263,701 HiddenTabBar=1 Selected=0x12EF0F59
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
[RUN] Executed app.exe successfully.

View File

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

View File

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

View File

@ -369,12 +369,12 @@ void Engine::Run()
if (!playing)
{
Logger::LogVerbose("[RestoreScene] Saving original scene");
SaveScene(tempScenePath);
SaveState();
}
else
{
Logger::LogVerbose("[RestoreScene] Reloading original scene");
LoadScene(tempScenePath);
LoadState();
}
selected = nullptr;
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());
}
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()
{
ImGui_ImplOpenGL3_Shutdown();

View File

@ -3,28 +3,26 @@
#include <fstream>
#include <string>
class Object;
class Engine {
class Engine
{
public:
Engine();
~Engine();
void Run();
std::shared_ptr<Object> GetObjectByTag(const std::string &tag);
private:
void Init();
void Shutdown();
void DrawObjectNode(const std::shared_ptr<Object>& obj); // make sure this matches Engine.cpp
void SaveScene(const std::string& path);
void LoadScene(const std::string& path);
void DrawObjectNode(const std::shared_ptr<Object> &obj); // make sure this matches Engine.cpp
void SaveScene(const std::string &path);
void LoadScene(const std::string &path);
void ShowDebugOverlay(float deltaTime);
void SaveState();
void LoadState();
int m_Reserved_draws;
};

View File

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

View File

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

View File

@ -2,6 +2,8 @@
#include "../utils/Logging.h"
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) {
if (obj->GetName() == tag)

View File

@ -5,4 +5,6 @@
#include "../Entitys/Object.h"
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);

View File

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