From 43353238798b6910fe0a2a7e185994fd6ea27539 Mon Sep 17 00:00:00 2001 From: Danilo Date: Wed, 5 Jun 2024 09:26:33 -0300 Subject: [PATCH] Corrige Initialize em GraphicsDevice --- framework/platform-dx/device.cpp | 13 +++++++------ framework/platform-dx/gdevicemanager.cpp | 7 +++---- inc/xna/graphics/device.hpp | 2 +- inc/xna/platform-dx/implementations.hpp | 18 ++++++++++++++++++ 4 files changed, 29 insertions(+), 11 deletions(-) diff --git a/framework/platform-dx/device.cpp b/framework/platform-dx/device.cpp index 3619c6d..18961cd 100644 --- a/framework/platform-dx/device.cpp +++ b/framework/platform-dx/device.cpp @@ -2,7 +2,7 @@ #include "xna/game/gdevicemanager.hpp" namespace xna { - void reset(GraphicsDevice::PlatformImplementation& impl) + static void reset(GraphicsDevice::PlatformImplementation& impl) { if (impl._device) { impl._device->Release(); @@ -24,7 +24,7 @@ namespace xna { impl._renderTarget2D = nullptr; } - bool createDevice(GraphicsDevice::PlatformImplementation& impl) { + static bool createDevice(GraphicsDevice::PlatformImplementation& impl) { auto createDeviceFlags = 0; #if _DEBUG createDeviceFlags = D3D11_CREATE_DEVICE_FLAG::D3D11_CREATE_DEVICE_DEBUG; @@ -75,6 +75,7 @@ namespace xna { impl = unew(); impl->_adapter = info.Adapter; + impl->_gameWindow = info.Window; impl->_presentationParameters = info.Parameters; impl->_adapter->CurrentDisplayMode( impl->_presentationParameters->BackBufferFormat, @@ -86,7 +87,7 @@ namespace xna { impl = nullptr; } - bool GraphicsDevice::Initialize(GameWindow& gameWindow) { + bool GraphicsDevice::Initialize() { if (!impl) impl = uptr(); @@ -101,14 +102,14 @@ namespace xna { if (FAILED(hr)) return false; - const auto bounds = gameWindow.ClientBounds(); + const auto bounds = impl->_gameWindow->ClientBounds(); impl->_viewport = xna::Viewport(0.0F, 0.0F, static_cast(bounds.Width), static_cast(bounds.Height), 0.0F, 1.F); - COLORREF color = gameWindow.impl->Color(); + COLORREF color = impl->_gameWindow->impl->Color(); impl->_backgroundColor[0] = GetRValue(color) / 255.0f; impl->_backgroundColor[1] = GetGValue(color) / 255.0f; impl->_backgroundColor[2] = GetBValue(color) / 255.0f; @@ -117,7 +118,7 @@ namespace xna { impl->_swapChain = New(_this); impl->_swapChain->Initialize(); - hr = impl->_factory->MakeWindowAssociation(gameWindow.impl->WindowHandle(), DXGI_MWA_NO_ALT_ENTER); + hr = impl->_factory->MakeWindowAssociation(impl->_gameWindow->impl->WindowHandle(), DXGI_MWA_NO_ALT_ENTER); if (FAILED(hr)) return false; impl->_renderTarget2D = New(_this); diff --git a/framework/platform-dx/gdevicemanager.cpp b/framework/platform-dx/gdevicemanager.cpp index 489fe75..4b89639 100644 --- a/framework/platform-dx/gdevicemanager.cpp +++ b/framework/platform-dx/gdevicemanager.cpp @@ -83,12 +83,11 @@ namespace xna { } bool initDevice(GraphicsDeviceInformation& info, Game& game, sptr& device) - { - auto& window = info.Window; + { device = New(info); - if (!device->Initialize(*window)) { - MessageBox(window->impl->WindowHandle(), "Falha na inicialização do dispositivo gráfico", "XN65", MB_OK); + if (!device->Initialize()) { + MessageBox(info.Window->impl->WindowHandle(), "Falha na inicialização do dispositivo gráfico", "XN65", MB_OK); device = nullptr; return false; } diff --git a/inc/xna/graphics/device.hpp b/inc/xna/graphics/device.hpp index 8642bc8..b3c7ea4 100644 --- a/inc/xna/graphics/device.hpp +++ b/inc/xna/graphics/device.hpp @@ -11,7 +11,7 @@ namespace xna { ~GraphicsDevice(); void Clear(); void Clear(Color const& color); - bool Initialize(GameWindow& gameWindow); + bool Initialize(); bool Present(); sptr Adapter() const; void Adapter(sptr const& adapter); diff --git a/inc/xna/platform-dx/implementations.hpp b/inc/xna/platform-dx/implementations.hpp index 2d1eefd..5228b4f 100644 --- a/inc/xna/platform-dx/implementations.hpp +++ b/inc/xna/platform-dx/implementations.hpp @@ -447,6 +447,23 @@ namespace xna { }; struct GraphicsDevice::PlatformImplementation { + ~PlatformImplementation() { + if (_device) { + _device->Release(); + _device = nullptr; + } + + if (_context) { + _context->Release(); + _device = nullptr; + } + + if (_factory) { + _factory->Release(); + _factory = nullptr; + } + } + ID3D11Device* _device{ nullptr }; ID3D11DeviceContext* _context{ nullptr }; IDXGIFactory1* _factory = nullptr; @@ -454,6 +471,7 @@ namespace xna { sptr _adapter{ nullptr }; sptr _renderTarget2D{ nullptr }; sptr _blendState{ nullptr }; + sptr _gameWindow = nullptr; xna::Viewport _viewport{}; sptr _presentationParameters; D3D_FEATURE_LEVEL _featureLevel{ D3D_FEATURE_LEVEL::D3D_FEATURE_LEVEL_11_0 };