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

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

2024-05-18 21:14:56 -03:00
#ifndef XNA_GRAPHICS_SPRITE_HPP
#define XNA_GRAPHICS_SPRITE_HPP
2024-04-14 16:11:15 -03:00
2024-06-03 21:55:09 -03:00
#include "../common/color.hpp"
2024-07-08 09:40:48 -03:00
#include "../common/numerics.hpp"
#include "../graphics/gresource.hpp"
#include <memory>
#include <optional>
#include <string>
#include <cstdint>
2024-04-14 16:11:15 -03:00
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.
2024-11-16 14:21:06 -03:00
class SpriteBatch : public GraphicsResource {
2024-04-14 16:11:15 -03:00
public:
SpriteBatch(std::shared_ptr<GraphicsDevice> const& device);
2024-07-08 09:40:48 -03:00
//Begins a sprite batch operation.
void Begin(
std::optional<SpriteSortMode> sortMode,
std::unique_ptr<BlendState> blendState,
std::unique_ptr<SamplerState> samplerState,
std::unique_ptr<DepthStencilState> depthStencil,
std::unique_ptr<RasterizerState> rasterizerState,
std::unique_ptr<Effect> effect,
Matrix const& transformMatrix = Matrix::Identity())
{
2024-07-08 09:40:48 -03:00
Begin(
sortMode.has_value() ? sortMode.value() : SpriteSortMode::Deferred,
2024-07-08 09:40:48 -03:00
blendState.get(),
samplerState.get(),
depthStencil.get(),
rasterizerState.get(),
effect.get(),
transformMatrix);
2024-07-08 09:40:48 -03:00
}
//Begins a sprite batch operation.
2024-07-08 09:40:48 -03:00
void Begin(
std::optional<SpriteSortMode> sortMode,
std::shared_ptr<BlendState> blendState,
std::shared_ptr<SamplerState> samplerState,
std::shared_ptr<DepthStencilState> depthStencil,
std::shared_ptr<RasterizerState> rasterizerState,
std::shared_ptr<Effect> effect,
Matrix const& transformMatrix = Matrix::Identity()) {
2024-07-08 09:40:48 -03:00
Begin(
sortMode.has_value() ? sortMode.value() : SpriteSortMode::Deferred,
2024-07-08 09:40:48 -03:00
blendState.get(),
samplerState.get(),
depthStencil.get(),
rasterizerState.get(),
effect.get(),
transformMatrix);
2024-07-08 09:40:48 -03:00
}
//Begins a sprite batch operation.
2024-05-18 19:58:38 -03:00
void Begin(
2024-04-14 16:11:15 -03:00
SpriteSortMode sortMode = SpriteSortMode::Deferred,
BlendState* blendState = nullptr,
SamplerState* samplerState = nullptr,
2024-05-18 19:58:38 -03:00
DepthStencilState* depthStencil = nullptr,
2024-04-14 16:11:15 -03:00
RasterizerState* rasterizerState = nullptr,
2024-06-25 22:04:09 -03:00
Effect* effect = nullptr,
Matrix const& transformMatrix = Matrix::Identity()
2024-05-18 19:58:38 -03:00
);
//Flushes the sprite batch and restores the device state to how it was before Begin was called.
2024-05-18 19:58:38 -03:00
void End();
2024-06-06 14:18:16 -03:00
//
// Draw - Adds a sprite to a batch of sprites to be rendered.
//
void Draw(std::unique_ptr<Texture2D> const& texture, Vector2 const& position, Color const& color) { Draw(*texture, position, color); }
void Draw(std::shared_ptr<Texture2D> const& texture, Vector2 const& position, Color const& color) { Draw(*texture, position, color); }
2024-05-18 19:58:38 -03:00
void Draw(Texture2D& texture, Vector2 const& position, Color const& color);
2024-05-27 16:44:01 -03:00
void Draw(std::unique_ptr<Texture2D> const& texture, Vector2 const& position, std::optional<Rectangle> const& sourceRectangle, Color const& color) { Draw(*texture, position, sourceRectangle, color); }
void Draw(std::shared_ptr<Texture2D> const& texture, Vector2 const& position, std::optional<Rectangle> const& sourceRectangle, Color const& color) { Draw(*texture, position, sourceRectangle, color); }
2024-06-06 14:18:16 -03:00
void Draw(Texture2D& texture, Vector2 const& position, std::optional<Rectangle> const& sourceRectangle, Color const& color);
2024-05-27 16:44:01 -03:00
void Draw(std::unique_ptr<Texture2D> const& texture, Vector2 const& position, std::optional<Rectangle> const& sourceRectangle, Color const& color,
2024-06-06 14:18:16 -03:00
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<Texture2D> const& texture, Vector2 const& position, std::optional<Rectangle> const& sourceRectangle, Color const& color,
2024-06-06 14:18:16 -03:00
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,
2024-05-18 19:58:38 -03:00
float rotation, Vector2 const& origin, float scale, SpriteEffects effects, float layerDepth);
2024-05-27 16:44:01 -03:00
void Draw(std::unique_ptr<Texture2D> const& texture, Vector2 const& position, std::optional<Rectangle> const& sourceRectangle, Color const& color,
2024-06-06 14:18:16 -03:00
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<Texture2D> const& texture, Vector2 const& position, std::optional<Rectangle> const& sourceRectangle, Color const& color,
2024-06-06 14:18:16 -03:00
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,
2024-05-18 19:58:38 -03:00
float rotation, Vector2 const& origin, Vector2 const& scale, SpriteEffects effects, float layerDepth);
2024-05-27 16:44:01 -03:00
void Draw(std::unique_ptr<Texture2D> const& texture, Rectangle const& destinationRectangle, Color const& color) { Draw(*texture, destinationRectangle, color); }
void Draw(std::shared_ptr<Texture2D> const& texture, Rectangle const& destinationRectangle, Color const& color) { Draw(*texture, destinationRectangle, color); }
2024-05-18 19:58:38 -03:00
void Draw(Texture2D& texture, Rectangle const& destinationRectangle, Color const& color);
2024-05-27 16:44:01 -03:00
void Draw(std::unique_ptr<Texture2D> const& texture, Rectangle const& destinationRectangle, std::optional<Rectangle> const& sourceRectangle, Color const& color) { Draw(*texture, destinationRectangle, sourceRectangle, color); }
void Draw(std::shared_ptr<Texture2D> const& texture, Rectangle const& destinationRectangle, std::optional<Rectangle> const& sourceRectangle, Color const& color) { Draw(*texture, destinationRectangle, sourceRectangle, color); }
2024-06-06 14:18:16 -03:00
void Draw(Texture2D& texture, Rectangle const& destinationRectangle, std::optional<Rectangle> const& sourceRectangle, Color const& color);
2024-05-30 17:37:40 -03:00
void Draw(std::unique_ptr<Texture2D> const& texture, Rectangle const& destinationRectangle, std::optional<Rectangle> const& sourceRectangle, Color const& color,
2024-06-06 14:18:16 -03:00
float rotation, Vector2 const& origin, SpriteEffects effects, float layerDepth) { Draw(*texture, destinationRectangle, sourceRectangle, color, rotation, origin, effects, layerDepth); }
void Draw(std::shared_ptr<Texture2D> const& texture, Rectangle const& destinationRectangle, std::optional<Rectangle> const& sourceRectangle, Color const& color,
2024-06-06 14:18:16 -03:00
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);
2024-05-27 16:44:01 -03:00
//
// DrawString - Adds a string to a batch of sprites to be rendered.
//
void DrawString(std::unique_ptr<SpriteFont> const& spriteFont, std::string const& text, Vector2 const& position, Color const& color) { DrawString(*spriteFont, text, position, color); }
void DrawString(std::shared_ptr<SpriteFont> 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);
2024-05-30 17:37:40 -03:00
void DrawString(std::unique_ptr<SpriteFont> const& spriteFont, std::string const& text, Vector2 const& position, Color const& color,
2024-06-06 14:18:16 -03:00
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<SpriteFont> const& spriteFont, std::string const& text, Vector2 const& position, Color const& color,
2024-06-06 14:18:16 -03:00
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,
2024-05-18 19:58:38 -03:00
float rotation, Vector2 const& origin, float scale, SpriteEffects effects, float layerDepth);
2024-11-16 14:21:06 -03:00
std::unique_ptr<SpriteBatchImplementation> Implementation;
2024-04-14 16:11:15 -03:00
};
2024-05-18 21:14:56 -03:00
struct SpriteFontImplementation;
2024-06-06 14:18:16 -03:00
//Represents a font texture.
2024-11-16 14:21:06 -03:00
class SpriteFont {
2024-05-18 21:14:56 -03:00
public:
2024-05-30 17:37:40 -03:00
SpriteFont(
std::shared_ptr<Texture2D> const& texture,
2024-05-30 17:37:40 -03:00
std::vector<Rectangle> const& glyphs,
std::vector<Rectangle> const& cropping,
std::vector<char16_t> const& charMap,
int32_t lineSpacing,
2024-05-30 17:37:40 -03:00
float spacing,
std::vector<Vector3> const& kerning,
std::optional<char16_t> const& defaultCharacter);
2024-06-06 14:18:16 -03:00
// Returns the width and height of a string.
Vector2 MeasureString(std::string const& text, bool ignoreWhiteSpace = true);
2024-06-06 14:18:16 -03:00
// Returns the width and height of a string.
Vector2 MeasureString(std::wstring const& text, bool ignoreWhiteSpace = true);
2024-06-06 14:18:16 -03:00
//Gets or sets the default character for the font.
char16_t DefaultCharacter() const;
2024-06-06 14:18:16 -03:00
//Gets or sets the default character for the font.
void DefaultCharacter(char16_t value);
2024-06-06 14:18:16 -03:00
//Gets or sets the vertical distance (in pixels) between the base lines of two consecutive lines of text
int32_t LineSpacing() const;
2024-06-06 14:18:16 -03:00
//Gets or sets the vertical distance (in pixels) between the base lines of two consecutive lines of text
void LineSpacing(float value);
2024-11-16 14:21:06 -03:00
std::unique_ptr<SpriteFontImplementation> Implementation;
2024-05-18 21:14:56 -03:00
};
2024-04-14 16:11:15 -03:00
}
#endif