1#include "Game/Engine/GraphicsContext.hpp"
2#include "Agui/Backends/Allegro5/Allegro5Font.hpp"
3
4namespace cge
5{
6 GraphicsContext::GraphicsContext
(void)
7 :m_shader
(NULL
),m_tripleBuffer
(false),
8 m_buffer
(NULL
),m_needsFlip
(false)
9 {
10 black
= al_map_rgb(0,
0,
0);
11 }
12
13 GraphicsContext::~GraphicsContext
(void)
14 {
15 if(m_buffer
)
16 {
17 delete m_buffer
;
18 m_buffer
= NULL
;
19 }
20 }
21
22 void GraphicsContext::drawSprite
( Sprite
* sprite,
float x,
float y,
int flags
)
23 {
24 al_draw_bitmap(sprite->getBitmap
(),x,y,flags
);
25 }
26
27 void GraphicsContext::drawTintedSprite
( Sprite
* sprite,
const Color
& tint,
float x,
float y,
int flags
)
28 {
29 al_draw_tinted_bitmap(sprite->getBitmap
(),tint.getColor
(),x,y,flags
);
30 }
31
32 void GraphicsContext::drawSpriteRegion
( Sprite
* sprite,
float sourceX,
float sourceY,
float sourceWidth,
float sourceHeight,
float x,
float y,
int flags
)
33 {
34 al_draw_bitmap_region(
35 sprite->getBitmap
(),
36 sourceX,
37 sourceY,
38 sourceWidth,
39 sourceHeight,
40 x,
41 y,
42 flags
);
43 }
44
45 void GraphicsContext::drawTintedSpriteRegion
( Sprite
* sprite,
const Color
& tint,
float sourceX,
float sourceY,
float sourceWidth,
float sourceHeight,
float x,
float y,
int flags
)
46 {
47 al_draw_tinted_bitmap_region(
48 sprite->getBitmap
(),
49 tint.getColor
(),
50 sourceX,
51 sourceY,
52 sourceWidth,
53 sourceHeight,
54 x,
55 y,
56 flags
);
57 }
58
59 void GraphicsContext::drawRotatedSprite
( Sprite
* sprite,
float centerX,
float centerY,
float x,
float y,
float radAngle,
int flags
)
60 {
61 al_draw_rotated_bitmap(
62 sprite->getBitmap
(),
63 centerX,
64 centerY,
65 x,
66 y,
67 radAngle,
68 flags
);
69 }
70
71 void GraphicsContext::drawTintedRotatedSprite
( Sprite
* sprite,
const Color
& tint,
float centerX,
float centerY,
float x,
float y,
float radAngle,
int flags
)
72 {
73 al_draw_tinted_rotated_bitmap(
74 sprite->getBitmap
(),
75 tint.getColor
(),
76 centerX,
77 centerY,
78 x,
79 y,
80 radAngle,
81 flags
);
82 }
83
84 void GraphicsContext::drawScaledRotatedSprite
( Sprite
* sprite,
float centerX,
float centerY,
float x,
float y,
float xScale,
float yScale,
float radAngle,
int flags
)
85 {
86 al_draw_scaled_rotated_bitmap(
87 sprite->getBitmap
(),
88 centerX,
89 centerY,
90 x,
91 y,
92 xScale,
93 yScale,
94 radAngle,
95 flags
);
96 }
97
98 void GraphicsContext::drawTintedScaledRotatedSprite
( Sprite
* sprite,
const Color
& tint,
float centerX,
float centerY,
float x,
float y,
float xScale,
float yScale,
float radAngle,
int flags
)
99 {
100 al_draw_tinted_scaled_rotated_bitmap(
101 sprite->getBitmap
(),
102 tint.getColor
(),
103 centerX,
104 centerY,
105 x,
106 y,
107 xScale,
108 yScale,
109 radAngle,
110 flags
);
111 }
112
113 void GraphicsContext::drawScaledSprite
( Sprite
*sprite,
float sourceX,
float sourceY,
float sourceWidth,
float sourceHeight,
float x,
float y,
float destinationWidth,
float destinationHeight,
int flags
)
114 {
115 al_draw_scaled_bitmap(
116 sprite->getBitmap
(),
117 sourceX,
118 sourceY,
119 sourceWidth,
120 sourceHeight,
121 x,
122 y,
123 destinationWidth,
124 destinationHeight,
125 flags
126 );
127 }
128
129 void GraphicsContext::clear
()
130 {
131 al_clear_to_color(black
);
132 }
133
134 void GraphicsContext::clear
( const Color
& color
)
135 {
136 al_clear_to_color(color.getColor
());
137 }
138
139 void GraphicsContext::clearTransparent
()
140 {
141 al_clear_to_color(al_map_rgba(0,
0,
0,
0));
142 }
143
144 void GraphicsContext::setTarget
( Sprite
* sprite
)
145 {
146 al_set_target_bitmap(sprite->getBitmap
());
147 }
148
149 void GraphicsContext::setTargetToBackbuffer
()
150 {
151 al_set_target_bitmap(al_get_backbuffer(al_get_current_display()));
152 }
153
154 void GraphicsContext::holdDrawing
()
155 {
156 al_hold_bitmap_drawing(true);
157 }
158
159 void GraphicsContext::unholdDrawing
()
160 {
161 al_hold_bitmap_drawing(false);
162 }
163
164 void GraphicsContext::flipDisplay
()
165 {
166 m_needsFlip
= true;
167
168 }
169
170 void GraphicsContext::setClippingRect
( int x,
int y,
int w,
int h
)
171 {
172 al_set_clipping_rectangle(x,y,w,h
);
173 }
174
175 void GraphicsContext::getClippingRect
( int&x,
int&y,
int&w,
int&h
)
176 {
177 al_get_clipping_rectangle(&x,
&y,
&w,
&h
);
178 }
179
180 void GraphicsContext::drawTintedScaledSprite
( Sprite
*sprite,
const Color
& color,
float sourceX,
float sourceY,
float sourceWidth,
float sourceHeight,
float x,
float y,
float destinationWidth,
float destinationHeight,
int flags
)
181 {
182 al_draw_tinted_scaled_rotated_bitmap(sprite->getBitmap
(),
183 color.getColor
(),sourceWidth
/ 2, sourceHeight
/ 2,x
+ (destinationWidth
/ 2),y
+ (destinationHeight
/ 2),destinationWidth
/ (float)sourceWidth,destinationHeight
/ (float)sourceHeight,
0.
0f,
0);
184 }
185
186 void GraphicsContext::drawText
(
187 const std::string
& text,Font
* font,
const Color
& color,
int x,
int y,
int flags
)
188 {
189 al_draw_text(font->getFont
(),
190 color.getColor
(),
191 x,y,flags,text.c_str
());
192 }
193
194 void GraphicsContext::drawText
(
195 const std::string
& text,agui::Font
* font,
const agui::Color
& color,
int x,
int y,
int flags
)
196 {
197 al_draw_text(((agui::Allegro5Font
*)font)->getFont
(),
198 al_map_rgba_f(color.getR
(),color.getG
(),color.getB
(),color.getA
()),
199 x,y,flags,text.c_str
());
200 }
201
202 void GraphicsContext::drawFilledRectangle
( int x,
int y,
int w,
int h,
const agui::Color
& color
)
203 {
204 al_draw_filled_rectangle(x,y,x
+ w,y
+ h,
al_map_rgba_f(color.getR
(),color.getG
(),color.getB
(),color.getA
()));
205 }
206
207 void GraphicsContext::setShader
( Shader
* shader
)
208 {
209 m_shader
= shader
;
210 }
211
212 void GraphicsContext::resetTransform
()
213 {
214 ALLEGRO_TRANSFORM t
;
215 al_identity_transform(&t
);
216 al_use_transform(&t
);
217 }
218
219 void GraphicsContext::useTransform
( Transformation
& t
)
220 {
221 al_use_transform(&t.getTransform
());
222 }
223
224 int GraphicsContext::drawRotatedText
( const std::string
& text,agui::Font
* font,
const agui::Color
& color,
int x,
int y,
float deg,
int flags
)
225 {
226 int fx
= font->getTextWidth
(text
) / 2;
227 int fy
= font->getLineHeight
() / 2;
228 m_transform.identity
();
229 m_transform.translate
(-fx,
-fy
);
230 m_transform.rotate
(deg
);
231 m_transform.translate
(x,y
);
232 useTransform
(m_transform
);
233 drawText
(text,
font,color,
0,
0,flags
);
234 resetTransform
();
235 return fx
* 2;
236 }
237
238 void GraphicsContext::begin
()
239 {
240 if(isBuffering
())
241 {
242 resetTransform
();
243 }
244
245 }
246
247 void GraphicsContext::resizeBuffer
(int w,
int h
)
248 {
249 if(isBuffering
())
250 {
251 delete m_buffer
;
252 m_buffer
= new Sprite
(w,h
);
253 }
254 }
255
256 bool GraphicsContext::isBuffering
() const
257 {
258 return m_tripleBuffer
;
259 }
260
261 void GraphicsContext::setBuffering
( bool buffering
)
262 {
263 m_tripleBuffer
= buffering
;
264 delete m_buffer
;
265 m_buffer
= NULL
;
266
267 if(buffering
)
268 {
269 resizeBuffer
(al_get_display_width(al_get_current_display())
270 ,
al_get_display_height(al_get_current_display()));
271 }
272 }
273
274 void GraphicsContext::end
()
275 {
276 if(m_needsFlip
)
277 {
278 m_needsFlip
= false;
279 al_flip_display();
280 }
281
282 }
283
284 void GraphicsContext::setTargetToBuffer
()
285 {
286 setTarget
(m_buffer
);
287 }
288
289 Sprite
* GraphicsContext::getBuffer
()
290 {
291 return m_buffer
;
292 }
293}