mirror of
https://github.com/borgesdan/xn65
synced 2024-12-29 21:54:47 +01:00
Implementa SpriteFont
This commit is contained in:
parent
a9aa7b4ae8
commit
10d4f15b1f
@ -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)
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include "types.hpp"
|
||||
#include "forward.hpp"
|
||||
#include "xnaerror.hpp"
|
||||
#include "enums.hpp"
|
||||
#include "enums.hpp"
|
||||
#include "helpers.hpp"
|
@ -117,6 +117,8 @@ namespace xna {
|
||||
using PShader = std::shared_ptr<Shader>;
|
||||
class SpriteBatch;
|
||||
using PSpriteBatch = std::shared_ptr<SpriteBatch>;
|
||||
class SpriteFont;
|
||||
using PSpriteFont = std::shared_ptr<SpriteFont>;
|
||||
class VertexBuffer;
|
||||
using PVertexBuffer = std::shared_ptr<VertexBuffer>;
|
||||
class VertexInputLayout;
|
||||
|
@ -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;
|
||||
};
|
||||
}
|
||||
|
||||
|
15
framework/graphics/spritefont.hpp
Normal file
15
framework/graphics/spritefont.hpp
Normal 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
|
@ -4,7 +4,7 @@
|
||||
#include <string>
|
||||
|
||||
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;
|
||||
|
@ -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;
|
||||
|
@ -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<DxSpriteEffects>(effects);
|
||||
const DxSpriteEffects _effects = static_cast<DxSpriteEffects>(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<DxSpriteEffects>(effects);
|
||||
const auto _effects = static_cast<DxSpriteEffects>(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<DxSpriteEffects>(effects);
|
||||
const auto _effects = static_cast<DxSpriteEffects>(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<DxSpriteEffects>(effects);
|
||||
|
||||
spriteFont._dxSpriteFont->DrawString(
|
||||
_dxspriteBatch.get(),
|
||||
text.c_str(),
|
||||
_position,
|
||||
_color,
|
||||
rotation,
|
||||
_origin,
|
||||
scale,
|
||||
_effects,
|
||||
layerDepth
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -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<DirectX::SpriteBatch> _dxspriteBatch = nullptr;
|
||||
|
||||
static constexpr void ConvertSpriteSort(SpriteSortMode value, DirectX::SpriteSortMode& target) {
|
||||
switch (value)
|
||||
{
|
||||
@ -60,6 +66,9 @@ namespace xna {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
sptr<DirectX::SpriteBatch> _dxspriteBatch = nullptr;
|
||||
};
|
||||
}
|
||||
|
||||
|
24
framework/platform/spritefont-dx.cpp
Normal file
24
framework/platform/spritefont-dx.cpp
Normal 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;
|
||||
}
|
||||
}
|
23
framework/platform/spritefont-dx.hpp
Normal file
23
framework/platform/spritefont-dx.hpp
Normal 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
|
@ -11,7 +11,7 @@ namespace xna {
|
||||
{
|
||||
auto texture2d = New<Texture2D>();
|
||||
ID3D11Resource* resource = nullptr;
|
||||
auto wstr = StringToWString(fileName);
|
||||
auto wstr = XnaHToWString(fileName);
|
||||
|
||||
HRESULT result = DirectX::CreateWICTextureFromFile(
|
||||
device._device,
|
||||
|
@ -18,8 +18,9 @@ public:
|
||||
graphics = New<GraphicsDeviceManager>(this);
|
||||
|
||||
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 {
|
||||
@ -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) {
|
||||
|
@ -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.
|
||||
|
Loading…
x
Reference in New Issue
Block a user