diff --git a/framework/platform/adapter-dx.cpp b/framework/platform/adapter-dx.cpp index d45e546..5887166 100644 --- a/framework/platform/adapter-dx.cpp +++ b/framework/platform/adapter-dx.cpp @@ -2,8 +2,8 @@ #include "graphics/displaymode.hpp" #include "platform-dx/dxheaders.hpp" #include "platform-dx/dxhelpers.hpp" -#include "platform-dx/gdevicemanager-dx.hpp" #include "platform-dx/implementations.hpp" +#include "game/gdevicemanager.hpp" namespace xna { static size_t getDisplayModesCount(IDXGIAdapter* adapter); diff --git a/framework/platform/device-dx.cpp b/framework/platform/device-dx.cpp index 133762a..861b2d8 100644 --- a/framework/platform/device-dx.cpp +++ b/framework/platform/device-dx.cpp @@ -3,9 +3,9 @@ #include "graphics/blendstate.hpp" #include "graphics/rendertarget.hpp" #include "platform-dx/device-dx.hpp" -#include "platform-dx/gdevicemanager-dx.hpp" #include "platform-dx/implementations.hpp" #include "game/gdeviceinfo.hpp" +#include "game/gdevicemanager.hpp" namespace xna { GraphicsDevice::GraphicsDevice() { diff --git a/framework/platform/game-dx.cpp b/framework/platform/game-dx.cpp index d90a3bf..efdf2df 100644 --- a/framework/platform/game-dx.cpp +++ b/framework/platform/game-dx.cpp @@ -2,8 +2,8 @@ #include "game/time.hpp" #include "platform-dx/audioengine-dx.hpp" #include "platform-dx/game-dx.hpp" -#include "platform-dx/gdevicemanager-dx.hpp" #include "platform-dx/implementations.hpp" +#include "game/gdevicemanager.hpp" namespace xna { Game::Game() { diff --git a/framework/platform/gdevicemanager-dx.cpp b/framework/platform/gdevicemanager-dx.cpp index 99c72ff..365313d 100644 --- a/framework/platform/gdevicemanager-dx.cpp +++ b/framework/platform/gdevicemanager-dx.cpp @@ -1,13 +1,14 @@ -#include "platform-dx/gdevicemanager-dx.hpp" -#include "platform-dx/device-dx.hpp" -#include "platform-dx/game-dx.hpp" +#include "game/gdevicemanager.hpp" #include "graphics/presentparams.hpp" #include "graphics/swapchain.hpp" +#include "platform-dx/device-dx.hpp" +#include "platform-dx/game-dx.hpp" #include "platform-dx/implementations.hpp" namespace xna { - GraphicsDeviceManager::GraphicsDeviceManager(Game*& game) : _game(game) { - sptr adp = GraphicsAdapter::DefaultAdapter(); + GraphicsDeviceManager::GraphicsDeviceManager(sptr const& game) : _game(game) + { + sptr adp = GraphicsAdapter::DefaultAdapter(); _information.Adapter = adp; _information.Profile = xna::GraphicsProfile::HiDef; @@ -18,9 +19,9 @@ namespace xna { parameters->Fullscreen = false; _information.Parameters = parameters; - if(_game) - _information.Window =_game->Window(); - } + if (_game) + _information.Window = _game->Window(); + } bool GraphicsDeviceManager::Initialize() { if (!_game) @@ -61,6 +62,44 @@ namespace xna { _backBufferHeight = value; _isDeviceDirty = true; } + + bool initWindow(GraphicsDeviceInformation& info, Game& game, int backWidth, int backHeight) + { + auto window = info.Window; + + if (!window) { + window = game.Window(); + info.Window = window; + } + + window->impl->Size(backWidth, backHeight); + + if (!window->impl->Create()) { + MessageBox(nullptr, "Falha na criação da janela", "XN65", MB_OK); + return false; + } + + info.Parameters->DeviceWindowHandle = reinterpret_cast(window->impl->WindowHandle()); + + return true; + } + + 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); + device = nullptr; + return false; + } + + game.graphicsDevice = device; + + return true; + } + bool GraphicsDeviceManager::CreateDevice() { if (_isDeviceDirty) { @@ -68,50 +107,15 @@ namespace xna { _information.Parameters->BackBufferHeight = _backBufferHeight; } - auto result = initWindow(); + auto result = initWindow(_information, *_game, _backBufferWidth, _backBufferHeight); if (!result) return false; - return initDevice(); + return initDevice(_information, *_game, _device); } void GraphicsDeviceManager::ChangeDevice() { } - bool GraphicsDeviceManager::initWindow() - { - auto window = _information.Window; - - if (!window) { - window = _game->Window(); - _information.Window = window; - } - - window->impl->Size(_backBufferWidth, _backBufferHeight); - - if (!window->impl->Create()) { - MessageBox(nullptr, "Falha na criação da janela", "XN65", MB_OK); - return false; - } - - _information.Parameters->DeviceWindowHandle = reinterpret_cast(window->impl->WindowHandle()); - - return true; - } - - bool GraphicsDeviceManager::initDevice() - { - auto window = _information.Window; - _device = New(_information); - - if (!_device->Initialize(*window)) { - MessageBox(window->impl->WindowHandle(), "Falha na inicialização do dispositivo gráfico", "XN65", MB_OK); - _device = nullptr; - return false; - } - - _game->graphicsDevice = _device; - - return true; - } + } \ No newline at end of file diff --git a/framework/xna.cpp b/framework/xna.cpp index 2ed6dda..672ad61 100644 --- a/framework/xna.cpp +++ b/framework/xna.cpp @@ -10,13 +10,12 @@ namespace xna { class Game1 : public Game { public: Game1() : Game() { - auto _game = reinterpret_cast(this); - graphics = New(_game); - Content()->RootDirectory("Content"); } void Initialize() override { + auto game = reinterpret_cast(this); + graphics = New(game->shared_from_this()); graphics->Initialize(); std::any device = graphicsDevice; @@ -64,7 +63,7 @@ namespace xna { int APIENTRY WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow) { xna::Platform::Init(); - auto game = xna::Game1(); - const auto result = game.Run(); + auto game = snew(); + const auto result = game->Run(); return result; } diff --git a/inc/game/gdevicemanager.hpp b/inc/game/gdevicemanager.hpp index 58ce364..4f0a1ff 100644 --- a/inc/game/gdevicemanager.hpp +++ b/inc/game/gdevicemanager.hpp @@ -2,21 +2,37 @@ #define XNA_GAME_GRAPHICSDEVICEMANAGER_HPP #include "../default.hpp" +#include "gdeviceinfo.hpp" namespace xna { - class IGraphicsDeviceManager { + class GraphicsDeviceManager { public: - virtual ~IGraphicsDeviceManager(){} - virtual void ApplyChanges() = 0; - virtual bool Initialize() = 0; - virtual bool ToggleFullScreen() = 0; - virtual Int PreferredBackBufferWidth() const = 0; - virtual Int PreferredBackBufferHeight() const = 0; - virtual void PreferredBackBufferWidth(Int value) = 0; - virtual void PreferredBackBufferHeight(Int value) = 0; + GraphicsDeviceManager(sptr const& game); + ~GraphicsDeviceManager() {} + 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 = 600; + protected: - virtual bool CreateDevice() = 0; - virtual void ChangeDevice() = 0; + bool CreateDevice(); + void ChangeDevice(); + + private: + sptr _game = nullptr; + Int _backBufferWidth{ DefaultBackBufferWidth }; + Int _backBufferHeight{ DefaultBackBufferHeight }; + bool _isDeviceDirty{ false }; + sptr _device = nullptr; + bool _isFullScreen{ false }; + GraphicsDeviceInformation _information{}; }; } diff --git a/inc/platform-dx/game-dx.hpp b/inc/platform-dx/game-dx.hpp index 66ffef0..adf3fff 100644 --- a/inc/platform-dx/game-dx.hpp +++ b/inc/platform-dx/game-dx.hpp @@ -8,7 +8,7 @@ #include "dxheaders.hpp" namespace xna { - class Game : public IGame { + class Game : public IGame, public std::enable_shared_from_this { public: Game(); diff --git a/inc/platform-dx/gdevicemanager-dx.hpp b/inc/platform-dx/gdevicemanager-dx.hpp deleted file mode 100644 index 1ba3e0e..0000000 --- a/inc/platform-dx/gdevicemanager-dx.hpp +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef XNA_PLATFORM_GDEVICEMANAGER_DX_HPP -#define XNA_PLATFORM_GDEVICEMANAGER_DX_HPP - -#include "../game/gdevicemanager.hpp" -#include "game/gdeviceinfo.hpp" - -namespace xna { - class GraphicsDeviceManager : public IGraphicsDeviceManager { - public: - GraphicsDeviceManager(Game*& game); - - virtual ~GraphicsDeviceManager() override{} - - virtual void ApplyChanges() override; - virtual bool Initialize() override; - virtual bool ToggleFullScreen() override; - - virtual constexpr Int PreferredBackBufferWidth() const { - return _backBufferWidth; - } - - virtual constexpr Int PreferredBackBufferHeight() const { - return _backBufferHeight; - } - - virtual void PreferredBackBufferWidth(Int value); - virtual void PreferredBackBufferHeight(Int value); - - protected: - virtual bool CreateDevice() override; - virtual void ChangeDevice() override; - - public: - static constexpr int DefaultBackBufferWidth = 800;//800; - static constexpr int DefaultBackBufferHeight = 600;// 480; - - private: - Game* _game; - Int _backBufferWidth{ DefaultBackBufferWidth }; - Int _backBufferHeight{ DefaultBackBufferHeight }; - bool _isDeviceDirty{ false }; - sptr _device = nullptr; - bool _isFullScreen{ false }; - GraphicsDeviceInformation _information{}; - - bool initWindow(); - bool initDevice(); - - }; -} - -#endif \ No newline at end of file diff --git a/inc/platform-dx/xna-dx.hpp b/inc/platform-dx/xna-dx.hpp index 357d37d..98381f9 100644 --- a/inc/platform-dx/xna-dx.hpp +++ b/inc/platform-dx/xna-dx.hpp @@ -4,7 +4,6 @@ #include "dx/StepTimer.hpp" #include "dxheaders.hpp" #include "game-dx.hpp" -#include "gdevicemanager-dx.hpp" #include "init-dx.hpp" #include "soundeffect-dx.hpp" #include "implementations.hpp" \ No newline at end of file