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

Implementa SpriteBatch

This commit is contained in:
Danilo 2024-05-18 19:58:38 -03:00
parent f6fcbf54be
commit f36f67f401
5 changed files with 76 additions and 103 deletions

View File

@ -3,8 +3,12 @@
#include "platform-dx/rasterizerstate-dx.hpp" #include "platform-dx/rasterizerstate-dx.hpp"
#include "platform-dx/depthstencilstate-dx.hpp" #include "platform-dx/depthstencilstate-dx.hpp"
#include "platform-dx/samplerstate-dx.hpp" #include "platform-dx/samplerstate-dx.hpp"
#include "platform-dx/spritebatch-dx.hpp"
#include "platform-dx/texture-dx.hpp" #include "platform-dx/texture-dx.hpp"
#include "common/color.hpp"
#include "common/numerics.hpp"
#include "graphics/spritebatch.hpp"
#include "graphics/viewport.hpp"
#include "platform-dx/spritefont-dx.hpp"
using DxSpriteBatch = DirectX::SpriteBatch; using DxSpriteBatch = DirectX::SpriteBatch;
using DxSpriteSortMode = DirectX::SpriteSortMode; using DxSpriteSortMode = DirectX::SpriteSortMode;
@ -16,18 +20,30 @@ using DirectX::XMVECTORF32;
using DirectX::GXMVECTOR; using DirectX::GXMVECTOR;
namespace xna { namespace xna {
struct SpriteBatch::PlatformImplementation {
sptr<DirectX::SpriteBatch> _dxspriteBatch = nullptr;
};
static constexpr void ConvertSpriteSort(SpriteSortMode value, DirectX::SpriteSortMode& target) {
target = static_cast<DirectX::SpriteSortMode>(static_cast<int>(value));
}
SpriteBatch::SpriteBatch(GraphicsDevice& device) { SpriteBatch::SpriteBatch(GraphicsDevice& device) {
if (!device._context) if (!device._context)
return; return;
_dxspriteBatch = New<DxSpriteBatch>(device._context); implementation = uNew<PlatformImplementation>();
implementation->_dxspriteBatch = New<DxSpriteBatch>(device._context);
Viewport(device.Viewport()); Viewport(device.Viewport());
} }
SpriteBatch::~SpriteBatch() {
}
void SpriteBatch::Begin(SpriteSortMode sortMode, BlendState* blendState, SamplerState* samplerState, DepthStencilState* depthStencil, RasterizerState* rasterizerState, Matrix const& transformMatrix) { void SpriteBatch::Begin(SpriteSortMode sortMode, BlendState* blendState, SamplerState* samplerState, DepthStencilState* depthStencil, RasterizerState* rasterizerState, Matrix const& transformMatrix) {
if (!_dxspriteBatch) if (!implementation->_dxspriteBatch)
return; return;
DxSpriteSortMode sort; DxSpriteSortMode sort;
@ -41,7 +57,7 @@ namespace xna {
t.M41, t.M42, t.M43, t.M44); t.M41, t.M42, t.M43, t.M44);
_dxspriteBatch->Begin( implementation->_dxspriteBatch->Begin(
sort, sort,
blendState ? blendState->dxBlendState : nullptr, blendState ? blendState->dxBlendState : nullptr,
samplerState ? samplerState->_samplerState : nullptr, samplerState ? samplerState->_samplerState : nullptr,
@ -53,14 +69,14 @@ namespace xna {
} }
void SpriteBatch::End() { void SpriteBatch::End() {
if (!_dxspriteBatch) if (!implementation->_dxspriteBatch)
return; return;
_dxspriteBatch->End(); implementation->_dxspriteBatch->End();
} }
void SpriteBatch::Draw(Texture2D& texture, Vector2 const& position, Color const& color) { void SpriteBatch::Draw(Texture2D& texture, Vector2 const& position, Color const& color) {
if (!_dxspriteBatch) if (!implementation->_dxspriteBatch)
return; return;
if (!texture.dxShaderResource) if (!texture.dxShaderResource)
@ -70,7 +86,7 @@ namespace xna {
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( implementation->_dxspriteBatch->Draw(
texture.dxShaderResource, texture.dxShaderResource,
_position, _position,
_color _color
@ -78,7 +94,7 @@ namespace xna {
} }
void SpriteBatch::Draw(Texture2D& texture, Vector2 const& position, Rectangle const* sourceRectangle, Color const& color) { void SpriteBatch::Draw(Texture2D& texture, Vector2 const& position, Rectangle const* sourceRectangle, Color const& color) {
if (!_dxspriteBatch) if (!implementation->_dxspriteBatch)
return; return;
if (!texture.dxShaderResource) if (!texture.dxShaderResource)
@ -97,7 +113,7 @@ namespace xna {
_sourceRect.bottom = sourceRectangle->Y + sourceRectangle->Height; _sourceRect.bottom = sourceRectangle->Y + sourceRectangle->Height;
}; };
_dxspriteBatch->Draw( implementation->_dxspriteBatch->Draw(
texture.dxShaderResource, texture.dxShaderResource,
_position, _position,
sourceRectangle ? &_sourceRect : nullptr, sourceRectangle ? &_sourceRect : nullptr,
@ -105,7 +121,7 @@ namespace xna {
} }
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, Rectangle const* sourceRectangle, Color const& color, float rotation, Vector2 const& origin, float scale, SpriteEffects effects, float layerDepth) {
if (!_dxspriteBatch) if (!implementation->_dxspriteBatch)
return; return;
if (!texture.dxShaderResource) if (!texture.dxShaderResource)
@ -127,7 +143,7 @@ namespace xna {
const DxSpriteEffects _effects = static_cast<DxSpriteEffects>(effects); const DxSpriteEffects _effects = static_cast<DxSpriteEffects>(effects);
_dxspriteBatch->Draw( implementation->_dxspriteBatch->Draw(
texture.dxShaderResource, texture.dxShaderResource,
_position, _position,
sourceRectangle ? &_sourceRect : nullptr, sourceRectangle ? &_sourceRect : nullptr,
@ -140,7 +156,7 @@ namespace xna {
} }
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, Rectangle const* sourceRectangle, Color const& color, float rotation, Vector2 const& origin, Vector2 const& scale, SpriteEffects effects, float layerDepth) {
if (!_dxspriteBatch) if (!implementation->_dxspriteBatch)
return; return;
if (!texture.dxShaderResource) if (!texture.dxShaderResource)
@ -163,7 +179,7 @@ namespace xna {
const auto _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( implementation->_dxspriteBatch->Draw(
texture.dxShaderResource, texture.dxShaderResource,
_position, _position,
sourceRectangle ? &_sourceRect : nullptr, sourceRectangle ? &_sourceRect : nullptr,
@ -176,7 +192,7 @@ namespace xna {
} }
void SpriteBatch::Draw(Texture2D& texture, Rectangle const& destinationRectangle, Color const& color) { void SpriteBatch::Draw(Texture2D& texture, Rectangle const& destinationRectangle, Color const& color) {
if (!_dxspriteBatch) if (!implementation->_dxspriteBatch)
return; return;
if (!texture.dxShaderResource) if (!texture.dxShaderResource)
@ -191,11 +207,11 @@ namespace xna {
const auto v4 = color.ToVector4(); const auto v4 = color.ToVector4();
const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W }; const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W };
_dxspriteBatch->Draw(texture.dxShaderResource, _destinationRect, _color); implementation->_dxspriteBatch->Draw(texture.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, Rectangle const* sourceRectangle, Color const& color) {
if (!_dxspriteBatch) if (!implementation->_dxspriteBatch)
return; return;
if (!texture.dxShaderResource) if (!texture.dxShaderResource)
@ -219,11 +235,11 @@ namespace xna {
_sourceRect.bottom = sourceRectangle->Y + sourceRectangle->Height; _sourceRect.bottom = sourceRectangle->Y + sourceRectangle->Height;
}; };
_dxspriteBatch->Draw(texture.dxShaderResource, _destinationRect, sourceRectangle ? &_sourceRect : nullptr, _color); implementation->_dxspriteBatch->Draw(texture.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, Rectangle const* sourceRectangle, Color const& color, float rotation, Vector2 const& origin, SpriteEffects effects, float layerDepth) {
if (!_dxspriteBatch) if (!implementation->_dxspriteBatch)
return; return;
if (!texture.dxShaderResource) if (!texture.dxShaderResource)
@ -250,7 +266,7 @@ namespace xna {
auto _origin = XMFLOAT2(origin.X, origin.Y); auto _origin = XMFLOAT2(origin.X, origin.Y);
const auto _effects = static_cast<DxSpriteEffects>(effects); const auto _effects = static_cast<DxSpriteEffects>(effects);
_dxspriteBatch->Draw( implementation->_dxspriteBatch->Draw(
texture.dxShaderResource, texture.dxShaderResource,
_destinationRect, _destinationRect,
sourceRectangle ? &_sourceRect : nullptr, sourceRectangle ? &_sourceRect : nullptr,
@ -262,7 +278,7 @@ namespace xna {
} }
void SpriteBatch::Viewport(xna::Viewport const& value) { void SpriteBatch::Viewport(xna::Viewport const& value) {
if (!_dxspriteBatch) if (!implementation->_dxspriteBatch)
return; return;
D3D11_VIEWPORT _view{}; D3D11_VIEWPORT _view{};
@ -273,11 +289,11 @@ namespace xna {
_view.MinDepth = value.MinDetph; _view.MinDepth = value.MinDetph;
_view.MaxDepth = value.MaxDepth; _view.MaxDepth = value.MaxDepth;
_dxspriteBatch->SetViewport(_view); implementation->_dxspriteBatch->SetViewport(_view);
} }
void SpriteBatch::DrawString(SpriteFont& spriteFont, String const& text, Vector2 const& position, Color const& color) { void SpriteBatch::DrawString(SpriteFont& spriteFont, String const& text, Vector2 const& position, Color const& color) {
if (!_dxspriteBatch || !spriteFont._dxSpriteFont) if (!implementation->_dxspriteBatch || !spriteFont._dxSpriteFont)
return; return;
const auto _position = XMFLOAT2(position.X, position.Y); const auto _position = XMFLOAT2(position.X, position.Y);
@ -285,7 +301,7 @@ namespace xna {
const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W }; const XMVECTORF32 _color = { v4.X, v4.Y, v4.Z, v4.W };
spriteFont._dxSpriteFont->DrawString( spriteFont._dxSpriteFont->DrawString(
_dxspriteBatch.get(), implementation->_dxspriteBatch.get(),
text.c_str(), text.c_str(),
_position, _position,
_color _color
@ -294,7 +310,7 @@ namespace xna {
void SpriteBatch::DrawString(SpriteFont& spriteFont, String const& text, Vector2 const& position, 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) { Color const& color, float rotation, Vector2 const& origin, float scale, SpriteEffects effects, float layerDepth) {
if (!_dxspriteBatch || !spriteFont._dxSpriteFont) if (!implementation->_dxspriteBatch || !spriteFont._dxSpriteFont)
return; return;
const auto _position = XMFLOAT2(position.X, position.Y); const auto _position = XMFLOAT2(position.X, position.Y);
@ -304,7 +320,7 @@ namespace xna {
const auto _effects = static_cast<DxSpriteEffects>(effects); const auto _effects = static_cast<DxSpriteEffects>(effects);
spriteFont._dxSpriteFont->DrawString( spriteFont._dxSpriteFont->DrawString(
_dxspriteBatch.get(), implementation->_dxspriteBatch.get(),
text.c_str(), text.c_str(),
_position, _position,
_color, _color,

View File

@ -26,7 +26,9 @@ namespace xna {
} }
void LoadContent() override { void LoadContent() override {
spriteBatch = New<SpriteBatch>(*graphicsDevice); spriteBatch = New<SpriteBatch>(*graphicsDevice);
texture = Content()->Load<sptr<Texture2D>>("idle");
Game::LoadContent(); Game::LoadContent();
} }
@ -41,12 +43,17 @@ namespace xna {
void Draw(GameTime const& gameTime) override { void Draw(GameTime const& gameTime) override {
graphicsDevice->Clear(Colors::CornflowerBlue); graphicsDevice->Clear(Colors::CornflowerBlue);
spriteBatch->Begin();
spriteBatch->Draw(*texture, Vector2(), Colors::White);
spriteBatch->End();
Game::Draw(gameTime); Game::Draw(gameTime);
} }
private: private:
sptr<GraphicsDeviceManager> graphics = nullptr; sptr<GraphicsDeviceManager> graphics = nullptr;
sptr<SpriteBatch> spriteBatch = nullptr; sptr<SpriteBatch> spriteBatch = nullptr;
sptr<Texture2D> texture = nullptr;
}; };
} }

View File

@ -5,33 +5,38 @@
#include "../common/numerics.hpp" #include "../common/numerics.hpp"
namespace xna { namespace xna {
class ISpriteBatch { class SpriteBatch {
public: public:
virtual ~ISpriteBatch(){} SpriteBatch(GraphicsDevice& device);
virtual void Begin( ~SpriteBatch();
void Begin(
SpriteSortMode sortMode = SpriteSortMode::Deferred, SpriteSortMode sortMode = SpriteSortMode::Deferred,
BlendState* blendState = nullptr, BlendState* blendState = nullptr,
SamplerState* samplerState = nullptr, SamplerState* samplerState = nullptr,
DepthStencilState * depthStencil = nullptr, DepthStencilState* depthStencil = nullptr,
RasterizerState* rasterizerState = nullptr, RasterizerState* rasterizerState = nullptr,
//Effect //Effect
Matrix const& transformMatrix = Matrix::Identity() Matrix const& transformMatrix = Matrix::Identity()
) = 0; );
virtual void End() = 0; void End();
virtual void Draw(Texture2D& texture, Vector2 const& position, Color const& color) = 0; void Draw(Texture2D& texture, Vector2 const& position, Color const& color);
virtual void Draw(Texture2D& texture, Vector2 const& position, Rectangle const * sourceRectangle, Color const& color) = 0; void Draw(Texture2D& texture, Vector2 const& position, Rectangle const* sourceRectangle, Color const& color);
virtual void Draw(Texture2D& texture, Vector2 const& position, Rectangle const* sourceRectangle, Color const& color, void Draw(Texture2D& texture, Vector2 const& position, Rectangle const* sourceRectangle, Color const& color,
float rotation, Vector2 const& origin, float scale, SpriteEffects effects, float layerDepth) = 0; float rotation, Vector2 const& origin, float scale, SpriteEffects effects, float layerDepth);
virtual void Draw(Texture2D& texture, Vector2 const& position, Rectangle const* sourceRectangle, Color const& color, void Draw(Texture2D& texture, Vector2 const& position, Rectangle const* sourceRectangle, Color const& color,
float rotation, Vector2 const& origin, Vector2 const& scale, SpriteEffects effects, float layerDepth) = 0; float rotation, Vector2 const& origin, Vector2 const& scale, SpriteEffects effects, float layerDepth);
virtual void Draw(Texture2D& texture, Rectangle const& destinationRectangle, Color const& color) = 0; void Draw(Texture2D& texture, Rectangle const& destinationRectangle, Color const& color);
virtual void Draw(Texture2D& texture, Rectangle const& destinationRectangle, Rectangle const* sourceRectangle, Color const& color) = 0; 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, 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);
virtual void Viewport(xna::Viewport const& value) = 0; void Viewport(xna::Viewport const& value);
virtual void DrawString(SpriteFont& spriteFont, String const& text, Vector2 const& position, Color const& color) = 0; void DrawString(SpriteFont& spriteFont, String const& text, Vector2 const& position, Color const& color);
virtual void DrawString(SpriteFont& spriteFont, String const& text, Vector2 const& position, Color const& color, 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; float rotation, Vector2 const& origin, float scale, SpriteEffects effects, float layerDepth);
private:
struct PlatformImplementation;
uptr<PlatformImplementation> implementation = nullptr;
}; };
} }

View File

@ -1,54 +0,0 @@
#ifndef XNA_PLATFORM_SPRITEBATCH_DX_HPP
#define XNA_PLATFORM_SPRITEBATCH_DX_HPP
#include "../common/color.hpp"
#include "../common/numerics.hpp"
#include "../graphics/spritebatch.hpp"
#include "../graphics/viewport.hpp"
#include "SpriteBatch.h"
#include "spritefont-dx.hpp"
namespace xna {
class SpriteBatch : public ISpriteBatch {
public:
SpriteBatch(GraphicsDevice& device);
virtual ~SpriteBatch() override {}
virtual void Begin(
SpriteSortMode sortMode = SpriteSortMode::Deferred,
BlendState* blendState = nullptr,
SamplerState* samplerState = nullptr,
DepthStencilState* depthStencil = nullptr,
RasterizerState* rasterizerState = nullptr,
//Effect
Matrix const& transformMatrix = Matrix::Identity()
) override;
virtual void End() override;
virtual void Draw(Texture2D& texture, Vector2 const& position, Color const& color) override;
virtual void Draw(Texture2D& texture, Vector2 const& position, Rectangle const* sourceRectangle, Color const& color) override;
virtual void Draw(Texture2D& texture, Vector2 const& position, Rectangle const* sourceRectangle, Color const& color,
float rotation, Vector2 const& origin, float scale, SpriteEffects effects, float layerDepth) override;
virtual void Draw(Texture2D& texture, Vector2 const& position, Rectangle const* sourceRectangle, Color const& color,
float rotation, Vector2 const& origin, Vector2 const& scale, SpriteEffects effects, float layerDepth) override;
virtual void Draw(Texture2D& texture, Rectangle const& destinationRectangle, Color const& color) override;
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 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:
static constexpr void ConvertSpriteSort(SpriteSortMode value, DirectX::SpriteSortMode& target) {
target = static_cast<DirectX::SpriteSortMode>(static_cast<int>(value));
}
public:
sptr<DirectX::SpriteBatch> _dxspriteBatch = nullptr;
};
}
#endif

View File

@ -22,7 +22,6 @@
#include "samplerstate-dx.hpp" #include "samplerstate-dx.hpp"
#include "shader-dx.hpp" #include "shader-dx.hpp"
#include "soundeffect-dx.hpp" #include "soundeffect-dx.hpp"
#include "spritebatch-dx.hpp"
#include "spritefont-dx.hpp" #include "spritefont-dx.hpp"
#include "swapchain-dx.hpp" #include "swapchain-dx.hpp"
#include "texture-dx.hpp" #include "texture-dx.hpp"