From 0065bf80e94b73c1bcffd6dc74140dcfa525d539 Mon Sep 17 00:00:00 2001 From: Danilo Date: Thu, 6 Jun 2024 14:28:00 -0300 Subject: [PATCH] Implementa SpriteBatch com um GraphicsResource --- framework/platform-dx/sprite.cpp | 8 ++++---- inc/xna/graphics/sprite.hpp | 18 ++++++++++++++++-- samples/01_blank/xna.cpp | 2 +- samples/02_PlatfformerStarterKit/game.cpp | 2 +- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/framework/platform-dx/sprite.cpp b/framework/platform-dx/sprite.cpp index 3221aa7..e316927 100644 --- a/framework/platform-dx/sprite.cpp +++ b/framework/platform-dx/sprite.cpp @@ -123,17 +123,17 @@ namespace xna { impl->_dxSpriteFont->SetLineSpacing(static_cast(value)); } - SpriteBatch::SpriteBatch(GraphicsDevice& device) { - if (!device.impl->_context) + SpriteBatch::SpriteBatch(sptr const& device) : GraphicsResource(device) { + if (!device->impl->_context) return; implementation = uNew(); implementation->_dxspriteBatch = New( //ID3D11DeviceContext* deviceContext - device.impl->_context + device->impl->_context ); - Viewport(device.Viewport()); + Viewport(device->Viewport()); } void SpriteBatch::Begin(SpriteSortMode sortMode, BlendState* blendState, SamplerState* samplerState, DepthStencilState* depthStencil, RasterizerState* rasterizerState, Matrix const& transformMatrix) { diff --git a/inc/xna/graphics/sprite.hpp b/inc/xna/graphics/sprite.hpp index a719a32..38d6dbd 100644 --- a/inc/xna/graphics/sprite.hpp +++ b/inc/xna/graphics/sprite.hpp @@ -5,11 +5,15 @@ #include "../common/numerics.hpp" #include "../common/color.hpp" #include +#include "../graphics/gresource.hpp" namespace xna { - class SpriteBatch { + //Enables a group of sprites to be drawn using the same settings. + class SpriteBatch : public GraphicsResource { public: - SpriteBatch(GraphicsDevice& device); + SpriteBatch(sptr const& device); + + //Begins a sprite batch operation. void Begin( SpriteSortMode sortMode = SpriteSortMode::Deferred, BlendState* blendState = nullptr, @@ -19,8 +23,14 @@ namespace xna { //Effect Matrix const& transformMatrix = Matrix::Identity() ); + + //Flushes the sprite batch and restores the device state to how it was before Begin was called. void End(); + // + // Draw - Adds a sprite to a batch of sprites to be rendered. + // + void Draw(uptr const& texture, Vector2 const& position, Color const& color) { Draw(*texture, position, color); } void Draw(sptr const& texture, Vector2 const& position, Color const& color) { Draw(*texture, position, color); } void Draw(Texture2D& texture, Vector2 const& position, Color const& color); @@ -58,6 +68,10 @@ namespace xna { void Draw(Texture2D& texture, Rectangle const& destinationRectangle, std::optional const& sourceRectangle, Color const& color, float rotation, Vector2 const& origin, SpriteEffects effects, float layerDepth); + // + // DrawString - Adds a string to a batch of sprites to be rendered. + // + void DrawString(uptr const& spriteFont, String const& text, Vector2 const& position, Color const& color) { DrawString(*spriteFont, text, position, color); } void DrawString(sptr 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); diff --git a/samples/01_blank/xna.cpp b/samples/01_blank/xna.cpp index 3ed637b..38352e0 100644 --- a/samples/01_blank/xna.cpp +++ b/samples/01_blank/xna.cpp @@ -25,7 +25,7 @@ namespace xna { } void LoadContent() override { - spriteBatch = New(*graphicsDevice); + spriteBatch = New(graphicsDevice); auto texture = Content()->Load("Idle"); Game::LoadContent(); } diff --git a/samples/02_PlatfformerStarterKit/game.cpp b/samples/02_PlatfformerStarterKit/game.cpp index dfcc7a5..63b77be 100644 --- a/samples/02_PlatfformerStarterKit/game.cpp +++ b/samples/02_PlatfformerStarterKit/game.cpp @@ -31,7 +31,7 @@ namespace PlatformerStarterKit { } void LoadContent() override { - spriteBatch = New(*graphicsDevice); + spriteBatch = New(graphicsDevice); // Load fonts hudFont = Content()->Load("Fonts/Hud");