diff --git a/Game/game.cpp b/Game/game.cpp index f6940bc..7fbd809 100644 --- a/Game/game.cpp +++ b/Game/game.cpp @@ -5,13 +5,16 @@ #include +#include #include +#include #include #include #include #include #include #include +#include GameState* gGameState = nullptr; GameState* GetGameState() @@ -92,5 +95,19 @@ extern "C" JULIET_API void __cdecl GameUpdate(Juliet::GameData* params, [[maybe_ printf("Rock has %d health points\n", rock->Health); } - // printf("Updating game...\n"); + if (IsKeyPressed(ScanCode::F1)) + { + gGameState->Mode = static_cast((ToUnderlying(gGameState->Mode) + 1) % 2); + } + + if (gGameState->Mode == GameMode::Play) + { + // update game + } + + if (gGameState->Mode == GameMode::Debug) + { + ImGui::Begin("Debug"); + ImGui::End(); + } } diff --git a/Game/game.h b/Game/game.h index 91bc696..46faa99 100644 --- a/Game/game.h +++ b/Game/game.h @@ -10,12 +10,20 @@ struct World EntityManager* EntityManager; }; +enum class GameMode +{ + Play, + Debug +}; + struct GameState { Juliet::Arena* TotalArena; World* World; + GameMode Mode = GameMode::Play; + float TotalTime; int Score; }; diff --git a/Juliet/include/Core/Application/IApplication.h b/Juliet/include/Core/Application/IApplication.h index 7a21e36..9b85e6a 100644 --- a/Juliet/include/Core/Application/IApplication.h +++ b/Juliet/include/Core/Application/IApplication.h @@ -17,7 +17,7 @@ namespace Juliet virtual ~IApplication() = default; virtual void Init(NonNullPtr arena) = 0; virtual void Shutdown() = 0; - virtual void Update() = 0; + virtual void Update(float deltaTime) = 0; virtual bool IsRunning() = 0; // Accessors for Engine Systems diff --git a/Juliet/include/Core/HAL/Event/SystemEvent.h b/Juliet/include/Core/HAL/Event/SystemEvent.h index 36aff95..e0b17fd 100644 --- a/Juliet/include/Core/HAL/Event/SystemEvent.h +++ b/Juliet/include/Core/HAL/Event/SystemEvent.h @@ -118,4 +118,6 @@ namespace Juliet // Add an event onto the event queue. // TODO : support array of events extern JULIET_API bool AddEvent(SystemEvent& event); + + extern void Events_NewFrame(float deltaTime); } // namespace Juliet diff --git a/Juliet/include/Core/HAL/Keyboard/Keyboard.h b/Juliet/include/Core/HAL/Keyboard/Keyboard.h index 716e6dd..e183b94 100644 --- a/Juliet/include/Core/HAL/Keyboard/Keyboard.h +++ b/Juliet/include/Core/HAL/Keyboard/Keyboard.h @@ -8,12 +8,18 @@ namespace Juliet { using KeyboardID = uint8; - enum class KeyState : bool + enum class KeyPosition : bool { Up = false, Down = true }; + struct KeyState + { + KeyPosition Position; + float Time; + }; + struct Key { ScanCode ScanCode; @@ -22,6 +28,7 @@ namespace Juliet }; extern JULIET_API bool IsKeyDown(ScanCode scanCode); + extern JULIET_API bool IsKeyPressed(ScanCode scanCode); extern JULIET_API KeyMod GetKeyModState(); extern JULIET_API KeyCode GetKeyCodeFromScanCode(ScanCode scanCode, KeyMod keyModState); diff --git a/Juliet/include/Core/HAL/OS/OS.h b/Juliet/include/Core/HAL/OS/OS.h index 406ebdd..fe4d30d 100644 --- a/Juliet/include/Core/HAL/OS/OS.h +++ b/Juliet/include/Core/HAL/OS/OS.h @@ -31,6 +31,11 @@ namespace Juliet } // namespace Memory + namespace Time + { + uint64 Timestamp(); + } // namespace Time + namespace Debug { JULIET_API bool IsDebuggerPresent(); diff --git a/Juliet/src/Core/HAL/Display/Win32/Win32DisplayEvent.cpp b/Juliet/src/Core/HAL/Display/Win32/Win32DisplayEvent.cpp index 4e58f61..5a9815b 100644 --- a/Juliet/src/Core/HAL/Display/Win32/Win32DisplayEvent.cpp +++ b/Juliet/src/Core/HAL/Display/Win32/Win32DisplayEvent.cpp @@ -12,6 +12,7 @@ #include // For GET_X_LPARAM, GET_Y_LPARAM. +#include #include #ifdef JULIET_ENABLE_IMGUI @@ -157,7 +158,7 @@ namespace Juliet::Win32 LRESULT returnCode = -1; // Wait until the window state is created before doing anything - auto* windowState = GetWindowStateFromHandle(handle); + Window32State* windowState = GetWindowStateFromHandle(handle); if (!windowState) { return CallWindowProcA(DefWindowProcA, handle, message, wParam, lParam); @@ -188,7 +189,7 @@ namespace Juliet::Win32 SendWindowEvent(windowState->Window, EventType::Window_Close_Request); } - SendKeyboardKey(1, kGlobalKeyboardID, key, KeyState::Down); + SendKeyboardKey(Time::Timestamp(), kGlobalKeyboardID, key, KeyPosition::Down); returnCode = 0; break; @@ -197,7 +198,7 @@ namespace Juliet::Win32 case WM_KEYUP: { Key key = GetScanScodeFromWinScanCode(wParam, lParam); - SendKeyboardKey(1, kGlobalKeyboardID, key, KeyState::Up); + SendKeyboardKey(Time::Timestamp(), kGlobalKeyboardID, key, KeyPosition::Up); returnCode = 0; break; diff --git a/Juliet/src/Core/HAL/Event/Keyboard.cpp b/Juliet/src/Core/HAL/Event/Keyboard.cpp index f86a018..5447677 100644 --- a/Juliet/src/Core/HAL/Event/Keyboard.cpp +++ b/Juliet/src/Core/HAL/Event/Keyboard.cpp @@ -15,14 +15,14 @@ namespace Juliet KeyMod KeyModState; } KeyboardState; - bool SendKeyboardKey_Internal(uint64 timestamp, KeyboardID /*ID*/, Key key, KeyState keyState) + bool SendKeyboardKey_Internal(uint64 timestamp, KeyboardID /*ID*/, Key key, KeyPosition keyPosition) { Assert(key.KeyCode == KeyCode::Unknown); // At this point Keycode is not yet extracted auto& keyboardState = KeyboardState; // Needed because MSVC debugger ignores variable in anonymouse namespace auto type = EventType::None; - const bool isKeyDown = keyState == KeyState::Down; + const bool isKeyDown = keyPosition == KeyPosition::Down; if (isKeyDown) { type = EventType::Key_Down; @@ -40,21 +40,21 @@ namespace Juliet // If state didn't change, this is a key repeat if (isKeyDown) { - if (currentKeyState == KeyState::Down) + if (currentKeyState.Position == KeyPosition::Down) { isKeyRepeat = true; } } else { - if (currentKeyState == KeyState::Up) + if (currentKeyState.Position == KeyPosition::Up) { return false; } } - keyboardState.KeyState[ToUnderlying(key.ScanCode)] = keyState; - key.KeyCode = GetKeyCodeFromScanCode(key.ScanCode, keyboardState.KeyModState); + currentKeyState.Position = keyPosition; + key.KeyCode = GetKeyCodeFromScanCode(key.ScanCode, keyboardState.KeyModState); } else if (key.Raw == 0) { @@ -99,7 +99,7 @@ namespace Juliet evt.Type = type; evt.Data.Keyboard.AssociatedKeyboardID = kGlobalKeyboardID; evt.Data.Keyboard.Key = key; - evt.Data.Keyboard.KeyState = keyState; + evt.Data.Keyboard.KeyState = { keyPosition }; evt.Data.Keyboard.KeyModeState = keyboardState.KeyModState; bool evtPosted = AddEvent(evt); @@ -108,14 +108,42 @@ namespace Juliet } } // namespace - bool SendKeyboardKey(uint64 timestamp, KeyboardID ID, Key key, KeyState keyState) + bool SendKeyboardKey(uint64 timestamp, KeyboardID ID, Key key, KeyPosition keyPosition) { - return SendKeyboardKey_Internal(timestamp, ID, key, keyState); + return SendKeyboardKey_Internal(timestamp, ID, key, keyPosition); + } + + void UpdateKeyboardstate(float deltaTime) + { + for (KeyState& state : KeyboardState.KeyState) + { + if (state.Position == KeyPosition::Down) + { + if (state.Time < 0.0f) + { + state.Time = 0.0f; + } + else + { + state.Time += deltaTime; + } + } + else + { + state.Time = -1.0f; + } + } } bool IsKeyDown(ScanCode scanCode) { - return KeyboardState.KeyState[ToUnderlying(scanCode)] == KeyState::Down; + return KeyboardState.KeyState[ToUnderlying(scanCode)].Position == KeyPosition::Down; + } + + bool IsKeyPressed(ScanCode scanCode) + { + auto& keyState = KeyboardState.KeyState[ToUnderlying(scanCode)]; + return keyState.Position == KeyPosition::Down && keyState.Time == 0.0f; } KeyMod GetKeyModState() @@ -129,9 +157,9 @@ namespace Juliet return GetKeyCodeFromDefaultMapping(scanCode, keyModState); } - static_assert(sizeof(KeyState) == sizeof(bool)); - static_assert(ToUnderlying(KeyState::Down) == true); - static_assert(ToUnderlying(KeyState::Up) == false); + static_assert(sizeof(KeyPosition) == sizeof(bool)); + static_assert(ToUnderlying(KeyPosition::Down) == true); + static_assert(ToUnderlying(KeyPosition::Up) == false); static_assert(sizeof(ScanCode) == sizeof(uint16)); static_assert(ToUnderlying(ScanCode::Count) == 512); } // namespace Juliet diff --git a/Juliet/src/Core/HAL/Event/Keyboard_Private.h b/Juliet/src/Core/HAL/Event/Keyboard_Private.h index 8c1c3c3..00f8e7d 100644 --- a/Juliet/src/Core/HAL/Event/Keyboard_Private.h +++ b/Juliet/src/Core/HAL/Event/Keyboard_Private.h @@ -5,7 +5,8 @@ namespace Juliet { - extern bool SendKeyboardKey(uint64 timestamp, KeyboardID ID, Key key, KeyState keyState); + extern bool SendKeyboardKey(uint64 timestamp, KeyboardID ID, Key key, KeyPosition keyPosition); + extern void UpdateKeyboardstate(float deltaTime); extern const KeyboardID kGlobalKeyboardID; } // namespace Juliet diff --git a/Juliet/src/Core/HAL/Event/SystemEvent.cpp b/Juliet/src/Core/HAL/Event/SystemEvent.cpp index 0dbf4a3..5d65146 100644 --- a/Juliet/src/Core/HAL/Event/SystemEvent.cpp +++ b/Juliet/src/Core/HAL/Event/SystemEvent.cpp @@ -1,6 +1,8 @@ #include #include +#include + #pragma push_macro("global") #undef global @@ -87,4 +89,10 @@ namespace Juliet return AddEvent_Internal(event); } + + void Events_NewFrame(float deltaTime) + { + UpdateKeyboardstate(deltaTime); + } + } // namespace Juliet diff --git a/Juliet/src/Core/HAL/OS/OS.cpp b/Juliet/src/Core/HAL/OS/OS.cpp index 9b90063..128ddb7 100644 --- a/Juliet/src/Core/HAL/OS/OS.cpp +++ b/Juliet/src/Core/HAL/OS/OS.cpp @@ -21,6 +21,14 @@ namespace Juliet } } // namespace Memory + namespace Time + { + uint64 Timestamp() + { + return Internal::OS_Timestamp(); + } + } // namespace Time + namespace Debug { bool IsDebuggerPresent() diff --git a/Juliet/src/Core/HAL/OS/OS_Private.h b/Juliet/src/Core/HAL/OS/OS_Private.h index d5b2336..201a320 100644 --- a/Juliet/src/Core/HAL/OS/OS_Private.h +++ b/Juliet/src/Core/HAL/OS/OS_Private.h @@ -13,6 +13,12 @@ namespace Juliet { bool IsDebuggerPresent(); } // namespace Debug::Internal + + namespace Time::Internal + { + uint64 OS_Timestamp(); + } + namespace Internal { int OS_Main(EntryPointFunc entryPointFunc, int argc, wchar_t** argv); diff --git a/Juliet/src/Core/HAL/OS/Win32/Win32OS.cpp b/Juliet/src/Core/HAL/OS/Win32/Win32OS.cpp index 1e94f25..57bb574 100644 --- a/Juliet/src/Core/HAL/OS/Win32/Win32OS.cpp +++ b/Juliet/src/Core/HAL/OS/Win32/Win32OS.cpp @@ -45,6 +45,17 @@ namespace Juliet } } // namespace Memory::Internal + namespace Time::Internal + { + uint64 OS_Timestamp() + { + FILETIME ft; + GetSystemTimeAsFileTime(&ft); + + return (static_cast(ft.dwHighDateTime) << 32) + ft.dwLowDateTime; + } + } // namespace Time::Internal + namespace Debug::Internal { bool IsDebuggerPresent() diff --git a/Juliet/src/Engine/Engine.cpp b/Juliet/src/Engine/Engine.cpp index 7c51854..59c690d 100644 --- a/Juliet/src/Engine/Engine.cpp +++ b/Juliet/src/Engine/Engine.cpp @@ -1,6 +1,7 @@ #include #include +#include #include #include #include @@ -18,6 +19,14 @@ #include #endif +// TODO Move to os hal for query perf +#ifdef _WIN32 +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#include +#endif + namespace Juliet { #if JULIET_DEBUG @@ -210,8 +219,25 @@ namespace Juliet ImGuiRenderer_NewFrame(); #endif + // TODO : Move to os side as it is win32 only + static LARGE_INTEGER frequency = {}; + static LARGE_INTEGER lastTime = {}; + if (frequency.QuadPart == 0) + { + QueryPerformanceFrequency(&frequency); + QueryPerformanceCounter(&lastTime); + } + + LARGE_INTEGER currentTime; + QueryPerformanceCounter(¤tTime); + float deltaTime = + static_cast(currentTime.QuadPart - lastTime.QuadPart) / static_cast(frequency.QuadPart); + lastTime = currentTime; + + Events_NewFrame(deltaTime); + // Logic tick - EngineInstance.Application->Update(); + EngineInstance.Application->Update(deltaTime); // Render tick RenderFrame(); diff --git a/JulietApp/main.cpp b/JulietApp/main.cpp index adad648..54672a7 100644 --- a/JulietApp/main.cpp +++ b/JulietApp/main.cpp @@ -225,20 +225,9 @@ void JulietApplication::Shutdown() Log(LogLevel::Message, LogCategory::Tool, "Juliet App shutdown Completed"); } -void JulietApplication::Update() +void JulietApplication::Update(float deltaTime) { - static LARGE_INTEGER frequency = {}; - static LARGE_INTEGER lastTime = {}; - if (frequency.QuadPart == 0) - { - QueryPerformanceFrequency(&frequency); - QueryPerformanceCounter(&lastTime); - } - LARGE_INTEGER currentTime; - QueryPerformanceCounter(¤tTime); - float deltaTime = static_cast(currentTime.QuadPart - lastTime.QuadPart) / static_cast(frequency.QuadPart); - lastTime = currentTime; CameraTime += deltaTime; diff --git a/JulietApp/main.h b/JulietApp/main.h index a9d42c0..930b211 100644 --- a/JulietApp/main.h +++ b/JulietApp/main.h @@ -20,7 +20,7 @@ class JulietApplication : public Juliet::IApplication protected: void Init(Juliet::NonNullPtr arena) override; void Shutdown() override; - void Update() override; + void Update(float deltaTime) override; bool IsRunning() override; Juliet::Window* GetPlatformWindow() override { return MainWindow; }