From 399e8f8c247f682964e2d0df52a05a5ef04c0564 Mon Sep 17 00:00:00 2001 From: Danilo Date: Mon, 1 Apr 2024 11:29:32 -0300 Subject: [PATCH] =?UTF-8?q?Implementa=20mudan=C3=A7as=20no=20dispositivo?= =?UTF-8?q?=20gr=C3=A1fico?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/common/rectangle.hpp | 4 +-- framework/game/gdeviceinfo.hpp | 2 ++ framework/platform/device-dx.cpp | 9 +++--- framework/platform/game-dx.cpp | 13 ++------- framework/platform/game-dx.hpp | 4 ++- framework/platform/gdeviceinfo-dx.hpp | 18 +++++++++--- framework/platform/gdevicemanager-dx.cpp | 35 +++++++++++++++++++++--- framework/platform/gdevicemanager-dx.hpp | 2 +- framework/platform/swapchain-dx.cpp | 4 +++ framework/xna.cpp | 14 ++++++++-- framework/xna.h | 1 + 11 files changed, 77 insertions(+), 29 deletions(-) diff --git a/framework/common/rectangle.hpp b/framework/common/rectangle.hpp index 42ed50b..c454bc8 100644 --- a/framework/common/rectangle.hpp +++ b/framework/common/rectangle.hpp @@ -12,8 +12,8 @@ namespace xna { constexpr Rectangle() = default; - constexpr Rectangle(const Int& Height, const Int& Width, const Int& X, const Int& Y) - : Height(Height), Width(Width), X(X), Y(Y) {} + constexpr Rectangle(const Int& X, const Int& Y, const Int& Width, const Int& Height): + X(X), Y(Y), Width(Width), Height(Height) {} }; } diff --git a/framework/game/gdeviceinfo.hpp b/framework/game/gdeviceinfo.hpp index e071fd9..88a7ae0 100644 --- a/framework/game/gdeviceinfo.hpp +++ b/framework/game/gdeviceinfo.hpp @@ -13,6 +13,8 @@ namespace xna { virtual void PresentationParameters(xna::PresentationParameters const& value) = 0; virtual xna::GraphicsProfile GraphicsProfile() const = 0; virtual void GraphicsProfile(xna::GraphicsProfile value) = 0; + virtual PGameWindow Window() const = 0; + virtual void Window(PGameWindow const& window) = 0; }; } diff --git a/framework/platform/device-dx.cpp b/framework/platform/device-dx.cpp index 7ba2bbb..ff3e82c 100644 --- a/framework/platform/device-dx.cpp +++ b/framework/platform/device-dx.cpp @@ -9,8 +9,7 @@ namespace xna { GraphicsDevice::GraphicsDevice() { _blendState = BlendState::NonPremultiplied(); - _adapter = GraphicsAdapter::DefaultAdapter(); - auto a = _adapter->DeviceId(); + _adapter = GraphicsAdapter::DefaultAdapter(); } GraphicsDevice::GraphicsDevice(GraphicsDeviceInformation const& info) { @@ -32,9 +31,11 @@ namespace xna { if (_context) { _context->Release(); _context = nullptr; - } + } + gameWindow.Size(_presentParameters.BackBufferWidth, _presentParameters.BackBufferHeight); const auto bounds = gameWindow.ClientBounds(); + _viewport = xna::Viewport(0.0F, 0.0F, static_cast(bounds.Width), static_cast(bounds.Height), @@ -103,7 +104,7 @@ namespace xna { bool GraphicsDevice::createDevice() { #if _DEBUG _createDeviceFlags |= D3D11_CREATE_DEVICE_FLAG::D3D11_CREATE_DEVICE_DEBUG; -#endif +#endif if FAILED( D3D11CreateDevice( diff --git a/framework/platform/game-dx.cpp b/framework/platform/game-dx.cpp index 65463fa..59c44bc 100644 --- a/framework/platform/game-dx.cpp +++ b/framework/platform/game-dx.cpp @@ -13,18 +13,11 @@ namespace xna { _gameWindow->Size( GraphicsDeviceManager::DefaultBackBufferWidth, GraphicsDeviceManager::DefaultBackBufferHeight, false); - - _graphicsDevice = New(); } - int Game::Run() { - if (!_gameWindow->Create()) { - MessageBox(nullptr, "Falha na criação da janela", "Xna Game Engine", MB_OK); - return EXIT_FAILURE; - } - - if (!_graphicsDevice->Initialize(*_gameWindow)) { - MessageBox(nullptr, "Falha na inicialização do dispositivo gráfico", "Xna Game Engine", MB_OK); + int Game::Run() { + if (_graphicsDevice == nullptr) { + MessageBox(nullptr, "O dispositivo gráfico não foi inicializar corretamente", "Xna Game Engine", MB_OK); return EXIT_FAILURE; } diff --git a/framework/platform/game-dx.hpp b/framework/platform/game-dx.hpp index 7a3c7dc..71e54ff 100644 --- a/framework/platform/game-dx.hpp +++ b/framework/platform/game-dx.hpp @@ -26,8 +26,10 @@ namespace xna { virtual void Initialize() override{} virtual void Update(GameTime const& gameTime) override{} - protected: + public: PGraphicsDevice _graphicsDevice{ nullptr }; + + protected: PGameWindow _gameWindow{ nullptr }; GameClock _clock{}; diff --git a/framework/platform/gdeviceinfo-dx.hpp b/framework/platform/gdeviceinfo-dx.hpp index dc4d81f..6018898 100644 --- a/framework/platform/gdeviceinfo-dx.hpp +++ b/framework/platform/gdeviceinfo-dx.hpp @@ -3,6 +3,7 @@ #include "../game/gdeviceinfo.hpp" #include "adapter-dx.hpp" +#include "window-dx.hpp" namespace xna { class GraphicsDeviceInformation : public IGraphicsDeviceInformation { @@ -15,26 +16,35 @@ namespace xna { _adapter = value; } - inline virtual xna::PresentationParameters PresentationParameters() const override{ + constexpr virtual xna::PresentationParameters PresentationParameters() const override{ return _parameters; }; - inline virtual void PresentationParameters(xna::PresentationParameters const& value) override{ + constexpr virtual void PresentationParameters(xna::PresentationParameters const& value) override{ _parameters = value; }; - inline virtual xna::GraphicsProfile GraphicsProfile() const override { + constexpr virtual xna::GraphicsProfile GraphicsProfile() const override { return _profile; }; - inline virtual void GraphicsProfile(xna::GraphicsProfile value) override { + constexpr virtual void GraphicsProfile(xna::GraphicsProfile value) override { _profile = value; }; + inline virtual PGameWindow Window() const override { + return _window; + } + + inline virtual void Window(PGameWindow const& window) override { + _window = window; + } + private: PGraphicsAdapter _adapter{ nullptr }; xna::GraphicsProfile _profile{xna::GraphicsProfile::Reach}; xna::PresentationParameters _parameters{}; + PGameWindow _window{ nullptr }; }; } diff --git a/framework/platform/gdevicemanager-dx.cpp b/framework/platform/gdevicemanager-dx.cpp index a32c982..9fa3019 100644 --- a/framework/platform/gdevicemanager-dx.cpp +++ b/framework/platform/gdevicemanager-dx.cpp @@ -5,19 +5,46 @@ #include "gdeviceinfo-dx.hpp" namespace xna { + GraphicsDeviceManager::GraphicsDeviceManager(Game* game) : _game(game){ + GraphicsDeviceInformation information; + + const auto adp = GraphicsAdapter::DefaultAdapter(); + information.Adapter(adp); + information.GraphicsProfile(xna::GraphicsProfile::HiDef); + + PresentationParameters parameters; + parameters.BackBufferWidth = _backBufferWidth; + parameters.BackBufferHeight = _backBufferHeight; + information.PresentationParameters(parameters); + + information.Window(game->Window()); + + CreateDevice(information); + } + void GraphicsDeviceManager::ApplyChanges() { + //_game->_graphicsDevice = _device; } void GraphicsDeviceManager::ToggleFullScreen() { } void GraphicsDeviceManager::CreateDevice(GraphicsDeviceInformation const& info) { - _device = New(); - + _device = New(info); auto window = _game->Window(); - window->Size(_backBufferWidth, _backBufferHeight); - _device->Initialize(*window); + if (!window->Create()) { + MessageBox(nullptr, "Falha na criação da janela", "Xna Game Engine", MB_OK); + return; + } + + //_device->Initialize(*window); + if (!_device->Initialize(*window)) { + MessageBox(nullptr, "Falha na inicialização do dispositivo gráfico", "Xna Game Engine", MB_OK); + return; + } + + _game->_graphicsDevice = _device; } void GraphicsDeviceManager::ChangeDevice() { diff --git a/framework/platform/gdevicemanager-dx.hpp b/framework/platform/gdevicemanager-dx.hpp index b25fa75..2ba6a7f 100644 --- a/framework/platform/gdevicemanager-dx.hpp +++ b/framework/platform/gdevicemanager-dx.hpp @@ -6,7 +6,7 @@ namespace xna { class GraphicsDeviceManager : public IGraphicsDeviceManager { public: - GraphicsDeviceManager(Game* game) : _game(game){} + GraphicsDeviceManager(Game* game); virtual void ApplyChanges() override; virtual void ToggleFullScreen() override; diff --git a/framework/platform/swapchain-dx.cpp b/framework/platform/swapchain-dx.cpp index 7fba654..463548b 100644 --- a/framework/platform/swapchain-dx.cpp +++ b/framework/platform/swapchain-dx.cpp @@ -39,7 +39,11 @@ namespace xna { auto dxdevice = _device->_device; if FAILED(dxFactory->CreateSwapChain(dxdevice, &_swapDescription, &_swapChain)) + { + dxFactory->Release(); + dxFactory = nullptr; return false; + } dxFactory->Release(); dxFactory = nullptr; diff --git a/framework/xna.cpp b/framework/xna.cpp index 6261fae..1f064cc 100644 --- a/framework/xna.cpp +++ b/framework/xna.cpp @@ -13,6 +13,11 @@ using namespace xna; //} class Game1 : public Game { +public: + Game1() { + manager = New(this); + } + virtual void Update(GameTime const& gameTime) { Game::Update(gameTime); @@ -23,13 +28,16 @@ class Game1 : public Game { Game::Draw(gameTime); } + +private: + PGraphicsDeviceManager manager; }; -int APIENTRY WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow) { - FileStream stream("D:/VS_EXPBSLN_x64_enu.CAB"); +int APIENTRY WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow) { + /*FileStream stream("D:/VS_EXPBSLN_x64_enu.CAB"); auto pos = stream.Position(); auto len = stream.Length(); - pos = stream.Position(); + pos = stream.Position();*/ Game1 game; game.Run(); diff --git a/framework/xna.h b/framework/xna.h index c21405b..577deec 100644 --- a/framework/xna.h +++ b/framework/xna.h @@ -11,5 +11,6 @@ #include "platform/device-dx.hpp" #include "platform/game-dx.hpp" #include "csharp/stream.hpp" +#include "platform/gdevicemanager-dx.hpp" // TODO: Reference additional headers your program requires here.