Space Fighter
A "shmup" game for Computer Programming C++
Loading...
Searching...
No Matches
SpriteBatch.cpp
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#include "KatanaEngine.h"
16
17namespace KatanaEngine
18{
19 void SpriteBatch::Begin(const SpriteSortMode sortMode,
20 const BlendState blendState, ALLEGRO_TRANSFORM *pTransformation)
21 {
22 m_isStarted = true;
23 m_it = m_inactiveDrawables.begin();
24
25 m_sortMode = sortMode;
26
27 if (sortMode == SpriteSortMode::Immediate)
28 {
29 if (pTransformation != NULL) al_use_transform(pTransformation);
30 }
31 else
32 {
33 m_pTransformation = pTransformation;
34 }
35
36 m_blendState = blendState;
37
38 switch (blendState)
39 {
41 al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA);
42 break;
44 al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ONE);
45 break;
46 }
47 }
48
49 void SpriteBatch::End(/*bool test*/)
50 {
51 //double time = al_get_time();
52
53
54 if (m_sortMode != SpriteSortMode::Immediate)
55 {
56 if (m_pTransformation != NULL) al_use_transform(m_pTransformation);
57
58 switch (m_sortMode)
59 {
61 std::sort(m_drawables.begin(), m_drawables.end(), CompareBackToFront());
62 break;
63
65 std::sort(m_drawables.begin(), m_drawables.end(), CompareFrontToBack());
66 break;
67
69 al_hold_bitmap_drawing(true);
70 break;
71 };
72 //if (test) std::cout << "Sort time: " << (float)(al_get_time() - time) * 1000 << ", ";
73 //time = al_get_time();
74
75 for (std::vector<Drawable *>::iterator it = m_drawables.begin(); it != m_drawables.end(); ++it)
76 {
77 if ((*it)->isBitmap) DrawBitmap(*it);
78 else DrawFont(*it);
79 }
80
81 //if (test) std::cout << "Draw time: " << (float)(al_get_time() - time) * 1000 << ", ";
82 //time = al_get_time();
83 }
84
85 if (m_drawables.size() > 0) m_drawables.clear();
86
87 //if (test) std::cout << "Clear time: " << (float)(al_get_time() - time) * 1000 << std::endl;
88
89 if (m_sortMode == SpriteSortMode::Texture) al_hold_bitmap_drawing(false);
90
91 ALLEGRO_TRANSFORM identity;
92 al_identity_transform(&identity);
93 al_use_transform(&identity);
94
95 m_isStarted = false;
96 }
97
98 void SpriteBatch::DrawBitmap(Drawable *pDrawable)
99 {
100 al_draw_tinted_scaled_rotated_bitmap_region(
101 pDrawable->Union.pBitmap,
102 pDrawable->Union.sx, pDrawable->Union.sy,
103 pDrawable->Union.sw, pDrawable->Union.sh,
104 pDrawable->color,
105 pDrawable->Union.cx, pDrawable->Union.cy,
106 pDrawable->x, pDrawable->y,
107 pDrawable->Union.scx, pDrawable->Union.scy,
108 pDrawable->Union.rotation, 0);
109 }
110
111 void SpriteBatch::DrawFont(Drawable *pDrawable)
112 {
113 al_draw_multiline_text(
114 pDrawable->Union.pFont,
115 pDrawable->color,
116 pDrawable->x, pDrawable->y,
118 (int)pDrawable->Union.align,
119 pDrawable->Union.text->c_str());
120 }
121
122
123
124 void SpriteBatch::DrawString(const Font *pFont, std::string *text, const Vector2 position,
125 const Color color, const TextAlign alignment, const float drawDepth)
126 {
127 assert(m_isStarted && "Begin must be called before a Draw function can be run!");
128
129 Drawable *pDrawable;
130
131 if (m_it != m_inactiveDrawables.end())
132 {
133 pDrawable = *m_it;
134 m_it++;
135 }
136 else
137 {
138 pDrawable = new Drawable();
139 m_inactiveDrawables.push_back(pDrawable);
140 m_it = m_inactiveDrawables.end();
141 }
142
143 pDrawable->isBitmap = false;
144 pDrawable->Union.pFont = pFont->GetAllegroFont();
145 pDrawable->Union.text = text;
146 pDrawable->Union.align = alignment;
147 pDrawable->color = color.GetAllegroColor();
148 pDrawable->x = position.X;
149 pDrawable->y = position.Y;
150 pDrawable->depth = drawDepth;
151
152 if (m_sortMode == SpriteSortMode::Immediate)
153 {
154 DrawFont(pDrawable);
155 }
156 else
157 {
158 m_drawables.push_back(pDrawable);
159 }
160 }
161
162
163
164 void SpriteBatch::Draw(const Texture *pTexture, const Vector2 position,
165 const Color color, const Vector2 origin, const Vector2 scale,
166 const float rotation, const float drawDepth)
167 {
168 assert(m_isStarted && "Begin must be called before a Draw function can be run!");
169
170 Drawable *pDrawable;
171
172 if (m_it != m_inactiveDrawables.end())
173 {
174 pDrawable = *m_it;
175 m_it++;
176 }
177 else
178 {
179 pDrawable = new Drawable();
180 m_inactiveDrawables.push_back(pDrawable);
181 m_it = m_inactiveDrawables.end();
182 }
183
184 pDrawable->isBitmap = true;
185 pDrawable->Union.pBitmap = pTexture->GetAllegroBitmap();
186 pDrawable->Union.sx = 0;
187 pDrawable->Union.sy = 0;
188 pDrawable->Union.sw = pTexture->GetWidth();
189 pDrawable->Union.sh = pTexture->GetHeight();
190 pDrawable->Union.cx = origin.X;
191 pDrawable->Union.cy = origin.Y;
192 pDrawable->Union.scx = scale.X;
193 pDrawable->Union.scy = scale.Y;
194 pDrawable->Union.rotation = rotation;
195 pDrawable->Union.id = pTexture->GetResourceID();
196 pDrawable->color = color.GetAllegroColor();
197 pDrawable->x = position.X;
198 pDrawable->y = position.Y;
199 pDrawable->depth = drawDepth;
200
201 if (m_sortMode == SpriteSortMode::Immediate)
202 {
203 DrawBitmap(pDrawable);
204 }
205 else
206 {
207 m_drawables.push_back(pDrawable);
208 }
209 }
210
211
212 void SpriteBatch::Draw(const Texture *pTexture, const Vector2 position,
213 const Region region, const Color color, const Vector2 origin, const Vector2 scale,
214 const float rotation, const float drawDepth)
215 {
216 assert(m_isStarted && "Begin must be called before a Draw function can be run!");
217
218 Drawable *pDrawable;
219
220 if (m_it != m_inactiveDrawables.end())
221 {
222 pDrawable = *m_it;
223 m_it++;
224 }
225 else
226 {
227 pDrawable = new Drawable();
228 m_inactiveDrawables.push_back(pDrawable);
229 m_it = m_inactiveDrawables.end();
230 }
231
232 pDrawable->isBitmap = true;
233 pDrawable->Union.pBitmap = pTexture->GetAllegroBitmap();
234 pDrawable->Union.cx = origin.X;
235 pDrawable->Union.cy = origin.Y;
236 pDrawable->Union.scx = scale.X;
237 pDrawable->Union.scy = scale.Y;
238 pDrawable->Union.rotation = rotation;
239 pDrawable->Union.id = pTexture->GetResourceID();
240 pDrawable->color = color.GetAllegroColor();
241 pDrawable->x = position.X;
242 pDrawable->y = position.Y;
243 pDrawable->depth = drawDepth;
244
245 pDrawable->Union.sx = (&region)->X;
246 pDrawable->Union.sy = (&region)->Y;
247 pDrawable->Union.sw = (&region)->Width;
248 pDrawable->Union.sh = (&region)->Height;
249
250 if (m_sortMode == SpriteSortMode::Immediate)
251 {
252 DrawBitmap(pDrawable);
253 }
254 else
255 {
256 m_drawables.push_back(pDrawable);
257 }
258 }
259
260 void SpriteBatch::Draw(Animation *pAnimation, const Vector2 position,
261 const Color color, const Vector2 origin, const Vector2 scale,
262 const float rotation, float drawDepth)
263 {
264 Draw(pAnimation->GetTexture(), position, *pAnimation->GetCurrentFrame(),
265 color, origin, scale, rotation, drawDepth);
266 }
267
269 BlendState &blendState, ALLEGRO_TRANSFORM *pTransformation)
270 {
271 assert(m_isStarted && "Begin must be called before the settings can be retrieved.");
272
273 sortMode = m_sortMode;
274 blendState = m_blendState;
275 pTransformation = m_pTransformation;
276 }
277}
Represents timing and framing values for texture animations.
Definition Animation.h:21
virtual Texture * GetTexture() const
Gets a pointer to the texture of the animation.
Definition Animation.h:63
virtual Region * GetCurrentFrame()
Gets a pointer to the current frame.
Definition Animation.h:51
Represents a four-component color using red, green, blue, and alpha data.
Definition Color.h:21
ALLEGRO_COLOR GetAllegroColor() const
Get the color-equivalent allegro color.
Definition Color.h:57
Represents a font or true-type font.
Definition Font.h:21
virtual ALLEGRO_FONT * GetAllegroFont() const
Get the allegro font representation of the texture.
Definition Font.h:62
static int GetScreenWidth()
Gets the screen width in pixels.
Definition Game.h:36
Defines a rectangular region defined by a point, height, width.
Definition Region.h:21
unsigned short GetResourceID() const
Gets the ID of the resource.
Definition Resource.h:38
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
int GetHeight() const
Gets the height of the texture in pixels.
Definition Texture.h:40
int GetWidth() const
Gets the width of the texture in pixels.
Definition Texture.h:36
virtual ALLEGRO_BITMAP * GetAllegroBitmap() const
Get the allegro bitmap representation of the texture.
Definition Texture.h:57
Defines a vector with 2 components (x and y).
Definition Vector2.h:21
float Y
The y-coordinate of the vector.
Definition Vector2.h:187
float X
The x-coordinate of the vector.
Definition Vector2.h:186
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