From 77adeb28729df99278b07d196338fdb767b0d0fb Mon Sep 17 00:00:00 2001 From: CatChow0 Date: Fri, 22 Mar 2024 16:51:17 +0100 Subject: [PATCH] imguiManager et start Fullscreen --- enginecustom/Systemclass.cpp | 79 ++++++++++------- enginecustom/applicationclass.cpp | 22 ++++- enginecustom/applicationclass.h | 6 +- enginecustom/d3dclass.cpp | 135 ++++++++++++++++++++++++++++++ enginecustom/d3dclass.h | 6 ++ enginecustom/enginecustom.vcxproj | 2 + enginecustom/imgui.ini | 4 +- enginecustom/imguiManager.cpp | 48 +++++++++++ enginecustom/imguiManager.h | 26 ++++++ enginecustom/systemclass.h | 7 +- 10 files changed, 298 insertions(+), 37 deletions(-) create mode 100644 enginecustom/imguiManager.cpp create mode 100644 enginecustom/imguiManager.h diff --git a/enginecustom/Systemclass.cpp b/enginecustom/Systemclass.cpp index 991419d..5011268 100644 --- a/enginecustom/Systemclass.cpp +++ b/enginecustom/Systemclass.cpp @@ -6,6 +6,7 @@ SystemClass::SystemClass() { m_Input = 0; m_Application = 0; + m_imguiManager = 0; } SystemClass::SystemClass(const SystemClass& other) @@ -27,6 +28,9 @@ bool SystemClass::Initialize() screenWidth = 0; screenHeight = 0; + // Create and initialize the application class object. This object will handle rendering all the graphics for this application. + m_Application = new ApplicationClass; + // Initialize the windows api. InitializeWindows(screenWidth, screenHeight); @@ -35,11 +39,6 @@ bool SystemClass::Initialize() m_Input->Initialize(); - // Create and initialize the application class object. This object will handle rendering all the graphics for this application. - m_Application = new ApplicationClass; - - // Initialize the application object. - result = m_Application->Initialize(screenWidth, screenHeight, m_hwnd); if (!result) { @@ -47,13 +46,8 @@ bool SystemClass::Initialize() } // Initialize imgui - IMGUI_CHECKVERSION(); - ImGui::CreateContext(); - ImGuiIO& io = ImGui::GetIO(); - - ImGui_ImplWin32_Init(m_hwnd); - ImGui_ImplDX11_Init(m_Application->GetDirect3D()->GetDevice(), m_Application->GetDirect3D()->GetDeviceContext()); - ImGui::StyleColorsDark(); + m_imguiManager = new imguiManager; + m_imguiManager->Initialize(m_hwnd, m_Application->GetDirect3D()->GetDevice(), m_Application->GetDirect3D()->GetDeviceContext()); return true; } @@ -76,9 +70,12 @@ void SystemClass::Shutdown() } // Shutdown imgui - ImGui_ImplDX11_Shutdown(); - ImGui_ImplWin32_Shutdown(); - ImGui::DestroyContext(); + if (m_imguiManager) + { + m_imguiManager->Shutdown(); + delete m_imguiManager; + m_imguiManager = 0; + } // Shutdown the window. ShutdownWindows(); @@ -146,20 +143,23 @@ bool SystemClass::Frame() } // Start the Dear ImGui frame - ImGui_ImplDX11_NewFrame(); - ImGui_ImplWin32_NewFrame(); - ImGui::NewFrame(); + m_imguiManager->NewFrame(); //ImGui Widget ImGui::Begin("Khaotic Engine", NULL); - ImGui::Text("Salam"); - ImGui::SliderFloat("Slider", &value, 0.0f, 1.0f); + + bool fullscreen = m_Application->GetFullscreen(); + m_imguiManager->WidgetFullscreenBox(&fullscreen); + if (fullscreen != m_Application->GetFullscreen()) + { + m_Application->SetFullscreen(fullscreen); + m_Application->GetDirect3D()->SetFullscreen(fullscreen); + } + ImGui::End(); - // Assemble Together Draw Data - ImGui::Render(); - // Render Draw Data - ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData()); + //render imgui + m_imguiManager->Render(); this->m_Application->GetDirect3D()->m_swapChain->Present(0, NULL); @@ -214,7 +214,7 @@ void SystemClass::InitializeWindows(int& screenWidth, int& screenHeight) m_hinstance = GetModuleHandle(NULL); // Give the application a name. - m_applicationName = L"Engine"; + m_applicationName = L"Khaotic Engine"; // Setup the windows class with default settings. wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; @@ -238,7 +238,7 @@ void SystemClass::InitializeWindows(int& screenWidth, int& screenHeight) screenHeight = GetSystemMetrics(SM_CYSCREEN); // Setup the screen settings depending on whether it is running in full screen or in windowed mode. - if (FULL_SCREEN) + if (m_Application->GetFullscreen()) { // If full screen set the screen to maximum size of the users desktop and 32bit. memset(&dmScreenSettings, 0, sizeof(dmScreenSettings)); @@ -267,7 +267,7 @@ void SystemClass::InitializeWindows(int& screenWidth, int& screenHeight) // Create the window with the screen settings and get the handle to it. m_hwnd = CreateWindowEx(WS_EX_APPWINDOW, m_applicationName, m_applicationName, - WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_POPUP, + WS_CLIPSIBLINGS | WS_CLIPCHILDREN , posX, posY, screenWidth, screenHeight, NULL, NULL, m_hinstance, NULL); // Bring the window up on the screen and set it as main focus. @@ -276,7 +276,7 @@ void SystemClass::InitializeWindows(int& screenWidth, int& screenHeight) SetFocus(m_hwnd); // Hide the mouse cursor. - ShowCursor(false); + ShowCursor(true); return; } @@ -287,7 +287,7 @@ void SystemClass::ShutdownWindows() ShowCursor(true); // Fix the display settings if leaving full screen mode. - if (FULL_SCREEN) + if (m_Application->GetFullscreen()) { ChangeDisplaySettings(NULL, 0); } @@ -336,3 +336,24 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT umessage, WPARAM wparam, LPARAM lparam) } } } + +void SystemClass::SetScreen(bool fullscreen) +{ + if (fullscreen) + { + DEVMODE dmScreenSettings; + memset(&dmScreenSettings, 0, sizeof(dmScreenSettings)); + dmScreenSettings.dmSize = sizeof(dmScreenSettings); + dmScreenSettings.dmPelsWidth = (unsigned long)GetSystemMetrics(SM_CXSCREEN); // donne la largeur de l'écran en pixel + dmScreenSettings.dmPelsHeight = (unsigned long)GetSystemMetrics(SM_CYSCREEN); // donne la hauteur de l'écran en pixel + dmScreenSettings.dmBitsPerPel = 32; + dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT; + + + ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN); + } + else + { + ChangeDisplaySettings(NULL, 0); + } +} \ No newline at end of file diff --git a/enginecustom/applicationclass.cpp b/enginecustom/applicationclass.cpp index 0ab0b3a..f2b694d 100644 --- a/enginecustom/applicationclass.cpp +++ b/enginecustom/applicationclass.cpp @@ -31,7 +31,7 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd) // Create and initialize the Direct3D object. m_Direct3D = new D3DClass; - result = m_Direct3D->Initialize(screenWidth, screenHeight, VSYNC_ENABLED, hwnd, FULL_SCREEN, SCREEN_DEPTH, SCREEN_NEAR); + result = m_Direct3D->Initialize(screenWidth, screenHeight, VSYNC_ENABLED, hwnd, m_fullscreen, SCREEN_DEPTH, SCREEN_NEAR); if (!result) { MessageBox(hwnd, L"Could not initialize Direct3D", L"Error", MB_OK); @@ -157,4 +157,24 @@ bool ApplicationClass::Render() D3DClass* ApplicationClass::GetDirect3D() { return m_Direct3D; +} + +void ApplicationClass::SetFullscreen(bool fullscreen) +{ + m_fullscreen = fullscreen; +} + +bool ApplicationClass::GetFullscreen() const +{ + return m_fullscreen; +} + +int ApplicationClass::GetScreenWidth() const +{ + return GetSystemMetrics(SM_CXSCREEN); +} + +int ApplicationClass::GetScreenHeight() const +{ + return GetSystemMetrics(SM_CYSCREEN); } \ No newline at end of file diff --git a/enginecustom/applicationclass.h b/enginecustom/applicationclass.h index 5e37594..701446c 100644 --- a/enginecustom/applicationclass.h +++ b/enginecustom/applicationclass.h @@ -14,7 +14,6 @@ ///////////// // GLOBALS // ///////////// -const bool FULL_SCREEN = false; const bool VSYNC_ENABLED = true; const float SCREEN_DEPTH = 1000.0f; const float SCREEN_NEAR = 0.3f; @@ -30,6 +29,10 @@ public: bool Initialize(int, int, HWND); void Shutdown(); bool Frame(); + void SetFullscreen(bool fullscreen); + bool GetFullscreen() const; + int GetScreenWidth() const; + int GetScreenHeight() const; private: bool Render(); @@ -40,6 +43,7 @@ private: ModelClass* m_Model; ColorShaderClass* m_ColorShader; IDXGISwapChain* m_swapChain; + bool m_fullscreen = false; }; #endif \ No newline at end of file diff --git a/enginecustom/d3dclass.cpp b/enginecustom/d3dclass.cpp index e87bc65..69f861f 100644 --- a/enginecustom/d3dclass.cpp +++ b/enginecustom/d3dclass.cpp @@ -504,4 +504,139 @@ void D3DClass::ResetViewport() m_deviceContext->RSSetViewports(1, &m_viewport); return; +} + +void D3DClass::ReleaseResources() +{ + // libere la vue + if (m_renderTargetView) + { + m_renderTargetView->Release(); + m_renderTargetView = 0; + } + + // libere le buffer de profondeur + if (m_depthStencilBuffer) + { + m_depthStencilBuffer->Release(); + m_depthStencilBuffer = 0; + } + + // libere la vue de profondeur + if (m_depthStencilView) + { + m_depthStencilView->Release(); + m_depthStencilView = 0; + } +} + +bool D3DClass::RecreateResources() +{ + HRESULT result; + ID3D11Texture2D* backBufferPtr; + D3D11_TEXTURE2D_DESC depthBufferDesc; + D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc; + + // Recréez la vue de rendu. + result = m_swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&backBufferPtr); + if (FAILED(result)) + { + return false; + } + + result = m_device->CreateRenderTargetView(backBufferPtr, NULL, &m_renderTargetView); + if (FAILED(result)) + { + return false; + } + + backBufferPtr->Release(); + backBufferPtr = 0; + + // Recréez le tampon de profondeur et la vue de profondeur. + ZeroMemory(&depthBufferDesc, sizeof(depthBufferDesc)); + depthBufferDesc.Width = m_viewport.Width; + depthBufferDesc.Height = m_viewport.Height; + depthBufferDesc.MipLevels = 1; + depthBufferDesc.ArraySize = 1; + depthBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; + depthBufferDesc.SampleDesc.Count = 1; + depthBufferDesc.SampleDesc.Quality = 0; + depthBufferDesc.Usage = D3D11_USAGE_DEFAULT; + depthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL; + depthBufferDesc.CPUAccessFlags = 0; + depthBufferDesc.MiscFlags = 0; + + result = m_device->CreateTexture2D(&depthBufferDesc, NULL, &m_depthStencilBuffer); + if (FAILED(result)) + { + return false; + } + + ZeroMemory(&depthStencilViewDesc, sizeof(depthStencilViewDesc)); + depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; + depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D; + depthStencilViewDesc.Texture2D.MipSlice = 0; + + result = m_device->CreateDepthStencilView(m_depthStencilBuffer, &depthStencilViewDesc, &m_depthStencilView); + if (FAILED(result)) + { + return false; + } + + // Liez la vue de rendu et le tampon de profondeur à la pipeline de rendu de sortie. + m_deviceContext->OMSetRenderTargets(1, &m_renderTargetView, m_depthStencilView); + + return true; +} + + +bool D3DClass::SetFullscreen(bool fullscreen) +{ + HRESULT result; + + ReleaseResources(); + + // Definie les options de plein ecran + DXGI_MODE_DESC newScreenSize; + ZeroMemory(&newScreenSize, sizeof(newScreenSize)); + newScreenSize.Width = 1920; + newScreenSize.Height = 1080; + newScreenSize.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + + // Redimensionne la cible + result = m_swapChain->ResizeTarget(&newScreenSize); + if (FAILED(result)) + { + return false; + } + + // Change le mode d'affichage + result = m_swapChain->SetFullscreenState(fullscreen, NULL); + if (FAILED(result)) + { + return false; + } + + // Recréez les ressources + if (!RecreateResources()) + { + return false; + } + + // Informez ImGui du changement de taille de la fenêtre + ImGui::GetIO().DisplaySize = ImVec2((float)newScreenSize.Width, (float)newScreenSize.Height); + + // Libérez les anciens objets de rendu ImGui + ImGui_ImplDX11_InvalidateDeviceObjects(); + + // Recréez les objets de rendu ImGui + ImGui_ImplDX11_CreateDeviceObjects(); + + return true; +} + +IDXGISwapChain* D3DClass::GetSwapChain() +{ + return m_swapChain; } \ No newline at end of file diff --git a/enginecustom/d3dclass.h b/enginecustom/d3dclass.h index a2f4e31..240cdc7 100644 --- a/enginecustom/d3dclass.h +++ b/enginecustom/d3dclass.h @@ -18,6 +18,7 @@ ////////////// #include #include +#include "imguiManager.h" using namespace DirectX; @@ -40,6 +41,7 @@ public: ID3D11Device* GetDevice(); ID3D11DeviceContext* GetDeviceContext(); IDXGISwapChain* m_swapChain; + IDXGISwapChain* GetSwapChain(); void GetProjectionMatrix(XMMATRIX&); void GetWorldMatrix(XMMATRIX&); @@ -50,6 +52,10 @@ public: void SetBackBufferRenderTarget(); void ResetViewport(); + void ReleaseResources(); + bool SetFullscreen(bool fullscreen); + bool RecreateResources(); + private: bool m_vsync_enabled; int m_videoCardMemory; diff --git a/enginecustom/enginecustom.vcxproj b/enginecustom/enginecustom.vcxproj index 40f5725..a3264f1 100644 --- a/enginecustom/enginecustom.vcxproj +++ b/enginecustom/enginecustom.vcxproj @@ -24,6 +24,7 @@ + @@ -41,6 +42,7 @@ + diff --git a/enginecustom/imgui.ini b/enginecustom/imgui.ini index 49ade17..5325a49 100644 --- a/enginecustom/imgui.ini +++ b/enginecustom/imgui.ini @@ -3,6 +3,6 @@ Pos=60,60 Size=400,400 [Window][Khaotic Engine] -Pos=137,66 -Size=144,109 +Pos=59,59 +Size=694,353 diff --git a/enginecustom/imguiManager.cpp b/enginecustom/imguiManager.cpp new file mode 100644 index 0000000..95890cc --- /dev/null +++ b/enginecustom/imguiManager.cpp @@ -0,0 +1,48 @@ +#include "imguiManager.h" + +imguiManager::imguiManager() +{ + io = nullptr; +} + +imguiManager::~imguiManager() +{ +} + +bool imguiManager::Initialize(HWND hwnd, ID3D11Device* device, ID3D11DeviceContext* deviceContext) +{ + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + io = &ImGui::GetIO(); + + ImGui_ImplWin32_Init(hwnd); + ImGui_ImplDX11_Init(device, deviceContext); + ImGui::StyleColorsDark(); + + return true; +} + +void imguiManager::Shutdown() +{ + ImGui_ImplDX11_Shutdown(); + ImGui_ImplWin32_Shutdown(); + ImGui::DestroyContext(); +} + +void imguiManager::Render() +{ + ImGui::Render(); + ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData()); +} + +void imguiManager::NewFrame() +{ + ImGui_ImplDX11_NewFrame(); + ImGui_ImplWin32_NewFrame(); + ImGui::NewFrame(); +} + +void imguiManager::WidgetFullscreenBox(bool* fullscreen) +{ + ImGui::Checkbox("Fullscreen", fullscreen); +} \ No newline at end of file diff --git a/enginecustom/imguiManager.h b/enginecustom/imguiManager.h new file mode 100644 index 0000000..91362ed --- /dev/null +++ b/enginecustom/imguiManager.h @@ -0,0 +1,26 @@ +#pragma once +#ifndef _IMGUI_MANAGER_H_ +#define _IMGUI_MANAGER_H_ + +#include +#include +#include +#include + +class imguiManager +{ +public: + imguiManager(); + ~imguiManager(); + + bool Initialize(HWND hwnd, ID3D11Device* device, ID3D11DeviceContext* deviceContext); + void Shutdown(); + void Render(); + void NewFrame(); + void WidgetFullscreenBox(bool* fullscreen); + +private: + ImGuiIO* io; +}; + +#endif \ No newline at end of file diff --git a/enginecustom/systemclass.h b/enginecustom/systemclass.h index e50f4c3..04e9e85 100644 --- a/enginecustom/systemclass.h +++ b/enginecustom/systemclass.h @@ -2,12 +2,9 @@ #define _SYSTEMCLASS_H_ #define WIN32_LEAN_AND_MEAN -#include #include "inputclass.h" #include "applicationclass.h" -#include "imgui.h" -#include "imgui_impl_win32.h" -#include "imgui_impl_dx11.h" +#include "imguiManager.h" class SystemClass { @@ -19,6 +16,7 @@ public: bool Initialize(); void Shutdown(); void Run(); + void SetScreen(bool fullscreen); LRESULT CALLBACK MessageHandler(HWND, UINT, WPARAM, LPARAM); @@ -34,6 +32,7 @@ private: InputClass* m_Input; ApplicationClass* m_Application; + imguiManager* m_imguiManager; };