#ifndef XNA_GRAPHICS_SPRITE_HPP #define XNA_GRAPHICS_SPRITE_HPP #include "../common/color.hpp" #include "../common/numerics.hpp" #include "../graphics/gresource.hpp" #include #include #include #include namespace xna { //Defines sprite sort-rendering options. enum class SpriteSortMode { //Sprites are not drawn until End is called. //End will apply graphics device settings and draw all the sprites in one batch, in the same order calls to Draw were received. //This mode allows Draw calls to two or more instances of SpriteBatch without introducing conflicting graphics device settings. //SpriteBatch defaults to Deferred mode. Deferred, //Begin will apply new graphics device settings, and sprites will be drawn within each Draw call. //In Immediate mode there can only be one active SpriteBatch instance without introducing conflicting device settings. Immediate, //Same as Deferred mode, except sprites are sorted by texture prior to drawing. //This can improve performance when drawing non-overlapping sprites of uniform depth. Texture, //Same as Deferred mode, except sprites are sorted by depth in back-to-front order prior to drawing. //This procedure is recommended when drawing transparent sprites of varying depths. BackToFront, //Same as Deferred mode, except sprites are sorted by depth in front-to-back order prior to drawing. //This procedure is recommended when drawing opaque sprites of varying depths. FrontToBack, }; enum class SpriteEffects { None = 0, FlipHorizontally = 1, FlipVertically = 2, Both = FlipHorizontally | FlipVertically }; class BlendState; class SamplerState; class DepthStencilState; class RasterizerState; class Effect; class Texture2D; class SpriteFont; struct SpriteBatchImplementation; //Enables a group of sprites to be drawn using the same settings. class SpriteBatch : public GraphicsResource { public: SpriteBatch(std::shared_ptr const& device); //Begins a sprite batch operation. void Begin( std::optional sortMode, std::unique_ptr blendState, std::unique_ptr samplerState, std::unique_ptr depthStencil, std::unique_ptr rasterizerState, std::unique_ptr effect, Matrix const& transformMatrix = Matrix::Identity()) { Begin( sortMode.has_value() ? sortMode.value() : SpriteSortMode::Deferred, blendState.get(), samplerState.get(), depthStencil.get(), rasterizerState.get(), effect.get(), transformMatrix); } //Begins a sprite batch operation. void Begin( std::optional sortMode, std::shared_ptr blendState, std::shared_ptr samplerState, std::shared_ptr depthStencil, std::shared_ptr rasterizerState, std::shared_ptr effect, Matrix const& transformMatrix = Matrix::Identity()) { Begin( sortMode.has_value() ? sortMode.value() : SpriteSortMode::Deferred, blendState.get(), samplerState.get(), depthStencil.get(), rasterizerState.get(), effect.get(), transformMatrix); } //Begins a sprite batch operation. void Begin( SpriteSortMode sortMode = SpriteSortMode::Deferred, BlendState* blendState = nullptr, SamplerState* samplerState = nullptr, DepthStencilState* depthStencil = nullptr, RasterizerState* rasterizerState = nullptr, Effect* effect = nullptr, 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(std::unique_ptr const& texture, Vector2 const& position, Color const& color) { Draw(*texture, position, color); } void Draw(std::shared_ptr const& texture, Vector2 const& position, Color const& color) { Draw(*texture, position, color); } void Draw(Texture2D& texture, Vector2 const& position, Color const& color); void Draw(std::unique_ptr const& texture, Vector2 const& position, std::optional const& sourceRectangle, Color const& color) { Draw(*texture, position, sourceRectangle, color); } void Draw(std::shared_ptr const& texture, Vector2 const& position, std::optional const& sourceRectangle, Color const& color) { Draw(*texture, position, sourceRectangle, color); } void Draw(Texture2D& texture, Vector2 const& position, std::optional const& sourceRectangle, Color const& color); void Draw(std::unique_ptr const& texture, Vector2 const& position, std::optional 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(std::shared_ptr const& texture, Vector2 const& position, std::optional 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 const& sourceRectangle, Color const& color, float rotation, Vector2 const& origin, float scale, SpriteEffects effects, float layerDepth); void Draw(std::unique_ptr const& texture, Vector2 const& position, std::optional 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(std::shared_ptr const& texture, Vector2 const& position, std::optional 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 const& sourceRectangle, Color const& color, float rotation, Vector2 const& origin, Vector2 const& scale, SpriteEffects effects, float layerDepth); void Draw(std::unique_ptr const& texture, Rectangle const& destinationRectangle, Color const& color) { Draw(*texture, destinationRectangle, color); } void Draw(std::shared_ptr const& texture, Rectangle const& destinationRectangle, Color const& color) { Draw(*texture, destinationRectangle, color); } void Draw(Texture2D& texture, Rectangle const& destinationRectangle, Color const& color); void Draw(std::unique_ptr const& texture, Rectangle const& destinationRectangle, std::optional const& sourceRectangle, Color const& color) { Draw(*texture, destinationRectangle, sourceRectangle, color); } void Draw(std::shared_ptr const& texture, Rectangle const& destinationRectangle, std::optional const& sourceRectangle, Color const& color) { Draw(*texture, destinationRectangle, sourceRectangle, color); } void Draw(Texture2D& texture, Rectangle const& destinationRectangle, std::optional const& sourceRectangle, Color const& color); void Draw(std::unique_ptr const& texture, Rectangle const& destinationRectangle, std::optional 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(std::shared_ptr const& texture, Rectangle const& destinationRectangle, std::optional 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 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(std::unique_ptr const& spriteFont, std::string const& text, Vector2 const& position, Color const& color) { DrawString(*spriteFont, text, position, color); } void DrawString(std::shared_ptr const& spriteFont, std::string const& text, Vector2 const& position, Color const& color) { DrawString(*spriteFont, text, position, color); } void DrawString(SpriteFont& spriteFont, std::string const& text, Vector2 const& position, Color const& color); void DrawString(std::unique_ptr const& spriteFont, std::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(std::shared_ptr const& spriteFont, std::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(SpriteFont& spriteFont, std::string const& text, Vector2 const& position, Color const& color, float rotation, Vector2 const& origin, float scale, SpriteEffects effects, float layerDepth); std::unique_ptr Implementation; }; struct SpriteFontImplementation; //Represents a font texture. class SpriteFont { public: SpriteFont( std::shared_ptr const& texture, std::vector const& glyphs, std::vector const& cropping, std::vector const& charMap, int32_t lineSpacing, float spacing, std::vector const& kerning, std::optional const& defaultCharacter); // Returns the width and height of a string. Vector2 MeasureString(std::string const& text, bool ignoreWhiteSpace = true); // Returns the width and height of a string. Vector2 MeasureString(std::wstring const& text, bool ignoreWhiteSpace = true); //Gets or sets the default character for the font. char16_t DefaultCharacter() const; //Gets or sets the default character for the font. void DefaultCharacter(char16_t value); //Gets or sets the vertical distance (in pixels) between the base lines of two consecutive lines of text int32_t LineSpacing() const; //Gets or sets the vertical distance (in pixels) between the base lines of two consecutive lines of text void LineSpacing(float value); std::unique_ptr Implementation; }; } #endif