From bce659e55d1d44042d91df501a3951f06c4fea5d Mon Sep 17 00:00:00 2001 From: CatChow0 Date: Fri, 10 Jan 2025 20:43:36 +0100 Subject: [PATCH] Minor - Vsync toggle New Method : + Set Vsync + Set Screen Width + Set Screen Height + Get Hwnd + Set Hwnd + IsWindowed + SetWindowed New UI For Engine Settings : + Add Vsync Toggle to UI --- enginecustom/Systemclass.cpp | 2 ++ enginecustom/applicationclass.cpp | 52 +++++++++++++++++++++++++++++++ enginecustom/applicationclass.h | 16 +++++++++- enginecustom/d3dclass.cpp | 8 ++++- enginecustom/d3dclass.h | 2 ++ enginecustom/imgui.ini | 10 ++++-- enginecustom/imguiManager.cpp | 24 ++++++++++++++ enginecustom/imguiManager.h | 2 ++ 8 files changed, 111 insertions(+), 5 deletions(-) diff --git a/enginecustom/Systemclass.cpp b/enginecustom/Systemclass.cpp index b36806b..1e85468 100644 --- a/enginecustom/Systemclass.cpp +++ b/enginecustom/Systemclass.cpp @@ -257,6 +257,8 @@ LRESULT CALLBACK SystemClass::MessageHandler(HWND hwnd, UINT umsg, WPARAM wparam // If Direct3D is initialized, update the swap chain. Otherwise, store the window dimensions if (m_isDirect3DInitialized && m_Application && m_Application->GetDirect3D()) { + m_Application->SetScreenWidth(newWidth); + m_Application->SetScreenHeight(newHeight); m_Application->GetDirect3D()->ResizeSwapChain(newWidth, newHeight); } else diff --git a/enginecustom/applicationclass.cpp b/enginecustom/applicationclass.cpp index db7f9a9..83064ba 100644 --- a/enginecustom/applicationclass.cpp +++ b/enginecustom/applicationclass.cpp @@ -56,6 +56,11 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd) m_screenWidth = screenWidth; m_screenHeight = screenHeight; + SetHwnd(hwnd); + SetWindowed(FULL_SCREEN); + SetScreenHeight(screenHeight); + SetScreenWidth(screenWidth); + // Create the Direct3D object. m_Direct3D = new D3DClass; if (!m_Direct3D) @@ -1974,4 +1979,51 @@ void ApplicationClass::SetLightPosition(int index, XMVECTOR position) //set the position m_Lights[index]->SetPosition(lightPosition.x, lightPosition.y, lightPosition.z); +} + +void ApplicationClass::SetVsync(bool vsync) +{ + VSYNC_ENABLED = vsync; + + if (m_Direct3D) + { + Logger::Get().Log("Setting Vsync to " + std::to_string(vsync) + " with a screen width : " + std::to_string(GetScreenWidth()) + "and a screen height : " + std::to_string(GetScreenHeight()), __FILE__, __LINE__); + m_Direct3D->SetVsync(vsync); + } +} + +HWND ApplicationClass::GetHwnd() const +{ + return m_hwnd; +} + +void ApplicationClass::SetHwnd(HWND hwnd) +{ + m_hwnd = hwnd; +} + +bool ApplicationClass::IsWindowed() const +{ + return m_windowed; +} + +void ApplicationClass::SetWindowed(bool windowed) +{ + // log the new windowed mode + Logger::Get().Log("Setting windowed mode to " + std::to_string(windowed), __FILE__, __LINE__); + m_windowed = windowed; +} + +void ApplicationClass::SetScreenHeight(int height) +{ + // log the new screen height + Logger::Get().Log("Setting screen height to " + std::to_string(height), __FILE__, __LINE__); + m_screenHeight = height; +} + +void ApplicationClass::SetScreenWidth(int width) +{ + // log the new screen width + Logger::Get().Log("Setting screen width to " + std::to_string(width), __FILE__, __LINE__); + m_screenWidth = width; } \ No newline at end of file diff --git a/enginecustom/applicationclass.h b/enginecustom/applicationclass.h index 1dcb0a2..1337bc3 100644 --- a/enginecustom/applicationclass.h +++ b/enginecustom/applicationclass.h @@ -38,7 +38,6 @@ // GLOBALS // ///////////// const bool FULL_SCREEN = false; -const bool VSYNC_ENABLED = false; const float SCREEN_DEPTH = 1000.0f; const float SCREEN_NEAR = 0.3f; @@ -59,7 +58,9 @@ public: bool Frame(InputClass*); int GetScreenWidth() const; + void SetScreenWidth(int screenWidth); int GetScreenHeight() const; + void SetScreenHeight(int screenHeight); float GetSpeed() const { return m_speed; }; void SetSpeed(float speed) { this->m_speed = speed; }; @@ -92,6 +93,15 @@ public: std::vector textures; + void SetVsync(bool vsync); + bool GetVsync() const { return VSYNC_ENABLED; }; + + HWND GetHwnd() const; + void SetHwnd(HWND hwnd); + + bool IsWindowed() const; + void SetWindowed(bool windowed); + private: bool Render(float, float, float, float, float); bool UpdateMouseStrings(int, int, bool); @@ -111,6 +121,10 @@ private : IDXGISwapChain* m_swapChain; ModelClass* m_Model,* m_GroundModel, * m_WallModel, * m_BathModel, * m_WaterModel; ModelListClass* m_ModelList; + bool VSYNC_ENABLED = true; + + HWND m_hwnd; + bool m_windowed; // ------------------------------------- // // ------------- RENDERING ------------- // diff --git a/enginecustom/d3dclass.cpp b/enginecustom/d3dclass.cpp index 15a3ea9..034e248 100644 --- a/enginecustom/d3dclass.cpp +++ b/enginecustom/d3dclass.cpp @@ -695,7 +695,8 @@ IDXGISwapChain* D3DClass::GetSwapChain() void D3DClass::ResizeSwapChain(int newWidth, int newHeight) { - Logger::Get().Log("Resizing swap chain", __FILE__, __LINE__); + // log the new width and height + Logger::Get().Log("Resizing swap chain to " + std::to_string(newWidth) + "x" + std::to_string(newHeight), __FILE__, __LINE__); HRESULT result; @@ -766,3 +767,8 @@ void D3DClass::DisableAlphaBlending() return; } + +void D3DClass::SetVsync(bool vsync) +{ + m_vsync_enabled = vsync; +} \ No newline at end of file diff --git a/enginecustom/d3dclass.h b/enginecustom/d3dclass.h index 6576d1a..095e6f7 100644 --- a/enginecustom/d3dclass.h +++ b/enginecustom/d3dclass.h @@ -46,6 +46,8 @@ public: IDXGISwapChain* m_swapChain; IDXGISwapChain* GetSwapChain(); void ResizeSwapChain(int, int); + void SetVsync(bool vsync); + XMMATRIX GetProjectionMatrix() const { return m_projectionMatrix; }; XMMATRIX GetWorldMatrix() const { return m_worldMatrix;}; diff --git a/enginecustom/imgui.ini b/enginecustom/imgui.ini index c15b616..475a861 100644 --- a/enginecustom/imgui.ini +++ b/enginecustom/imgui.ini @@ -3,7 +3,7 @@ Pos=60,60 Size=400,400 [Window][Khaotic Engine] -Pos=1113,54 +Pos=1143,44 Size=392,273 [Window][Objects] @@ -20,6 +20,10 @@ Pos=1548,17 Size=358,535 [Window][Shader Manager] -Pos=28,261 -Size=172,284 +Pos=471,90 +Size=180,79 + +[Window][Engine Settings] +Pos=561,352 +Size=168,77 diff --git a/enginecustom/imguiManager.cpp b/enginecustom/imguiManager.cpp index dcae250..f9c2067 100644 --- a/enginecustom/imguiManager.cpp +++ b/enginecustom/imguiManager.cpp @@ -318,6 +318,11 @@ bool imguiManager::ImGuiWidgetRenderer(ApplicationClass* app) showShaderWindow = true; } + if (ImGui::Button("Open Engine Settings Window")) + { + showEngineSettingsWindow = true; + } + ImGui::End(); // Show windows if their corresponding variables are true @@ -341,6 +346,11 @@ bool imguiManager::ImGuiWidgetRenderer(ApplicationClass* app) WidgetShaderWindow(app); } + if (showEngineSettingsWindow) + { + WidgetEngineSettingsWindow(app); + } + //render imgui Render(); @@ -379,5 +389,19 @@ void imguiManager::WidgetLightWindow(ApplicationClass* app) index++; }; + ImGui::End(); +} + +void imguiManager::WidgetEngineSettingsWindow(ApplicationClass* app) +{ + ImGui::Begin("Engine Settings", &showEngineSettingsWindow); + + // Checkbox for toggling vsync globally in the application class by calling the SetVsync function in the application class when the checkbox state changes + bool vsync = app->GetVsync(); + if (ImGui::Checkbox("Vsync", &vsync)) + { + app->SetVsync(vsync); + } + ImGui::End(); } \ No newline at end of file diff --git a/enginecustom/imguiManager.h b/enginecustom/imguiManager.h index a6977cb..8a81e2c 100644 --- a/enginecustom/imguiManager.h +++ b/enginecustom/imguiManager.h @@ -32,6 +32,7 @@ public: void WidgetTerrainWindow(ApplicationClass* app); void WidgetLightWindow(ApplicationClass* app); void WidgetShaderWindow(ApplicationClass* app); + void WidgetEngineSettingsWindow(ApplicationClass* app); bool ImGuiWidgetRenderer(ApplicationClass* app); @@ -44,6 +45,7 @@ private : bool showTerrainWindow = false; bool showLightWindow = false; bool showShaderWindow = false; + bool showEngineSettingsWindow = false; private: ImGuiIO* io;