From 2518d7584d7a81d5476c7eb37eba2e98828ff448 Mon Sep 17 00:00:00 2001 From: Danilo Date: Sat, 27 Apr 2024 15:21:47 -0300 Subject: [PATCH] =?UTF-8?q?Implementa=C3=A7=C3=B5es=20em=20GameComponent?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/game/component.cpp | 51 ++++++++++++++++++++++- framework/game/component.hpp | 74 ++++++++++++---------------------- framework/game/game.hpp | 1 - framework/platform/game-dx.cpp | 61 +++++++++++++++------------- framework/platform/game-dx.hpp | 7 ++-- 5 files changed, 113 insertions(+), 81 deletions(-) diff --git a/framework/game/component.cpp b/framework/game/component.cpp index cb36050..f3efee3 100644 --- a/framework/game/component.cpp +++ b/framework/game/component.cpp @@ -1,4 +1,53 @@ #include "component.hpp" -namespace xna { +namespace xna { + sptr GameComponentCollection::operator[](size_t index) const + { + if (index >= components.size()) + return nullptr; + + return components[index]; + } + + sptr GameComponentCollection::At(size_t index) const + { + if (index >= components.size()) + return nullptr; + + return components[index]; + } + + bool GameComponentCollection::UpdateOrderComparer(sptr const& x, sptr const& y) + { + auto comp1 = std::reinterpret_pointer_cast(x); + auto comp2 = std::reinterpret_pointer_cast(y); + + if (!comp1 && !comp2) + return false; + + if (!comp1) + return true; + + if (!comp2) + return false; + + return comp1->UpdateOrder() < comp2->UpdateOrder(); + } + + bool GameComponentCollection::DrawOrderComparer(sptr const& x, sptr const& y) + { + auto comp1 = std::reinterpret_pointer_cast(x); + auto comp2 = std::reinterpret_pointer_cast(y); + + if (!comp1 && !comp2) + return false; + + if (!comp1) + return true; + + if (!comp2) + return false; + + return comp1->DrawOrder() < comp2->DrawOrder(); + } } \ No newline at end of file diff --git a/framework/game/component.hpp b/framework/game/component.hpp index cab3087..2c9a628 100644 --- a/framework/game/component.hpp +++ b/framework/game/component.hpp @@ -104,84 +104,60 @@ namespace xna { class GameComponentCollection { public: - void InsertItem(size_t index, sptr const& item) { + constexpr void InsertItem(size_t index, sptr const& item) { const auto it = components.begin(); components.insert(it + index, item); - std::sort(components.begin(), components.end(), UpdateOrderComparer); + if (AutoSort) + std::sort(components.begin(), components.end(), UpdateOrderComparer); } - void RemoveItem(size_t index) { + constexpr void RemoveItem(size_t index) { if (index >= components.size()) return; const auto it = components.begin(); components.erase(it + index); - std::sort(components.begin(), components.end(), UpdateOrderComparer); + if (AutoSort) + std::sort(components.begin(), components.end(), UpdateOrderComparer); } - void ClearItems() { + constexpr void ClearItems() { components.clear(); } - void Add(sptr const& item) { + constexpr void Add(sptr const& item) { components.push_back(item); - std::sort(components.begin(), components.end(), UpdateOrderComparer); + if(AutoSort) std::sort(components.begin(), components.end(), UpdateOrderComparer); } constexpr size_t Count() const { return components.size(); } - sptr operator[](size_t index) const { - if (index >= components.size()) - return nullptr; + sptr operator[](size_t index) const; - return components[index]; + sptr At(size_t index) const; + + constexpr void Sort() { + std::sort(components.begin(), components.end(), UpdateOrderComparer); + } + + static bool UpdateOrderComparer(sptr const& x, sptr const& y); + static bool DrawOrderComparer(sptr const& x, sptr const& y); + + static constexpr void UpdateSort(std::vector>& components) { + std::sort(components.begin(), components.end(), UpdateOrderComparer); } - sptr At(size_t index) const { - if (index >= components.size()) - return nullptr; - - return components[index]; - } - - static bool UpdateOrderComparer(sptr const& x, sptr const& y) { - auto comp1 = std::reinterpret_pointer_cast(x); - auto comp2 = std::reinterpret_pointer_cast(y); - - if (!comp1 && !comp2) - return false; - - if (!comp1) - return true; - - if (!comp2) - return false; - - return comp1->UpdateOrder() < comp2->UpdateOrder(); - } - - static bool DrawOrderComparer(sptr const& x, sptr const& y) { - auto comp1 = std::reinterpret_pointer_cast(x); - auto comp2 = std::reinterpret_pointer_cast(y); - - if (!comp1 && !comp2) - return false; - - if (!comp1) - return true; - - if (!comp2) - return false; - - return comp1->UpdateOrder() < comp2->UpdateOrder(); + static constexpr void DrawSort(std::vector>& components) { + std::sort(components.begin(), components.end(), DrawOrderComparer); } public: - std::vector> components; + std::vector> components; + bool AutoSort{ false }; }; } diff --git a/framework/game/game.hpp b/framework/game/game.hpp index de2c48a..936335a 100644 --- a/framework/game/game.hpp +++ b/framework/game/game.hpp @@ -18,7 +18,6 @@ namespace xna { virtual sptr Window() = 0; virtual sptr GetGraphicsDevice() = 0; virtual sptr Components() = 0; - virtual void DisableGameComponets(bool value) = 0; protected: virtual void Draw(GameTime const& gameTime) = 0; diff --git a/framework/platform/game-dx.cpp b/framework/platform/game-dx.cpp index 8a92e1c..3121df2 100644 --- a/framework/platform/game-dx.cpp +++ b/framework/platform/game-dx.cpp @@ -36,28 +36,7 @@ namespace xna { } return startLoop(); - } - - void Game::Draw(GameTime const& gameTime) { - if (!_disableGameComponent) { - std::sort(_drawableGameComponents.begin(), _drawableGameComponents.end(), GameComponentCollection::DrawOrderComparer); - - for (size_t i = 0; i < _drawableGameComponents.size(); ++i) { - auto& component = _drawableGameComponents[i]; - - if (!component) continue; - - auto drawable = reinterpret_pointer_cast(component); - - if(drawable && drawable->Visible()) - drawable->Draw(gameTime); - } - - _drawableGameComponents.clear(); - } - - _graphicsDevice->Present(); - } + } void Game::Initialize() { Keyboard::Initialize(); @@ -82,23 +61,51 @@ namespace xna { LoadContent(); } + void Game::Draw(GameTime const& gameTime) { + if (_enabledGameComponents && !_drawableGameComponents.empty()) { + const auto count = _drawableGameComponents.size(); + + if (count != _drawableGameComponentsCount && _gameComponents->AutoSort) { + GameComponentCollection::DrawSort(_drawableGameComponents); + _drawableGameComponentsCount = count; + } + + for (size_t i = 0; i < count; ++i) { + auto& component = _drawableGameComponents[i]; + + if (!component) continue; + + auto drawable = reinterpret_pointer_cast(component); + + if (drawable && drawable->Visible()) + drawable->Draw(gameTime); + } + + _drawableGameComponents.clear(); + } + + _graphicsDevice->Present(); + } + void Game::Update(GameTime const& gameTime) { _audioEngine->Update(); - if (!_disableGameComponent) { - for (size_t i = 0; i < _gameComponents->Count(); ++i) { + if (_enabledGameComponents && _gameComponents->Count() > 0) { + const auto count = _gameComponents->Count(); + for (size_t i = 0; i < count; ++i) { auto component = _gameComponents->At(i); if (!component) continue; - if (component->Type() == GameComponentType::Drawable) - _drawableGameComponents.push_back(component); + if (component->Type() == GameComponentType::Drawable) { + _drawableGameComponents.push_back(component); + } auto updatable = reinterpret_pointer_cast(component); if(updatable && updatable->Enabled()) updatable->Update(gameTime); - } + } } } diff --git a/framework/platform/game-dx.hpp b/framework/platform/game-dx.hpp index a4d262c..769cea5 100644 --- a/framework/platform/game-dx.hpp +++ b/framework/platform/game-dx.hpp @@ -30,8 +30,8 @@ namespace xna { return _gameComponents; } - void DisableGameComponets(bool value) override { - _disableGameComponent = value; + constexpr void EnableGameComponents(bool value) { + _enabledGameComponents = value; } protected: @@ -58,7 +58,8 @@ namespace xna { void step(); sptr _gameComponents = nullptr; std::vector> _drawableGameComponents; - bool _disableGameComponent{ false }; + Uint _drawableGameComponentsCount{ 0 }; + bool _enabledGameComponents{ false }; }; }