From da53a0a272b67f86610a92fc8fbcff1c196ecf52 Mon Sep 17 00:00:00 2001 From: Danilo Date: Mon, 6 May 2024 14:54:13 -0300 Subject: [PATCH] Limpeza em Game --- framework/content/manager.hpp | 9 ++- framework/game/game.hpp | 4 +- framework/platform/game-dx.cpp | 36 ++++-------- framework/platform/game-dx.hpp | 74 ++++++++---------------- framework/platform/gdevicemanager-dx.cpp | 6 +- framework/xna.cpp | 27 +++------ 6 files changed, 50 insertions(+), 106 deletions(-) diff --git a/framework/content/manager.hpp b/framework/content/manager.hpp index 49ce509..45ce5f1 100644 --- a/framework/content/manager.hpp +++ b/framework/content/manager.hpp @@ -15,8 +15,7 @@ namespace xna { friend class ContentReader; ContentManager(String const& rootDirectory, sptr const& services) : - _rootDirectory(rootDirectory), - _path(rootDirectory){ + _rootDirectory(rootDirectory){ _services = services; }; @@ -34,7 +33,6 @@ namespace xna { void RootDirectory(String const& value) { _rootDirectory = value; - _path = value; } virtual void Unload() { @@ -56,7 +54,9 @@ namespace xna { } auto obj2 = ReadAsset(assetName); - //auto obj3 = reinterpret_pointer_cast(obj2); + //auto voidAsset = reinterpret_pointer_cast(obj2); + _loadedAssets.insert({ assetName , obj2 }); + return obj2; } @@ -77,7 +77,6 @@ namespace xna { private: String _rootDirectory; - std::filesystem::path _path; std::map> _loadedAssets; inline const static String contentExtension = ".xnb"; std::vector byteBuffer; diff --git a/framework/game/game.hpp b/framework/game/game.hpp index d57154e..3656808 100644 --- a/framework/game/game.hpp +++ b/framework/game/game.hpp @@ -3,21 +3,19 @@ #include "../default.hpp" #include "time.hpp" -#include "window.hpp" #include "component.hpp" #include "servicecontainer.hpp" namespace xna { class IGame { public: - virtual ~IGame(){} - virtual void Exit() = 0; virtual int Run() = 0; virtual sptr Window() = 0; virtual sptr GetGraphicsDevice() = 0; virtual sptr Components() = 0; virtual sptr Services() = 0; + virtual sptr Content() = 0; protected: virtual void Draw(GameTime const& gameTime) = 0; diff --git a/framework/platform/game-dx.cpp b/framework/platform/game-dx.cpp index 5aff1c0..de5ff30 100644 --- a/framework/platform/game-dx.cpp +++ b/framework/platform/game-dx.cpp @@ -1,4 +1,4 @@ -#define NOMINMAX +#include "../csharp/type.hpp" #include "../game/time.hpp" #include "audioengine-dx.hpp" #include "device-dx.hpp" @@ -8,15 +8,14 @@ #include "keyboard-dx.hpp" #include "mouse-dx.hpp" #include "window-dx.hpp" -#include -#include "../csharp/type.hpp" namespace xna { Game::Game() { - _services = New(); + services = New(); + _contentManager = New("", services); _gameWindow = New(); - _gameWindow->Color(255, 155, 55); + _gameWindow->Color(146, 150, 154); _gameWindow->Title("XN65"); _gameWindow->Size( GraphicsDeviceManager::DefaultBackBufferWidth, @@ -25,15 +24,14 @@ namespace xna { _gameComponents = New(); } - void Game::Exit() - { + void Game::Exit() { _gameWindow->Close(); } int Game::Run() { Initialize(); - if (_graphicsDevice == nullptr) { + if (graphicsDevice == nullptr) { MessageBox(nullptr, "O dispositivo gráfico não foi inicializado corretamente", "XN65", MB_OK); return EXIT_FAILURE; } @@ -43,33 +41,19 @@ namespace xna { void Game::Initialize() { Keyboard::Initialize(); - Mouse::Initialize(); - - ////initialize é requisito para GamePad - //Microsoft::WRL::Wrappers::RoInitializeWrapper initialize(RO_INIT_MULTITHREADED); - - //if (FAILED(initialize)) - // MessageBox(nullptr, "Ocorreu um erro ao executar Microsoft::WRL::Wrappers::RoInitializeWrapper. O GamePad não foi inicializado corretamente.", "XN65", MB_OK); - - //GamePad.Initialize(); - - ////CoInitializeEx é requisito para biblioteca DirectXTK - //const auto hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED); - - //if (FAILED(hr)) - // MessageBox(nullptr, "Ocorreu um erro ao executar CoInitializeEx. O AudioEngine não foi inicializado corretamente.", "XN65", MB_OK); + Mouse::Initialize(); #if (_WIN32_WINNT >= 0x0A00 /*_WIN32_WINNT_WIN10*/) Microsoft::WRL::Wrappers::RoInitializeWrapper initialize(RO_INIT_MULTITHREADED); if (FAILED(initialize)) { - + MessageBox(nullptr, "Ocorreu um erro ao chamar Microsoft::WRL::Wrappers::RoInitializeWrapper.", "XN65", MB_OK); } #else HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED); if (FAILED(hr)) { - + MessageBox(nullptr, "Ocorreu um erro ao chamar CoInitializeEx.", "XN65", MB_OK); } #endif @@ -101,7 +85,7 @@ namespace xna { _drawableGameComponents.clear(); } - _graphicsDevice->Present(); + graphicsDevice->Present(); } void Game::Update(GameTime const& gameTime) { diff --git a/framework/platform/game-dx.hpp b/framework/platform/game-dx.hpp index 93549be..66ffef0 100644 --- a/framework/platform/game-dx.hpp +++ b/framework/platform/game-dx.hpp @@ -1,76 +1,52 @@ #ifndef XNA_PLATFORM_GAME_DX_HPP #define XNA_PLATFORM_GAME_DX_HPP +#include "../content/manager.hpp" #include "../default.hpp" #include "../game/game.hpp" -#include "dxheaders.hpp" #include "dx/StepTimer.hpp" -#include "../content/manager.hpp" +#include "dxheaders.hpp" namespace xna { class Game : public IGame { public: - Game(); + Game(); - virtual ~Game() override { - } - - virtual void Exit() override; - - virtual int Run() override; + void Exit() override; + int Run() override; - virtual sptr Window() override { - return _gameWindow; - } - - virtual sptr GetGraphicsDevice() override { - return _graphicsDevice; - } - - sptr Components() override { - return _gameComponents; - } - - sptr Services() override { - return _services; - } - - sptr Content() { - return contentManager; - } - - constexpr void EnableGameComponents(bool value) { - _enabledGameComponents = value; - } + inline sptr Window() override { return _gameWindow; } + inline sptr GetGraphicsDevice() override { return graphicsDevice; } + inline sptr Components() override { return _gameComponents; } + inline sptr Services() override { return services; } + inline sptr Content() override { return _contentManager; } + constexpr void EnableGameComponents(bool value) { _enabledGameComponents = value; } protected: - virtual void Draw(GameTime const& gameTime) override; - + virtual void Draw(GameTime const& gameTime) override; virtual void Initialize() override; - - virtual void LoadContent() override{} - + virtual void LoadContent() override{} virtual void Update(GameTime const& gameTime) override; public: - sptr _graphicsDevice{ nullptr }; + sptr graphicsDevice = nullptr; - protected: - sptr _gameWindow{ nullptr }; - sptr _audioEngine = nullptr; - - GameTime _currentGameTime{}; - DX::StepTimer _stepTimer; - sptr contentManager; - sptr _services = nullptr; + protected: + sptr services = nullptr; - private: - int startLoop(); - void step(); + private: sptr _gameComponents = nullptr; + sptr _gameWindow{ nullptr }; + sptr _audioEngine = nullptr; + sptr _contentManager; std::vector> _drawableGameComponents; size_t _drawableGameComponentsCount{ 0 }; bool _enabledGameComponents{ false }; + GameTime _currentGameTime{}; + DX::StepTimer _stepTimer{}; + + int startLoop(); + void step(); }; } diff --git a/framework/platform/gdevicemanager-dx.cpp b/framework/platform/gdevicemanager-dx.cpp index d656c69..8226f68 100644 --- a/framework/platform/gdevicemanager-dx.cpp +++ b/framework/platform/gdevicemanager-dx.cpp @@ -33,10 +33,10 @@ namespace xna { } bool GraphicsDeviceManager::ToggleFullScreen() { - if (!_game || !_game->_graphicsDevice || !_game->_graphicsDevice->_swapChain) + if (!_game || !_game->graphicsDevice || !_game->graphicsDevice->_swapChain) return false; - auto& swap = _game->_graphicsDevice->_swapChain; + auto& swap = _game->graphicsDevice->_swapChain; BOOL state = false; auto hr = swap->dxSwapChain->GetFullscreenState(&state, nullptr); @@ -110,7 +110,7 @@ namespace xna { return false; } - _game->_graphicsDevice = _device; + _game->graphicsDevice = _device; return true; } diff --git a/framework/xna.cpp b/framework/xna.cpp index 571e62d..8107cf6 100644 --- a/framework/xna.cpp +++ b/framework/xna.cpp @@ -12,25 +12,22 @@ namespace xna { Game1() : Game() { auto _game = reinterpret_cast(this); graphics = New(_game); - graphics->PreferredBackBufferWidth(1280); - graphics->PreferredBackBufferHeight(720); - contentManager = New("Content", _services); + + Content()->RootDirectory("Content"); } void Initialize() override { graphics->Initialize(); - std::any device = _graphicsDevice; - _services->AddService(*typeof(), device); + std::any device = graphicsDevice; + services->AddService(*typeof(), device); Game::Initialize(); } void LoadContent() override { - spriteBatch = New(*_graphicsDevice); + spriteBatch = New(*graphicsDevice); - XnaErrorCode err{0}; - texture = contentManager->Load("Idle"); Game::LoadContent(); } @@ -42,11 +39,9 @@ namespace xna { } void Draw(GameTime const& gameTime) override { - _graphicsDevice->Clear(Colors::CornflowerBlue); + graphicsDevice->Clear(Colors::CornflowerBlue); - spriteBatch->Begin(); - spriteBatch->Draw(*texture, position, Colors::White); - spriteBatch->End(); + Game::Draw(gameTime); } @@ -54,14 +49,6 @@ namespace xna { private: sptr graphics = nullptr; sptr spriteBatch = nullptr; - sptr texture = nullptr; //200x200 - Vector2 position{}; - std::vector points; - MouseState currentState{}; - MouseState oldState{}; - float vel = 1; - int var = 0; - //Texture2D tx; }; }