From f7f5ffa4e425bbb7b2ec04b052ab0bc1c6fcc6f8 Mon Sep 17 00:00:00 2001 From: OusmBlueNinja <89956790+OusmBlueNinja@users.noreply.github.com> Date: Wed, 1 Jan 2025 01:36:53 -0600 Subject: [PATCH] Added Lua User Input System. --- Makefile | 2 +- assets/scripts/camera.lua | 54 ++++++--- assets/scripts/keycode.lua | 1 + imgui.ini | 40 +++---- src/Componenets/GameObject.cpp | 1 + src/Engine.cpp | 12 +- src/Engine/InputManager.cpp | 29 +++++ src/Engine/InputManager.h | 20 ++++ src/Engine/KeyCode.h | 128 ++++++++++++++++++++++ src/Engine/LuaAPI.cpp | 176 ++++++++++++++++++++++++++++++ src/Engine/LuaAPI.h | 17 ++- src/Engine/ScopedTimer.h | 16 +++ src/Windows/InspectorWindow.cpp | 3 + src/Windows/LoggerWindow.cpp | 31 ++++-- src/Windows/PerformanceWindow.cpp | 15 +-- src/Windows/ProfilerWindow.cpp | 5 +- src/Windows/SceneWindow.cpp | 2 + 17 files changed, 484 insertions(+), 68 deletions(-) create mode 100644 assets/scripts/keycode.lua create mode 100644 src/Engine/InputManager.cpp create mode 100644 src/Engine/InputManager.h create mode 100644 src/Engine/KeyCode.h diff --git a/Makefile b/Makefile index a9f981a..5aefad3 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ # Compiler and Flags CXX := g++ -CXXFLAGS := -Wall -Wextra -std=c++17 -g -DDEBUG +CXXFLAGS := -Wall -Wextra -std=c++17 -g -DDEBUG -DTIMERS # Directories SRC_DIR := src diff --git a/assets/scripts/camera.lua b/assets/scripts/camera.lua index be828da..4583feb 100644 --- a/assets/scripts/camera.lua +++ b/assets/scripts/camera.lua @@ -1,4 +1,5 @@ local Engine = require("./assets/scripts/engine") +local KeyCode = require("./assets/scripts/keycode") local transform = nil @@ -6,30 +7,49 @@ local transform = nil local new_rotation = 0 local new_rotationSpeed = 15 +local Move_Speec = 300 -function OnInit() +function OnInit() local gameObject = Engine.GetGameObjectByTag("Camera") transform = gameObject:GetComponent("Transform") Engine.Log("Init Done") end - function OnUpdate(deltaTime) - local gameObject = Engine.GetGameObjectByTag("Camera") - transform = gameObject:GetComponent("Transform") + local gameObject = Engine.GetGameObjectByTag("Camera") + transform = gameObject:GetComponent("Transform") - local old_rotation = transform:GetRotation() + local old_rotation = transform:GetRotation() - local rotation = { - x = old_rotation.x, -- Preserving existing new_rotation on X-axis - y = new_rotation, -- Updated new_rotation on Y-axis for spinning - z = old_rotation.z -- Preserving existing new_rotation on Z-axis - } - -- Apply the new new_rotation to the Transform component - transform:SetRotation(rotation) - new_rotation = new_rotation + (deltaTime * new_rotationSpeed) - if new_rotation >= 360 then - new_rotation = 0 - end -end \ No newline at end of file + local Position = transform:GetPosition() + + if Engine.KeyDown(KeyCode.W) then + Position.x = Position.x + (deltaTime * Move_Speec) + end + if Engine.KeyDown(KeyCode.S) then + Position.x = Position.x - (deltaTime * Move_Speec) + end + if Engine.KeyDown(KeyCode.D) then + Position.z = Position.z + (deltaTime * Move_Speec) + end + if Engine.KeyDown(KeyCode.A) then + Position.z = Position.z - (deltaTime * Move_Speec) + end + + transform:SetPosition(Position) + + + + local rotation = { + x = old_rotation.x, -- Preserving existing new_rotation on X-axis + y = new_rotation, -- Updated new_rotation on Y-axis for spinning + z = old_rotation.z -- Preserving existing new_rotation on Z-axis + } + -- Apply the new new_rotation to the Transform component + transform:SetRotation(rotation) + new_rotation = new_rotation + (deltaTime * new_rotationSpeed) + if new_rotation >= 360 then + new_rotation = 0 + end +end diff --git a/assets/scripts/keycode.lua b/assets/scripts/keycode.lua new file mode 100644 index 0000000..e472082 --- /dev/null +++ b/assets/scripts/keycode.lua @@ -0,0 +1 @@ +return _T_KeyCode_Table \ No newline at end of file diff --git a/imgui.ini b/imgui.ini index d3ad093..6fb685d 100644 --- a/imgui.ini +++ b/imgui.ini @@ -1,6 +1,6 @@ [Window][DockSpace] Pos=0,0 -Size=1280,720 +Size=1920,1177 Collapsed=0 [Window][Debug##Default] @@ -80,8 +80,8 @@ Collapsed=0 DockId=0x0000001F,0 [Window][Performance##performance] -Pos=8,435 -Size=322,277 +Pos=8,706 +Size=322,463 Collapsed=0 DockId=0x0000001C,0 @@ -105,7 +105,7 @@ DockId=0x0000000F,0 [Window][Scene Window##SceneWindow] Pos=8,28 -Size=322,405 +Size=322,676 Collapsed=0 DockId=0x0000001B,0 @@ -134,26 +134,26 @@ Collapsed=0 DockId=0x0000001E,0 [Window][ Logger##logger] -Pos=607,354 -Size=277,358 +Pos=1070,811 +Size=454,358 Collapsed=0 DockId=0x00000024,0 [Window][ Editor##EditorWindow] Pos=332,28 -Size=552,324 +Size=1192,781 Collapsed=0 DockId=0x0000001F,0 [Window][ Inspector##InspectorWindow] -Pos=886,28 -Size=386,684 +Pos=1526,28 +Size=386,1141 Collapsed=0 DockId=0x00000022,0 [Window][ Profiler] -Pos=332,354 -Size=273,358 +Pos=332,811 +Size=736,358 Collapsed=0 DockId=0x00000023,0 @@ -164,14 +164,14 @@ Column 2 Weight=0.6474 Column 3 Weight=1.0088 [Table][0x08173DEA,4] -Column 0 Weight=1.0000 -Column 1 Weight=1.0000 -Column 2 Weight=1.0000 -Column 3 Weight=1.0000 +Column 0 Weight=1.2209 +Column 1 Weight=1.1411 +Column 2 Weight=1.2270 +Column 3 Weight=0.4110 [Docking][Data] -DockSpace ID=0x14621557 Window=0x3DA2F1DE Pos=172,215 Size=1264,684 Split=X Selected=0xF7365A5A - DockNode ID=0x00000021 Parent=0x14621557 SizeRef=876,1139 Split=X +DockSpace ID=0x14621557 Window=0x3DA2F1DE Pos=8,51 Size=1904,1141 Split=X Selected=0xF7365A5A + DockNode ID=0x00000021 Parent=0x14621557 SizeRef=1516,1141 Split=X DockNode ID=0x00000013 Parent=0x00000021 SizeRef=322,1142 Split=Y Selected=0x818D04BB DockNode ID=0x0000001B Parent=0x00000013 SizeRef=264,675 HiddenTabBar=1 Selected=0x1D5D92B6 DockNode ID=0x0000001C Parent=0x00000013 SizeRef=264,463 HiddenTabBar=1 Selected=0x818D04BB @@ -194,8 +194,8 @@ DockSpace ID=0x14621557 Window=0x3DA2F1DE Pos=172,215 DockNode ID=0x0000001D Parent=0x00000017 SizeRef=518,720 Split=Y Selected=0xDFF75B3F DockNode ID=0x0000001F Parent=0x0000001D SizeRef=518,779 CentralNode=1 Selected=0x9A7B23B9 DockNode ID=0x00000020 Parent=0x0000001D SizeRef=518,358 Split=X Selected=0x7A66B86B - DockNode ID=0x00000023 Parent=0x00000020 SizeRef=273,358 HiddenTabBar=1 Selected=0x7A66B86B - DockNode ID=0x00000024 Parent=0x00000020 SizeRef=277,358 HiddenTabBar=1 Selected=0x1F29F1F5 + DockNode ID=0x00000023 Parent=0x00000020 SizeRef=736,358 HiddenTabBar=1 Selected=0x7A66B86B + DockNode ID=0x00000024 Parent=0x00000020 SizeRef=454,358 HiddenTabBar=1 Selected=0x1F29F1F5 DockNode ID=0x0000001E Parent=0x00000017 SizeRef=518,417 Selected=0xC74E1AEE DockNode ID=0x00000018 Parent=0x0000000D SizeRef=1202,364 Split=X Selected=0x1C0788A1 DockNode ID=0x00000019 Parent=0x00000018 SizeRef=601,364 Selected=0x1C0788A1 @@ -204,7 +204,7 @@ DockSpace ID=0x14621557 Window=0x3DA2F1DE Pos=172,215 DockNode ID=0x00000004 Parent=0x00000001 SizeRef=1202,291 Selected=0x9DD4E196 DockNode ID=0x00000002 Parent=0x00000008 SizeRef=334,1142 HiddenTabBar=1 Selected=0x36DC96AB DockNode ID=0x00000016 Parent=0x00000014 SizeRef=420,1142 Selected=0x8D0E8380 - DockNode ID=0x00000022 Parent=0x14621557 SizeRef=386,1139 Selected=0xD1D25642 + DockNode ID=0x00000022 Parent=0x14621557 SizeRef=386,1141 Selected=0xD1D25642 DockSpace ID=0xC6145A92 Pos=8,27 Size=1904,1142 Split=X DockNode ID=0x0000000F Parent=0xC6145A92 SizeRef=301,1142 Selected=0xA8433A03 DockNode ID=0x00000010 Parent=0xC6145A92 SizeRef=1601,1142 CentralNode=1 diff --git a/src/Componenets/GameObject.cpp b/src/Componenets/GameObject.cpp index d325f84..947af8b 100644 --- a/src/Componenets/GameObject.cpp +++ b/src/Componenets/GameObject.cpp @@ -88,6 +88,7 @@ bool GameObject::RemoveComponent(const std::shared_ptr &component) return true; } } + return false; } void GameObject::Update(float deltaTime) diff --git a/src/Engine.cpp b/src/Engine.cpp index 8df220b..2d095cb 100644 --- a/src/Engine.cpp +++ b/src/Engine.cpp @@ -33,7 +33,7 @@ #include "Engine/SceneManager.h" #include "Engine/LuaAPI.h" #include "Engine/Utilitys.h" - +#include "Engine/InputManager.h" #include "Engine/ScopedTimer.h" #include "Engine/Profiler.h" @@ -48,6 +48,9 @@ LoggerWindow *g_LoggerWindow; SceneManager g_SceneManager; +InputManager g_InputManager; + + std::vector> g_GameObjects; std::shared_ptr g_RuntimeCameraObject; @@ -202,6 +205,13 @@ void MyEngine::Run() glfwPollEvents(); } + + + { + SCOPE_TIMER("InputManagerUpdate"); + g_InputManager.Update(m_Window); + } + // Calculate current time double current_time = glfwGetTime(); diff --git a/src/Engine/InputManager.cpp b/src/Engine/InputManager.cpp new file mode 100644 index 0000000..96df075 --- /dev/null +++ b/src/Engine/InputManager.cpp @@ -0,0 +1,29 @@ +// InputManager.cpp +#include "InputManager.h" +#include + + + +void InputManager::Update(GLFWwindow* window) +{ + if (!window) + { + return; + } + + // Update the state of each key + for (int key = 0; key <= GLFW_KEY_LAST; ++key) + { + m_KeyStates[key] = glfwGetKey(window, key) == GLFW_PRESS; + } +} + +bool InputManager::IsKeyPressed(KeyCode key) const +{ + int keyInt = static_cast(key); + if (keyInt >= 0 && keyInt <= GLFW_KEY_LAST) + { + return m_KeyStates[keyInt]; + } + return false; +} diff --git a/src/Engine/InputManager.h b/src/Engine/InputManager.h new file mode 100644 index 0000000..6dae226 --- /dev/null +++ b/src/Engine/InputManager.h @@ -0,0 +1,20 @@ +// InputManager.h +#pragma once + +#include "KeyCode.h" +#include + + +class InputManager +{ +public: + // Update key states (to be called every frame) + void Update(GLFWwindow* window); + + // Check if a key is pressed + bool IsKeyPressed(KeyCode key) const; + +private: + // Array or map storing key states + bool m_KeyStates[GLFW_KEY_LAST + 1] = { false }; +}; diff --git a/src/Engine/KeyCode.h b/src/Engine/KeyCode.h new file mode 100644 index 0000000..9373f96 --- /dev/null +++ b/src/Engine/KeyCode.h @@ -0,0 +1,128 @@ +// KeyCode.h +#pragma once + +#include + +enum class KeyCode +{ + Space = GLFW_KEY_SPACE, + Apostrophe = GLFW_KEY_APOSTROPHE, + Comma = GLFW_KEY_COMMA, + Minus = GLFW_KEY_MINUS, + Period = GLFW_KEY_PERIOD, + Slash = GLFW_KEY_SLASH, + D0 = GLFW_KEY_0, + D1 = GLFW_KEY_1, + D2 = GLFW_KEY_2, + D3 = GLFW_KEY_3, + D4 = GLFW_KEY_4, + D5 = GLFW_KEY_5, + D6 = GLFW_KEY_6, + D7 = GLFW_KEY_7, + D8 = GLFW_KEY_8, + D9 = GLFW_KEY_9, + Semicolon = GLFW_KEY_SEMICOLON, + Equal = GLFW_KEY_EQUAL, + A = GLFW_KEY_A, + B = GLFW_KEY_B, + C = GLFW_KEY_C, + D = GLFW_KEY_D, + E = GLFW_KEY_E, + F = GLFW_KEY_F, + G = GLFW_KEY_G, + H = GLFW_KEY_H, + I = GLFW_KEY_I, + J = GLFW_KEY_J, + K = GLFW_KEY_K, + L = GLFW_KEY_L, + M = GLFW_KEY_M, + N = GLFW_KEY_N, + O = GLFW_KEY_O, + P = GLFW_KEY_P, + Q = GLFW_KEY_Q, + R = GLFW_KEY_R, + S = GLFW_KEY_S, + T = GLFW_KEY_T, + U = GLFW_KEY_U, + V = GLFW_KEY_V, + W = GLFW_KEY_W, + X = GLFW_KEY_X, + Y = GLFW_KEY_Y, + Z = GLFW_KEY_Z, + LeftBracket = GLFW_KEY_LEFT_BRACKET, + Backslash = GLFW_KEY_BACKSLASH, + RightBracket = GLFW_KEY_RIGHT_BRACKET, + GraveAccent = GLFW_KEY_GRAVE_ACCENT, + World1 = GLFW_KEY_WORLD_1, + World2 = GLFW_KEY_WORLD_2, + Escape = GLFW_KEY_ESCAPE, + Enter = GLFW_KEY_ENTER, + Tab = GLFW_KEY_TAB, + Backspace = GLFW_KEY_BACKSPACE, + Insert = GLFW_KEY_INSERT, + Delete = GLFW_KEY_DELETE, + Right = GLFW_KEY_RIGHT, + Left = GLFW_KEY_LEFT, + Down = GLFW_KEY_DOWN, + Up = GLFW_KEY_UP, + PageUp = GLFW_KEY_PAGE_UP, + PageDown = GLFW_KEY_PAGE_DOWN, + Home = GLFW_KEY_HOME, + End = GLFW_KEY_END, + CapsLock = GLFW_KEY_CAPS_LOCK, + ScrollLock = GLFW_KEY_SCROLL_LOCK, + NumLock = GLFW_KEY_NUM_LOCK, + PrintScreen = GLFW_KEY_PRINT_SCREEN, + Pause = GLFW_KEY_PAUSE, + F1 = GLFW_KEY_F1, + F2 = GLFW_KEY_F2, + F3 = GLFW_KEY_F3, + F4 = GLFW_KEY_F4, + F5 = GLFW_KEY_F5, + F6 = GLFW_KEY_F6, + F7 = GLFW_KEY_F7, + F8 = GLFW_KEY_F8, + F9 = GLFW_KEY_F9, + F10 = GLFW_KEY_F10, + F11 = GLFW_KEY_F11, + F12 = GLFW_KEY_F12, + F13 = GLFW_KEY_F13, + F14 = GLFW_KEY_F14, + F15 = GLFW_KEY_F15, + F16 = GLFW_KEY_F16, + F17 = GLFW_KEY_F17, + F18 = GLFW_KEY_F18, + F19 = GLFW_KEY_F19, + F20 = GLFW_KEY_F20, + F21 = GLFW_KEY_F21, + F22 = GLFW_KEY_F22, + F23 = GLFW_KEY_F23, + F24 = GLFW_KEY_F24, + F25 = GLFW_KEY_F25, + KP0 = GLFW_KEY_KP_0, + KP1 = GLFW_KEY_KP_1, + KP2 = GLFW_KEY_KP_2, + KP3 = GLFW_KEY_KP_3, + KP4 = GLFW_KEY_KP_4, + KP5 = GLFW_KEY_KP_5, + KP6 = GLFW_KEY_KP_6, + KP7 = GLFW_KEY_KP_7, + KP8 = GLFW_KEY_KP_8, + KP9 = GLFW_KEY_KP_9, + KPDecimal = GLFW_KEY_KP_DECIMAL, + KPDivide = GLFW_KEY_KP_DIVIDE, + KPMultiply = GLFW_KEY_KP_MULTIPLY, + KPSubtract = GLFW_KEY_KP_SUBTRACT, + KPAdd = GLFW_KEY_KP_ADD, + KPEnter = GLFW_KEY_KP_ENTER, + KPEqual = GLFW_KEY_KP_EQUAL, + LeftShift = GLFW_KEY_LEFT_SHIFT, + LeftControl = GLFW_KEY_LEFT_CONTROL, + LeftAlt = GLFW_KEY_LEFT_ALT, + LeftSuper = GLFW_KEY_LEFT_SUPER, + RightShift = GLFW_KEY_RIGHT_SHIFT, + RightControl = GLFW_KEY_RIGHT_CONTROL, + RightAlt = GLFW_KEY_RIGHT_ALT, + RightSuper = GLFW_KEY_RIGHT_SUPER, + Menu = GLFW_KEY_MENU +}; diff --git a/src/Engine/LuaAPI.cpp b/src/Engine/LuaAPI.cpp index 40d6e52..112876a 100644 --- a/src/Engine/LuaAPI.cpp +++ b/src/Engine/LuaAPI.cpp @@ -9,6 +9,9 @@ #include "Componenets/ScriptComponent.h" #include "Componenets/GameObject.h" #include "Windows/LoggerWindow.h" +#include "Engine/InputManager.h" + +#include "KeyCode.h" #include #include @@ -21,6 +24,10 @@ // External LoggerWindow instance for logging extern LoggerWindow *g_LoggerWindow; + +// Forward declaration of the InputManager instance +extern InputManager g_InputManager; + // External GameObjects list extern std::vector> g_GameObjects; @@ -43,6 +50,149 @@ LuaManager::~LuaManager() } } + + + + +// Function to create and register the KeyCode table +void LuaManager::RegisterKeyCodeTable(lua_State* L) +{ + lua_newtable(L); // Create a new table and push it onto the stack + + // Push key-value pairs + #define REGISTER_KEYCODE(name) \ + lua_pushstring(L, #name); \ + lua_pushinteger(L, static_cast(KeyCode::name)); \ + lua_settable(L, -3); + + REGISTER_KEYCODE(Space) + REGISTER_KEYCODE(Apostrophe) + REGISTER_KEYCODE(Comma) + REGISTER_KEYCODE(Minus) + REGISTER_KEYCODE(Period) + REGISTER_KEYCODE(Slash) + REGISTER_KEYCODE(D0) + REGISTER_KEYCODE(D1) + REGISTER_KEYCODE(D2) + REGISTER_KEYCODE(D3) + REGISTER_KEYCODE(D4) + REGISTER_KEYCODE(D5) + REGISTER_KEYCODE(D6) + REGISTER_KEYCODE(D7) + REGISTER_KEYCODE(D8) + REGISTER_KEYCODE(D9) + REGISTER_KEYCODE(Semicolon) + REGISTER_KEYCODE(Equal) + REGISTER_KEYCODE(A) + REGISTER_KEYCODE(B) + REGISTER_KEYCODE(C) + REGISTER_KEYCODE(D) + REGISTER_KEYCODE(E) + REGISTER_KEYCODE(F) + REGISTER_KEYCODE(G) + REGISTER_KEYCODE(H) + REGISTER_KEYCODE(I) + REGISTER_KEYCODE(J) + REGISTER_KEYCODE(K) + REGISTER_KEYCODE(L) + REGISTER_KEYCODE(M) + REGISTER_KEYCODE(N) + REGISTER_KEYCODE(O) + REGISTER_KEYCODE(P) + REGISTER_KEYCODE(Q) + REGISTER_KEYCODE(R) + REGISTER_KEYCODE(S) + REGISTER_KEYCODE(T) + REGISTER_KEYCODE(U) + REGISTER_KEYCODE(V) + REGISTER_KEYCODE(W) + REGISTER_KEYCODE(X) + REGISTER_KEYCODE(Y) + REGISTER_KEYCODE(Z) + REGISTER_KEYCODE(LeftBracket) + REGISTER_KEYCODE(Backslash) + REGISTER_KEYCODE(RightBracket) + REGISTER_KEYCODE(GraveAccent) + REGISTER_KEYCODE(World1) + REGISTER_KEYCODE(World2) + REGISTER_KEYCODE(Escape) + REGISTER_KEYCODE(Enter) + REGISTER_KEYCODE(Tab) + REGISTER_KEYCODE(Backspace) + REGISTER_KEYCODE(Insert) + REGISTER_KEYCODE(Delete) + REGISTER_KEYCODE(Right) + REGISTER_KEYCODE(Left) + REGISTER_KEYCODE(Down) + REGISTER_KEYCODE(Up) + REGISTER_KEYCODE(PageUp) + REGISTER_KEYCODE(PageDown) + REGISTER_KEYCODE(Home) + REGISTER_KEYCODE(End) + REGISTER_KEYCODE(CapsLock) + REGISTER_KEYCODE(ScrollLock) + REGISTER_KEYCODE(NumLock) + REGISTER_KEYCODE(PrintScreen) + REGISTER_KEYCODE(Pause) + REGISTER_KEYCODE(F1) + REGISTER_KEYCODE(F2) + REGISTER_KEYCODE(F3) + REGISTER_KEYCODE(F4) + REGISTER_KEYCODE(F5) + REGISTER_KEYCODE(F6) + REGISTER_KEYCODE(F7) + REGISTER_KEYCODE(F8) + REGISTER_KEYCODE(F9) + REGISTER_KEYCODE(F10) + REGISTER_KEYCODE(F11) + REGISTER_KEYCODE(F12) + REGISTER_KEYCODE(F13) + REGISTER_KEYCODE(F14) + REGISTER_KEYCODE(F15) + REGISTER_KEYCODE(F16) + REGISTER_KEYCODE(F17) + REGISTER_KEYCODE(F18) + REGISTER_KEYCODE(F19) + REGISTER_KEYCODE(F20) + REGISTER_KEYCODE(F21) + REGISTER_KEYCODE(F22) + REGISTER_KEYCODE(F23) + REGISTER_KEYCODE(F24) + REGISTER_KEYCODE(F25) + REGISTER_KEYCODE(KP0) + REGISTER_KEYCODE(KP1) + REGISTER_KEYCODE(KP2) + REGISTER_KEYCODE(KP3) + REGISTER_KEYCODE(KP4) + REGISTER_KEYCODE(KP5) + REGISTER_KEYCODE(KP6) + REGISTER_KEYCODE(KP7) + REGISTER_KEYCODE(KP8) + REGISTER_KEYCODE(KP9) + REGISTER_KEYCODE(KPDecimal) + REGISTER_KEYCODE(KPDivide) + REGISTER_KEYCODE(KPMultiply) + REGISTER_KEYCODE(KPSubtract) + REGISTER_KEYCODE(KPAdd) + REGISTER_KEYCODE(KPEnter) + REGISTER_KEYCODE(KPEqual) + REGISTER_KEYCODE(LeftShift) + REGISTER_KEYCODE(LeftControl) + REGISTER_KEYCODE(LeftAlt) + REGISTER_KEYCODE(LeftSuper) + REGISTER_KEYCODE(RightShift) + REGISTER_KEYCODE(RightControl) + REGISTER_KEYCODE(RightAlt) + REGISTER_KEYCODE(RightSuper) + REGISTER_KEYCODE(Menu) + + #undef REGISTER_KEYCODE + + // Set the table in the global namespace as 'KeyCode' + lua_setglobal(L, "_T_KeyCode_Table"); +} + + // Initialize the LuaManager with the given script path bool LuaManager::Initialize(const std::string &scriptPath) { @@ -84,6 +234,8 @@ bool LuaManager::Initialize(const std::string &scriptPath) // Register all necessary metatables RegisterAllMetatables(); + RegisterKeyCodeTable(m_LuaState); + // Create the Engine table lua_newtable(m_LuaState); lua_setglobal(m_LuaState, "_T_Engine_Table"); @@ -96,6 +248,10 @@ bool LuaManager::Initialize(const std::string &scriptPath) lua_pushcfunction(m_LuaState, Lua_Engine_Expose); lua_setfield(m_LuaState, -2, "Expose"); + // Bind the KeyDown function to Engine.KeyDown + lua_pushcfunction(m_LuaState, Lua_Engine_KeyDown); + lua_setfield(m_LuaState, -2, "KeyDown"); + // Add the ScriptName binding lua_pushcfunction(m_LuaState, Lua_Engine_ScriptName); lua_setfield(m_LuaState, -2, "ScriptName"); @@ -212,6 +368,26 @@ void LuaManager::UpdateVariable(const std::string &name, const LuaManager::LuaEx lua_pop(m_LuaState, 1); } +// Lua binding for Engine.KeyDown +int LuaManager::Lua_Engine_KeyDown(lua_State* L) +{ + // Check if the first argument is an integer (keycode) + if (!lua_isinteger(L, 1)) + { + lua_pushstring(L, "KeyDown expects an integer keycode argument."); + lua_error(L); // Raises a Lua error and does not return + return 0; // This line won't be reached + } + + int keycode = static_cast(lua_tointeger(L, 1)); + + // Check if the key is currently pressed + bool isPressed = g_InputManager.IsKeyPressed(static_cast(keycode)); + + lua_pushboolean(L, isPressed ? 1 : 0); // Push true or false onto the Lua stack + + return 1; // Number of return values +} int LuaManager::Lua_Engine_Expose(lua_State *L) { // Check that at least two arguments are passed: name and value diff --git a/src/Engine/LuaAPI.h b/src/Engine/LuaAPI.h index b955620..c19cf7d 100644 --- a/src/Engine/LuaAPI.h +++ b/src/Engine/LuaAPI.h @@ -62,12 +62,9 @@ public: using LuaExposedVariant = std::variant; - - // Retrieve exposed variables std::unordered_map GetExposedVariables(); - void UpdateVariable(const std::string& name, const LuaExposedVariant& value); - + void UpdateVariable(const std::string &name, const LuaExposedVariant &value); private: // Lua state @@ -79,13 +76,14 @@ private: static std::unordered_map m_ExposedVariables; - // Last error message to prevent duplicate logging std::string m_LastErrorMessage; + void RegisterKeyCodeTable(lua_State *L); + + void LogError(const char *message); + void HandleLuaError(const std::string &functionName); - void LogError(const char* message); - void HandleLuaError(const std::string& functionName); void RegisterAllMetatables(); void RegisterComponentMetaTable(); @@ -120,8 +118,7 @@ private: static int Lua_Engine_Log(lua_State *L); static int Lua_Engine_ScriptName(lua_State *L); static int Lua_Engine_GetGameObjectByTag(lua_State *L); - static int Lua_Engine_Expose(lua_State* L); - - + static int Lua_Engine_Expose(lua_State *L); + static int Lua_Engine_KeyDown(lua_State* L); }; diff --git a/src/Engine/ScopedTimer.h b/src/Engine/ScopedTimer.h index 46f44c2..1fcf60c 100644 --- a/src/Engine/ScopedTimer.h +++ b/src/Engine/ScopedTimer.h @@ -5,6 +5,22 @@ #include class Profiler; // Forward declaration +// Helper macros for token concatenation +#define CONCATENATE_DETAIL(x, y) x##y +#define CONCATENATE(x, y) CONCATENATE_DETAIL(x, y) + +#ifdef TIMERS + // Use __COUNTER__ for unique variable names if available + #ifdef __COUNTER__ + #define SCOPE_TIMER(name) ScopedTimer CONCATENATE(timer_, __COUNTER__)(name) + #else + // Fallback to __LINE__ if __COUNTER__ is not supported + #define SCOPE_TIMER(name) ScopedTimer CONCATENATE(timer_, __LINE__)(name) + #endif +#else + // When TIMERS is not defined, expand to nothing + #define SCOPE_TIMER(name) +#endif class ScopedTimer { public: diff --git a/src/Windows/InspectorWindow.cpp b/src/Windows/InspectorWindow.cpp index a186930..4190bb4 100644 --- a/src/Windows/InspectorWindow.cpp +++ b/src/Windows/InspectorWindow.cpp @@ -3,6 +3,7 @@ #include // for strcpy, if needed #include // Required for glm::value_ptr +#include "Engine/ScopedTimer.h" #include @@ -18,6 +19,8 @@ extern LoggerWindow *g_LoggerWindow; void InspectorWindow::Show() { + SCOPE_TIMER("InspectorWindow::Show"); + // Increase window/item spacing for a cleaner look ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(12, 12)); ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(6, 4)); diff --git a/src/Windows/LoggerWindow.cpp b/src/Windows/LoggerWindow.cpp index 94793c9..556207f 100644 --- a/src/Windows/LoggerWindow.cpp +++ b/src/Windows/LoggerWindow.cpp @@ -5,15 +5,18 @@ #include #include #include "Icons.h" +#include "Engine/ScopedTimer.h" // Helper function to format strings -static std::string FormatString(const char* fmt, va_list args) { +static std::string FormatString(const char *fmt, va_list args) +{ char buffer[1024]; vsnprintf(buffer, sizeof(buffer), fmt, args); return std::string(buffer); } -void LoggerWindow::AddLog(const char* fmt, ...) { +void LoggerWindow::AddLog(const char *fmt, ...) +{ va_list args; va_start(args, fmt); std::string formatted = FormatString(fmt, args); @@ -23,7 +26,8 @@ void LoggerWindow::AddLog(const char* fmt, ...) { m_ScrollToBottom = true; } -void LoggerWindow::AddLog(const char* fmt, std::optional color, ...) { +void LoggerWindow::AddLog(const char *fmt, std::optional color, ...) +{ va_list args; va_start(args, color); std::string formatted = FormatString(fmt, args); @@ -33,10 +37,14 @@ void LoggerWindow::AddLog(const char* fmt, std::optional color, ...) { m_ScrollToBottom = true; } -void LoggerWindow::Show() { +void LoggerWindow::Show() +{ + SCOPE_TIMER("LoggerWindow::Show"); + ImGui::Begin(ICON_FA_TERMINAL " Logger##logger"); - if (ImGui::Button("Clear")) { + if (ImGui::Button("Clear")) + { m_Logs.clear(); } @@ -45,18 +53,23 @@ void LoggerWindow::Show() { // Begin a child region to enable scrolling ImGui::BeginChild("LoggerScrollRegion", ImVec2(0, 0), false, ImGuiWindowFlags_HorizontalScrollbar); - for (const auto& logEntry : m_Logs) { - if (logEntry.color.has_value()) { + for (const auto &logEntry : m_Logs) + { + if (logEntry.color.has_value()) + { ImGui::PushStyleColor(ImGuiCol_Text, logEntry.color.value()); ImGui::TextUnformatted(logEntry.text.c_str()); ImGui::PopStyleColor(); - } else { + } + else + { ImGui::TextUnformatted(logEntry.text.c_str()); } } // Auto-scroll to bottom if new logs are added - if (m_ScrollToBottom) { + if (m_ScrollToBottom) + { ImGui::SetScrollHereY(1.0f); m_ScrollToBottom = false; } diff --git a/src/Windows/PerformanceWindow.cpp b/src/Windows/PerformanceWindow.cpp index daf8f5e..018e9e2 100644 --- a/src/Windows/PerformanceWindow.cpp +++ b/src/Windows/PerformanceWindow.cpp @@ -2,14 +2,12 @@ #include "imgui.h" #include // for std::max_element, etc. #include "Engine/ThemeManager.h" - - - +#include "Engine/ScopedTimer.h" extern int LoadedAssets; extern int g_GPU_Triangles_drawn_to_screen; -const char* polygonModeOptions[] = { "Fill", "Wireframe", "Points" }; +const char *polygonModeOptions[] = {"Fill", "Wireframe", "Points"}; const int numPolygonModes = sizeof(polygonModeOptions) / sizeof(polygonModeOptions[0]); int polygonMode = 0; @@ -49,11 +47,12 @@ void PerformanceWindow::UpdatePerformanceStats(int newCallCount, int newTriangle m_TriangleCount = newTriangleCount; g_GPU_Triangles_drawn_to_screen = 0; - } void PerformanceWindow::Show(float fps, float ms) { + SCOPE_TIMER("PerformanceWindow::Show"); + // 1) Get current time from ImGui's internal clock double currentTime = ImGui::GetTime(); @@ -154,7 +153,7 @@ void PerformanceWindow::Show(float fps, float ms) 0, nullptr, 0.0f, - m_TriangleCount*2.5, + m_TriangleCount * 2.5, ImVec2(0, 50)); ImGui::Separator(); @@ -164,8 +163,6 @@ void PerformanceWindow::Show(float fps, float ms) ImGui::Separator(); - - const char *options[] = {"Bootsrap", "Duck Red", "Windark", "Deep Dark", "Tesseract Black"}; static int current_option = -1; // No selection initially @@ -177,7 +174,7 @@ void PerformanceWindow::Show(float fps, float ms) bool is_selected = (current_option == n); if (ImGui::Selectable(options[n], is_selected)) { - current_option = n; // Update current option + current_option = n; // Update current option ThemeManager_ChangeTheme(n); // Call the function with the selected option } // Set the initial focus when opening the combo (optional) diff --git a/src/Windows/ProfilerWindow.cpp b/src/Windows/ProfilerWindow.cpp index 07f8177..506eb9b 100644 --- a/src/Windows/ProfilerWindow.cpp +++ b/src/Windows/ProfilerWindow.cpp @@ -4,6 +4,7 @@ #include #include // For debug statements #include "Icons.h" +#include "Engine/ScopedTimer.h" // Constructor ProfilerWindow::ProfilerWindow() @@ -87,6 +88,8 @@ void ProfilerWindow::UpdateHistory(const std::unordered_map elapsed = now - m_LastUpdateTime; @@ -99,7 +102,7 @@ void ProfilerWindow::Show() } // Begin ImGui window with improved styling - ImGui::Begin(ICON_FA_GAUGE " Profiler", nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_HorizontalScrollbar); + ImGui::Begin(ICON_FA_GAUGE " Profiler", nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_HorizontalScrollbar); const auto &data = Profiler::Get().GetLastFrameData(); diff --git a/src/Windows/SceneWindow.cpp b/src/Windows/SceneWindow.cpp index 7ef7b92..5681d10 100644 --- a/src/Windows/SceneWindow.cpp +++ b/src/Windows/SceneWindow.cpp @@ -6,6 +6,7 @@ #include "Engine/AssetManager.h" #include "TestModel.h" #include "gcml.h" +#include "Engine/ScopedTimer.h" #include @@ -28,6 +29,7 @@ std::shared_ptr CreateDefaultCube() void SceneWindow::Show() { + SCOPE_TIMER("SceneWindow::Show"); if (ImGui::Begin("Scene Window##SceneWindow")) { // Add Button