Space Fighter
A "shmup" game for Computer Programming C++
Loading...
Searching...
No Matches
SpriteBatch.h
Go to the documentation of this file.
1
2/*
3 ██╗ ██╗ █████╗ ████████╗ █████╗ ███╗ ██╗ █████╗
4 ██║ ██╔╝ ██╔══██╗ ╚══██╔══╝ ██╔══██╗ ████╗ ██║ ██╔══██╗
5 █████╔╝ ███████║ ██║ ███████║ ██╔██╗ ██║ ███████║
6 ██╔═██╗ ██╔══██║ ██║ ██╔══██║ ██║╚██╗██║ ██╔══██║
7 ██║ ██╗ ██║ ██║ ██║ ██║ ██║ ██║ ╚████║ ██║ ██║
8 ╚═╝ ╚═╝ ╚═╝ ╚═╝/\ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝
9 /vvvvvvvvvvvvvvvvvvv \=========================================,
10 `^^^^^^^^^^^^^^^^^^^ /---------------------------------------"
11 Katana Engine \/ © 2012 - Shuriken Studios LLC
12 http://www.shurikenstudios.com
13*/
14
15#pragma once
16
17namespace KatanaEngine
18{
20 enum class TextAlign
21 {
22 Left,
23 Center,
24 Right
25 };
26
30 enum class SpriteSortMode
31 {
33 Deferred,
35 Immediate,
36 Texture
37 };
38
40 enum class BlendState
41 {
42 Alpha,
44 };
45
48 {
49
50 public:
51
52 SpriteBatch() { m_isStarted = false; }
54
59 void Begin(const SpriteSortMode sortMode = SpriteSortMode::Deferred,
60 const BlendState blendState = BlendState::Alpha,
61 ALLEGRO_TRANSFORM *pTransformation = NULL);
62
65 void End(/*bool test = false*/);
66
77 void DrawString(const Font *pFont, std::string *text, const Vector2 position,
78 const Color color = Color::White, const TextAlign alignment = TextAlign::Left,
79 const float drawDepth = 0);
80
93 void Draw(const Texture *pTexture, const Vector2 position, const Region region,
94 const Color color = Color::White, const Vector2 origin = Vector2::ZERO,
95 const Vector2 scale = Vector2::ONE, const float rotation = 0,
96 const float drawDepth = 0);
97
111 void Draw(const Texture *pTexture, const Vector2 position,
112 const Color color = Color::White, const Vector2 origin = Vector2::ZERO,
113 const Vector2 scale = Vector2::ONE, const float rotation = 0,
114 const float drawDepth = 0);
115
129 void Draw(Animation *pAnimation, const Vector2 position,
130 const Color color = Color::White, const Vector2 origin = Vector2::ZERO,
131 const Vector2 scale = Vector2::ONE, const float rotation = 0,
132 float drawDepth = 0);
133
138 void GetBatchSettings(SpriteSortMode &sortMode, BlendState &blendState, ALLEGRO_TRANSFORM *pTransformation);
139
140
141 private:
142
143 struct Drawable
144 {
145 bool isBitmap;
146 ALLEGRO_COLOR color;
147 int x, y;
148 float depth;
149
150 union
151 {
152 struct
153 {
154 ALLEGRO_FONT *pFont;
155 std::string *text;
156 TextAlign align;
157 };
158
159 struct
160 {
161 ALLEGRO_BITMAP *pBitmap;
162 float rotation;
163 int cx, cy;
164 int sx, sy, sw, sh;
165 float scx, scy;
166 unsigned short id;
167 };
168
169 } Union;
170
171 bool operator<(const Drawable& other) const
172 {
173 return (depth < other.depth);
174 }
175 };
176
177 struct CompareBackToFront
178 {
179 bool operator()(const Drawable* l, const Drawable* r) { return (*l < *r); }
180 };
181
182 struct CompareFrontToBack
183 {
184 bool operator()(const Drawable* l, const Drawable* r) { return (*r < *l); }
185 };
186
187 std::vector<Drawable *> m_drawables;
188 std::vector<Drawable *> m_inactiveDrawables;
189 std::vector<Drawable *>::iterator m_it;
190
191 unsigned short m_lastID = -1;
192
194
195 BlendState m_blendState = BlendState::Alpha;
196
197 ALLEGRO_TRANSFORM *m_pTransformation = nullptr;
198
199 bool m_isStarted = false;
200
201 void DrawBitmap(Drawable *drawable);
202 void DrawFont(Drawable *drawable);
203
204 };
205}
Represents timing and framing values for texture animations.
Definition Animation.h:21
Represents a four-component color using red, green, blue, and alpha data.
Definition Color.h:21
static const Color White
White.
Definition Color.h:201
Represents a font or true-type font.
Definition Font.h:21
Defines a rectangular region defined by a point, height, width.
Definition Region.h:21
Enables a group of sprites to be drawn using the same settings.
Definition SpriteBatch.h:48
void GetBatchSettings(SpriteSortMode &sortMode, BlendState &blendState, ALLEGRO_TRANSFORM *pTransformation)
Gets the current settings from the sprite batch.
void Begin(const SpriteSortMode sortMode=SpriteSortMode::Deferred, const BlendState blendState=BlendState::Alpha, ALLEGRO_TRANSFORM *pTransformation=NULL)
Begins a sprite batch operation.
void End()
Flushes the sprite batch and restores the device state to how it was before Begin was called.
void Draw(const Texture *pTexture, const Vector2 position, const Region region, const Color color=Color::White, const Vector2 origin=Vector2::ZERO, const Vector2 scale=Vector2::ONE, const float rotation=0, const float drawDepth=0)
Adds a sprite to a batch of sprites to be rendered.
void DrawString(const Font *pFont, std::string *text, const Vector2 position, const Color color=Color::White, const TextAlign alignment=TextAlign::Left, const float drawDepth=0)
Adds a string to a batch of sprites to be rendered.
Represents a 2D grid of texels.
Definition Texture.h:21
Defines a vector with 2 components (x and y).
Definition Vector2.h:21
static const Vector2 ZERO
A vector with both of its components set to zero.
Definition Vector2.h:31
static const Vector2 ONE
A vector with both of its components set to one.
Definition Vector2.h:32
Katana Engine is a library of classes, interfaces, and value types that provides a foundation for dev...
Definition Animation.cpp:18
SpriteSortMode
Defines the methods for sorting sprites before rendering.
Definition SpriteBatch.h:31
BlendState
Defines the way in which textures blending will be calculated.
Definition SpriteBatch.h:41
TextAlign
Defines the states for text alignment.
Definition SpriteBatch.h:21