From 34d953326131764319fa8fba45e8f291fb5ea95d Mon Sep 17 00:00:00 2001 From: Danilo Date: Thu, 1 Aug 2024 16:10:05 -0300 Subject: [PATCH 01/10] =?UTF-8?q?Implementa=C3=A7=C3=B5es=20em=20Game?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/platform-dx/game.cpp | 61 ++++++++++++++++++--- inc/xna/game/game.hpp | 56 +++++++++++++++---- samples/02_PlatfformerStarterKit/game.cpp | 2 +- samples/02_PlatfformerStarterKit/player.cpp | 2 +- 4 files changed, 101 insertions(+), 20 deletions(-) diff --git a/framework/platform-dx/game.cpp b/framework/platform-dx/game.cpp index ec10b7d..da44a36 100644 --- a/framework/platform-dx/game.cpp +++ b/framework/platform-dx/game.cpp @@ -5,8 +5,8 @@ namespace xna { impl = unew(); services = snew(); auto iservice = reinterpret_pointer_cast(services); - _contentManager = snew(services, ""); - _contentManager->mainGameService = iservice; + contentManager = snew(services, ""); + contentManager->mainGameService = iservice; _gameWindow = snew(); _gameWindow->impl->Color(146, 150, 154); @@ -16,6 +16,9 @@ namespace xna { GraphicsDeviceManager::DefaultBackBufferHeight, false); _gameComponents = snew(); + + IsFixedTimeStep(isFixedTimeStep); + TargetElapsedTime(targetElapsedTime); } void Game::Exit() { @@ -34,7 +37,7 @@ namespace xna { DispatchMessage(&msg); } else { - Step(); + Tick(); } } while (msg.message != WM_QUIT); @@ -42,7 +45,7 @@ namespace xna { return static_cast(msg.wParam); } - void Game::Step() + void Game::Tick() { impl->_stepTimer.Tick([&]() { @@ -59,6 +62,9 @@ namespace xna { } int Game::Run() { + if (isRunning) + return EXIT_FAILURE; + try { if (!_gameWindow->impl->Create()) { Exception::Throw(Exception::FAILED_TO_CREATE); @@ -72,6 +78,7 @@ namespace xna { return EXIT_FAILURE; } + isRunning = true; return StartGameLoop(); } catch (std::exception& e) { @@ -138,10 +145,10 @@ namespace xna { } sptr Game::Window() { return _gameWindow; } - sptr Game::GetGraphicsDevice() { return graphicsDevice; } - sptr Game::Components() { return _gameComponents; } + sptr Game::Device() const { return graphicsDevice; } + sptr Game::Components() const { return _gameComponents; } sptr Game::Services() { return services; } - sptr Game::Content() { return _contentManager; } + sptr Game::Content() const { return contentManager; } void Game::EnableGameComponents(bool value) { _enabledGameComponents = value; } void Game::AttachGraphicsDevice(sptr const& device) { @@ -158,4 +165,44 @@ namespace xna { _gameWindow->impl->Update(); } } + + void Game::Content(sptr const& value) { + contentManager = value; + auto iservice = reinterpret_pointer_cast(services); + contentManager->mainGameService = iservice; + } + + void Game::IsFixedTimeStep(bool value) { + isFixedTimeStep = value; + impl->_stepTimer.SetFixedTimeStep(value); + } + + bool Game::IsMouseVisible() const { + if (!Mouse::impl) + return false; + + return Mouse::impl->_dxMouse->IsVisible(); + } + + void Game::IsMouseVisible(bool value) { + if (!Mouse::impl) + return; + + Mouse::impl->_dxMouse->SetVisible(value); + } + void Game::TargetElapsedTime(TimeSpan const& value) { + if (!isFixedTimeStep) + return; + + const auto ticks = targetElapsedTime.Ticks(); + impl->_stepTimer.SetTargetElapsedTicks(ticks); + } + + void Game::ResetElapsedTime() const { + impl->_stepTimer.ResetElapsedTime(); + } + + void Game::RunOneFrame() { + Tick(); + } } diff --git a/inc/xna/game/game.hpp b/inc/xna/game/game.hpp index 6f67ee5..1749ddd 100644 --- a/inc/xna/game/game.hpp +++ b/inc/xna/game/game.hpp @@ -5,18 +5,50 @@ #include "time.hpp" namespace xna { + //Provides basic graphics device initialization, game logic, and rendering code. class Game : public std::enable_shared_from_this { public: Game(); - void Exit(); - int Run(); - sptr Window(); - sptr GetGraphicsDevice(); - sptr Components(); - sptr Services(); - sptr Content(); - void EnableGameComponents(bool value); + //Gets the collection of GameComponents owned by the game. + sptr Components() const; + //Gets or sets the current ContentManager. + sptr Content() const; + //Gets or sets the current ContentManager. + void Content(sptr const& value); + //Gets the current GraphicsDevice. + sptr Device() const; + //Gets or sets a value indicating whether to use fixed time steps. + //The default value for IsFixedTimeStep is true. + constexpr bool IsFixedTimeStep() const { return isFixedTimeStep; } + //Gets or sets a value indicating whether to use fixed time steps. + //The default value for IsFixedTimeStep is true. + void IsFixedTimeStep(bool value); + //Gets or sets a value indicating whether the mouse cursor should be visible. + bool IsMouseVisible() const; + //Gets or sets a value indicating whether the mouse cursor should be visible. + void IsMouseVisible(bool value); + //Gets the GameServiceContainer holding all the service providers attached to the Game. + sptr Services(); + //Gets or sets the target time between calls to Update when IsFixedTimeStep is true. + constexpr TimeSpan TargetElapsedTime() const { return targetElapsedTime; } + //Gets or sets the target time between calls to Update when IsFixedTimeStep is true. + void TargetElapsedTime(TimeSpan const& value); + //Gets the underlying operating system window. + sptr Window(); + + //Exits the game. + void Exit(); + //Resets the elapsed time counter. + void ResetElapsedTime() const; + //Call this method to initialize the game, begin running the game loop, and start processing events for the game. + int Run(); + //Run the game through what would happen in a single tick of the game clock; this method is designed for debugging only. + void RunOneFrame(); + //Updates the game's clock and calls Update and Draw. + void Tick(); + + void EnableGameComponents(bool value); void AttachGraphicsDevice(sptr const& graphicsDevice); void ResizeWindow(int width, int heigth); @@ -25,8 +57,7 @@ namespace xna { virtual void Initialize(); virtual void LoadContent(){} virtual void Update(GameTime const& gameTime); - int StartGameLoop(); - void Step(); + int StartGameLoop(); public: sptr graphicsDevice = nullptr; @@ -38,11 +69,14 @@ namespace xna { sptr _gameComponents = nullptr; sptr _gameWindow{ nullptr }; sptr _audioEngine = nullptr; - sptr _contentManager; + sptr contentManager; std::vector> _drawableGameComponents; size_t _drawableGameComponentsCount{ 0 }; bool _enabledGameComponents{ false }; GameTime _currentGameTime{}; + bool isFixedTimeStep{ true }; + TimeSpan targetElapsedTime{ TimeSpan::FromTicks(166667L) }; + bool isRunning{ false }; public: struct PlatformImplementation; diff --git a/samples/02_PlatfformerStarterKit/game.cpp b/samples/02_PlatfformerStarterKit/game.cpp index cd3d5fd..fc96f09 100644 --- a/samples/02_PlatfformerStarterKit/game.cpp +++ b/samples/02_PlatfformerStarterKit/game.cpp @@ -73,7 +73,7 @@ namespace PlatformerStarterKit { auto keyboardState = Keyboard::GetState(); auto gamepadState = GamePad::GetState(PlayerIndex::One); - if (gamepadState.Buttons.Back == ButtonState::Pressed) + if (gamepadState.Buttons().Back() == ButtonState::Pressed) Exit(); bool continuePressed = diff --git a/samples/02_PlatfformerStarterKit/player.cpp b/samples/02_PlatfformerStarterKit/player.cpp index 9d98c5b..e2b7961 100644 --- a/samples/02_PlatfformerStarterKit/player.cpp +++ b/samples/02_PlatfformerStarterKit/player.cpp @@ -92,7 +92,7 @@ namespace PlatformerStarterKit { auto gamePadState = xna::GamePad::GetState(xna::PlayerIndex::One); auto keyboardState = xna::Keyboard::GetState(); - movement = gamePadState.ThumbSticks.Left().X * MoveStickScale; + movement = gamePadState.ThumbSticks().Left().X * MoveStickScale; if (std::abs(movement) < 0.5f) movement = 0.0f; From 2135b5859b0151bac47a87f87056ddcfac8da656 Mon Sep 17 00:00:00 2001 From: Danilo Date: Thu, 1 Aug 2024 16:33:06 -0300 Subject: [PATCH 02/10] =?UTF-8?q?Implementa=C3=A7=C3=B5es=20em=20Game?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/platform-dx/game.cpp | 6 ++++- inc/xna/game/game.hpp | 28 ++++++++++++++++------- samples/01_blank/xna.cpp | 8 +++---- samples/02_PlatfformerStarterKit/game.cpp | 12 +++++----- samples/CMakeLists.txt | 2 +- 5 files changed, 36 insertions(+), 20 deletions(-) diff --git a/framework/platform-dx/game.cpp b/framework/platform-dx/game.cpp index da44a36..e673ef6 100644 --- a/framework/platform-dx/game.cpp +++ b/framework/platform-dx/game.cpp @@ -28,7 +28,7 @@ namespace xna { int Game::StartGameLoop() { MSG msg{}; - impl->_stepTimer = xna::StepTimer(); + impl->_stepTimer = xna::StepTimer(); do { if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) @@ -42,6 +42,7 @@ namespace xna { } while (msg.message != WM_QUIT); + EndRun(); return static_cast(msg.wParam); } @@ -58,7 +59,9 @@ namespace xna { Update(_currentGameTime); }); + BeginDraw(); Draw(_currentGameTime); + EndDraw(); } int Game::Run() { @@ -79,6 +82,7 @@ namespace xna { } isRunning = true; + BeginRun(); return StartGameLoop(); } catch (std::exception& e) { diff --git a/inc/xna/game/game.hpp b/inc/xna/game/game.hpp index 1749ddd..2ac9070 100644 --- a/inc/xna/game/game.hpp +++ b/inc/xna/game/game.hpp @@ -53,19 +53,29 @@ namespace xna { void ResizeWindow(int width, int heigth); protected: + //Starts the drawing of a frame. This method is followed by calls to Draw and EndDraw. + virtual void BeginDraw(){} + //Called after all components are initialized but before the first update in the game loop. + virtual void BeginRun() {}; + //Called when the game determines it is time to draw a frame. virtual void Draw(GameTime const& gameTime); + //Ends the drawing of a frame. This method is preceeded by calls to Draw and BeginDraw. + virtual void EndDraw() {} + //Called after the game loop has stopped running before exiting. + virtual void EndRun() {}; + //Called after the Game and GraphicsDevice are created, but before LoadContent. virtual void Initialize(); + //Called when graphics resources need to be loaded. virtual void LoadContent(){} - virtual void Update(GameTime const& gameTime); - int StartGameLoop(); - - public: - sptr graphicsDevice = nullptr; - - protected: - sptr services = nullptr; + //Called when the game has determined that game logic needs to be processed. + virtual void Update(GameTime const& gameTime); private: + int StartGameLoop(); + + private: + friend class GraphicsDeviceManager; + sptr _gameComponents = nullptr; sptr _gameWindow{ nullptr }; sptr _audioEngine = nullptr; @@ -77,6 +87,8 @@ namespace xna { bool isFixedTimeStep{ true }; TimeSpan targetElapsedTime{ TimeSpan::FromTicks(166667L) }; bool isRunning{ false }; + sptr services = nullptr; + sptr graphicsDevice = nullptr; public: struct PlatformImplementation; diff --git a/samples/01_blank/xna.cpp b/samples/01_blank/xna.cpp index ae50f89..cc68a70 100644 --- a/samples/01_blank/xna.cpp +++ b/samples/01_blank/xna.cpp @@ -20,14 +20,14 @@ namespace xna { //graphics->Initialize(); graphics->ApplyChanges(); - std::any device = graphicsDevice; - services->AddService(*typeof(), device); + std::any device = Device(); + Services()->AddService(*typeof(), device); Game::Initialize(); } void LoadContent() override { - spriteBatch = snew(graphicsDevice); + spriteBatch = snew(Device()); Game::LoadContent(); } @@ -39,7 +39,7 @@ namespace xna { } void Draw(GameTime const& gameTime) override { - graphicsDevice->Clear(Colors::CornflowerBlue); + Device()->Clear(Colors::CornflowerBlue); Game::Draw(gameTime); } diff --git a/samples/02_PlatfformerStarterKit/game.cpp b/samples/02_PlatfformerStarterKit/game.cpp index fc96f09..1d51833 100644 --- a/samples/02_PlatfformerStarterKit/game.cpp +++ b/samples/02_PlatfformerStarterKit/game.cpp @@ -24,14 +24,14 @@ namespace PlatformerStarterKit { graphics = snew(game->shared_from_this()); graphics->ApplyChanges(); - std::any device = graphicsDevice; - services->AddService(*typeof(), device); + std::any device = Device(); + Services()->AddService(*typeof(), device); Game::Initialize(); } void LoadContent() override { - spriteBatch = snew(graphicsDevice); + spriteBatch = snew(Device()); // Load fonts hudFont = Content()->Load("Fonts/Hud"); @@ -55,7 +55,7 @@ namespace PlatformerStarterKit { } void Draw(GameTime const& gameTime) override { - graphicsDevice->Clear(Colors::CornflowerBlue); + Device()->Clear(Colors::CornflowerBlue); spriteBatch->Begin(); @@ -111,7 +111,7 @@ namespace PlatformerStarterKit { levelIndex = -1; } - level = snew(services, levelPath); + level = snew(Services(), levelPath); level->Initialize(); } @@ -122,7 +122,7 @@ namespace PlatformerStarterKit { void DrawHud() { - auto titleSafeArea = graphicsDevice->Viewport().Bounds(); + auto titleSafeArea = Device()->Viewport().Bounds(); auto hudLocation = Vector2(titleSafeArea.X, titleSafeArea.Y); auto center = Vector2(titleSafeArea.X + titleSafeArea.Width / 2.0f, titleSafeArea.Y + titleSafeArea.Height / 2.0f); diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index 24ef843..45257e9 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -4,4 +4,4 @@ # Add source to this project's executable. add_subdirectory ("01_blank") -#add_subdirectory ("02_PlatfformerStarterKit") +add_subdirectory ("02_PlatfformerStarterKit") From 7b7fcddc4e77392308eae3b86b835dfd053903b9 Mon Sep 17 00:00:00 2001 From: Danilo Date: Thu, 1 Aug 2024 16:41:50 -0300 Subject: [PATCH 03/10] =?UTF-8?q?Corre=C3=A7=C3=B5es=20de=20nome=20de=20va?= =?UTF-8?q?ri=C3=A1veis=20em=20Game?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/platform-dx/game.cpp | 60 +++++++++++++++++----------------- inc/xna/game/game.hpp | 14 ++++---- samples/CMakeLists.txt | 2 +- 3 files changed, 38 insertions(+), 38 deletions(-) diff --git a/framework/platform-dx/game.cpp b/framework/platform-dx/game.cpp index e673ef6..a52595a 100644 --- a/framework/platform-dx/game.cpp +++ b/framework/platform-dx/game.cpp @@ -8,21 +8,21 @@ namespace xna { contentManager = snew(services, ""); contentManager->mainGameService = iservice; - _gameWindow = snew(); - _gameWindow->impl->Color(146, 150, 154); - _gameWindow->Title("XN65"); - _gameWindow->impl->Size( + gameWindow = snew(); + gameWindow->impl->Color(146, 150, 154); + gameWindow->Title("XN65"); + gameWindow->impl->Size( GraphicsDeviceManager::DefaultBackBufferWidth, GraphicsDeviceManager::DefaultBackBufferHeight, false); - _gameComponents = snew(); + gameComponents = snew(); IsFixedTimeStep(isFixedTimeStep); TargetElapsedTime(targetElapsedTime); } void Game::Exit() { - _gameWindow->impl->Close(); + gameWindow->impl->Close(); } int Game::StartGameLoop() { @@ -54,13 +54,13 @@ namespace xna { const auto total = impl->_stepTimer.GetTotalSeconds(); const auto elapsedTimeSpan = TimeSpan::FromSeconds(elapsed); const auto totalTimeSpan = TimeSpan::FromSeconds(total); - _currentGameTime.ElapsedGameTime = elapsedTimeSpan; - _currentGameTime.TotalGameTime = totalTimeSpan; - Update(_currentGameTime); + currentGameTime.ElapsedGameTime = elapsedTimeSpan; + currentGameTime.TotalGameTime = totalTimeSpan; + Update(currentGameTime); }); BeginDraw(); - Draw(_currentGameTime); + Draw(currentGameTime); EndDraw(); } @@ -69,7 +69,7 @@ namespace xna { return EXIT_FAILURE; try { - if (!_gameWindow->impl->Create()) { + if (!gameWindow->impl->Create()) { Exception::Throw(Exception::FAILED_TO_CREATE); return false; } @@ -93,7 +93,7 @@ namespace xna { void Game::Initialize() { Keyboard::Initialize(); - Mouse::Initialize(_gameWindow->Handle()); + Mouse::Initialize(gameWindow->Handle()); GamePad::Initialize(); AudioEngine::Initialize(); @@ -101,16 +101,16 @@ namespace xna { } void Game::Draw(GameTime const& gameTime) { - if (_enabledGameComponents && !_drawableGameComponents.empty()) { - const auto count = _drawableGameComponents.size(); + if (enabledGameComponents && !drawableGameComponents.empty()) { + const auto count = drawableGameComponents.size(); - if (count != _drawableGameComponentsCount && _gameComponents->AutoSort) { - GameComponentCollection::DrawSort(_drawableGameComponents); - _drawableGameComponentsCount = count; + if (count != drawableGameComponentsCount && gameComponents->AutoSort) { + GameComponentCollection::DrawSort(drawableGameComponents); + drawableGameComponentsCount = count; } for (size_t i = 0; i < count; ++i) { - auto& component = _drawableGameComponents[i]; + auto& component = drawableGameComponents[i]; if (!component) continue; @@ -120,24 +120,24 @@ namespace xna { drawable->Draw(gameTime); } - _drawableGameComponents.clear(); + drawableGameComponents.clear(); } graphicsDevice->Present(); } void Game::Update(GameTime const& gameTime) { - _audioEngine->Update(); + audioEngine->Update(); - if (_enabledGameComponents && _gameComponents->Count() > 0) { - const auto count = _gameComponents->Count(); + if (enabledGameComponents && gameComponents->Count() > 0) { + const auto count = gameComponents->Count(); for (size_t i = 0; i < count; ++i) { - auto component = _gameComponents->At(i); + auto component = gameComponents->At(i); if (!component) continue; if (component->Type() == GameComponentType::Drawable) { - _drawableGameComponents.push_back(component); + drawableGameComponents.push_back(component); } auto updatable = reinterpret_pointer_cast(component); @@ -148,25 +148,25 @@ namespace xna { } } - sptr Game::Window() { return _gameWindow; } + sptr Game::Window() { return gameWindow; } sptr Game::Device() const { return graphicsDevice; } - sptr Game::Components() const { return _gameComponents; } + sptr Game::Components() const { return gameComponents; } sptr Game::Services() { return services; } sptr Game::Content() const { return contentManager; } - void Game::EnableGameComponents(bool value) { _enabledGameComponents = value; } + void Game::EnableGameComponents(bool value) { enabledGameComponents = value; } void Game::AttachGraphicsDevice(sptr const& device) { graphicsDevice = device; } void Game::ResizeWindow(int width, int heigth) { - const auto windowBounds = _gameWindow->ClientBounds(); + const auto windowBounds = gameWindow->ClientBounds(); if (windowBounds.Width != width || windowBounds.Height != heigth) { - _gameWindow->impl->Size( + gameWindow->impl->Size( width, heigth); - _gameWindow->impl->Update(); + gameWindow->impl->Update(); } } diff --git a/inc/xna/game/game.hpp b/inc/xna/game/game.hpp index 2ac9070..5cccc65 100644 --- a/inc/xna/game/game.hpp +++ b/inc/xna/game/game.hpp @@ -76,14 +76,14 @@ namespace xna { private: friend class GraphicsDeviceManager; - sptr _gameComponents = nullptr; - sptr _gameWindow{ nullptr }; - sptr _audioEngine = nullptr; + sptr gameComponents = nullptr; + sptr gameWindow{ nullptr }; + sptr audioEngine = nullptr; sptr contentManager; - std::vector> _drawableGameComponents; - size_t _drawableGameComponentsCount{ 0 }; - bool _enabledGameComponents{ false }; - GameTime _currentGameTime{}; + std::vector> drawableGameComponents; + size_t drawableGameComponentsCount{ 0 }; + bool enabledGameComponents{ false }; + GameTime currentGameTime{}; bool isFixedTimeStep{ true }; TimeSpan targetElapsedTime{ TimeSpan::FromTicks(166667L) }; bool isRunning{ false }; diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index 45257e9..24ef843 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -4,4 +4,4 @@ # Add source to this project's executable. add_subdirectory ("01_blank") -add_subdirectory ("02_PlatfformerStarterKit") +#add_subdirectory ("02_PlatfformerStarterKit") From 0dd43eb377f8b2d60ab418ed2461fbcc850c595f Mon Sep 17 00:00:00 2001 From: Danilo Date: Fri, 2 Aug 2024 11:26:22 -0300 Subject: [PATCH 04/10] =?UTF-8?q?Coment=C3=A1rios=20e=20remove=20Windows?= =?UTF-8?q?=20em=20GraphicsDEviceInformation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/platform-dx/gdevicemanager.cpp | 3 +-- inc/xna/game/gdeviceinfo.hpp | 5 ++++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/framework/platform-dx/gdevicemanager.cpp b/framework/platform-dx/gdevicemanager.cpp index 13ee4f7..df38105 100644 --- a/framework/platform-dx/gdevicemanager.cpp +++ b/framework/platform-dx/gdevicemanager.cpp @@ -14,8 +14,7 @@ namespace xna { parameters->BackBufferHeight = backBufferHeight; parameters->BackBufferFormat = SurfaceFormat::Color; parameters->IsFullscreen = false; - information.PresentParameters = parameters; - information.Window = game->Window(); + information.PresentParameters = parameters; } void GraphicsDeviceManager::ApplyChanges() { diff --git a/inc/xna/game/gdeviceinfo.hpp b/inc/xna/game/gdeviceinfo.hpp index a3a5d07..9734ce3 100644 --- a/inc/xna/game/gdeviceinfo.hpp +++ b/inc/xna/game/gdeviceinfo.hpp @@ -4,15 +4,18 @@ #include "../default.hpp" namespace xna { + //Holds the settings for creating a graphics device on Windows. struct GraphicsDeviceInformation { GraphicsDeviceInformation() { PresentParameters = snew(); } + //Specifies which graphics adapter to create the device on. sptr Adapter = nullptr; + //Gets the graphics profile, which determines the graphics feature set. xna::GraphicsProfile Profile{ xna::GraphicsProfile::Reach }; + //Specifies the presentation parameters to use when creating a graphics device. sptr PresentParameters = nullptr; - sptr Window = nullptr; }; } From eb16fefa15910c7c369b96b8c45c2889e005e8e4 Mon Sep 17 00:00:00 2001 From: Danilo Date: Fri, 2 Aug 2024 11:29:56 -0300 Subject: [PATCH 05/10] Comentarios e adiciona RemoveService em GameServiceContainer --- framework/game/servicecontainer.cpp | 8 ++++++++ inc/xna/game/servicecontainer.hpp | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/framework/game/servicecontainer.cpp b/framework/game/servicecontainer.cpp index 3673966..1749d3b 100644 --- a/framework/game/servicecontainer.cpp +++ b/framework/game/servicecontainer.cpp @@ -15,4 +15,12 @@ namespace xna { ? services[hashCode] : std::any(); } + + void GameServiceContainer::RemoveService(Type& type) { + auto hashCode = type.GetHashCode(); + + if (services.contains(hashCode)) + services.erase(hashCode); + + } } \ No newline at end of file diff --git a/inc/xna/game/servicecontainer.hpp b/inc/xna/game/servicecontainer.hpp index 69255d0..d50d4fc 100644 --- a/inc/xna/game/servicecontainer.hpp +++ b/inc/xna/game/servicecontainer.hpp @@ -6,13 +6,18 @@ #include namespace xna { + //A collection of game services. class GameServiceContainer : public IServiceProvider { public: + //Adds a service to the GameServiceContainer. void AddService(Type& type, std::any& provider); // Inherited via IServiceProvider std::any GetService(Type& serviceType) override; + //Removes the object providing a specified service. + void RemoveService(Type& type); + private: std::map services; }; From a60e0f366dd277fde173688dd6f00471c6d7c864 Mon Sep 17 00:00:00 2001 From: Danilo Date: Fri, 2 Aug 2024 11:31:54 -0300 Subject: [PATCH 06/10] =?UTF-8?q?Coment=C3=A1rios=20em=20GameTime?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/xna/game/time.hpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/inc/xna/game/time.hpp b/inc/xna/game/time.hpp index 474eb89..72439e0 100644 --- a/inc/xna/game/time.hpp +++ b/inc/xna/game/time.hpp @@ -5,6 +5,7 @@ #include "../csharp/timespan.hpp" namespace xna { + //Snapshot of the game timing state expressed in values that can be used by variable-step (real time) or fixed-step (game time) games. class GameTime { public: constexpr GameTime() = default; @@ -14,8 +15,11 @@ namespace xna { IsRunningSlowly(isRunningSlowly), TotalGameTime(totalGameTime) { } + //The amount of elapsed game time since the last update. TimeSpan ElapsedGameTime{ 0 }; + //Gets a value indicating that the game loop is taking longer than its TargetElapsedTime. In this case, the game loop can be considered to be running too slowly and should do something to "catch up." bool IsRunningSlowly{ false }; + //The amount of game time since the start of the game. TimeSpan TotalGameTime{ 0 }; }; } From 022c6aad4882322e48b92b3091e327e96c2b04ec Mon Sep 17 00:00:00 2001 From: Danilo Date: Fri, 2 Aug 2024 11:57:08 -0300 Subject: [PATCH 07/10] =?UTF-8?q?Implementa=C3=A7=C3=B5es=20em=20uso=20de?= =?UTF-8?q?=20vsync=20com=20swapchain?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/platform-dx/device.cpp | 31 ++++++++++++++++++++++------- framework/platform-dx/swapchain.cpp | 6 ++++-- inc/xna/enumerations.hpp | 5 +++++ inc/xna/graphics/device.hpp | 4 ++-- inc/xna/graphics/swapchain.hpp | 2 +- inc/xna/xna-dx.hpp | 2 +- 6 files changed, 37 insertions(+), 13 deletions(-) diff --git a/framework/platform-dx/device.cpp b/framework/platform-dx/device.cpp index 5cbc57a..1f625b8 100644 --- a/framework/platform-dx/device.cpp +++ b/framework/platform-dx/device.cpp @@ -168,13 +168,30 @@ namespace xna { initAndApplyState(*impl, _this); + const auto currentPresenInterval = impl->_presentationParameters->PresentationInterval; + + switch (currentPresenInterval) + { + case PresentInterval::Default: + case PresentInterval::One: + case PresentInterval::Two: + impl->vSyncValue = 1; + break; + case PresentInterval::Immediate: + impl->vSyncValue = 0; + break; + default: + impl->vSyncValue = 1; + break; + } + return true; } - bool GraphicsDevice::Present() { - if (!impl) return false; - - const auto result = impl->_swapChain->Present(impl->_usevsync); + bool GraphicsDevice::Present() const { + const auto currentPresenInterval = impl->_presentationParameters->PresentationInterval; + bool result = impl->_swapChain->Present(impl->vSyncValue != 0); + impl->_context->OMSetRenderTargets( 1, impl->_renderTarget2D->render_impl->_renderTargetView.GetAddressOf(), @@ -183,7 +200,7 @@ namespace xna { return result; } - void GraphicsDevice::Clear(Color const& color) { + void GraphicsDevice::Clear(Color const& color) const { if (!impl) return; const auto v4 = color.ToVector4(); @@ -241,10 +258,10 @@ namespace xna { impl->_viewport = viewport; } - void GraphicsDevice::UseVSync(bool use) { + void GraphicsDevice::UseVSync(bool value) { if (!impl) return; - impl->_usevsync = use; + impl->vSyncValue = static_cast(value); } diff --git a/framework/platform-dx/swapchain.cpp b/framework/platform-dx/swapchain.cpp index 6ac7818..dc76cd9 100644 --- a/framework/platform-dx/swapchain.cpp +++ b/framework/platform-dx/swapchain.cpp @@ -79,11 +79,13 @@ namespace xna { return !FAILED(hr); } - bool SwapChain::Present(bool vsync) { + bool SwapChain::Present(bool vsync) const { if (!impl || !impl->dxSwapChain) return false; - const auto hr = impl->dxSwapChain->Present(vsync, NULL); + const auto presentValue = static_cast(vsync); + + const auto hr = impl->dxSwapChain->Present(presentValue, NULL); return !FAILED(hr); } } \ No newline at end of file diff --git a/inc/xna/enumerations.hpp b/inc/xna/enumerations.hpp index dd0657f..1821daa 100644 --- a/inc/xna/enumerations.hpp +++ b/inc/xna/enumerations.hpp @@ -223,10 +223,15 @@ namespace xna { Four, }; + //Defines flags that describe the relationship between the adapter refresh rate and the rate at which Present operations are completed. enum class PresentInterval { + //Equivalent to setting One. Default, + //The driver waits for the vertical retrace period (the runtime will beam trace to prevent tearing). Present operations are not affected more frequently than the screen refresh rate; the runtime completes one Present operation per adapter refresh period, at most. This option is always available for both windowed and full-screen swap chains. One, + //The driver waits for the vertical retrace period. Present operations are not affected more frequently than every second screen refresh. Two, + //The runtime updates the window client area immediately, and might do so more than once during the adapter refresh period. Present operations might be affected immediately. This option is always available for both windowed and full-screen swap chains. Immediate }; diff --git a/inc/xna/graphics/device.hpp b/inc/xna/graphics/device.hpp index 6ce3e31..46c6a31 100644 --- a/inc/xna/graphics/device.hpp +++ b/inc/xna/graphics/device.hpp @@ -43,11 +43,11 @@ namespace xna { return PresentationParameters(); } - void Clear(Color const& color); + void Clear(Color const& color) const; void Clear(ClearOptions options, Color const& color, float depth, Int stencil); void Clear(ClearOptions options, Vector4 const& color, float depth, Int stencil); bool Initialize(); - bool Present(); + bool Present() const; void Reset(sptr const& presentationParameters, sptr const& graphicsAdapter); diff --git a/inc/xna/graphics/swapchain.hpp b/inc/xna/graphics/swapchain.hpp index 2674027..b4e489a 100644 --- a/inc/xna/graphics/swapchain.hpp +++ b/inc/xna/graphics/swapchain.hpp @@ -11,7 +11,7 @@ namespace xna { SwapChain(sptr const& device); ~SwapChain() override; bool Initialize(); - bool Present(bool vsync); + bool Present(bool vsync) const; bool GetBackBuffer(Texture2D& texture2D); public: struct PlatformImplementation; diff --git a/inc/xna/xna-dx.hpp b/inc/xna/xna-dx.hpp index e5c19c7..15d64af 100644 --- a/inc/xna/xna-dx.hpp +++ b/inc/xna/xna-dx.hpp @@ -873,7 +873,7 @@ namespace xna { private: friend class GraphicsDevice; float _backgroundColor[4] = { 0, 0, 0, 0 }; - bool _usevsync{ true }; + UINT vSyncValue = 1; }; struct Game::PlatformImplementation { From 5fffba8e594081e2eb6b27e80270a27837617982 Mon Sep 17 00:00:00 2001 From: Danilo Date: Fri, 2 Aug 2024 16:40:06 -0300 Subject: [PATCH 08/10] =?UTF-8?q?Implementa=C3=A7=C3=B5es=20em=20GraphicsD?= =?UTF-8?q?evice?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/platform-dx/blendstate.cpp | 2 +- framework/platform-dx/depthstencilstate.cpp | 2 +- framework/platform-dx/device.cpp | 412 +++++++++----------- framework/platform-dx/gdevicemanager.cpp | 15 +- framework/platform-dx/swapchain.cpp | 2 +- inc/xna/default.hpp | 8 + inc/xna/graphics/device.hpp | 71 ++-- inc/xna/xna-dx.hpp | 25 +- 8 files changed, 234 insertions(+), 303 deletions(-) diff --git a/framework/platform-dx/blendstate.cpp b/framework/platform-dx/blendstate.cpp index 74ffe21..6ee8ebe 100644 --- a/framework/platform-dx/blendstate.cpp +++ b/framework/platform-dx/blendstate.cpp @@ -122,7 +122,7 @@ namespace xna { } if (!impl->dxBlendState) { - Exception::Throw(Exception::INVALID_OPERATION); + Initialize(); } m_device->impl->_context->OMSetBlendState( diff --git a/framework/platform-dx/depthstencilstate.cpp b/framework/platform-dx/depthstencilstate.cpp index 0ee93db..dcbf545 100644 --- a/framework/platform-dx/depthstencilstate.cpp +++ b/framework/platform-dx/depthstencilstate.cpp @@ -63,7 +63,7 @@ namespace xna { } if (!impl->dxDepthStencil) { - Exception::Throw(Exception::INVALID_OPERATION); + Initialize(); } m_device->impl->_context->OMSetDepthStencilState(impl->dxDepthStencil.Get(), 0); diff --git a/framework/platform-dx/device.cpp b/framework/platform-dx/device.cpp index 1f625b8..1e297d8 100644 --- a/framework/platform-dx/device.cpp +++ b/framework/platform-dx/device.cpp @@ -1,8 +1,167 @@ #include "xna/xna-dx.hpp" -#include "xna/game/gdevicemanager.hpp" namespace xna { - static void reset(GraphicsDevice::PlatformImplementation& impl) + static void reset(GraphicsDevice::PlatformImplementation& impl); + static void createDevice(GraphicsDevice::PlatformImplementation& impl, GraphicsAdapter& currentAdapter); + static void initAndApplyState(P_BlendState& blendState, P_RasterizerState& rasterizerState, + P_DepthStencilState& depthStencilState, P_SamplerStateCollection& samplerStates, P_GraphicsDevice const& device); + + GraphicsDevice::GraphicsDevice() { + impl = unew(); + adapter = GraphicsAdapter::DefaultAdapter(); + } + + GraphicsDevice::GraphicsDevice(sptr const& adapter, GraphicsProfile const& graphicsProfile, sptr const& presentationParameters) + : adapter(adapter), graphicsProfile(graphicsProfile), presentationParameters(presentationParameters) { + impl = unew(); + + blendState = xna::BlendState::Opaque(); + depthStencilState = xna::DepthStencilState::Default(); + rasterizerState = xna::RasterizerState::CullCounterClockwise(); + samplerStateCollection = snew(); + } + + void GraphicsDevice::Initialize() { + auto _this = shared_from_this(); + + reset(*impl); + createDevice(*impl, *adapter); + + auto hr = CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&impl->_factory); + + if FAILED(hr) + Exception::Throw(Exception::FAILED_TO_CREATE); + + viewport = xna::Viewport(0.0F, 0.0F, + presentationParameters->BackBufferWidth, + presentationParameters->BackBufferHeight, + 0.0F, 1.F); + + const auto backColor = Colors::CornflowerBlue; + const auto backColorV3 = backColor.ToVector3(); + + impl->_backgroundColor[0] = backColorV3.X; + impl->_backgroundColor[1] = backColorV3.Y; + impl->_backgroundColor[2] = backColorV3.Z; + impl->_backgroundColor[3] = 1.0f; + + impl->_swapChain = snew(_this); + impl->_swapChain->Initialize(); + + auto hwnd = reinterpret_cast(presentationParameters->DeviceWindowHandle); + hr = impl->_factory->MakeWindowAssociation(hwnd, DXGI_MWA_NO_ALT_ENTER); + + if (FAILED(hr)) + Exception::Throw(Exception::FAILED_TO_MAKE_WINDOW_ASSOCIATION); + + impl->_renderTarget2D = snew(_this); + impl->_renderTarget2D->Initialize(); + impl->_renderTarget2D->Apply(); + + D3D11_VIEWPORT view = DxHelpers::ViewportToDx(viewport); + impl->_context->RSSetViewports(1, &view); + + initAndApplyState(blendState, rasterizerState, depthStencilState, samplerStateCollection, _this); + + const auto currentPresenInterval = presentationParameters->PresentationInterval; + + switch (currentPresenInterval) + { + case PresentInterval::Default: + case PresentInterval::One: + case PresentInterval::Two: + impl->vSyncValue = 1; + break; + case PresentInterval::Immediate: + impl->vSyncValue = 0; + break; + default: + impl->vSyncValue = 1; + break; + } + } + + bool GraphicsDevice::Present() const { + const auto currentPresenInterval = presentationParameters->PresentationInterval; + bool result = impl->_swapChain->Present(impl->vSyncValue != 0); + + impl->_context->OMSetRenderTargets( + 1, + impl->_renderTarget2D->render_impl->_renderTargetView.GetAddressOf(), + nullptr); + + return result; + } + + void GraphicsDevice::Clear(Color const& color) const { + if (!impl) return; + + const auto v4 = color.ToVector4(); + + impl->_backgroundColor[0] = v4.X; + impl->_backgroundColor[1] = v4.Y; + impl->_backgroundColor[2] = v4.Z; + impl->_backgroundColor[3] = v4.W; + + impl->_context->ClearRenderTargetView( + impl->_renderTarget2D->render_impl->_renderTargetView.Get(), + impl->_backgroundColor); + } + + void GraphicsDevice::Clear(ClearOptions options, Color const& color, float depth, Int stencil) const { + if (!impl) return; + + switch (options) + { + case xna::ClearOptions::DepthBuffer: + Exception::Throw(Exception::NOT_IMPLEMENTED); + break; + case xna::ClearOptions::Stencil: + Exception::Throw(Exception::NOT_IMPLEMENTED); + break; + case xna::ClearOptions::Target: + Clear(color); + break; + default: + return; + } + } + + void GraphicsDevice::Viewport(xna::Viewport const& value) { + viewport = value; + const auto view = DxHelpers::ViewportToDx(viewport); + + impl->_context->RSSetViewports(1, &view); + } + + void GraphicsDevice::BlendState(sptr const& value) { + blendState = value; + blendState->Apply(); + } + + void GraphicsDevice::DepthStencilState(sptr const& value) { + depthStencilState = value; + depthStencilState->Apply(); + } + + void GraphicsDevice::RasterizerState(sptr const& value) { + rasterizerState = value; + rasterizerState->Apply(); + } + + void GraphicsDevice::Reset(sptr const& parameters, sptr const& graphicsAdapter){ + impl = unew(); + adapter = graphicsAdapter; + presentationParameters = parameters; + + Initialize(); + } + + // + // INTERNAL + // + + void reset(GraphicsDevice::PlatformImplementation& impl) { if (impl._device) { impl._device->Release(); @@ -20,7 +179,7 @@ namespace xna { } } - static void createDevice(GraphicsDevice::PlatformImplementation& impl) { + void createDevice(GraphicsDevice::PlatformImplementation& impl, GraphicsAdapter& currentAdapter) { // // See ref // @@ -34,11 +193,10 @@ namespace xna { auto createDeviceFlags = 0; #if _DEBUG createDeviceFlags = D3D11_CREATE_DEVICE_FLAG::D3D11_CREATE_DEVICE_DEBUG; -#endif +#endif + + const auto& pAdapter = GraphicsAdapter::UseNullDevice() ? NULL : currentAdapter.impl->dxAdapter.Get(); - const auto& currentAdapter = impl._adapter; - const auto& pAdapter = GraphicsAdapter::UseNullDevice() ? NULL : currentAdapter->impl->dxAdapter.Get(); - // // if pAdapter is not NULL driverType must be D3D_DRIVER_TYPE_UNKNOWN // @@ -75,237 +233,19 @@ namespace xna { Exception::Throw(Exception::FAILED_TO_CREATE); } - static void initAndApplyState(GraphicsDevice::PlatformImplementation& impl, PGraphicsDevice const& device) { - impl._blendState->Bind(device); - impl._blendState->Initialize(); - impl._blendState->Apply(); + static void initAndApplyState(P_BlendState& blendState, P_RasterizerState& rasterizerState, P_DepthStencilState& depthStencilState, P_SamplerStateCollection& samplerStates, P_GraphicsDevice const& device) { + blendState->Bind(device); + blendState->Initialize(); + blendState->Apply(); - impl._rasterizerState->Bind(device); - impl._rasterizerState->Initialize(); - impl._rasterizerState->Apply(); + rasterizerState->Bind(device); + rasterizerState->Initialize(); + rasterizerState->Apply(); - impl._depthStencilState->Bind(device); - impl._depthStencilState->Initialize(); - impl._depthStencilState->Apply(); + depthStencilState->Bind(device); + depthStencilState->Initialize(); + depthStencilState->Apply(); - impl._samplerStates->Apply(*device); - } - - GraphicsDevice::GraphicsDevice() { - impl = unew(); - impl->_adapter = GraphicsAdapter::DefaultAdapter(); - } - - GraphicsDevice::GraphicsDevice(GraphicsDeviceInformation const& info) { - impl = unew(); - - impl->_adapter = info.Adapter; - impl->_presentationParameters = info.PresentParameters; - } - - GraphicsDevice::GraphicsDevice(sptr const& adapter, GraphicsProfile const& graphicsProfile, sptr const& presentationParameters) { - impl = unew(); - impl->_adapter = adapter; - impl->_presentationParameters = presentationParameters; - } - - bool GraphicsDevice::Initialize() { - auto _this = shared_from_this(); - - if (!impl) - impl = uptr(); - - reset(*impl); - - createDevice(*impl); - - auto hr = CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void**)&impl->_factory); - - if FAILED(hr) - Exception::Throw(Exception::FAILED_TO_CREATE); - - //const auto bounds = impl->_gameWindow->ClientBounds(); - - impl->_viewport = xna::Viewport(0.0F, 0.0F, - impl->_presentationParameters->BackBufferWidth, - impl->_presentationParameters->BackBufferHeight, - 0.0F, 1.F); - - //COLORREF color = impl->_gameWindow->impl->Color(); - const auto backColor = Colors::CornflowerBlue; - const auto backColorV3 = backColor.ToVector3(); - - impl->_backgroundColor[0] = backColorV3.X; - impl->_backgroundColor[1] = backColorV3.Y; - impl->_backgroundColor[2] = backColorV3.Z; - impl->_backgroundColor[3] = 1.0f; - - impl->_swapChain = snew(_this); - impl->_swapChain->Initialize(); - - auto hwnd = reinterpret_cast(impl->_presentationParameters->DeviceWindowHandle); - hr = impl->_factory->MakeWindowAssociation(hwnd, DXGI_MWA_NO_ALT_ENTER); - - if (FAILED(hr)) - Exception::Throw(Exception::FAILED_TO_MAKE_WINDOW_ASSOCIATION); - - impl->_renderTarget2D = snew(_this); - - if (!impl->_renderTarget2D->Initialize()) - return false; - - impl->_renderTarget2D->Apply(); - - D3D11_VIEWPORT view{}; - view.TopLeftX = impl->_viewport.X; - view.TopLeftY = impl->_viewport.Y; - view.Width = impl->_viewport.Width; - view.Height = impl->_viewport.Height; - view.MinDepth = impl->_viewport.MinDetph; - view.MaxDepth = impl->_viewport.MaxDepth; - - impl->_context->RSSetViewports(1, &view); - - initAndApplyState(*impl, _this); - - const auto currentPresenInterval = impl->_presentationParameters->PresentationInterval; - - switch (currentPresenInterval) - { - case PresentInterval::Default: - case PresentInterval::One: - case PresentInterval::Two: - impl->vSyncValue = 1; - break; - case PresentInterval::Immediate: - impl->vSyncValue = 0; - break; - default: - impl->vSyncValue = 1; - break; - } - - return true; - } - - bool GraphicsDevice::Present() const { - const auto currentPresenInterval = impl->_presentationParameters->PresentationInterval; - bool result = impl->_swapChain->Present(impl->vSyncValue != 0); - - impl->_context->OMSetRenderTargets( - 1, - impl->_renderTarget2D->render_impl->_renderTargetView.GetAddressOf(), - nullptr); - - return result; - } - - void GraphicsDevice::Clear(Color const& color) const { - if (!impl) return; - - const auto v4 = color.ToVector4(); - - impl->_backgroundColor[0] = v4.X; - impl->_backgroundColor[1] = v4.Y; - impl->_backgroundColor[2] = v4.Z; - impl->_backgroundColor[3] = v4.W; - - impl->_context->ClearRenderTargetView( - impl->_renderTarget2D->render_impl->_renderTargetView.Get(), - impl->_backgroundColor); - } - - void GraphicsDevice::Clear(ClearOptions options, Color const& color, float depth, Int stencil) { - if (!impl) return; - - switch (options) - { - case xna::ClearOptions::DepthBuffer: - Exception::Throw(Exception::NOT_IMPLEMENTED); - break; - case xna::ClearOptions::Stencil: - Exception::Throw(Exception::NOT_IMPLEMENTED); - break; - case xna::ClearOptions::Target: - Clear(color); - break; - default: - return; - } - } - - void GraphicsDevice::Clear(ClearOptions options, Vector4 const& color, float depth, Int stencil) { - if (!impl) return; - - - } - - sptr GraphicsDevice::Adapter() const { - if (!impl) return nullptr; - - return impl->_adapter; - } - - xna::Viewport GraphicsDevice::Viewport() const { - if (!impl) return {}; - - return impl->_viewport; - } - - void GraphicsDevice::Viewport(xna::Viewport const& viewport) { - if (!impl) return; - - impl->_viewport = viewport; - } - - void GraphicsDevice::UseVSync(bool value) { - if (!impl) return; - - impl->vSyncValue = static_cast(value); - } - - - sptr GraphicsDevice::BlendState() const { - return impl->_blendState; - } - - void GraphicsDevice::BlendState(sptr const& value) { - impl->_blendState = value; - } - - sptr GraphicsDevice::DepthStencilState() const { - return impl->_depthStencilState; - } - - void GraphicsDevice::DepthStencilState(sptr const& value) { - impl->_depthStencilState = value; - } - - sptr GraphicsDevice::RasterizerState() const { - return impl->_rasterizerState; - } - - void GraphicsDevice::RasterizerState(sptr const& value) { - impl->_rasterizerState = value; - } - - sptr GraphicsDevice::SamplerStates() const { - return impl->_samplerStates; - } - - Int GraphicsDevice::MultiSampleMask() const { - return impl->_multiSampleMask; - } - - void GraphicsDevice::MultiSampleMask(Int value) { - impl->_multiSampleMask = value; - } - - void GraphicsDevice::Reset(sptr const& presentationParameters, sptr const& graphicsAdapter){ - impl = unew(); - impl->_adapter = graphicsAdapter; - impl->_presentationParameters = presentationParameters; - - Initialize(); + samplerStates->Apply(*device); } } \ No newline at end of file diff --git a/framework/platform-dx/gdevicemanager.cpp b/framework/platform-dx/gdevicemanager.cpp index df38105..bfdacdf 100644 --- a/framework/platform-dx/gdevicemanager.cpp +++ b/framework/platform-dx/gdevicemanager.cpp @@ -74,19 +74,18 @@ namespace xna { } if (flag2) - CreateDevice(*bestDevice); - - auto presentationParameters = device->PresentParameters(); + CreateDevice(*bestDevice); screenDeviceName = device->Adapter()->DeviceName(); + const auto presentationParameters = device->PresentParameters(); - isReallyFullScreen = presentationParameters.IsFullscreen; + isReallyFullScreen = presentationParameters->IsFullscreen; - if (presentationParameters.BackBufferWidth != 0) - clientWidth = presentationParameters.BackBufferWidth; + if (presentationParameters->BackBufferWidth != 0) + clientWidth = presentationParameters->BackBufferWidth; - if (presentationParameters.BackBufferHeight != 0) - clientHeight = presentationParameters.BackBufferHeight; + if (presentationParameters->BackBufferHeight != 0) + clientHeight = presentationParameters->BackBufferHeight; isDeviceDirty = false; diff --git a/framework/platform-dx/swapchain.cpp b/framework/platform-dx/swapchain.cpp index dc76cd9..04e6ebe 100644 --- a/framework/platform-dx/swapchain.cpp +++ b/framework/platform-dx/swapchain.cpp @@ -48,7 +48,7 @@ namespace xna { Exception::Throw(Exception::UNABLE_TO_INITIALIZE); } - const auto parameters = m_device->impl->_presentationParameters; + const auto parameters = m_device->PresentParameters(); impl->dxDescription.Width = static_cast(parameters->BackBufferWidth); impl->dxDescription.Height = static_cast(parameters->BackBufferHeight); diff --git a/inc/xna/default.hpp b/inc/xna/default.hpp index ad0327a..4725e7a 100644 --- a/inc/xna/default.hpp +++ b/inc/xna/default.hpp @@ -194,6 +194,14 @@ namespace xna { struct GamePadState; struct KeyboardState; struct MouseState; + + using P_BlendState = sptr; + using P_DepthStencilState = sptr; + using P_GraphicsAdapter = sptr; + using P_GraphicsDevice = sptr; + using P_RasterizerState = sptr; + using P_PresentationParameters = sptr; + using P_SamplerStateCollection = sptr; } diff --git a/inc/xna/graphics/device.hpp b/inc/xna/graphics/device.hpp index 46c6a31..08f259e 100644 --- a/inc/xna/graphics/device.hpp +++ b/inc/xna/graphics/device.hpp @@ -3,59 +3,60 @@ #include "../default.hpp" #include "presentparams.hpp" +#include "viewport.hpp" namespace xna { //Performs primitive-based rendering, creates resources, handles system-level variables, adjusts gamma ramp levels, and creates shaders. class GraphicsDevice : public std::enable_shared_from_this { public: GraphicsDevice(); - GraphicsDevice(GraphicsDeviceInformation const& info); - GraphicsDevice(sptr const& adapter, GraphicsProfile const& graphicsProfile, sptr const& presentationParameters); + + GraphicsDevice(P_GraphicsAdapter const& adapter, GraphicsProfile const& graphicsProfile, P_PresentationParameters const& presentationParameters); //Gets the graphics adapter. - sptr Adapter() const; - + inline P_GraphicsAdapter Adapter() const { return adapter; } //Gets or sets a system-defined instance of a blend state object initialized for alpha blending. The default value is BlendState.Opaque. - sptr BlendState() const; + inline P_BlendState BlendState() const { return blendState; } //Gets or sets a system-defined instance of a blend state object initialized for alpha blending. The default value is BlendState.Opaque. - void BlendState(sptr const& value); + void BlendState(P_BlendState const& value); //Gets or sets a system-defined instance of a depth-stencil state object. The default value is DepthStencilState.Default. - sptr DepthStencilState() const; + inline P_DepthStencilState DepthStencilState() const { return depthStencilState; } //Gets or sets a system-defined instance of a depth-stencil state object. The default value is DepthStencilState.Default. - void DepthStencilState(sptr const& value); + void DepthStencilState(P_DepthStencilState const& value); //Gets or sets rasterizer state. The default value is RasterizerState.CullCounterClockwise. - sptr RasterizerState() const; + inline P_RasterizerState RasterizerState() const { return rasterizerState; } //Gets or sets rasterizer state. The default value is RasterizerState.CullCounterClockwise. - void RasterizerState(sptr const& value); + void RasterizerState(P_RasterizerState const& value); //Retrieves a collection of SamplerState objects for the current GraphicsDevice. - sptr SamplerStates() const; - - //Gets or sets a bitmask controlling modification of the samples in a multisample render target. The default value is -1 (0xffffffff). - Int MultiSampleMask() const; - //Gets or sets a bitmask controlling modification of the samples in a multisample render target. The default value is -1 (0xffffffff). - void MultiSampleMask(Int value); - - constexpr GraphicsProfile Profile() const { - return GraphicsProfile::HiDef; - } - - constexpr PresentationParameters PresentParameters() const { - return PresentationParameters(); - } - + inline P_SamplerStateCollection SamplerStates() const { return samplerStateCollection; } + //Gets the graphics profile. + constexpr GraphicsProfile Profile() const { return graphicsProfile; } + //Gets the presentation parameters associated with this graphics device. + P_PresentationParameters PresentParameters() const { return presentationParameters; } + //Clears resource buffers. void Clear(Color const& color) const; - void Clear(ClearOptions options, Color const& color, float depth, Int stencil); - void Clear(ClearOptions options, Vector4 const& color, float depth, Int stencil); - bool Initialize(); - bool Present() const; - - void Reset(sptr const& presentationParameters, sptr const& graphicsAdapter); - - xna::Viewport Viewport() const; + //Clears resource buffers. + void Clear(ClearOptions options, Color const& color, float depth, Int stencil) const; + //Presents the display with the contents of the next buffer in the sequence of back buffers owned by the GraphicsDevice. + bool Present() const; + //Resets the presentation parameters for the current GraphicsDevice. + void Reset(P_PresentationParameters const& presentationParameters, P_GraphicsAdapter const& graphicsAdapter); + //Gets or sets a viewport identifying the portion of the render target to receive draw calls. + constexpr xna::Viewport Viewport() const { return viewport; } + //Gets or sets a viewport identifying the portion of the render target to receive draw calls. void Viewport(xna::Viewport const& viewport); - void UseVSync(bool use); + + void Initialize(); - //void DrawPrimitives(PrimitiveType primitiveType, Int startVertex, Int primitiveCount); + private: + P_GraphicsAdapter adapter{ nullptr }; + P_BlendState blendState{ nullptr }; + P_DepthStencilState depthStencilState{ nullptr }; + P_RasterizerState rasterizerState{ nullptr }; + P_SamplerStateCollection samplerStateCollection{ nullptr }; + P_PresentationParameters presentationParameters{ nullptr }; + GraphicsProfile graphicsProfile{ GraphicsProfile::HiDef }; + xna::Viewport viewport{}; public: struct PlatformImplementation; diff --git a/inc/xna/xna-dx.hpp b/inc/xna/xna-dx.hpp index 15d64af..0493488 100644 --- a/inc/xna/xna-dx.hpp +++ b/inc/xna/xna-dx.hpp @@ -831,31 +831,14 @@ namespace xna { uptr _dxAudioEngine = nullptr; }; - struct GraphicsDevice::PlatformImplementation { - PlatformImplementation() { - _blendState = xna::BlendState::Opaque(); - _depthStencilState = xna::DepthStencilState::Default(); - _rasterizerState = xna::RasterizerState::CullCounterClockwise(); - _samplerStates = snew(); - } - - public: + struct GraphicsDevice::PlatformImplementation { comptr _device = nullptr; comptr _context = nullptr; - comptr _factory = nullptr; - - PBlendState _blendState = nullptr; - PRasterizerState _rasterizerState = nullptr; - PDepthStencilState _depthStencilState = nullptr; - PSamplerStateCollection _samplerStates = nullptr; - Int _multiSampleMask = 0xffffffff; - + comptr _factory = nullptr; + sptr _swapChain = nullptr; - sptr _adapter = nullptr; sptr _renderTarget2D = nullptr; - intptr_t windowHandle{ 0 }; - xna::Viewport _viewport{}; - sptr _presentationParameters; + intptr_t windowHandle{ 0 }; D3D_FEATURE_LEVEL featureLevels[7] = { From 540332d71c2d4f5055ccfe8180a1b66237d6c2a1 Mon Sep 17 00:00:00 2001 From: Danilo Date: Fri, 2 Aug 2024 17:17:33 -0300 Subject: [PATCH 09/10] =?UTF-8?q?Corre=C3=A7=C3=A3o=20em=20RenderTarget2D:?= =?UTF-8?q?:Apply?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/platform-dx/rendertarget.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/framework/platform-dx/rendertarget.cpp b/framework/platform-dx/rendertarget.cpp index d154417..d89678b 100644 --- a/framework/platform-dx/rendertarget.cpp +++ b/framework/platform-dx/rendertarget.cpp @@ -37,8 +37,10 @@ namespace xna { Exception::Throw(Exception::FAILED_TO_APPLY); } - if(!render_impl->_renderTargetView) - Exception::Throw(Exception::INVALID_OPERATION); + if (!render_impl->_renderTargetView) + { + Initialize(); + } auto& context = m_device->impl->_context; context->OMSetRenderTargets(1, render_impl->_renderTargetView.GetAddressOf(), nullptr); From 3c4a25e27ebb929e170e169767050355e5794112 Mon Sep 17 00:00:00 2001 From: Danilo Date: Fri, 2 Aug 2024 22:10:40 -0300 Subject: [PATCH 10/10] Implementa Format e LevelCount em Texture --- framework/platform-dx/texture.cpp | 13 ++++++------ inc/xna/default.hpp | 2 ++ inc/xna/graphics/texture.hpp | 33 +++++++++++++++++++++---------- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/framework/platform-dx/texture.cpp b/framework/platform-dx/texture.cpp index 3f54d5a..2d74881 100644 --- a/framework/platform-dx/texture.cpp +++ b/framework/platform-dx/texture.cpp @@ -37,7 +37,7 @@ namespace xna { return texture2d; } - bool Texture2D::Initialize() + void Texture2D::Initialize() { if (!m_device || !m_device->impl->_device) { Exception::Throw(Exception::UNABLE_TO_INITIALIZE); @@ -62,7 +62,8 @@ namespace xna { Exception::Throw(Exception::FAILED_TO_CREATE); } - return true; + surfaceFormat = DxHelpers::SurfaceFormatToXna(impl->dxDescription.Format); + levelCount = static_cast(impl->dxShaderDescription.Texture2D.MipLevels); } void setDefaultDesc(Texture2D::PlatformImplementation& impl) { @@ -79,24 +80,24 @@ namespace xna { impl.dxShaderDescription.Texture2D.MostDetailedMip = 0; } - Texture2D::Texture2D() : GraphicsResource(nullptr) { + Texture2D::Texture2D() : Texture(nullptr) { impl = unew(); setDefaultDesc(*impl); } - Texture2D::Texture2D(sptr const& device, size_t width, size_t height) : GraphicsResource(device) { + Texture2D::Texture2D(sptr const& device, size_t width, size_t height) : Texture(device) { impl = unew(); setDefaultDesc(*impl); impl->dxDescription.Width = static_cast(width); impl->dxDescription.Height = static_cast(height); } - Texture2D::Texture2D(sptr const& device) : GraphicsResource(device) { + Texture2D::Texture2D(sptr const& device) : Texture(device) { impl = unew(); setDefaultDesc(*impl); } - Texture2D::Texture2D(sptr const& device, size_t width, size_t height, size_t mipMap, SurfaceFormat format) : GraphicsResource(device) + Texture2D::Texture2D(sptr const& device, size_t width, size_t height, size_t mipMap, SurfaceFormat format) : Texture(device) { impl = unew(); setDefaultDesc(*impl); diff --git a/inc/xna/default.hpp b/inc/xna/default.hpp index 4725e7a..9d2c563 100644 --- a/inc/xna/default.hpp +++ b/inc/xna/default.hpp @@ -202,6 +202,8 @@ namespace xna { using P_RasterizerState = sptr; using P_PresentationParameters = sptr; using P_SamplerStateCollection = sptr; + using P_Texture = sptr; + using P_Texture2D = sptr; } diff --git a/inc/xna/graphics/texture.hpp b/inc/xna/graphics/texture.hpp index e5afae4..6d6a00c 100644 --- a/inc/xna/graphics/texture.hpp +++ b/inc/xna/graphics/texture.hpp @@ -5,28 +5,41 @@ #include "gresource.hpp" namespace xna { - class Texture { + //Represents a texture resource. + class Texture : public GraphicsResource { public: - ~Texture() {} + Texture(P_GraphicsDevice const& graphicsDevice) : GraphicsResource(graphicsDevice) {} + + virtual ~Texture() {} + + //Gets the format of the texture data. + constexpr SurfaceFormat Format() const { return surfaceFormat; } + //Gets the number of texture levels in a multilevel texture. + constexpr Int LevelCount() const { return levelCount; } + + protected: + SurfaceFormat surfaceFormat{SurfaceFormat::Color}; + Int levelCount{ 0 }; }; - class Texture2D : public Texture, public GraphicsResource { + class Texture2D : public Texture { public: Texture2D(); - Texture2D(sptr const& device); - Texture2D(sptr const& device, size_t width, size_t height); - Texture2D(sptr const& device, size_t width, size_t height, size_t mipMap, SurfaceFormat format); + Texture2D(P_GraphicsDevice const& device); + Texture2D(P_GraphicsDevice const& device, size_t width, size_t height); + Texture2D(P_GraphicsDevice const& device, size_t width, size_t height, size_t mipMap, SurfaceFormat format); ~Texture2D() override; Int Width() const; Int Height() const; Rectangle Bounds() const; - bool Initialize(); void SetData(std::vector const& data, size_t startIndex = 0, size_t elementCount = 0); void SetData(std::vector const& data, size_t startIndex = 0, size_t elementCount = 0); void SetData(std::vector const& data, size_t startIndex = 0, size_t elementCount = 0); void SetData(Int level, Rectangle* rect, std::vector const& data, size_t startIndex, size_t elementCount); - static sptr FromStream(GraphicsDevice& device, String const& fileName); - static sptr FromMemory(GraphicsDevice& device, std::vector const& data); + static P_Texture2D FromStream(GraphicsDevice& device, String const& fileName); + static P_Texture2D FromMemory(GraphicsDevice& device, std::vector const& data); + + void Initialize(); public: struct PlatformImplementation; @@ -34,7 +47,7 @@ namespace xna { }; - using PTexture2D = sptr; + using PTexture2D = P_Texture2D; } #endif \ No newline at end of file