From 47717f17551cb376762924b808a665fdcb1be93d Mon Sep 17 00:00:00 2001 From: CatChow0 Date: Mon, 25 Mar 2024 16:11:43 +0100 Subject: [PATCH] Maximize screen MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit pas finie, par fois ça crash au démarrage --- enginecustom/Systemclass.cpp | 37 +++++++++++++++++--- enginecustom/applicationclass.cpp | 1 - enginecustom/d3dclass.cpp | 56 +++++++++++++++++++++++++++++++ enginecustom/d3dclass.h | 1 + enginecustom/imgui.ini | 2 +- enginecustom/imguiManager.cpp | 5 +++ enginecustom/imguiManager.h | 1 + enginecustom/systemclass.h | 4 +++ 8 files changed, 100 insertions(+), 7 deletions(-) diff --git a/enginecustom/Systemclass.cpp b/enginecustom/Systemclass.cpp index a0b5a86..fa2c014 100644 --- a/enginecustom/Systemclass.cpp +++ b/enginecustom/Systemclass.cpp @@ -27,24 +27,35 @@ bool SystemClass::Initialize() // Initialize the width and height of the screen to zero before sending the variables into the function. 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; + + m_initialWindowWidth = 0; + m_initialWindowHeight = 0; + m_isDirect3DInitialized = false; // Initialize the windows api. InitializeWindows(screenWidth, screenHeight); // Create and initialize the input object. This object will be used to handle reading the keyboard input from the user. m_Input = new InputClass; - 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; + result = m_Application->Initialize(screenWidth, screenHeight, m_hwnd); if (!result) { return false; } + m_isDirect3DInitialized = true; + + // If we received a WM_SIZE message before Direct3D was initialized, resize the swap chain now + if (m_initialWindowWidth > 0 && m_initialWindowHeight > 0) + { + m_Application->GetDirect3D()->ResizeSwapChain(m_initialWindowWidth, m_initialWindowHeight); + } + // Initialize imgui m_imguiManager = new imguiManager; m_imguiManager->Initialize(m_hwnd, m_Application->GetDirect3D()->GetDevice(), m_Application->GetDirect3D()->GetDeviceContext()); @@ -188,6 +199,22 @@ LRESULT CALLBACK SystemClass::MessageHandler(HWND hwnd, UINT umsg, WPARAM wparam m_Input->KeyUp((unsigned int)wparam); return 0; } + case WM_SIZE: + { + int newWidth = LOWORD(lparam); + int newHeight = HIWORD(lparam); + + // If Direct3D is initialized, update the swap chain. Otherwise, store the window dimensions + if (m_isDirect3DInitialized && m_Application && m_Application->GetDirect3D()) + { + m_Application->GetDirect3D()->ResizeSwapChain(newWidth, newHeight); + } + else + { + m_initialWindowWidth = newWidth; + m_initialWindowHeight = newHeight; + } + } // Any other messages send to the default message handler as our application won't make use of them. default: @@ -264,7 +291,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_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX, posX, posY, screenWidth, screenHeight, NULL, NULL, m_hinstance, NULL); // Bring the window up on the screen and set it as main focus. diff --git a/enginecustom/applicationclass.cpp b/enginecustom/applicationclass.cpp index 86631e4..16c4e00 100644 --- a/enginecustom/applicationclass.cpp +++ b/enginecustom/applicationclass.cpp @@ -26,7 +26,6 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd) char textureFilename[128]; bool result; - // Create the Direct3D object. m_Direct3D = new D3DClass; if (!m_Direct3D) diff --git a/enginecustom/d3dclass.cpp b/enginecustom/d3dclass.cpp index 84b40ae..92d5fed 100644 --- a/enginecustom/d3dclass.cpp +++ b/enginecustom/d3dclass.cpp @@ -593,4 +593,60 @@ bool D3DClass::RecreateResources() IDXGISwapChain* D3DClass::GetSwapChain() { return m_swapChain; +} + +void D3DClass::ResizeSwapChain(int newWidth, int newHeight) +{ + HRESULT result; + + // Release existing DirectX resources + m_renderTargetView->Release(); + m_depthStencilBuffer->Release(); + + // Resize the swap chain + m_swapChain->ResizeBuffers(0, newWidth, newHeight, DXGI_FORMAT_UNKNOWN, 0); + if (FAILED(m_swapChain)) + { + MessageBox(NULL, L"Failed to resize swap chain buffers", L"Error", MB_OK); + return; + } + + // Recreate the render target view + ID3D11Texture2D* backBuffer; + m_swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&backBuffer); + m_device->CreateRenderTargetView(backBuffer, NULL, &m_renderTargetView); + backBuffer->Release(); + + // Recreate the depth/stencil buffer and view + D3D11_TEXTURE2D_DESC depthBufferDesc; + ZeroMemory(&depthBufferDesc, sizeof(depthBufferDesc)); + depthBufferDesc.Width = newWidth; + depthBufferDesc.Height = newHeight; + 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; + + D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc; + ZeroMemory(&depthStencilViewDesc, sizeof(depthStencilViewDesc)); + depthStencilViewDesc.Format = depthBufferDesc.Format; + depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D; + depthStencilViewDesc.Texture2D.MipSlice = 0; + + + // Other depthStencilDesc settings... + m_device->CreateTexture2D(&depthBufferDesc, NULL, &m_depthStencilBuffer); + m_device->CreateDepthStencilView(m_depthStencilBuffer, &depthStencilViewDesc, &m_depthStencilView); + + // Set the new render target and depth/stencil views for rendering + m_deviceContext->OMSetRenderTargets(1, &m_renderTargetView, m_depthStencilView); + // Update the viewport + m_viewport.Width = static_cast(newWidth); + m_viewport.Height = static_cast(newHeight); + m_deviceContext->RSSetViewports(1, &m_viewport); } \ No newline at end of file diff --git a/enginecustom/d3dclass.h b/enginecustom/d3dclass.h index 09a8044..47990cc 100644 --- a/enginecustom/d3dclass.h +++ b/enginecustom/d3dclass.h @@ -42,6 +42,7 @@ public: ID3D11DeviceContext* GetDeviceContext(); IDXGISwapChain* m_swapChain; IDXGISwapChain* GetSwapChain(); + void ResizeSwapChain(int, int); void GetProjectionMatrix(XMMATRIX&); void GetWorldMatrix(XMMATRIX&); diff --git a/enginecustom/imgui.ini b/enginecustom/imgui.ini index 7b107a3..e04071a 100644 --- a/enginecustom/imgui.ini +++ b/enginecustom/imgui.ini @@ -3,6 +3,6 @@ Pos=60,60 Size=400,400 [Window][Khaotic Engine] -Pos=31,27 +Pos=21,21 Size=694,367 diff --git a/enginecustom/imguiManager.cpp b/enginecustom/imguiManager.cpp index 248b70e..0ef131b 100644 --- a/enginecustom/imguiManager.cpp +++ b/enginecustom/imguiManager.cpp @@ -60,4 +60,9 @@ void imguiManager::WidgetButton() void imguiManager::WidgetFPS() { ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io->Framerate, io->Framerate); +} + +void imguiManager::WidgetFullscreen(bool* fullscreen) +{ + ImGui::Checkbox("Fullscreen", fullscreen); } \ No newline at end of file diff --git a/enginecustom/imguiManager.h b/enginecustom/imguiManager.h index 739b560..84ed906 100644 --- a/enginecustom/imguiManager.h +++ b/enginecustom/imguiManager.h @@ -22,6 +22,7 @@ public: void WidgetSpeedSlider(float* speed); void WidgetButton(); void WidgetFPS(); + void WidgetFullscreen(bool* fullscreen); private: ImGuiIO* io; diff --git a/enginecustom/systemclass.h b/enginecustom/systemclass.h index ef53ecb..b699b63 100644 --- a/enginecustom/systemclass.h +++ b/enginecustom/systemclass.h @@ -32,6 +32,10 @@ private: InputClass* m_Input; ApplicationClass* m_Application; imguiManager* m_imguiManager; + + int m_initialWindowWidth; + int m_initialWindowHeight; + bool m_isDirect3DInitialized; };