From d35b564dddbc2e24f46e2693e2f86010e535b366 Mon Sep 17 00:00:00 2001 From: Danilo Date: Sat, 18 May 2024 21:14:56 -0300 Subject: [PATCH] Implementa Sprite --- framework/CMakeLists.txt | 4 +- .../{spritebatch-dx.cpp => sprite-dx.cpp} | 40 +++++++++++++++---- framework/platform/spritefont-dx.cpp | 24 ----------- inc/graphics/{spritebatch.hpp => sprite.hpp} | 16 +++++++- inc/graphics/spritefont.hpp | 15 ------- inc/platform-dx/spritefont-dx.hpp | 23 ----------- inc/platform-dx/xna-dx.hpp | 1 - inc/xna.hpp | 3 +- 8 files changed, 50 insertions(+), 76 deletions(-) rename framework/platform/{spritebatch-dx.cpp => sprite-dx.cpp} (90%) delete mode 100644 framework/platform/spritefont-dx.cpp rename inc/graphics/{spritebatch.hpp => sprite.hpp} (83%) delete mode 100644 inc/graphics/spritefont.hpp delete mode 100644 inc/platform-dx/spritefont-dx.hpp diff --git a/framework/CMakeLists.txt b/framework/CMakeLists.txt index 56da810..be1e3aa 100644 --- a/framework/CMakeLists.txt +++ b/framework/CMakeLists.txt @@ -24,8 +24,8 @@ add_executable (xna WIN32 "platform/constbuffer-dx.cpp" "platform/databuffer-dx.cpp" "platform/samplerstate-dx.cpp" -"platform/spritebatch-dx.cpp" -"platform/spritefont-dx.cpp" +"platform/sprite-dx.cpp" + "platform/depthstencilstate-dx.cpp" "platform/keyboard-dx.cpp" "platform/mouse-dx.cpp" diff --git a/framework/platform/spritebatch-dx.cpp b/framework/platform/sprite-dx.cpp similarity index 90% rename from framework/platform/spritebatch-dx.cpp rename to framework/platform/sprite-dx.cpp index 835f53c..4d9be14 100644 --- a/framework/platform/spritebatch-dx.cpp +++ b/framework/platform/sprite-dx.cpp @@ -6,9 +6,8 @@ #include "platform-dx/texture-dx.hpp" #include "common/color.hpp" #include "common/numerics.hpp" -#include "graphics/spritebatch.hpp" +#include "graphics/sprite.hpp" #include "graphics/viewport.hpp" -#include "platform-dx/spritefont-dx.hpp" using DxSpriteBatch = DirectX::SpriteBatch; using DxSpriteSortMode = DirectX::SpriteSortMode; @@ -18,8 +17,35 @@ using DirectX::XMFLOAT2; using DirectX::FXMVECTOR; using DirectX::XMVECTORF32; using DirectX::GXMVECTOR; +using DxSpriteFont = DirectX::SpriteFont; namespace xna { + struct SpriteFont::PlatformImplementation { + sptr _dxSpriteFont = nullptr; + }; + + SpriteFont::SpriteFont(GraphicsDevice& device, String const& fontFileName) + { + const auto wString = XnaHToWString(fontFileName); + implementation = uNew(); + implementation->_dxSpriteFont = New(device._device, wString.c_str()); + } + + SpriteFont::~SpriteFont() {} + + Vector2 SpriteFont::MeasureString(String const& text, bool ignoreWhiteSpace) + { + if (!implementation->_dxSpriteFont) + return Vector2(); + + const auto size = implementation->_dxSpriteFont->MeasureString(text.c_str(), ignoreWhiteSpace); + Vector2 vec2{}; + vec2.X = size.m128_f32[0]; + vec2.Y = size.m128_f32[1]; + + return vec2; + } + struct SpriteBatch::PlatformImplementation { sptr _dxspriteBatch = nullptr; }; @@ -290,17 +316,17 @@ namespace xna { _view.MaxDepth = value.MaxDepth; implementation->_dxspriteBatch->SetViewport(_view); - } + } void SpriteBatch::DrawString(SpriteFont& spriteFont, String const& text, Vector2 const& position, Color const& color) { - if (!implementation->_dxspriteBatch || !spriteFont._dxSpriteFont) + if (!implementation->_dxspriteBatch || !spriteFont.implementation->_dxSpriteFont) return; const auto _position = XMFLOAT2(position.X, position.Y); const auto v4 = color.ToVector4(); const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W }; - spriteFont._dxSpriteFont->DrawString( + spriteFont.implementation->_dxSpriteFont->DrawString( implementation->_dxspriteBatch.get(), text.c_str(), _position, @@ -310,7 +336,7 @@ namespace xna { void SpriteBatch::DrawString(SpriteFont& spriteFont, String const& text, Vector2 const& position, Color const& color, float rotation, Vector2 const& origin, float scale, SpriteEffects effects, float layerDepth) { - if (!implementation->_dxspriteBatch || !spriteFont._dxSpriteFont) + if (!implementation->_dxspriteBatch || !spriteFont.implementation->_dxSpriteFont) return; const auto _position = XMFLOAT2(position.X, position.Y); @@ -319,7 +345,7 @@ namespace xna { const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W }; const auto _effects = static_cast(effects); - spriteFont._dxSpriteFont->DrawString( + spriteFont.implementation->_dxSpriteFont->DrawString( implementation->_dxspriteBatch.get(), text.c_str(), _position, diff --git a/framework/platform/spritefont-dx.cpp b/framework/platform/spritefont-dx.cpp deleted file mode 100644 index aeb2354..0000000 --- a/framework/platform/spritefont-dx.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "platform-dx/spritefont-dx.hpp" - -using DxSpriteFont = DirectX::SpriteFont; - -namespace xna { - SpriteFont::SpriteFont(GraphicsDevice& device, String const& fontFileName) - { - const auto wString = XnaHToWString(fontFileName); - _dxSpriteFont = New(device._device, wString.c_str()); - } - - Vector2 SpriteFont::MeasureString(String const& text, bool ignoreWhiteSpace) - { - if (!_dxSpriteFont) - return Vector2(); - - const auto size = _dxSpriteFont->MeasureString(text.c_str(), ignoreWhiteSpace); - Vector2 vec2{}; - vec2.X = size.m128_f32[0]; - vec2.Y = size.m128_f32[1]; - - return vec2; - } -} \ No newline at end of file diff --git a/inc/graphics/spritebatch.hpp b/inc/graphics/sprite.hpp similarity index 83% rename from inc/graphics/spritebatch.hpp rename to inc/graphics/sprite.hpp index 55c09f1..49053a5 100644 --- a/inc/graphics/spritebatch.hpp +++ b/inc/graphics/sprite.hpp @@ -1,8 +1,9 @@ -#ifndef XNA_GRAPHICS_SPRITEBATCH_HPP -#define XNA_GRAPHICS_SPRITEBATCH_HPP +#ifndef XNA_GRAPHICS_SPRITE_HPP +#define XNA_GRAPHICS_SPRITE_HPP #include "../default.hpp" #include "../common/numerics.hpp" +#include "common/color.hpp" namespace xna { class SpriteBatch { @@ -38,6 +39,17 @@ namespace xna { struct PlatformImplementation; uptr implementation = nullptr; }; + + class SpriteFont { + public: + SpriteFont(GraphicsDevice& device, String const& fontFileName); + ~SpriteFont(); + Vector2 MeasureString(String const& text, bool ignoreWhiteSpace = true); + + private: + struct PlatformImplementation; + uptr implementation = nullptr; + }; } #endif \ No newline at end of file diff --git a/inc/graphics/spritefont.hpp b/inc/graphics/spritefont.hpp deleted file mode 100644 index f640196..0000000 --- a/inc/graphics/spritefont.hpp +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef XNA_GRAPHICS_SPRITEFONT_HPP -#define XNA_GRAPHICS_SPRITEFONT_HPP - -#include "../default.hpp" - -namespace xna { - class ISpriteFont { - public: - virtual ~ISpriteFont(){} - - virtual Vector2 MeasureString(String const& text, bool ignoreWhiteSpace = true) = 0; - }; -} - -#endif \ No newline at end of file diff --git a/inc/platform-dx/spritefont-dx.hpp b/inc/platform-dx/spritefont-dx.hpp deleted file mode 100644 index eaa51ea..0000000 --- a/inc/platform-dx/spritefont-dx.hpp +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef XNA_PLATFORM_SPRITEFONT_DX_HPP -#define XNA_PLATFORM_SPRITEFONT_DX_HPP - -#include "../graphics/spritefont.hpp" -#include "../common/numerics.hpp" -#include "SpriteFont.h" -#include "device-dx.hpp" - -namespace xna { - class SpriteFont : public ISpriteFont { - public: - SpriteFont(GraphicsDevice& device, String const& fontFileName); - - virtual ~SpriteFont() override {} - - Vector2 MeasureString(String const& text, bool ignoreWhiteSpace = true) override; - - public: - sptr _dxSpriteFont = nullptr; - }; -} - -#endif \ No newline at end of file diff --git a/inc/platform-dx/xna-dx.hpp b/inc/platform-dx/xna-dx.hpp index 97712d9..07d5375 100644 --- a/inc/platform-dx/xna-dx.hpp +++ b/inc/platform-dx/xna-dx.hpp @@ -22,7 +22,6 @@ #include "samplerstate-dx.hpp" #include "shader-dx.hpp" #include "soundeffect-dx.hpp" -#include "spritefont-dx.hpp" #include "swapchain-dx.hpp" #include "texture-dx.hpp" #include "vertexbuffer-dx.hpp" diff --git a/inc/xna.hpp b/inc/xna.hpp index 29ba111..83b3ab0 100644 --- a/inc/xna.hpp +++ b/inc/xna.hpp @@ -46,8 +46,7 @@ #include "graphics/rendertarget.hpp" #include "graphics/samplerstate.hpp" #include "graphics/shader.hpp" -#include "graphics/spritebatch.hpp" -#include "graphics/spritefont.hpp" +#include "graphics/sprite.hpp" #include "graphics/swapchain.hpp" #include "graphics/texture.hpp" #include "graphics/vertexbuffer.hpp"