From cdff5cc0f8b9d88de42bbba0e9442836c4880466 Mon Sep 17 00:00:00 2001 From: Danilo Date: Mon, 22 Apr 2024 16:14:55 -0300 Subject: [PATCH] Implementa ToggleFullScreen --- framework/platform/adapter-dx.cpp | 12 ++- framework/platform/adapter-dx.hpp | 3 +- framework/platform/device-dx.hpp | 8 +- framework/platform/game-dx.cpp | 7 +- framework/platform/game-dx.hpp | 3 +- framework/platform/gdevicemanager-dx.cpp | 17 ++++- framework/platform/gdevicemanager-dx.hpp | 8 +- framework/xna.cpp | 94 ++++++++++++++---------- 8 files changed, 98 insertions(+), 54 deletions(-) diff --git a/framework/platform/adapter-dx.cpp b/framework/platform/adapter-dx.cpp index 5661b5d..8f6304f 100644 --- a/framework/platform/adapter-dx.cpp +++ b/framework/platform/adapter-dx.cpp @@ -17,7 +17,7 @@ namespace xna { adp->_index = 0; adp->_adapter = pAdapter; - return std::move(adp); + return adp; } pFactory->Release(); @@ -239,5 +239,15 @@ namespace xna { collection->_displayModes = displayList; return std::move(collection); + } + + bool GraphicsAdapter::GetOutput(UINT slot, IDXGIOutput*& output) { + if (!_adapter) return false; + + + if (_adapter->EnumOutputs(slot, &output) != DXGI_ERROR_NOT_FOUND) + return true; + + return false; } } \ No newline at end of file diff --git a/framework/platform/adapter-dx.hpp b/framework/platform/adapter-dx.hpp index d54c63f..5537b3d 100644 --- a/framework/platform/adapter-dx.hpp +++ b/framework/platform/adapter-dx.hpp @@ -27,7 +27,8 @@ namespace xna { virtual Uint SubSystemId() const override; virtual Uint VendorId() const override; virtual UDisplayModeCollection SupportedDisplayModes() const override; - virtual constexpr bool IsDefaultAdapter() const { return _index == 0; } + virtual constexpr bool IsDefaultAdapter() const { return _index == 0; } + bool GetOutput(UINT slot, IDXGIOutput*& output); public: IDXGIAdapter1* _adapter{ nullptr }; diff --git a/framework/platform/device-dx.hpp b/framework/platform/device-dx.hpp index 58e2eca..b20065b 100644 --- a/framework/platform/device-dx.hpp +++ b/framework/platform/device-dx.hpp @@ -75,16 +75,16 @@ namespace xna { ID3D11DeviceContext* _context{ nullptr }; IDXGIFactory1* _factory = nullptr; PSwapChain _swapChain{ nullptr }; + PGraphicsAdapter _adapter{ nullptr }; + PRenderTarget2D _renderTarget2D{ nullptr }; + PBlendState _blendState{ nullptr }; + xna::Viewport _viewport{}; private: 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; - PGraphicsAdapter _adapter{ nullptr }; - PRenderTarget2D _renderTarget2D{ nullptr }; - xna::Viewport _viewport{}; - PBlendState _blendState{ nullptr }; bool _usevsync{ true }; bool createDevice(); diff --git a/framework/platform/game-dx.cpp b/framework/platform/game-dx.cpp index 8adbcd1..c691bad 100644 --- a/framework/platform/game-dx.cpp +++ b/framework/platform/game-dx.cpp @@ -33,7 +33,12 @@ namespace xna { } - int Game::Run() { + void Game::Exit() + { + _gameWindow->Close(); + } + + int Game::Run() { Initialize(); if (_graphicsDevice == nullptr) { diff --git a/framework/platform/game-dx.hpp b/framework/platform/game-dx.hpp index c50d787..84c471d 100644 --- a/framework/platform/game-dx.hpp +++ b/framework/platform/game-dx.hpp @@ -15,7 +15,8 @@ namespace xna { virtual ~Game() override { } - virtual void Exit() override{} + virtual void Exit() override; + virtual int Run() override; virtual PGameWindow Window() override { diff --git a/framework/platform/gdevicemanager-dx.cpp b/framework/platform/gdevicemanager-dx.cpp index aefab78..ad92ab9 100644 --- a/framework/platform/gdevicemanager-dx.cpp +++ b/framework/platform/gdevicemanager-dx.cpp @@ -3,6 +3,7 @@ #include "game-dx.hpp" #include "window-dx.hpp" #include "gdeviceinfo-dx.hpp" +#include "adapter-dx.hpp" namespace xna { GraphicsDeviceManager::GraphicsDeviceManager(Game*& game) : _game(game) { @@ -29,23 +30,31 @@ namespace xna { } void GraphicsDeviceManager::ApplyChanges() { - //_game->_graphicsDevice = _device; } void GraphicsDeviceManager::ToggleFullScreen() { + if (!_game || !_game->_graphicsDevice || !_game->_graphicsDevice->_swapChain) + return; + + auto& swap = _game->_graphicsDevice->_swapChain; + + BOOL state = false; + swap->_swapChain->GetFullscreenState(&state, nullptr); + swap->_swapChain->SetFullscreenState(!state, nullptr); } void GraphicsDeviceManager::CreateDevice(GraphicsDeviceInformation const& info) { _device = New(info); - //auto window = _game->Window(); + _device->Adapter(info.Adapter()); auto window = info.Window(); + window->Size(_backBufferWidth, _backBufferHeight); + 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; diff --git a/framework/platform/gdevicemanager-dx.hpp b/framework/platform/gdevicemanager-dx.hpp index d4c4227..e514481 100644 --- a/framework/platform/gdevicemanager-dx.hpp +++ b/framework/platform/gdevicemanager-dx.hpp @@ -37,14 +37,14 @@ namespace xna { virtual void ChangeDevice() override; public: - static constexpr int DefaultBackBufferWidth = 1280;//800; - static constexpr int DefaultBackBufferHeight = 720;// 480; + static constexpr int DefaultBackBufferWidth = 800;//800; + static constexpr int DefaultBackBufferHeight = 480;// 480; private: - Game*& _game; + Game* _game; Int _backBufferWidth{ DefaultBackBufferWidth }; Int _backBufferHeight{ DefaultBackBufferHeight }; - bool _isDeviceDirty{ false }; + bool _isDeviceDirty{ false }; PGraphicsDevice _device; }; } diff --git a/framework/xna.cpp b/framework/xna.cpp index 1cccff0..b44e540 100644 --- a/framework/xna.cpp +++ b/framework/xna.cpp @@ -11,60 +11,78 @@ using namespace xna; // cout << "Hello CMake." << endl; // return 0; //} +namespace xna { + class Game1 : public Game { + public: + Game1() { + auto _game = reinterpret_cast(this); + graphics = New(_game); + graphics->PreferredBackBufferWidth(1280); + graphics->PreferredBackBufferHeight(720); + } -class Game1 : public Game { -public: - Game1() { - auto _game = reinterpret_cast(this); - graphics = New(_game); - } + void Initialize() override { + graphics->Initialize(); + Game::Initialize(); + } - void Initialize() override { - graphics->Initialize(); + void LoadContent() override { + spriteBatch = New(*_graphicsDevice); - Game::Initialize(); - } + XnaErrorCode err; + texture = Texture2D::FromStream(*_graphicsDevice, "D:\\sprite.jpg", &err); - void LoadContent() override { - spriteBatch = New(*_graphicsDevice); + auto audio = AudioEngine(); - XnaErrorCode err; - texture = Texture2D::FromStream(*_graphicsDevice, "D:\\sprite.jpg", &err); + Game::LoadContent(); + } - auto audio = AudioEngine(); + void Update(GameTime const& gameTime) override { + if (Keyboard::GetState().IsKeyDown(Keys::Escape) || GamePad::GetState(PlayerIndex::One).IsButtonDown(Buttons::Back)) + Exit(); - Game::LoadContent(); - } + oldState = currentState; + currentState = Mouse::GetState(); + const auto rec = Rectangle((graphics->PreferredBackBufferWidth() / 2) - 100, (graphics->PreferredBackBufferHeight() / 2) - 100, 200, 200); - void Update(GameTime const& gameTime) override { + if (currentState.LeftButton == ButtonState::Pressed && oldState.LeftButton == ButtonState::Released) { + graphics->ToggleFullScreen(); + } - Game::Update(gameTime); - } + if (currentState.RightButton == ButtonState::Pressed && oldState.RightButton == ButtonState::Released) { + position.X += 50; + } - void Draw(GameTime const& gameTime) override { - _graphicsDevice->Clear(Colors::CornflowerBlue); + Game::Update(gameTime); + } - spriteBatch->Begin(); - spriteBatch->Draw(*texture, position, nullptr, Colors::White, 0, { 0,0 }, 0.5F, SpriteEffects::None, 0); - spriteBatch->End(); + void Draw(GameTime const& gameTime) override { + _graphicsDevice->Clear(Colors::CornflowerBlue); - Game::Draw(gameTime); - } + spriteBatch->Begin(); + spriteBatch->Draw(*texture, position, Colors::White); + spriteBatch->End(); + + Game::Draw(gameTime); + } + + private: + PGraphicsDeviceManager graphics = nullptr; + PSpriteBatch spriteBatch = nullptr; + PTexture2D texture = nullptr; //200x200 + Vector2 position{}; + std::vector points; + MouseState currentState{}; + MouseState oldState{}; + float vel = 1; + int var = 0; + }; +} -private: - PGraphicsDeviceManager graphics = nullptr; - PSpriteBatch spriteBatch = nullptr; - PTexture2D texture = nullptr; - Vector2 position{}; - std::vector points; - MouseState currentState{}; - MouseState oldState{}; - float vel = 1; -}; int APIENTRY WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow) { - auto game = Game1(); + auto game = xna::Game1(); const auto result = game.Run(); return result; }