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

350 lines
11 KiB
C++
Raw Permalink Normal View History

2024-07-13 22:55:36 -03:00
#include "xna/xna-dx.hpp"
2024-06-27 16:08:42 -03:00
#include <functional>
2024-04-14 16:11:15 -03:00
using DxSpriteBatch = DirectX::SpriteBatch;
using DxSpriteSortMode = DirectX::SpriteSortMode;
using DxMatrix = DirectX::FXMMATRIX;
using DxSpriteEffects = DirectX::SpriteEffects;
using DirectX::XMFLOAT2;
using DirectX::FXMVECTOR;
using DirectX::XMVECTORF32;
using DirectX::GXMVECTOR;
2024-05-18 21:14:56 -03:00
using DxSpriteFont = DirectX::SpriteFont;
2024-05-30 17:37:40 -03:00
using DxGlyph = DirectX::SpriteFont::Glyph;
2024-04-14 16:11:15 -03:00
namespace xna {
2024-05-30 17:37:40 -03:00
SpriteFont::SpriteFont(
sptr<Texture2D> const& texture,
std::vector<Rectangle> const& glyphs,
std::vector<Rectangle> const& cropping,
2024-05-31 22:30:45 -03:00
std::vector<Char> const& charMap,
2024-05-30 17:37:40 -03:00
Int lineSpacing,
float spacing,
std::vector<Vector3> const& kerning,
2024-06-06 14:18:16 -03:00
std::optional<Char> const& defaultCharacter)
2024-05-18 21:14:56 -03:00
{
2024-07-16 09:33:57 -03:00
Exception::ThrowIfNull(texture, nameof(texture));
Exception::ThrowIfNull(texture->impl->dxShaderResource.Get(), nameof(texture->impl->dxShaderResource));
2024-05-30 17:37:40 -03:00
2024-06-06 14:18:16 -03:00
if(cropping.size() != glyphs.size() || charMap.size() != glyphs.size() || (!kerning.empty() && kerning.size() != glyphs.size()))
2024-07-16 09:33:57 -03:00
Exception::Throw("Cropping, charmap and kerning (if not empty) must all be the same size.");
2024-06-06 14:18:16 -03:00
2024-05-31 22:30:45 -03:00
std::vector<DxGlyph> dxGlyps(glyphs.size());
2024-05-30 17:37:40 -03:00
for (size_t i = 0; i < dxGlyps.size(); ++i) {
DxGlyph g;
2024-06-06 14:18:16 -03:00
g.Subrect.left = static_cast<LONG>(glyphs[i].Left());
g.Subrect.right = static_cast<LONG>(glyphs[i].Right());
g.Subrect.top = static_cast<LONG>(glyphs[i].Top());
g.Subrect.bottom = static_cast<LONG>(glyphs[i].Bottom());
2024-05-31 22:30:45 -03:00
g.Character = static_cast<Uint>(charMap[i]);
2024-06-06 14:18:16 -03:00
if (!kerning.empty()) {
g.XOffset = kerning[i].X;
g.YOffset = static_cast<float>(cropping[i].Y);
g.XAdvance = kerning[i].Z + spacing;
}
else {
g.XOffset = 0;
g.YOffset = 0;
g.XAdvance = spacing;
}
2024-05-31 22:30:45 -03:00
dxGlyps[i] = g;
}
impl = unew<PlatformImplementation>();
2024-07-16 09:33:57 -03:00
impl->dxSpriteFont = unew<DxSpriteFont>(
2024-05-30 17:37:40 -03:00
//ID3D11ShaderResourceView* texture
2024-06-25 17:06:37 -03:00
texture->impl->dxShaderResource.Get(),
2024-05-30 17:37:40 -03:00
//Glyph const* glyphs
dxGlyps.data(),
//size_t glyphCount
glyphs.size(),
//float lineSpacing
static_cast<float>(lineSpacing)
);
if (defaultCharacter.has_value()) {
const auto defChar = static_cast<wchar_t>(defaultCharacter.value());
2024-07-16 09:33:57 -03:00
impl->dxSpriteFont->SetDefaultCharacter(defChar);
2024-05-30 17:37:40 -03:00
}
2024-06-06 14:18:16 -03:00
}
2024-05-18 21:14:56 -03:00
2024-07-16 09:33:57 -03:00
Vector2 SpriteFont::MeasureString(String const& text, bool ignoreWhiteSpace) {
const auto size = impl->dxSpriteFont->MeasureString(text.c_str(), ignoreWhiteSpace);
2024-05-18 21:14:56 -03:00
Vector2 vec2{};
vec2.X = size.m128_f32[0];
vec2.Y = size.m128_f32[1];
return vec2;
2024-05-30 17:37:40 -03:00
}
2024-05-18 19:58:38 -03:00
2024-07-16 09:33:57 -03:00
Vector2 SpriteFont::MeasureString(WString const& text, bool ignoreWhiteSpace){
const auto size = impl->dxSpriteFont->MeasureString(text.c_str(), ignoreWhiteSpace);
2024-06-01 12:43:11 -03:00
Vector2 vec2{};
vec2.X = size.m128_f32[0];
vec2.Y = size.m128_f32[1];
return vec2;
}
2024-06-06 14:18:16 -03:00
Char SpriteFont::DefaultCharacter() const {
2024-07-16 09:33:57 -03:00
const auto defChar = impl->dxSpriteFont->GetDefaultCharacter();
2024-06-06 14:18:16 -03:00
return static_cast<Char>(defChar);
}
void SpriteFont::DefaultCharacter(Char value) {
const auto defChar = static_cast<wchar_t>(value);
2024-07-16 09:33:57 -03:00
impl->dxSpriteFont->SetDefaultCharacter(defChar);
2024-06-06 14:18:16 -03:00
}
Int SpriteFont::LineSpacing() const {
2024-07-16 09:33:57 -03:00
const auto space = impl->dxSpriteFont->GetLineSpacing();
2024-06-06 14:18:16 -03:00
return static_cast<Int>(space);
2024-05-18 19:58:38 -03:00
}
2024-07-16 09:33:57 -03:00
void SpriteFont::LineSpacing(float value) {
impl->dxSpriteFont->SetLineSpacing(value);
2024-06-06 14:18:16 -03:00
}
SpriteBatch::SpriteBatch(sptr<GraphicsDevice> const& device) : GraphicsResource(device) {
2024-07-16 09:33:57 -03:00
Exception::ThrowIfNull(device, nameof(device));
Exception::ThrowIfNull(device->impl->_context.Get(), nameof(device->impl->_context));
2024-04-25 14:51:33 -03:00
2024-06-27 16:08:42 -03:00
impl = unew<PlatformImplementation>();
2024-07-16 09:33:57 -03:00
impl->dxSpriteBatch = snew<DxSpriteBatch>(
2024-06-06 14:18:16 -03:00
//ID3D11DeviceContext* deviceContext
2024-06-25 17:06:37 -03:00
device->impl->_context.Get()
2024-06-06 14:18:16 -03:00
);
2024-04-14 16:11:15 -03:00
Viewport(device->Viewport());
2024-06-27 16:08:42 -03:00
}
2024-05-30 17:37:40 -03:00
2024-06-25 22:04:09 -03:00
void SpriteBatch::Begin(SpriteSortMode sortMode, BlendState* blendState, SamplerState* samplerState, DepthStencilState* depthStencil, RasterizerState* rasterizerState, Effect* effect, Matrix const& transformMatrix) {
std::function<void __cdecl()> effectFunc = nullptr;
2024-06-27 16:08:42 -03:00
2024-07-16 09:33:57 -03:00
//if Effect is not null set dxEffectBuffer and inputLayout
if (effect) {
bool dxEffectBufferChanged = false;
2024-06-27 16:08:42 -03:00
2024-07-16 09:33:57 -03:00
if (!impl->dxEffectBuffer || impl->dxEffectBuffer != effect->impl->dxEffect) {
impl->dxEffectBuffer = effect->impl->dxEffect;
dxEffectBufferChanged = true;
}
2024-06-27 16:08:42 -03:00
2024-07-16 09:33:57 -03:00
if (!impl->dxInputLayout || dxEffectBufferChanged) {
void const* shaderByteCode;
size_t byteCodeLength;
2024-06-27 16:08:42 -03:00
effect->impl->dxEffect->GetVertexShaderBytecode(&shaderByteCode, &byteCodeLength);
2024-06-27 16:08:42 -03:00
m_device->impl->_device->CreateInputLayout(
DirectX::VertexPositionColorTexture::InputElements,
DirectX::VertexPositionColorTexture::InputElementCount,
shaderByteCode, byteCodeLength,
impl->dxInputLayout.GetAddressOf());
}
auto& context = m_device->impl->_context;
effectFunc = [=] {
2024-07-16 09:33:57 -03:00
impl->dxEffectBuffer->Apply(context.Get());
context->IASetInputLayout(impl->dxInputLayout.Get());
};
}
2024-05-30 17:37:40 -03:00
2024-07-16 09:33:57 -03:00
auto _sortMode = DxHelpers::SpriteSortToDx(sortMode);
auto _transformMatrix = DxHelpers::MatrixToDx(transformMatrix);
impl->dxSpriteBatch->Begin(
_sortMode,
2024-06-25 17:06:37 -03:00
blendState ? blendState->impl->dxBlendState.Get() : nullptr,
samplerState ? samplerState->impl->_samplerState.Get() : nullptr,
depthStencil ? depthStencil->impl->dxDepthStencil.Get() : nullptr,
rasterizerState ? rasterizerState->impl->dxRasterizerState.Get() : nullptr,
effectFunc,
2024-07-16 09:33:57 -03:00
_transformMatrix
2024-04-14 16:11:15 -03:00
);
}
void SpriteBatch::End() {
2024-07-16 09:33:57 -03:00
impl->dxSpriteBatch->End();
2024-04-14 16:11:15 -03:00
}
void SpriteBatch::Draw(Texture2D& texture, Vector2 const& position, Color const& color) {
2024-07-16 09:33:57 -03:00
const auto _position = DxHelpers::VectorToDx(position);
2024-04-14 16:11:15 -03:00
const auto v4 = color.ToVector4();
2024-07-16 09:33:57 -03:00
const auto _color = DxHelpers::VectorToDx(v4);
2024-04-14 16:11:15 -03:00
2024-07-16 09:33:57 -03:00
impl->dxSpriteBatch->Draw(
2024-06-25 17:06:37 -03:00
texture.impl->dxShaderResource.Get(),
2024-04-14 16:11:15 -03:00
_position,
_color
);
}
2024-07-16 09:33:57 -03:00
void SpriteBatch::Draw(Texture2D& texture, Vector2 const& position, std::optional<Rectangle> const& sourceRectangle, Color const& color) {
const auto _position = DxHelpers::VectorToDx(position);
2024-04-14 16:11:15 -03:00
const auto v4 = color.ToVector4();
2024-07-16 09:33:57 -03:00
const auto _color = DxHelpers::VectorToDx(v4);
2024-04-14 16:11:15 -03:00
RECT _sourceRect{};
2024-06-06 14:18:16 -03:00
if (sourceRectangle.has_value()) {
2024-07-16 09:33:57 -03:00
_sourceRect = DxHelpers::RectangleToDx(sourceRectangle.value());
2024-04-14 16:11:15 -03:00
};
2024-07-16 09:33:57 -03:00
impl->dxSpriteBatch->Draw(
2024-06-25 17:06:37 -03:00
texture.impl->dxShaderResource.Get(),
2024-04-14 16:11:15 -03:00
_position,
sourceRectangle ? &_sourceRect : nullptr,
_color);
}
2024-06-06 14:18:16 -03:00
void SpriteBatch::Draw(Texture2D& texture, Vector2 const& position, std::optional<Rectangle> const& sourceRectangle, Color const& color, float rotation, Vector2 const& origin, float scale, SpriteEffects effects, float layerDepth) {
2024-07-16 09:33:57 -03:00
const auto _position = DxHelpers::VectorToDx(position);
const auto _origin = DxHelpers::VectorToDx(origin);
2024-04-14 16:11:15 -03:00
const auto v4 = color.ToVector4();
2024-07-16 09:33:57 -03:00
const auto _color = DxHelpers::VectorToDx(v4);
2024-04-14 16:11:15 -03:00
RECT _sourceRect{};
2024-06-06 14:18:16 -03:00
if (sourceRectangle.has_value()) {
2024-07-16 09:33:57 -03:00
_sourceRect = DxHelpers::RectangleToDx(sourceRectangle.value());
2024-04-14 16:11:15 -03:00
};
2024-04-14 21:23:09 -03:00
const DxSpriteEffects _effects = static_cast<DxSpriteEffects>(effects);
2024-04-14 16:11:15 -03:00
2024-07-16 09:33:57 -03:00
impl->dxSpriteBatch->Draw(
2024-06-25 17:06:37 -03:00
texture.impl->dxShaderResource.Get(),
2024-04-14 16:11:15 -03:00
_position,
sourceRectangle ? &_sourceRect : nullptr,
_color,
rotation,
_origin,
scale,
_effects,
layerDepth);
}
2024-06-06 14:18:16 -03:00
void SpriteBatch::Draw(Texture2D& texture, Vector2 const& position, std::optional<Rectangle> const& sourceRectangle, Color const& color, float rotation, Vector2 const& origin, Vector2 const& scale, SpriteEffects effects, float layerDepth) {
2024-04-14 21:23:09 -03:00
const auto _position = XMFLOAT2(position.X, position.Y);
const auto _origin = XMFLOAT2(origin.X, origin.Y);
2024-04-14 16:11:15 -03:00
const auto v4 = color.ToVector4();
2024-04-14 21:23:09 -03:00
const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W };
2024-04-14 16:11:15 -03:00
RECT _sourceRect{};
2024-06-06 14:18:16 -03:00
if (sourceRectangle.has_value()) {
2024-07-16 09:33:57 -03:00
_sourceRect = DxHelpers::RectangleToDx(sourceRectangle.value());
2024-04-14 16:11:15 -03:00
};
2024-04-14 21:23:09 -03:00
const auto _effects = static_cast<DxSpriteEffects>(effects);
2024-04-14 16:11:15 -03:00
const XMFLOAT2 _scale = { scale.X, scale.Y };
2024-07-16 09:33:57 -03:00
impl->dxSpriteBatch->Draw(
2024-06-25 17:06:37 -03:00
texture.impl->dxShaderResource.Get(),
2024-04-14 16:11:15 -03:00
_position,
sourceRectangle ? &_sourceRect : nullptr,
_color,
rotation,
_origin,
_scale,
_effects,
layerDepth);
}
void SpriteBatch::Draw(Texture2D& texture, Rectangle const& destinationRectangle, Color const& color) {
2024-07-16 09:33:57 -03:00
RECT _destinationRect = DxHelpers::RectangleToDx(destinationRectangle);
2024-04-14 16:11:15 -03:00
const auto v4 = color.ToVector4();
2024-04-14 21:23:09 -03:00
const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W };
2024-04-14 16:11:15 -03:00
2024-07-16 09:33:57 -03:00
impl->dxSpriteBatch->Draw(texture.impl->dxShaderResource.Get(), _destinationRect, _color);
2024-04-14 16:11:15 -03:00
}
2024-06-06 14:18:16 -03:00
void SpriteBatch::Draw(Texture2D& texture, Rectangle const& destinationRectangle, std::optional<Rectangle> const& sourceRectangle, Color const& color) {
2024-07-16 09:33:57 -03:00
RECT _destinationRect = DxHelpers::RectangleToDx(destinationRectangle);
2024-04-14 16:11:15 -03:00
const auto v4 = color.ToVector4();
2024-04-14 21:23:09 -03:00
const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W };
2024-04-14 16:11:15 -03:00
RECT _sourceRect{};
2024-06-06 14:18:16 -03:00
if (sourceRectangle.has_value()) {
2024-04-14 16:11:15 -03:00
_sourceRect.top = sourceRectangle->Y;
_sourceRect.left = sourceRectangle->X;
_sourceRect.right = sourceRectangle->X + sourceRectangle->Width;
_sourceRect.bottom = sourceRectangle->Y + sourceRectangle->Height;
};
2024-07-16 09:33:57 -03:00
impl->dxSpriteBatch->Draw(texture.impl->dxShaderResource.Get(), _destinationRect, sourceRectangle ? &_sourceRect : nullptr, _color);
2024-04-14 16:11:15 -03:00
}
2024-06-06 14:18:16 -03:00
void SpriteBatch::Draw(Texture2D& texture, Rectangle const& destinationRectangle, std::optional<Rectangle> const& sourceRectangle, Color const& color, float rotation, Vector2 const& origin, SpriteEffects effects, float layerDepth) {
2024-07-16 09:33:57 -03:00
RECT _destinationRect = DxHelpers::RectangleToDx(destinationRectangle);
2024-04-14 16:11:15 -03:00
const auto v4 = color.ToVector4();
2024-04-14 21:23:09 -03:00
const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W };
2024-04-14 16:11:15 -03:00
RECT _sourceRect{};
2024-06-06 14:18:16 -03:00
if (sourceRectangle.has_value()) {
2024-07-16 09:33:57 -03:00
_sourceRect = DxHelpers::RectangleToDx(sourceRectangle.value());
2024-04-14 16:11:15 -03:00
};
auto _origin = XMFLOAT2(origin.X, origin.Y);
2024-04-14 21:23:09 -03:00
const auto _effects = static_cast<DxSpriteEffects>(effects);
2024-04-14 16:11:15 -03:00
2024-07-16 09:33:57 -03:00
impl->dxSpriteBatch->Draw(
2024-06-25 17:06:37 -03:00
texture.impl->dxShaderResource.Get(),
2024-04-14 16:11:15 -03:00
_destinationRect,
sourceRectangle ? &_sourceRect : nullptr,
_color,
rotation,
_origin,
_effects,
layerDepth);
}
2024-05-30 17:37:40 -03:00
void SpriteBatch::Viewport(xna::Viewport const& value) {
2024-07-16 09:33:57 -03:00
const auto _view = DxHelpers::ViewportToDx(value);
impl->dxSpriteBatch->SetViewport(_view);
2024-05-30 17:37:40 -03:00
}
2024-04-14 21:23:09 -03:00
void SpriteBatch::DrawString(SpriteFont& spriteFont, String const& text, Vector2 const& position, Color const& color) {
const auto _position = XMFLOAT2(position.X, position.Y);
const auto v4 = color.ToVector4();
const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W };
2024-07-16 09:33:57 -03:00
spriteFont.impl->dxSpriteFont->DrawString(
impl->dxSpriteBatch.get(),
2024-04-14 21:23:09 -03:00
text.c_str(),
_position,
_color
);
}
2024-05-30 17:37:40 -03:00
2024-04-14 21:23:09 -03:00
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) {
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);
2024-07-16 09:33:57 -03:00
spriteFont.impl->dxSpriteFont->DrawString(
impl->dxSpriteBatch.get(),
2024-04-14 21:23:09 -03:00
text.c_str(),
_position,
2024-05-30 17:37:40 -03:00
_color,
rotation,
_origin,
scale,
_effects,
2024-04-14 21:23:09 -03:00
layerDepth
);
}
2024-04-14 16:11:15 -03:00
}