From 17365b2af38e8ae943b1a5a38a2600b77ecc585d Mon Sep 17 00:00:00 2001 From: Danilo Date: Sat, 30 Mar 2024 14:25:08 -0300 Subject: [PATCH] =?UTF-8?q?Implementa=C3=A7=C3=B5es=20de=20GraphicsDeviceM?= =?UTF-8?q?anager?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/CMakeLists.txt | 2 +- framework/enums.hpp | 25 ++++++++++++ framework/forward.hpp | 13 ++++-- framework/game/game.hpp | 3 +- framework/game/gdeviceinfo.hpp | 19 +++++++++ framework/game/gdevicemanager.hpp | 23 +++++++++++ framework/graphics/presentparams.hpp | 23 +++++++++++ framework/platform/device-dx.cpp | 11 +++++- framework/platform/device-dx.hpp | 3 ++ framework/platform/game-dx.cpp | 4 ++ framework/platform/game-dx.hpp | 4 ++ framework/platform/gdeviceinfo-dx.hpp | 41 +++++++++++++++++++ framework/platform/gdevicemanager-dx.cpp | 25 ++++++++++++ framework/platform/gdevicemanager-dx.hpp | 50 ++++++++++++++++++++++++ framework/platform/window-dx.cpp | 38 +++++++++++++++++- framework/platform/window-dx.hpp | 5 ++- 16 files changed, 281 insertions(+), 8 deletions(-) create mode 100644 framework/game/gdeviceinfo.hpp create mode 100644 framework/game/gdevicemanager.hpp create mode 100644 framework/graphics/presentparams.hpp create mode 100644 framework/platform/gdeviceinfo-dx.hpp create mode 100644 framework/platform/gdevicemanager-dx.cpp create mode 100644 framework/platform/gdevicemanager-dx.hpp diff --git a/framework/CMakeLists.txt b/framework/CMakeLists.txt index 8c10639..4a1c5d6 100644 --- a/framework/CMakeLists.txt +++ b/framework/CMakeLists.txt @@ -3,7 +3,7 @@ # # Add source to this project's executable. -add_executable (xna WIN32 "xna.cpp" "xna.h" "platform/window-dx.cpp" "platform/device-dx.cpp" "platform/adapter-dx.cpp" "platform/swapchain-dx.cpp" "platform/rendertarget-dx.cpp" "platform/texture-dx.cpp" "platform/blendstate-dx.cpp" "platform/game-dx.cpp" "platform/clock-dx.cpp" "csharp/stream.cpp") +add_executable (xna WIN32 "xna.cpp" "xna.h" "platform/window-dx.cpp" "platform/device-dx.cpp" "platform/adapter-dx.cpp" "platform/swapchain-dx.cpp" "platform/rendertarget-dx.cpp" "platform/texture-dx.cpp" "platform/blendstate-dx.cpp" "platform/game-dx.cpp" "platform/clock-dx.cpp" "csharp/stream.cpp" "platform/gdevicemanager-dx.cpp") if (CMAKE_VERSION VERSION_GREATER 3.12) set_property(TARGET xna PROPERTY CXX_STANDARD 20) diff --git a/framework/enums.hpp b/framework/enums.hpp index 451fe35..e7c22e3 100644 --- a/framework/enums.hpp +++ b/framework/enums.hpp @@ -40,6 +40,13 @@ namespace xna { using BlendOperation = BlendFunction; + enum class DepthFormat { + None, + Depth16, + Depth24, + Depth24Stencil8 + }; + enum class DisplayOrientation { Default = 0, LandscapeLeft = 1, @@ -47,6 +54,24 @@ namespace xna { Portrait = 4, }; + enum class GraphicsProfile { + Reach, + HiDef + }; + + enum class PresentInterval { + Default, + One, + Two, + Immediate + }; + + enum RenderTargetUsage { + DiscardContents, + PreserveContents, + PlatformContents + }; + enum class SeekOrigin { Begin, Current, diff --git a/framework/forward.hpp b/framework/forward.hpp index 6cc546d..597eb50 100644 --- a/framework/forward.hpp +++ b/framework/forward.hpp @@ -50,7 +50,6 @@ namespace xna { struct Vector4; using PVector4 = std::shared_ptr; - //Game class DrawableGameComponent; using PDrawableGameComponent = std::shared_ptr; @@ -63,15 +62,19 @@ namespace xna { class GameClock; using PGameClock = std::shared_ptr; class GameTime; - using PGameTime = std::shared_ptr; + using PGameTime = std::shared_ptr; class GameWindow; using PGameWindow = std::shared_ptr; + class GraphicsDeviceInformation; + using PGraphicsDeviceInformation = std::shared_ptr; + class GraphicsDeviceManager; + using PGraphicsDeviceManager = std::shared_ptr; class IDrawable; using PIDrawable = std::shared_ptr; class IGameComponent; using PIGameComponent = std::shared_ptr; class IUpdatable; - using PIUpdatable = std::shared_ptr; + using PIUpdatable = std::shared_ptr; //Graphics class BlendState; @@ -84,6 +87,10 @@ namespace xna { using PGraphicsAdapter = std::shared_ptr; class GraphicsDevice; using PGraphicsDevice = std::shared_ptr; + class GraphicsDeviceInformation; + using PGraphicsDeviceInformation = std::shared_ptr; + class PresentationParameters; + using PPresentationParameters = std::shared_ptr< PresentationParameters>; class RenderTarget2D; using PRenderTarget2D = std::shared_ptr; class SwapChain; diff --git a/framework/game/game.hpp b/framework/game/game.hpp index 37e7dd4..0278a33 100644 --- a/framework/game/game.hpp +++ b/framework/game/game.hpp @@ -14,11 +14,12 @@ namespace xna { virtual void Exit() = 0; virtual int Run() = 0; + virtual PGameWindow Window() = 0; protected: virtual void Draw(GameTime const& gameTime) = 0; virtual void Initialize() = 0; - virtual void Update(GameTime const& gameTime) = 0; + virtual void Update(GameTime const& gameTime) = 0; }; } diff --git a/framework/game/gdeviceinfo.hpp b/framework/game/gdeviceinfo.hpp new file mode 100644 index 0000000..e071fd9 --- /dev/null +++ b/framework/game/gdeviceinfo.hpp @@ -0,0 +1,19 @@ +#ifndef XNA_GAME_GDEVICEINFO_HPP +#define XNA_GAME_GDEVICEINFO_HPP + +#include "../forward.hpp" +#include "../enums.hpp" + +namespace xna { + class IGraphicsDeviceInformation { + public: + virtual PGraphicsAdapter Adapter() const = 0; + virtual void Adapter(PGraphicsAdapter const& value) = 0; + virtual xna::PresentationParameters PresentationParameters() const = 0; + virtual void PresentationParameters(xna::PresentationParameters const& value) = 0; + virtual xna::GraphicsProfile GraphicsProfile() const = 0; + virtual void GraphicsProfile(xna::GraphicsProfile value) = 0; + }; +} + +#endif \ No newline at end of file diff --git a/framework/game/gdevicemanager.hpp b/framework/game/gdevicemanager.hpp new file mode 100644 index 0000000..409002b --- /dev/null +++ b/framework/game/gdevicemanager.hpp @@ -0,0 +1,23 @@ +#ifndef XNA_GAME_GRAPHICSDEVICEMANAGER_HPP +#define XNA_GAME_GRAPHICSDEVICEMANAGER_HPP + +#include "../types.hpp" +#include "../csharp/timespan.hpp" +#include "../forward.hpp" + +namespace xna { + class IGraphicsDeviceManager { + public: + virtual void ApplyChanges() = 0; + virtual void ToggleFullScreen() = 0; + virtual Int PreferredBackBufferWidth() const = 0; + virtual Int PreferredBackBufferHeight() const = 0; + virtual void PreferredBackBufferWidth(Int value) = 0; + virtual void PreferredBackBufferHeight(Int value) = 0; + protected: + virtual void CreateDevice(GraphicsDeviceInformation const& info) = 0; + virtual void ChangeDevice() = 0; + }; +} + +#endif \ No newline at end of file diff --git a/framework/graphics/presentparams.hpp b/framework/graphics/presentparams.hpp new file mode 100644 index 0000000..580c59a --- /dev/null +++ b/framework/graphics/presentparams.hpp @@ -0,0 +1,23 @@ +#ifndef XNA_GRAPHICS_PRESENTPARAMS_HPP +#define XNA_GRAPHICS_PRESENTPARAMS_HPP + +#include "../types.hpp" +#include "../enums.hpp" + +namespace xna { + class PresentationParameters { + public: + Int BackBufferWidth{ 0 }; + Int BackBufferHeight{ 0 }; + SurfaceFormat BackBufferFormat{ SurfaceFormat::Color }; + DepthFormat DepthStencilFormat{ DepthFormat::None }; + Int MultiSampleCount{ 0 }; + xna::DisplayOrientation DisplayOrientation{ xna::DisplayOrientation::Default }; + PresentInterval PresentationInterval{ PresentInterval::Default }; + xna::RenderTargetUsage RenderTargetUsage{ xna::RenderTargetUsage::DiscardContents }; + intptr_t DeviceWindowHandle{ 0 }; + bool IsFullScreen{ false }; + }; +} + +#endif \ No newline at end of file diff --git a/framework/platform/device-dx.cpp b/framework/platform/device-dx.cpp index 4cc714e..7ba2bbb 100644 --- a/framework/platform/device-dx.cpp +++ b/framework/platform/device-dx.cpp @@ -4,6 +4,7 @@ #include "rendertarget-dx.hpp" #include "adapter-dx.hpp" #include "blendstate-dx.hpp" +#include "gdeviceinfo-dx.hpp" namespace xna { GraphicsDevice::GraphicsDevice() { @@ -12,7 +13,15 @@ namespace xna { auto a = _adapter->DeviceId(); } - bool GraphicsDevice::Initialize(GameWindow& gameWindow) { + GraphicsDevice::GraphicsDevice(GraphicsDeviceInformation const& info) { + _adapter = info.Adapter(); + _presentParameters = info.PresentationParameters(); + } + + bool GraphicsDevice::Initialize(GameWindow& gameWindow) { + if (_blendState == nullptr) + _blendState = BlendState::NonPremultiplied(); + _createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG; if (_device) { diff --git a/framework/platform/device-dx.hpp b/framework/platform/device-dx.hpp index 865a528..20ef80a 100644 --- a/framework/platform/device-dx.hpp +++ b/framework/platform/device-dx.hpp @@ -2,6 +2,7 @@ #define XNA_PLATFORM_DEVICE_DX_HPP #include "../graphics/device.hpp" +#include "../graphics/presentparams.hpp" #include "dxgi.h" #include "d3d11.h" @@ -9,6 +10,7 @@ namespace xna { class GraphicsDevice : public IGraphicsDevice { public: GraphicsDevice(); + GraphicsDevice(GraphicsDeviceInformation const& info); virtual ~GraphicsDevice() override { if (_device) { @@ -44,6 +46,7 @@ namespace xna { unsigned int _createDeviceFlags{ 0 }; D3D_FEATURE_LEVEL _featureLevel{ D3D_FEATURE_LEVEL::D3D_FEATURE_LEVEL_11_0 }; float _backgroundColor[4] = { 0, 0, 0, 0 }; + xna::PresentationParameters _presentParameters; bool createDevice(); }; diff --git a/framework/platform/game-dx.cpp b/framework/platform/game-dx.cpp index 65ce6c3..65463fa 100644 --- a/framework/platform/game-dx.cpp +++ b/framework/platform/game-dx.cpp @@ -3,12 +3,16 @@ #include "device-dx.hpp" #include "Windows.h" #include "../game/time.hpp" +#include "gdevicemanager-dx.hpp" namespace xna { Game::Game() { _gameWindow = New(); _gameWindow->Color(255, 155, 55); _gameWindow->Title("Teste de título"); + _gameWindow->Size( + GraphicsDeviceManager::DefaultBackBufferWidth, + GraphicsDeviceManager::DefaultBackBufferHeight, false); _graphicsDevice = New(); } diff --git a/framework/platform/game-dx.hpp b/framework/platform/game-dx.hpp index c5e7b05..7a3c7dc 100644 --- a/framework/platform/game-dx.hpp +++ b/framework/platform/game-dx.hpp @@ -16,6 +16,10 @@ namespace xna { virtual void Exit() override{} virtual int Run() override; + + virtual PGameWindow Window() override { + return _gameWindow; + } protected: virtual void Draw(GameTime const& gameTime) override{} diff --git a/framework/platform/gdeviceinfo-dx.hpp b/framework/platform/gdeviceinfo-dx.hpp new file mode 100644 index 0000000..dc4d81f --- /dev/null +++ b/framework/platform/gdeviceinfo-dx.hpp @@ -0,0 +1,41 @@ +#ifndef XNA_PLATFORM_GDEVICEINFOR_DX_HPP +#define XNA_PLATFORM_GDEVICEINFOR_DX_HPP + +#include "../game/gdeviceinfo.hpp" +#include "adapter-dx.hpp" + +namespace xna { + class GraphicsDeviceInformation : public IGraphicsDeviceInformation { + public: + inline virtual PGraphicsAdapter Adapter() const override { + return _adapter; + }; + + inline virtual void Adapter(PGraphicsAdapter const& value) override { + _adapter = value; + } + + inline virtual xna::PresentationParameters PresentationParameters() const override{ + return _parameters; + }; + + inline virtual void PresentationParameters(xna::PresentationParameters const& value) override{ + _parameters = value; + }; + + inline virtual xna::GraphicsProfile GraphicsProfile() const override { + return _profile; + }; + + inline virtual void GraphicsProfile(xna::GraphicsProfile value) override { + _profile = value; + }; + + private: + PGraphicsAdapter _adapter{ nullptr }; + xna::GraphicsProfile _profile{xna::GraphicsProfile::Reach}; + xna::PresentationParameters _parameters{}; + }; +} + +#endif \ No newline at end of file diff --git a/framework/platform/gdevicemanager-dx.cpp b/framework/platform/gdevicemanager-dx.cpp new file mode 100644 index 0000000..a32c982 --- /dev/null +++ b/framework/platform/gdevicemanager-dx.cpp @@ -0,0 +1,25 @@ +#include "gdevicemanager-dx.hpp" +#include "device-dx.hpp" +#include "game-dx.hpp" +#include "window-dx.hpp" +#include "gdeviceinfo-dx.hpp" + +namespace xna { + void GraphicsDeviceManager::ApplyChanges() { + } + + void GraphicsDeviceManager::ToggleFullScreen() { + } + + void GraphicsDeviceManager::CreateDevice(GraphicsDeviceInformation const& info) { + _device = New(); + + auto window = _game->Window(); + window->Size(_backBufferWidth, _backBufferHeight); + + _device->Initialize(*window); + } + + void GraphicsDeviceManager::ChangeDevice() { + } +} \ No newline at end of file diff --git a/framework/platform/gdevicemanager-dx.hpp b/framework/platform/gdevicemanager-dx.hpp new file mode 100644 index 0000000..b25fa75 --- /dev/null +++ b/framework/platform/gdevicemanager-dx.hpp @@ -0,0 +1,50 @@ +#ifndef XNA_PLATFORM_GDEVICEMANAGER_DX_HPP +#define XNA_PLATFORM_GDEVICEMANAGER_DX_HPP + +#include "../game/gdevicemanager.hpp" + +namespace xna { + class GraphicsDeviceManager : public IGraphicsDeviceManager { + public: + GraphicsDeviceManager(Game* game) : _game(game){} + + virtual void ApplyChanges() override; + virtual void ToggleFullScreen() override; + + virtual constexpr Int PreferredBackBufferWidth() const { + return _backBufferWidth; + } + + virtual constexpr Int PreferredBackBufferHeight() const { + return _backBufferHeight; + } + + virtual void PreferredBackBufferWidth(Int value) { + _backBufferWidth = value; + _isDeviceDirty = true; + } + + virtual void PreferredBackBufferHeight(Int value) { + _backBufferHeight = value; + _isDeviceDirty = true; + } + + protected: + virtual void CreateDevice(GraphicsDeviceInformation const& info) override; + virtual void ChangeDevice() override; + + public: + static constexpr int DefaultBackBufferWidth = 800; + static constexpr int DefaultBackBufferHeight = 480; + + private: + Game*& _game; + Int _backBufferWidth{ DefaultBackBufferWidth }; + Int _backBufferHeight{ DefaultBackBufferHeight }; + bool _isDeviceDirty{ false }; + PGraphicsDevice _device; + + }; +} + +#endif \ No newline at end of file diff --git a/framework/platform/window-dx.cpp b/framework/platform/window-dx.cpp index 4bdf7fc..815700d 100644 --- a/framework/platform/window-dx.cpp +++ b/framework/platform/window-dx.cpp @@ -11,11 +11,21 @@ namespace xna { } - void GameWindow::Size(int width, int height) { + void GameWindow::Position(int width, int height, bool update) { + _windowPosX = width; + _windowPosY = height; + setCenter(); + + if(update) Update(); + } + + void GameWindow::Size(int width, int height, bool update) { _windowWidth = width; _windowHeight = height; setPosition(); setCenter(); + + if(update) Update(); } void GameWindow::Title(String const& title) { @@ -77,6 +87,32 @@ namespace xna { return true; } + bool GameWindow::Update() { + if (_windowStyle == static_cast(GameWindowMode::Windowed)) { + RECT winRect = { 0, 0, _windowWidth, _windowHeight }; + + AdjustWindowRectEx(&winRect, + GetWindowStyle(_windowHandle), + GetMenu(_windowHandle) != NULL, + GetWindowExStyle(_windowHandle)); + + _windowPosX = GetSystemMetrics(SM_CXSCREEN) / 2 - (winRect.right - winRect.left) / 2; + _windowPosY = GetSystemMetrics(SM_CYSCREEN) / 2 - (winRect.bottom - winRect.top) / 2; + + MoveWindow( + _windowHandle, + _windowPosX, + _windowPosY, + winRect.right - winRect.left, + winRect.bottom - winRect.top, + TRUE); + + return _windowHandle ? true : false; + } + + return true; + } + String GameWindow::Title() const { return _windowTitle; } diff --git a/framework/platform/window-dx.hpp b/framework/platform/window-dx.hpp index 32c9891..ef04c73 100644 --- a/framework/platform/window-dx.hpp +++ b/framework/platform/window-dx.hpp @@ -25,7 +25,8 @@ namespace xna { return static_cast(_windowStyle); } - void Size(int width, int height); + void Position(int width, int height, bool update = true); + void Size(int width, int height, bool update = true); inline HINSTANCE HInstance() const { return _hInstance; @@ -88,6 +89,8 @@ namespace xna { } bool Create(); + bool Update(); + static LRESULT CALLBACK WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); virtual String Title() const override;