diff --git a/framework/CMakeLists.txt b/framework/CMakeLists.txt index c363d0b..230ff80 100644 --- a/framework/CMakeLists.txt +++ b/framework/CMakeLists.txt @@ -3,7 +3,7 @@ # # Add source to this project's executable. -add_executable (xna WIN32 "xna.cpp" "xna.h" "platform/window-dx.cpp" "platform/device-dx.cpp" "platform/adapter-dx.cpp" "platform/swapchain-dx.cpp" "platform/rendertarget-dx.cpp" "platform/texture-dx.cpp" "platform/blendstate-dx.cpp" "platform/game-dx.cpp" "platform/clock-dx.cpp" "csharp/stream.cpp" "platform/gdevicemanager-dx.cpp" "platform/vertexinput-dx.cpp" "platform/shader-dx.cpp" "platform/rasterizerstate-dx.cpp" "platform/vertexbuffer-dx.cpp" "platform/indexbuffer-dx.cpp" "common/matrix.cpp" "platform/constbuffer-dx.cpp" "platform/databuffer-dx.cpp" "platform/samplerstate-dx.cpp" "platform/spritebatch-dx.cpp") +add_executable (xna WIN32 "xna.cpp" "xna.h" "platform/window-dx.cpp" "platform/device-dx.cpp" "platform/adapter-dx.cpp" "platform/swapchain-dx.cpp" "platform/rendertarget-dx.cpp" "platform/texture-dx.cpp" "platform/blendstate-dx.cpp" "platform/game-dx.cpp" "platform/clock-dx.cpp" "csharp/stream.cpp" "platform/gdevicemanager-dx.cpp" "platform/vertexinput-dx.cpp" "platform/shader-dx.cpp" "platform/rasterizerstate-dx.cpp" "platform/vertexbuffer-dx.cpp" "platform/indexbuffer-dx.cpp" "common/matrix.cpp" "platform/constbuffer-dx.cpp" "platform/databuffer-dx.cpp" "platform/samplerstate-dx.cpp" "platform/spritebatch-dx.cpp" "platform/spritefont-dx.cpp") if (CMAKE_VERSION VERSION_GREATER 3.12) set_property(TARGET xna PROPERTY CXX_STANDARD 20) diff --git a/framework/default.hpp b/framework/default.hpp index 00c72d8..fa6b06c 100644 --- a/framework/default.hpp +++ b/framework/default.hpp @@ -1,4 +1,5 @@ #include "types.hpp" #include "forward.hpp" #include "xnaerror.hpp" -#include "enums.hpp" \ No newline at end of file +#include "enums.hpp" +#include "helpers.hpp" \ No newline at end of file diff --git a/framework/forward.hpp b/framework/forward.hpp index 7c45d06..eb03b98 100644 --- a/framework/forward.hpp +++ b/framework/forward.hpp @@ -117,6 +117,8 @@ namespace xna { using PShader = std::shared_ptr; class SpriteBatch; using PSpriteBatch = std::shared_ptr; + class SpriteFont; + using PSpriteFont = std::shared_ptr; class VertexBuffer; using PVertexBuffer = std::shared_ptr; class VertexInputLayout; diff --git a/framework/graphics/spritebatch.hpp b/framework/graphics/spritebatch.hpp index 1ca8fa4..e0b7be4 100644 --- a/framework/graphics/spritebatch.hpp +++ b/framework/graphics/spritebatch.hpp @@ -31,6 +31,9 @@ namespace xna { virtual void Draw(Texture2D& texture, Rectangle const& destinationRectangle, Rectangle const* sourceRectangle, Color const& color, float rotation, Vector2 const& origin, SpriteEffects effects, float layerDepth) = 0; virtual void Viewport(xna::Viewport const& value) = 0; + virtual void DrawString(SpriteFont& spriteFont, String const& text, Vector2 const& position, Color const& color) = 0; + virtual void DrawString(SpriteFont& spriteFont, String const& text, Vector2 const& position, Color const& color, + float rotation, Vector2 const& origin, float scale, SpriteEffects effects, float layerDepth) = 0; }; } diff --git a/framework/graphics/spritefont.hpp b/framework/graphics/spritefont.hpp new file mode 100644 index 0000000..f640196 --- /dev/null +++ b/framework/graphics/spritefont.hpp @@ -0,0 +1,15 @@ +#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/framework/helpers.hpp b/framework/helpers.hpp index 076fa25..aba54eb 100644 --- a/framework/helpers.hpp +++ b/framework/helpers.hpp @@ -4,7 +4,7 @@ #include namespace xna { - inline std::wstring StringToWString(const std::string& str) + inline std::wstring XnaHToWString(const std::string& str) { std::wstring wstr; size_t size; @@ -13,7 +13,7 @@ namespace xna { return wstr; } - inline std::string WStringToString(const std::wstring& wstr) + inline std::string XnaHToString(const std::wstring& wstr) { std::string str; size_t size; diff --git a/framework/platform/adapter-dx.cpp b/framework/platform/adapter-dx.cpp index 7b9e836..b3a970c 100644 --- a/framework/platform/adapter-dx.cpp +++ b/framework/platform/adapter-dx.cpp @@ -16,7 +16,7 @@ namespace xna { String GraphicsAdapter::Description() const { DXGI_ADAPTER_DESC1 desc; _adapter->GetDesc1(&desc); - String description = WStringToString(desc.Description); + String description = XnaHToString(desc.Description); return description; } @@ -33,7 +33,7 @@ namespace xna { if (_adapter->EnumOutputs(0, &pOutput) != DXGI_ERROR_NOT_FOUND) { pOutput->GetDesc(&outputDesc); - String deviceName = WStringToString(outputDesc.DeviceName); + String deviceName = XnaHToString(outputDesc.DeviceName); pOutput->Release(); pOutput = nullptr; diff --git a/framework/platform/spritebatch-dx.cpp b/framework/platform/spritebatch-dx.cpp index b8b924f..c348c67 100644 --- a/framework/platform/spritebatch-dx.cpp +++ b/framework/platform/spritebatch-dx.cpp @@ -1,13 +1,9 @@ -#include "spritebatch-dx.hpp" -#include "device-dx.hpp" #include "blendstate-dx.hpp" -#include "samplerstate-dx.hpp" +#include "device-dx.hpp" #include "rasterizerstate-dx.hpp" -#include "../common/color.hpp" +#include "samplerstate-dx.hpp" +#include "spritebatch-dx.hpp" #include "texture-dx.hpp" -#include "../common/vectors.hpp" -#include "../common/rectangle.hpp" -#include "../graphics/viewport.hpp" using DxSpriteBatch = DirectX::SpriteBatch; using DxSpriteSortMode = DirectX::SpriteSortMode; @@ -66,9 +62,9 @@ namespace xna { if (!texture._textureView) return; - auto _position = XMFLOAT2(position.X, position.Y); + const auto _position = XMFLOAT2(position.X, position.Y); const auto v4 = color.ToVector4(); - XMVECTORF32 _color = { { { v4.X, v4.Y, v4.Z, v4.W } } }; + XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W }; _dxspriteBatch->Draw( texture._textureView, @@ -84,9 +80,9 @@ namespace xna { if (!texture._textureView) return; - auto _position = XMFLOAT2(position.X, position.Y); + const auto _position = XMFLOAT2(position.X, position.Y); const auto v4 = color.ToVector4(); - XMVECTORF32 _color = { { { v4.X, v4.Y, v4.Z, v4.W } } }; + const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W }; RECT _sourceRect{}; @@ -111,10 +107,10 @@ namespace xna { if (!texture._textureView) return; - auto _position = XMFLOAT2(position.X, position.Y); - auto _origin = XMFLOAT2(origin.X, origin.Y); + const auto _position = XMFLOAT2(position.X, position.Y); + const auto _origin = XMFLOAT2(origin.X, origin.Y); const auto v4 = color.ToVector4(); - XMVECTORF32 _color = { { { v4.X, v4.Y, v4.Z, v4.W } } }; + const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W }; RECT _sourceRect{}; @@ -125,7 +121,7 @@ namespace xna { _sourceRect.bottom = sourceRectangle->Y + sourceRectangle->Height; }; - DxSpriteEffects _effects = static_cast(effects); + const DxSpriteEffects _effects = static_cast(effects); _dxspriteBatch->Draw( texture._textureView, @@ -146,10 +142,10 @@ namespace xna { if (!texture._textureView) return; - auto _position = XMFLOAT2(position.X, position.Y); - auto _origin = XMFLOAT2(origin.X, origin.Y); + const auto _position = XMFLOAT2(position.X, position.Y); + const auto _origin = XMFLOAT2(origin.X, origin.Y); const auto v4 = color.ToVector4(); - XMVECTORF32 _color = { { { v4.X, v4.Y, v4.Z, v4.W } } }; + const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W }; RECT _sourceRect{}; @@ -160,7 +156,7 @@ namespace xna { _sourceRect.bottom = sourceRectangle->Y + sourceRectangle->Height; }; - DxSpriteEffects _effects = static_cast(effects); + const auto _effects = static_cast(effects); const XMFLOAT2 _scale = { scale.X, scale.Y }; _dxspriteBatch->Draw( @@ -189,7 +185,7 @@ namespace xna { _destinationRect.bottom = destinationRectangle.Y + destinationRectangle.Height; const auto v4 = color.ToVector4(); - XMVECTORF32 _color = { { { v4.X, v4.Y, v4.Z, v4.W } } }; + const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W }; _dxspriteBatch->Draw(texture._textureView, _destinationRect, _color); } @@ -208,7 +204,7 @@ namespace xna { _destinationRect.bottom = destinationRectangle.Y + destinationRectangle.Height; const auto v4 = color.ToVector4(); - XMVECTORF32 _color = { { { v4.X, v4.Y, v4.Z, v4.W } } }; + const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W }; RECT _sourceRect{}; @@ -236,7 +232,7 @@ namespace xna { _destinationRect.bottom = destinationRectangle.Y + destinationRectangle.Height; const auto v4 = color.ToVector4(); - XMVECTORF32 _color = { { { v4.X, v4.Y, v4.Z, v4.W } } }; + const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W }; RECT _sourceRect{}; @@ -248,7 +244,7 @@ namespace xna { }; auto _origin = XMFLOAT2(origin.X, origin.Y); - DxSpriteEffects _effects = static_cast(effects); + const auto _effects = static_cast(effects); _dxspriteBatch->Draw( texture._textureView, @@ -275,4 +271,44 @@ namespace xna { _dxspriteBatch->SetViewport(_view); } + + void SpriteBatch::DrawString(SpriteFont& spriteFont, String const& text, Vector2 const& position, Color const& color) { + if (!_dxspriteBatch || !spriteFont._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( + _dxspriteBatch.get(), + text.c_str(), + _position, + _color + ); + } + + 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 (!_dxspriteBatch || !spriteFont._dxSpriteFont) + return; + + const auto _position = XMFLOAT2(position.X, position.Y); + const auto _origin = XMFLOAT2(origin.X, origin.Y); + const auto v4 = color.ToVector4(); + const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W }; + const auto _effects = static_cast(effects); + + spriteFont._dxSpriteFont->DrawString( + _dxspriteBatch.get(), + text.c_str(), + _position, + _color, + rotation, + _origin, + scale, + _effects, + layerDepth + ); + } } diff --git a/framework/platform/spritebatch-dx.hpp b/framework/platform/spritebatch-dx.hpp index 44f170e..9f22dcd 100644 --- a/framework/platform/spritebatch-dx.hpp +++ b/framework/platform/spritebatch-dx.hpp @@ -1,8 +1,13 @@ #ifndef XNA_PLATFORM_SPRITEBATCH_DX_HPP #define XNA_PLATFORM_SPRITEBATCH_DX_HPP +#include "../common/color.hpp" +#include "../common/rectangle.hpp" +#include "../common/vectors.hpp" #include "../graphics/spritebatch.hpp" +#include "../graphics/viewport.hpp" #include "SpriteBatch.h" +#include "spritefont-dx.hpp" namespace xna { class SpriteBatch : public ISpriteBatch { @@ -32,11 +37,12 @@ namespace xna { virtual void Draw(Texture2D& texture, Rectangle const& destinationRectangle, Rectangle const* sourceRectangle, Color const& color) override; virtual void Draw(Texture2D& texture, Rectangle const& destinationRectangle, Rectangle const* sourceRectangle, Color const& color, float rotation, Vector2 const& origin, SpriteEffects effects, float layerDepth) override; - virtual void Viewport(xna::Viewport const& value) override; + virtual void Viewport(xna::Viewport const& value) override; + virtual void DrawString(SpriteFont& spriteFont, String const& text, Vector2 const& position, Color const& color) override; + virtual void DrawString(SpriteFont& spriteFont, String const& text, Vector2 const& position, Color const& color, + float rotation, Vector2 const& origin, float scale, SpriteEffects effects, float layerDepth) override; public: - sptr _dxspriteBatch = nullptr; - static constexpr void ConvertSpriteSort(SpriteSortMode value, DirectX::SpriteSortMode& target) { switch (value) { @@ -60,6 +66,9 @@ namespace xna { break; } } + + public: + sptr _dxspriteBatch = nullptr; }; } diff --git a/framework/platform/spritefont-dx.cpp b/framework/platform/spritefont-dx.cpp new file mode 100644 index 0000000..1ab2643 --- /dev/null +++ b/framework/platform/spritefont-dx.cpp @@ -0,0 +1,24 @@ +#include "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/framework/platform/spritefont-dx.hpp b/framework/platform/spritefont-dx.hpp new file mode 100644 index 0000000..14d00df --- /dev/null +++ b/framework/platform/spritefont-dx.hpp @@ -0,0 +1,23 @@ +#ifndef XNA_PLATFORM_SPRITEFONT_DX_HPP +#define XNA_PLATFORM_SPRITEFONT_DX_HPP + +#include "../graphics/spritefont.hpp" +#include "../common/vectors.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/framework/platform/texture-dx.cpp b/framework/platform/texture-dx.cpp index 74470e1..5c54814 100644 --- a/framework/platform/texture-dx.cpp +++ b/framework/platform/texture-dx.cpp @@ -11,7 +11,7 @@ namespace xna { { auto texture2d = New(); ID3D11Resource* resource = nullptr; - auto wstr = StringToWString(fileName); + auto wstr = XnaHToWString(fileName); HRESULT result = DirectX::CreateWICTextureFromFile( device._device, diff --git a/framework/xna.cpp b/framework/xna.cpp index 42678a8..88ba3e6 100644 --- a/framework/xna.cpp +++ b/framework/xna.cpp @@ -18,8 +18,9 @@ public: graphics = New(this); XnaErrorCode err; - Texture2D::FromStream(*_graphicsDevice, "D:\\sprite.jpg", &err); + texture = Texture2D::FromStream(*_graphicsDevice, "D:\\sprite.jpg", &err); + spriteBatch = New(*_graphicsDevice); } virtual void Update(GameTime const& gameTime) override { @@ -30,11 +31,17 @@ public: virtual void Draw(GameTime const& gameTime) override { _graphicsDevice->Clear(); + spriteBatch->Begin(); + spriteBatch->Draw(*texture, Vector2(20, 20), nullptr, Colors::White, 0, {0,0}, 0.5F, SpriteEffects::None, 0); + spriteBatch->End(); + Game::Draw(gameTime); } private: - PGraphicsDeviceManager graphics; + PGraphicsDeviceManager graphics = nullptr; + PSpriteBatch spriteBatch = nullptr; + PTexture2D texture = nullptr; }; int APIENTRY WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow) { diff --git a/framework/xna.h b/framework/xna.h index bca6a78..06cbcad 100644 --- a/framework/xna.h +++ b/framework/xna.h @@ -13,5 +13,7 @@ #include "csharp/stream.hpp" #include "platform/gdevicemanager-dx.hpp" #include "platform/texture-dx.hpp" +#include "platform/spritebatch-dx.hpp" +#include "common/color.hpp" // TODO: Reference additional headers your program requires here.