From 626bc01dad8b525fd8b7a0515bdd93413d85ded9 Mon Sep 17 00:00:00 2001 From: Danilo Date: Sun, 28 Jul 2024 17:03:13 -0300 Subject: [PATCH] =?UTF-8?q?Implementa=C3=A7=C3=B5es=20iniciais=20em=20Grap?= =?UTF-8?q?hicsDeviceManager?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/platform-dx/gdevicemanager.cpp | 72 ++++++----- inc/xna/game/gdevicemanager.hpp | 157 ++++++++++++++++++++--- inc/xna/game/window.hpp | 2 +- samples/01_blank/xna.cpp | 4 +- 4 files changed, 184 insertions(+), 51 deletions(-) diff --git a/framework/platform-dx/gdevicemanager.cpp b/framework/platform-dx/gdevicemanager.cpp index 0c0f447..d3e5cb3 100644 --- a/framework/platform-dx/gdevicemanager.cpp +++ b/framework/platform-dx/gdevicemanager.cpp @@ -1,41 +1,44 @@ -#include "xna/game/gdevicemanager.hpp" -#include "xna/graphics/presentparams.hpp" -#include "xna/graphics/swapchain.hpp" #include "xna/xna-dx.hpp" namespace xna { - GraphicsDeviceManager::GraphicsDeviceManager(sptr const& game) : _game(game) + GraphicsDeviceManager::GraphicsDeviceManager(sptr const& game) : game(game) { sptr adp = GraphicsAdapter::DefaultAdapter(); _information.Adapter = adp; _information.Profile = xna::GraphicsProfile::HiDef; auto parameters = snew(); - parameters->BackBufferWidth = _backBufferWidth; - parameters->BackBufferHeight = _backBufferHeight; + parameters->BackBufferWidth = backBufferWidth; + parameters->BackBufferHeight = backBufferHeight; parameters->BackBufferFormat = SurfaceFormat::Color; parameters->Fullscreen = false; _information.Parameters = parameters; - if (_game) - _information.Window = _game->Window(); + if (game) + _information.Window = game->Window(); } bool GraphicsDeviceManager::Initialize() { - if (!_game) + if (!game) return false; - return CreateDevice(); + CreateDevice(); + + return true; } void GraphicsDeviceManager::ApplyChanges() { + if (device && !isDeviceDirty) + return; + + ChangeDevice(false); } bool GraphicsDeviceManager::ToggleFullScreen() { - if (!_game || !_game->graphicsDevice || !_game->graphicsDevice->impl->_swapChain) + if (!game || !game->graphicsDevice || !game->graphicsDevice->impl->_swapChain) return false; - auto& swap = _game->graphicsDevice->impl->_swapChain; + auto& swap = game->graphicsDevice->impl->_swapChain; BOOL state = false; auto hr = swap->impl->dxSwapChain->GetFullscreenState(&state, nullptr); @@ -46,20 +49,10 @@ namespace xna { if (FAILED(hr)) return false; - _isFullScreen = !state; + isFullScreen = !state; return true; - } - - void GraphicsDeviceManager::PreferredBackBufferWidth(Int value) { - _backBufferWidth = value; - _isDeviceDirty = true; - } - - void GraphicsDeviceManager::PreferredBackBufferHeight(Int value) { - _backBufferHeight = value; - _isDeviceDirty = true; - } + } bool initWindow(GraphicsDeviceInformation& info, Game& game, int backWidth, int backHeight) { @@ -98,21 +91,36 @@ namespace xna { } - bool GraphicsDeviceManager::CreateDevice() { - if (_isDeviceDirty) { - _information.Parameters->BackBufferWidth = _backBufferWidth; - _information.Parameters->BackBufferHeight = _backBufferHeight; + void GraphicsDeviceManager::CreateDevice() { + if (isDeviceDirty) { + _information.Parameters->BackBufferWidth = backBufferWidth; + _information.Parameters->BackBufferHeight = backBufferHeight; } - auto result = initWindow(_information, *_game, _backBufferWidth, _backBufferHeight); + auto result = initWindow(_information, *game, backBufferWidth, backBufferHeight); - if (!result) return false; + //if (!result) return false; - return initDevice(_information, *_game, _device); + initDevice(_information, *game, device); } void GraphicsDeviceManager::ChangeDevice() { } - + void GraphicsDeviceManager::AddDevice(bool anySuitableDevice, std::vector>& foundDevices) { + const auto handle = game->Window()->Handle(); + + std::vector> adapters; + GraphicsAdapter::Adapters(adapters); + + for (size_t i = 0; adapters.size(); ++i) { + auto& adapter = adapters[i]; + + if (!anySuitableDevice) { + //TODO + //if (!this.IsWindowOnAdapter(handle, adapter)) + //continue; + } + } + } } \ No newline at end of file diff --git a/inc/xna/game/gdevicemanager.hpp b/inc/xna/game/gdevicemanager.hpp index 4f00060..ab0779b 100644 --- a/inc/xna/game/gdevicemanager.hpp +++ b/inc/xna/game/gdevicemanager.hpp @@ -3,36 +3,161 @@ #include "../default.hpp" #include "gdeviceinfo.hpp" +#include "../csharp/eventhandler.hpp" namespace xna { - class GraphicsDeviceManager { + struct IGraphicsDeviceService { + virtual sptr GetGraphicsDevice() = 0; + + EventHandler DeviceDisposing; + EventHandler DeviceReset; + EventHandler DeviceResetting; + EventHandler DeviceCreated; + }; + + class IGraphicsDeviceManager { + virtual void CreateDevice() = 0; + virtual bool BeginDraw() = 0; + virtual void EndDraw() = 0; + }; + + class GraphicsDeviceManager : public IGraphicsDeviceService, public IGraphicsDeviceManager { public: + //Creates a new GraphicsDeviceManager and registers it to handle the configuration and management of the graphics device for the specified Game. GraphicsDeviceManager(sptr const& game); - ~GraphicsDeviceManager() {} + + public: + //Specifies the default minimum back-buffer width. + static constexpr int DefaultBackBufferWidth = 800; + //Specifies the default minimum back-buffer height. + static constexpr int DefaultBackBufferHeight = 480; + + public: + //Gets the GraphicsDevice associated with the GraphicsDeviceManager. + sptr GetGraphicsDevice() override { + return device; + } + + //Gets or sets the graphics profile, which determines the graphics feature set. + constexpr GraphicsProfile PreferredGraphicsProfile() const { + return graphicsProfile; + } + + //Gets or sets the graphics profile, which determines the graphics feature set. + constexpr void PreferredGraphicsProfile(xna::GraphicsProfile value) { + graphicsProfile = value; + isDeviceDirty = true; + } + + //Gets or sets a value that indicates whether the device should start in full-screen mode. + constexpr bool IsFullScreen() const { + return isFullScreen; + } + + //Gets or sets a value that indicates whether the device should start in full-screen mode. + constexpr void IsFullScreen(bool value) { + isFullScreen = value; + isDeviceDirty = true; + } + + //Gets or sets the format of the back buffer. + constexpr SurfaceFormat PreferredBackBufferFormat() const { + return backBufferFormat; + } + + //Gets or sets the format of the back buffer. + constexpr void PreferredBackBufferFormat(SurfaceFormat value) { + backBufferFormat = value; + isDeviceDirty = true; + } + + //Gets or sets the preferred back-buffer height. + constexpr Int PreferredBackBufferHeight() const { + return backBufferHeight; + } + + //Gets or sets the preferred back-buffer height. + constexpr void PreferredBackBufferHeight(Int value) { + backBufferHeight = value; + isDeviceDirty = true; + } + + //Gets or sets the preferred back-buffer width. + constexpr Int PreferredBackBufferWidth() const { + return backBufferWidth; + } + + //Gets or sets the preferred back-buffer width. + constexpr void PreferredBackBufferWidth(Int value) { + backBufferWidth = value; + isDeviceDirty = true; + } + + //Gets or sets the format of the depth stencil. + constexpr DepthFormat PreferredDepthStencilFormat() const { + return depthStencilFormat; + } + + //Gets or sets the format of the depth stencil. + constexpr void PreferredDepthStencilFormat(DepthFormat value) { + depthStencilFormat = value; + isDeviceDirty = true; + } + + //Gets or sets the display orientations that are available if automatic rotation and scaling is enabled. + constexpr DisplayOrientation SupportedOrientations() const { + return supportedOrientations; + } + + //Gets or sets the display orientations that are available if automatic rotation and scaling is enabled. + constexpr void SupportedOrientations(DisplayOrientation value) { + supportedOrientations = value; + isDeviceDirty = true; + } + + //Gets or sets a value that indicates whether to sync to the vertical trace (vsync) when presenting the back buffer. + constexpr bool SynchronizeWithVerticalRetrace() const { + return synchronizeWithVerticalRetrace; + } + + // Gets or sets a value that indicates whether to sync to the vertical trace(vsync) when presenting the back buffer. + constexpr void SynchronizeWithVerticalRetrace(bool value) { + synchronizeWithVerticalRetrace = value; + isDeviceDirty = true; + } + + public: + //Applies any changes to device-related properties, changing the graphics device as necessary. void ApplyChanges(); bool Initialize(); bool ToggleFullScreen(); - Int PreferredBackBufferWidth() const; - Int PreferredBackBufferHeight() const; - void PreferredBackBufferWidth(Int value); - void PreferredBackBufferHeight(Int value); - public: - static constexpr int DefaultBackBufferWidth = 800; - static constexpr int DefaultBackBufferHeight = 480; + private: + void ChangeDevice(bool forceCreate){} + void AddDevice(bool anySuitableDevice, std::vector>& foundDevices); protected: - bool CreateDevice(); + void CreateDevice(); void ChangeDevice(); private: - sptr _game = nullptr; - Int _backBufferWidth{ DefaultBackBufferWidth }; - Int _backBufferHeight{ DefaultBackBufferHeight }; - bool _isDeviceDirty{ false }; - sptr _device = nullptr; - bool _isFullScreen{ false }; + bool BeginDraw() override { return false; } + void EndDraw() override{ } + + private: + sptr game = nullptr; + bool isDeviceDirty{ false }; + sptr device = nullptr; GraphicsDeviceInformation _information{}; + + bool isFullScreen{ false }; + Int backBufferWidth{ DefaultBackBufferWidth }; + Int backBufferHeight{ DefaultBackBufferHeight }; + GraphicsProfile graphicsProfile; + DepthFormat depthStencilFormat{ DepthFormat::Depth24 }; + SurfaceFormat backBufferFormat; + DisplayOrientation supportedOrientations; + bool synchronizeWithVerticalRetrace{ true }; }; } diff --git a/inc/xna/game/window.hpp b/inc/xna/game/window.hpp index 7dc99eb..55f0aad 100644 --- a/inc/xna/game/window.hpp +++ b/inc/xna/game/window.hpp @@ -12,7 +12,7 @@ namespace xna { String Title() const; void Title(String const& title); Rectangle ClientBounds() const; - intptr_t Handle() const; + intptr_t Handle() const; public: struct PlatformImplementation; diff --git a/samples/01_blank/xna.cpp b/samples/01_blank/xna.cpp index dc97e81..c57eaa3 100644 --- a/samples/01_blank/xna.cpp +++ b/samples/01_blank/xna.cpp @@ -16,7 +16,7 @@ namespace xna { void Initialize() override { auto game = reinterpret_cast(this); - graphics = snew(game->shared_from_this()); + graphics = snew(game->shared_from_this()); graphics->Initialize(); std::any device = graphicsDevice; @@ -45,7 +45,7 @@ namespace xna { private: sptr graphics = nullptr; sptr spriteBatch = nullptr; - sptr texture = nullptr; + sptr texture = nullptr; }; }