1
0
mirror of https://github.com/borgesdan/xn65 synced 2024-12-29 21:54:47 +01:00

Implementa SpriteFont

This commit is contained in:
Danilo 2024-04-14 21:23:09 -03:00
parent a9aa7b4ae8
commit 10d4f15b1f
14 changed files with 157 additions and 35 deletions

View File

@ -3,7 +3,7 @@
# #
# Add source to this project's executable. # 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) if (CMAKE_VERSION VERSION_GREATER 3.12)
set_property(TARGET xna PROPERTY CXX_STANDARD 20) set_property(TARGET xna PROPERTY CXX_STANDARD 20)

View File

@ -2,3 +2,4 @@
#include "forward.hpp" #include "forward.hpp"
#include "xnaerror.hpp" #include "xnaerror.hpp"
#include "enums.hpp" #include "enums.hpp"
#include "helpers.hpp"

View File

@ -117,6 +117,8 @@ namespace xna {
using PShader = std::shared_ptr<Shader>; using PShader = std::shared_ptr<Shader>;
class SpriteBatch; class SpriteBatch;
using PSpriteBatch = std::shared_ptr<SpriteBatch>; using PSpriteBatch = std::shared_ptr<SpriteBatch>;
class SpriteFont;
using PSpriteFont = std::shared_ptr<SpriteFont>;
class VertexBuffer; class VertexBuffer;
using PVertexBuffer = std::shared_ptr<VertexBuffer>; using PVertexBuffer = std::shared_ptr<VertexBuffer>;
class VertexInputLayout; class VertexInputLayout;

View File

@ -31,6 +31,9 @@ namespace xna {
virtual void Draw(Texture2D& texture, Rectangle const& destinationRectangle, Rectangle const* sourceRectangle, Color const& color, virtual void Draw(Texture2D& texture, Rectangle const& destinationRectangle, Rectangle const* sourceRectangle, Color const& color,
float rotation, Vector2 const& origin, SpriteEffects effects, float layerDepth) = 0; float rotation, Vector2 const& origin, SpriteEffects effects, float layerDepth) = 0;
virtual void Viewport(xna::Viewport const& value) = 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;
}; };
} }

View File

@ -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

View File

@ -4,7 +4,7 @@
#include <string> #include <string>
namespace xna { namespace xna {
inline std::wstring StringToWString(const std::string& str) inline std::wstring XnaHToWString(const std::string& str)
{ {
std::wstring wstr; std::wstring wstr;
size_t size; size_t size;
@ -13,7 +13,7 @@ namespace xna {
return wstr; return wstr;
} }
inline std::string WStringToString(const std::wstring& wstr) inline std::string XnaHToString(const std::wstring& wstr)
{ {
std::string str; std::string str;
size_t size; size_t size;

View File

@ -16,7 +16,7 @@ namespace xna {
String GraphicsAdapter::Description() const { String GraphicsAdapter::Description() const {
DXGI_ADAPTER_DESC1 desc; DXGI_ADAPTER_DESC1 desc;
_adapter->GetDesc1(&desc); _adapter->GetDesc1(&desc);
String description = WStringToString(desc.Description); String description = XnaHToString(desc.Description);
return description; return description;
} }
@ -33,7 +33,7 @@ namespace xna {
if (_adapter->EnumOutputs(0, &pOutput) != DXGI_ERROR_NOT_FOUND) { if (_adapter->EnumOutputs(0, &pOutput) != DXGI_ERROR_NOT_FOUND) {
pOutput->GetDesc(&outputDesc); pOutput->GetDesc(&outputDesc);
String deviceName = WStringToString(outputDesc.DeviceName); String deviceName = XnaHToString(outputDesc.DeviceName);
pOutput->Release(); pOutput->Release();
pOutput = nullptr; pOutput = nullptr;

View File

@ -1,13 +1,9 @@
#include "spritebatch-dx.hpp"
#include "device-dx.hpp"
#include "blendstate-dx.hpp" #include "blendstate-dx.hpp"
#include "samplerstate-dx.hpp" #include "device-dx.hpp"
#include "rasterizerstate-dx.hpp" #include "rasterizerstate-dx.hpp"
#include "../common/color.hpp" #include "samplerstate-dx.hpp"
#include "spritebatch-dx.hpp"
#include "texture-dx.hpp" #include "texture-dx.hpp"
#include "../common/vectors.hpp"
#include "../common/rectangle.hpp"
#include "../graphics/viewport.hpp"
using DxSpriteBatch = DirectX::SpriteBatch; using DxSpriteBatch = DirectX::SpriteBatch;
using DxSpriteSortMode = DirectX::SpriteSortMode; using DxSpriteSortMode = DirectX::SpriteSortMode;
@ -66,9 +62,9 @@ namespace xna {
if (!texture._textureView) if (!texture._textureView)
return; return;
auto _position = XMFLOAT2(position.X, position.Y); const auto _position = XMFLOAT2(position.X, position.Y);
const auto v4 = color.ToVector4(); 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( _dxspriteBatch->Draw(
texture._textureView, texture._textureView,
@ -84,9 +80,9 @@ namespace xna {
if (!texture._textureView) if (!texture._textureView)
return; return;
auto _position = XMFLOAT2(position.X, position.Y); const auto _position = XMFLOAT2(position.X, position.Y);
const auto v4 = color.ToVector4(); 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{}; RECT _sourceRect{};
@ -111,10 +107,10 @@ namespace xna {
if (!texture._textureView) if (!texture._textureView)
return; return;
auto _position = XMFLOAT2(position.X, position.Y); const auto _position = XMFLOAT2(position.X, position.Y);
auto _origin = XMFLOAT2(origin.X, origin.Y); const auto _origin = XMFLOAT2(origin.X, origin.Y);
const auto v4 = color.ToVector4(); 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{}; RECT _sourceRect{};
@ -125,7 +121,7 @@ namespace xna {
_sourceRect.bottom = sourceRectangle->Y + sourceRectangle->Height; _sourceRect.bottom = sourceRectangle->Y + sourceRectangle->Height;
}; };
DxSpriteEffects _effects = static_cast<DxSpriteEffects>(effects); const DxSpriteEffects _effects = static_cast<DxSpriteEffects>(effects);
_dxspriteBatch->Draw( _dxspriteBatch->Draw(
texture._textureView, texture._textureView,
@ -146,10 +142,10 @@ namespace xna {
if (!texture._textureView) if (!texture._textureView)
return; return;
auto _position = XMFLOAT2(position.X, position.Y); const auto _position = XMFLOAT2(position.X, position.Y);
auto _origin = XMFLOAT2(origin.X, origin.Y); const auto _origin = XMFLOAT2(origin.X, origin.Y);
const auto v4 = color.ToVector4(); 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{}; RECT _sourceRect{};
@ -160,7 +156,7 @@ namespace xna {
_sourceRect.bottom = sourceRectangle->Y + sourceRectangle->Height; _sourceRect.bottom = sourceRectangle->Y + sourceRectangle->Height;
}; };
DxSpriteEffects _effects = static_cast<DxSpriteEffects>(effects); const auto _effects = static_cast<DxSpriteEffects>(effects);
const XMFLOAT2 _scale = { scale.X, scale.Y }; const XMFLOAT2 _scale = { scale.X, scale.Y };
_dxspriteBatch->Draw( _dxspriteBatch->Draw(
@ -189,7 +185,7 @@ namespace xna {
_destinationRect.bottom = destinationRectangle.Y + destinationRectangle.Height; _destinationRect.bottom = destinationRectangle.Y + destinationRectangle.Height;
const auto v4 = color.ToVector4(); 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); _dxspriteBatch->Draw(texture._textureView, _destinationRect, _color);
} }
@ -208,7 +204,7 @@ namespace xna {
_destinationRect.bottom = destinationRectangle.Y + destinationRectangle.Height; _destinationRect.bottom = destinationRectangle.Y + destinationRectangle.Height;
const auto v4 = color.ToVector4(); 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{}; RECT _sourceRect{};
@ -236,7 +232,7 @@ namespace xna {
_destinationRect.bottom = destinationRectangle.Y + destinationRectangle.Height; _destinationRect.bottom = destinationRectangle.Y + destinationRectangle.Height;
const auto v4 = color.ToVector4(); 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{}; RECT _sourceRect{};
@ -248,7 +244,7 @@ namespace xna {
}; };
auto _origin = XMFLOAT2(origin.X, origin.Y); auto _origin = XMFLOAT2(origin.X, origin.Y);
DxSpriteEffects _effects = static_cast<DxSpriteEffects>(effects); const auto _effects = static_cast<DxSpriteEffects>(effects);
_dxspriteBatch->Draw( _dxspriteBatch->Draw(
texture._textureView, texture._textureView,
@ -275,4 +271,44 @@ namespace xna {
_dxspriteBatch->SetViewport(_view); _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<DxSpriteEffects>(effects);
spriteFont._dxSpriteFont->DrawString(
_dxspriteBatch.get(),
text.c_str(),
_position,
_color,
rotation,
_origin,
scale,
_effects,
layerDepth
);
}
} }

View File

@ -1,8 +1,13 @@
#ifndef XNA_PLATFORM_SPRITEBATCH_DX_HPP #ifndef XNA_PLATFORM_SPRITEBATCH_DX_HPP
#define 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/spritebatch.hpp"
#include "../graphics/viewport.hpp"
#include "SpriteBatch.h" #include "SpriteBatch.h"
#include "spritefont-dx.hpp"
namespace xna { namespace xna {
class SpriteBatch : public ISpriteBatch { class SpriteBatch : public ISpriteBatch {
@ -33,10 +38,11 @@ namespace xna {
virtual void Draw(Texture2D& texture, Rectangle const& destinationRectangle, Rectangle const* sourceRectangle, Color const& color, virtual void Draw(Texture2D& texture, Rectangle const& destinationRectangle, Rectangle const* sourceRectangle, Color const& color,
float rotation, Vector2 const& origin, SpriteEffects effects, float layerDepth) override; 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: public:
sptr<DirectX::SpriteBatch> _dxspriteBatch = nullptr;
static constexpr void ConvertSpriteSort(SpriteSortMode value, DirectX::SpriteSortMode& target) { static constexpr void ConvertSpriteSort(SpriteSortMode value, DirectX::SpriteSortMode& target) {
switch (value) switch (value)
{ {
@ -60,6 +66,9 @@ namespace xna {
break; break;
} }
} }
public:
sptr<DirectX::SpriteBatch> _dxspriteBatch = nullptr;
}; };
} }

View File

@ -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<DxSpriteFont>(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;
}
}

View File

@ -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<DirectX::SpriteFont> _dxSpriteFont = nullptr;
};
}
#endif

View File

@ -11,7 +11,7 @@ namespace xna {
{ {
auto texture2d = New<Texture2D>(); auto texture2d = New<Texture2D>();
ID3D11Resource* resource = nullptr; ID3D11Resource* resource = nullptr;
auto wstr = StringToWString(fileName); auto wstr = XnaHToWString(fileName);
HRESULT result = DirectX::CreateWICTextureFromFile( HRESULT result = DirectX::CreateWICTextureFromFile(
device._device, device._device,

View File

@ -18,8 +18,9 @@ public:
graphics = New<GraphicsDeviceManager>(this); graphics = New<GraphicsDeviceManager>(this);
XnaErrorCode err; XnaErrorCode err;
Texture2D::FromStream(*_graphicsDevice, "D:\\sprite.jpg", &err); texture = Texture2D::FromStream(*_graphicsDevice, "D:\\sprite.jpg", &err);
spriteBatch = New<SpriteBatch>(*_graphicsDevice);
} }
virtual void Update(GameTime const& gameTime) override { virtual void Update(GameTime const& gameTime) override {
@ -30,11 +31,17 @@ public:
virtual void Draw(GameTime const& gameTime) override { virtual void Draw(GameTime const& gameTime) override {
_graphicsDevice->Clear(); _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); Game::Draw(gameTime);
} }
private: 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) { int APIENTRY WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow) {

View File

@ -13,5 +13,7 @@
#include "csharp/stream.hpp" #include "csharp/stream.hpp"
#include "platform/gdevicemanager-dx.hpp" #include "platform/gdevicemanager-dx.hpp"
#include "platform/texture-dx.hpp" #include "platform/texture-dx.hpp"
#include "platform/spritebatch-dx.hpp"
#include "common/color.hpp"
// TODO: Reference additional headers your program requires here. // TODO: Reference additional headers your program requires here.