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

Implementações em Sprite

This commit is contained in:
Danilo 2024-06-06 14:18:16 -03:00
parent c822f6d342
commit d21d93eaf8
7 changed files with 118 additions and 94 deletions

View File

@ -28,26 +28,35 @@ namespace xna {
Int lineSpacing,
float spacing,
std::vector<Vector3> const& kerning,
std::optional<Char> defaultCharacter) :
textureValue(texture), glyphData(glyphs), croppingData(cropping),
characterMap(charMap), lineSpacing(lineSpacing), spacing(spacing),
kerning(kerning), defaultCharacter(defaultCharacter)
std::optional<Char> const& defaultCharacter)
{
if (!texture)
if (!texture || !texture->impl->dxShaderResource)
throw std::invalid_argument("SpriteFont: texture is null.");
if(cropping.size() != glyphs.size() || charMap.size() != glyphs.size() || (!kerning.empty() && kerning.size() != glyphs.size()))
throw std::invalid_argument("SpriteFont: cropping, charmap and kerning (if not empty) must all be the same size.");
std::vector<DxGlyph> dxGlyps(glyphs.size());
for (size_t i = 0; i < dxGlyps.size(); ++i) {
DxGlyph g;
g.Subrect.left = glyphs[i].Left();
g.Subrect.right = glyphs[i].Right();
g.Subrect.top = glyphs[i].Top();
g.Subrect.bottom = glyphs[i].Bottom();
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());
g.Character = static_cast<Uint>(charMap[i]);
g.XOffset = kerning[i].X;
g.YOffset = cropping[i].Y;
g.XAdvance = kerning[i].Z;
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;
}
dxGlyps[i] = g;
}
@ -67,14 +76,7 @@ namespace xna {
const auto defChar = static_cast<wchar_t>(defaultCharacter.value());
impl->_dxSpriteFont->SetDefaultCharacter(defChar);
}
else {
impl->_dxSpriteFont->SetDefaultCharacter(charMap[0]);
}
}
SpriteFont::~SpriteFont() {
impl = nullptr;
}
}
Vector2 SpriteFont::MeasureString(String const& text, bool ignoreWhiteSpace)
{
@ -102,22 +104,37 @@ namespace xna {
return vec2;
}
static constexpr void ConvertSpriteSort(SpriteSortMode value, DirectX::SpriteSortMode& target) {
target = static_cast<DirectX::SpriteSortMode>(static_cast<int>(value));
Char SpriteFont::DefaultCharacter() const {
const auto defChar = impl->_dxSpriteFont->GetDefaultCharacter();
return static_cast<Char>(defChar);
}
void SpriteFont::DefaultCharacter(Char value) {
const auto defChar = static_cast<wchar_t>(value);
impl->_dxSpriteFont->SetDefaultCharacter(defChar);
}
Int SpriteFont::LineSpacing() const {
const auto space = impl->_dxSpriteFont->GetLineSpacing();
return static_cast<Int>(space);
}
void SpriteFont::LineSpacing(Int value) {
impl->_dxSpriteFont->SetLineSpacing(static_cast<float>(value));
}
SpriteBatch::SpriteBatch(GraphicsDevice& device) {
if (!device.impl->_context)
return;
implementation = uNew<PlatformImplementation>();
implementation->_dxspriteBatch = New<DxSpriteBatch>(device.impl->_context);
implementation->_dxspriteBatch = New<DxSpriteBatch>(
//ID3D11DeviceContext* deviceContext
device.impl->_context
);
Viewport(device.Viewport());
}
SpriteBatch::~SpriteBatch() {
}
}
void SpriteBatch::Begin(SpriteSortMode sortMode, BlendState* blendState, SamplerState* samplerState, DepthStencilState* depthStencil, RasterizerState* rasterizerState, Matrix const& transformMatrix) {
@ -125,7 +142,7 @@ namespace xna {
return;
DxSpriteSortMode sort;
ConvertSpriteSort(sortMode, sort);
DxHelpers::ConvertSpriteSort(sortMode, sort);
const auto& t = transformMatrix;
DxMatrix matrix = DxMatrix(
@ -171,10 +188,10 @@ namespace xna {
);
}
void SpriteBatch::Draw(Texture2D& texture, Vector2 const& position, Rectangle const* sourceRectangle, Color const& color) {
void SpriteBatch::Draw(Texture2D& texture, Vector2 const& position, std::optional<Rectangle> const& sourceRectangle, Color const& color) {
if (!implementation->_dxspriteBatch)
return;
if (!texture.impl->dxShaderResource)
return;
@ -184,7 +201,7 @@ namespace xna {
RECT _sourceRect{};
if (sourceRectangle) {
if (sourceRectangle.has_value()) {
_sourceRect.top = sourceRectangle->Y;
_sourceRect.left = sourceRectangle->X;
_sourceRect.right = sourceRectangle->X + sourceRectangle->Width;
@ -198,7 +215,7 @@ namespace xna {
_color);
}
void SpriteBatch::Draw(Texture2D& texture, Vector2 const& position, Rectangle const* sourceRectangle, Color const& color, float rotation, Vector2 const& origin, float scale, SpriteEffects effects, float layerDepth) {
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) {
if (!implementation->_dxspriteBatch)
return;
@ -212,7 +229,7 @@ namespace xna {
RECT _sourceRect{};
if (sourceRectangle) {
if (sourceRectangle.has_value()) {
_sourceRect.top = sourceRectangle->Y;
_sourceRect.left = sourceRectangle->X;
_sourceRect.right = sourceRectangle->X + sourceRectangle->Width;
@ -233,7 +250,7 @@ namespace xna {
layerDepth);
}
void SpriteBatch::Draw(Texture2D& texture, Vector2 const& position, Rectangle const* sourceRectangle, Color const& color, float rotation, Vector2 const& origin, Vector2 const& scale, SpriteEffects effects, float layerDepth) {
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) {
if (!implementation->_dxspriteBatch)
return;
@ -247,7 +264,7 @@ namespace xna {
RECT _sourceRect{};
if (sourceRectangle) {
if (sourceRectangle.has_value()) {
_sourceRect.top = sourceRectangle->Y;
_sourceRect.left = sourceRectangle->X;
_sourceRect.right = sourceRectangle->X + sourceRectangle->Width;
@ -288,7 +305,7 @@ namespace xna {
implementation->_dxspriteBatch->Draw(texture.impl->dxShaderResource, _destinationRect, _color);
}
void SpriteBatch::Draw(Texture2D& texture, Rectangle const& destinationRectangle, Rectangle const* sourceRectangle, Color const& color) {
void SpriteBatch::Draw(Texture2D& texture, Rectangle const& destinationRectangle, std::optional<Rectangle> const& sourceRectangle, Color const& color) {
if (!implementation->_dxspriteBatch)
return;
@ -306,7 +323,7 @@ namespace xna {
RECT _sourceRect{};
if (sourceRectangle) {
if (sourceRectangle.has_value()) {
_sourceRect.top = sourceRectangle->Y;
_sourceRect.left = sourceRectangle->X;
_sourceRect.right = sourceRectangle->X + sourceRectangle->Width;
@ -316,7 +333,7 @@ namespace xna {
implementation->_dxspriteBatch->Draw(texture.impl->dxShaderResource, _destinationRect, sourceRectangle ? &_sourceRect : nullptr, _color);
}
void SpriteBatch::Draw(Texture2D& texture, Rectangle const& destinationRectangle, Rectangle const* sourceRectangle, Color const& color, float rotation, Vector2 const& origin, SpriteEffects effects, float layerDepth) {
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) {
if (!implementation->_dxspriteBatch)
return;
@ -334,7 +351,7 @@ namespace xna {
RECT _sourceRect{};
if (sourceRectangle) {
if (sourceRectangle.has_value()) {
_sourceRect.top = sourceRectangle->Y;
_sourceRect.left = sourceRectangle->X;
_sourceRect.right = sourceRectangle->X + sourceRectangle->Width;

View File

@ -3,6 +3,7 @@
#include <cmath>
#include "../default.hpp"
#include <optional>
namespace xna {
struct Point {
@ -34,6 +35,10 @@ namespace xna {
return Height == other.Height && Width == other.Width && X == other.X && Y == other.Y;
}
constexpr operator std::optional<Rectangle>() const {
return std::make_optional<Rectangle>(X, Y, Width, Height);
}
constexpr Int Left() const { return X; }
constexpr Int Right() const { return X + Width; }
constexpr Int Top() const { return Y; }

View File

@ -10,7 +10,6 @@ namespace xna {
class SpriteBatch {
public:
SpriteBatch(GraphicsDevice& device);
~SpriteBatch();
void Begin(
SpriteSortMode sortMode = SpriteSortMode::Deferred,
BlendState* blendState = nullptr,
@ -21,66 +20,63 @@ namespace xna {
Matrix const& transformMatrix = Matrix::Identity()
);
void End();
void Draw(sptr<Texture2D> const& texture, Vector2 const& position, Color const& color) {
Draw(*texture, position, color);
}
void Draw(uptr<Texture2D> const& texture, Vector2 const& position, Color const& color) { Draw(*texture, position, color); }
void Draw(sptr<Texture2D> const& texture, Vector2 const& position, Color const& color) { Draw(*texture, position, color); }
void Draw(Texture2D& texture, Vector2 const& position, Color const& color);
void Draw(sptr<Texture2D> const& texture, Vector2 const& position, Rectangle const* sourceRectangle, Color const& color) {
Draw(*texture, position, sourceRectangle, color);
}
void Draw(Texture2D& texture, Vector2 const& position, Rectangle const* sourceRectangle, Color const& color);
void Draw(uptr<Texture2D> const& texture, Vector2 const& position, std::optional<Rectangle> const& sourceRectangle, Color const& color) { Draw(*texture, position, sourceRectangle, color); }
void Draw(sptr<Texture2D> const& texture, Vector2 const& position, std::optional<Rectangle> const& sourceRectangle, Color const& color) { Draw(*texture, position, sourceRectangle, color); }
void Draw(Texture2D& texture, Vector2 const& position, std::optional<Rectangle> const& sourceRectangle, Color const& color);
void Draw(sptr<Texture2D> const& texture, Vector2 const& position, Rectangle const* sourceRectangle, Color const& color,
float rotation, Vector2 const& origin, float scale, SpriteEffects effects, float layerDepth) {
Draw(*texture, position, sourceRectangle, color, rotation, origin, scale, effects, layerDepth);
}
void Draw(Texture2D& texture, Vector2 const& position, Rectangle const* sourceRectangle, Color const& color,
void Draw(uptr<Texture2D> const& texture, Vector2 const& position, std::optional<Rectangle> const& sourceRectangle, Color const& color,
float rotation, Vector2 const& origin, float scale, SpriteEffects effects, float layerDepth) { Draw(*texture, position, sourceRectangle, color, rotation, origin, scale, effects, layerDepth); }
void Draw(sptr<Texture2D> const& texture, Vector2 const& position, std::optional<Rectangle> const& sourceRectangle, Color const& color,
float rotation, Vector2 const& origin, float scale, SpriteEffects effects, float layerDepth) { Draw(*texture, position, sourceRectangle, color, rotation, origin, scale, effects, layerDepth); }
void 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);
void Draw(sptr<Texture2D> const& texture, Vector2 const& position, Rectangle const* sourceRectangle, Color const& color,
float rotation, Vector2 const& origin, Vector2 const& scale, SpriteEffects effects, float layerDepth) {
Draw(*texture, position, sourceRectangle, color, rotation, origin, scale, effects, layerDepth);
}
void Draw(Texture2D& texture, Vector2 const& position, Rectangle const* sourceRectangle, Color const& color,
void Draw(uptr<Texture2D> const& texture, Vector2 const& position, std::optional<Rectangle> const& sourceRectangle, Color const& color,
float rotation, Vector2 const& origin, Vector2 const& scale, SpriteEffects effects, float layerDepth) { Draw(*texture, position, sourceRectangle, color, rotation, origin, scale, effects, layerDepth); }
void Draw(sptr<Texture2D> const& texture, Vector2 const& position, std::optional<Rectangle> const& sourceRectangle, Color const& color,
float rotation, Vector2 const& origin, Vector2 const& scale, SpriteEffects effects, float layerDepth) { Draw(*texture, position, sourceRectangle, color, rotation, origin, scale, effects, layerDepth); }
void 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);
void Draw(sptr<Texture2D> const& texture, Rectangle const& destinationRectangle, Color const& color) {
Draw(*texture, destinationRectangle, color);
}
void Draw(uptr<Texture2D> const& texture, Rectangle const& destinationRectangle, Color const& color) { Draw(*texture, destinationRectangle, color); }
void Draw(sptr<Texture2D> const& texture, Rectangle const& destinationRectangle, Color const& color) { Draw(*texture, destinationRectangle, color); }
void Draw(Texture2D& texture, Rectangle const& destinationRectangle, Color const& color);
void Draw(sptr<Texture2D> const& texture, Rectangle const& destinationRectangle, Rectangle const* sourceRectangle, Color const& color) {
Draw(*texture, destinationRectangle, sourceRectangle, color);
}
void Draw(Texture2D& texture, Rectangle const& destinationRectangle, Rectangle const* sourceRectangle, Color const& color);
void Draw(uptr<Texture2D> const& texture, Rectangle const& destinationRectangle, std::optional<Rectangle> const& sourceRectangle, Color const& color) { Draw(*texture, destinationRectangle, sourceRectangle, color); }
void Draw(sptr<Texture2D> const& texture, Rectangle const& destinationRectangle, std::optional<Rectangle> const& sourceRectangle, Color const& color) { Draw(*texture, destinationRectangle, sourceRectangle, color); }
void Draw(Texture2D& texture, Rectangle const& destinationRectangle, std::optional<Rectangle> const& sourceRectangle, Color const& color);
void Draw(sptr<Texture2D> const& texture, Rectangle const& destinationRectangle, Rectangle const* sourceRectangle, Color const& color,
float rotation, Vector2 const& origin, SpriteEffects effects, float layerDepth) {
Draw(*texture, destinationRectangle, sourceRectangle, color, rotation, origin, effects, layerDepth);
}
void Draw(Texture2D& texture, Rectangle const& destinationRectangle, Rectangle const* sourceRectangle, Color const& color,
float rotation, Vector2 const& origin, SpriteEffects effects, float layerDepth);
void Draw(uptr<Texture2D> const& texture, Rectangle const& destinationRectangle, std::optional<Rectangle> const& sourceRectangle, Color const& color,
float rotation, Vector2 const& origin, SpriteEffects effects, float layerDepth) { Draw(*texture, destinationRectangle, sourceRectangle, color, rotation, origin, effects, layerDepth); }
void Draw(sptr<Texture2D> const& texture, Rectangle const& destinationRectangle, std::optional<Rectangle> const& sourceRectangle, Color const& color,
float rotation, Vector2 const& origin, SpriteEffects effects, float layerDepth) { Draw(*texture, destinationRectangle, sourceRectangle, color, rotation, origin, effects, layerDepth); }
void Draw(Texture2D& texture, Rectangle const& destinationRectangle, std::optional<Rectangle> const& sourceRectangle, Color const& color,
float rotation, Vector2 const& origin, SpriteEffects effects, float layerDepth);
void Viewport(xna::Viewport const& value);
void DrawString(sptr<SpriteFont> const& spriteFont, String const& text, Vector2 const& position, Color const& color) {
DrawString(*spriteFont, text, position, color);
}
void DrawString(uptr<SpriteFont> const& spriteFont, String const& text, Vector2 const& position, Color const& color) { DrawString(*spriteFont, text, position, color); }
void DrawString(sptr<SpriteFont> const& spriteFont, String const& text, Vector2 const& position, Color const& color) { DrawString(*spriteFont, text, position, color); }
void DrawString(SpriteFont& spriteFont, String const& text, Vector2 const& position, Color const& color);
void DrawString(uptr<SpriteFont> const& spriteFont, String const& text, Vector2 const& position, Color const& color,
float rotation, Vector2 const& origin, float scale, SpriteEffects effects, float layerDepth) { DrawString(*spriteFont, text, position, color, rotation, origin, scale, effects, layerDepth); }
void DrawString(sptr<SpriteFont> const& spriteFont, String const& text, Vector2 const& position, Color const& color,
float rotation, Vector2 const& origin, float scale, SpriteEffects effects, float layerDepth) {
DrawString(*spriteFont, text, position, color, rotation, origin, scale, effects, layerDepth);
}
float rotation, Vector2 const& origin, float scale, SpriteEffects effects, float layerDepth) { DrawString(*spriteFont, text, position, color, rotation, origin, scale, effects, layerDepth); }
void DrawString(SpriteFont& spriteFont, String const& text, Vector2 const& position, Color const& color,
float rotation, Vector2 const& origin, float scale, SpriteEffects effects, float layerDepth);
void Viewport(xna::Viewport const& value);
public:
struct PlatformImplementation;
uptr<PlatformImplementation> implementation = nullptr;
};
//Represents a font texture.
class SpriteFont {
public:
SpriteFont(
@ -91,20 +87,21 @@ namespace xna {
Int lineSpacing,
float spacing,
std::vector<Vector3> const& kerning,
std::optional<Char> defaultCharacter);
~SpriteFont();
std::optional<Char> const& defaultCharacter);
// Returns the width and height of a string.
Vector2 MeasureString(String const& text, bool ignoreWhiteSpace = true);
Vector2 MeasureString(WString const& text, bool ignoreWhiteSpace = true);
// Returns the width and height of a string.
Vector2 MeasureString(WString const& text, bool ignoreWhiteSpace = true);
private:
sptr<Texture2D> textureValue = nullptr;
std::vector<Rectangle> glyphData;
std::vector<Rectangle> croppingData;
std::vector<Char> characterMap;
Int lineSpacing{0};
float spacing{0};
std::vector<Vector3> kerning;
std::optional<Char> defaultCharacter;
//Gets or sets the default character for the font.
Char DefaultCharacter() const;
//Gets or sets the default character for the font.
void DefaultCharacter(Char value);
//Gets or sets the vertical distance (in pixels) between the base lines of two consecutive lines of text
Int LineSpacing() const;
//Gets or sets the vertical distance (in pixels) between the base lines of two consecutive lines of text
void LineSpacing(Int value);
public:
struct PlatformImplementation;

View File

@ -92,6 +92,10 @@ namespace xna {
//==============================================//
struct DxHelpers {
static constexpr void ConvertSpriteSort(SpriteSortMode value, DirectX::SpriteSortMode& target) {
target = static_cast<DirectX::SpriteSortMode>(static_cast<int>(value));
}
static constexpr DXGI_FORMAT ConvertSurfaceToDXGIFORMAT(SurfaceFormat format)
{
switch (format)
@ -446,7 +450,7 @@ namespace xna {
//==============================================//
struct SpriteFont::PlatformImplementation {
sptr<DirectX::SpriteFont> _dxSpriteFont = nullptr;
uptr<DirectX::SpriteFont> _dxSpriteFont{ nullptr };
};
struct SpriteBatch::PlatformImplementation {

View File

@ -8,6 +8,7 @@
#include <memory>
#include <utility>
#include <cassert>
#include <optional>
namespace xna {
using Sbyte = int8_t;
@ -48,7 +49,7 @@ namespace xna {
//
using String = std::string;
using WString = std::wstring;
using WString = std::wstring;
template <typename T>
using sptr = std::shared_ptr<T>;

View File

@ -34,6 +34,6 @@ namespace PlatformerStarterKit {
const auto source = xna::Rectangle(frameIndex * animation->Texture()->Height(), 0, animation->Texture()->Height(), animation->Texture()->Height());
// Draw the current frame.
spriteBatch.Draw(animation->Texture(), position, &source, xna::Colors::White, 0.0f, Origin(), 1.0f, spriteEffects, 0.0f);
spriteBatch.Draw(animation->Texture(), position, source, xna::Colors::White, 0.0f, Origin(), 1.0f, spriteEffects, 0.0f);
}
}

View File

@ -34,6 +34,6 @@ namespace PlatformerStarterKit {
void Gem::Draw(xna::GameTime const& gameTime, xna::SpriteBatch& spriteBatch)
{
spriteBatch.Draw(texture, Position(), nullptr, Color, 0.0f, origin, 1.0f, xna::SpriteEffects::None, 0.0f);
spriteBatch.Draw(texture, Position(), std::nullopt, Color, 0.0f, origin, 1.0f, xna::SpriteEffects::None, 0.0f);
}
}