1#include <allegro5/allegro.h>
2#include <allegro5/allegro_image.h>
3#include <allegro5/allegro_primitives.h>
4#include <allegro5/allegro_font.h>
5#include <allegro5\allegro_audio.h>
6#include <allegro5\allegro_acodec.h>
7#include <allegro5/allegro_ttf.h>
8#include <math.h>
9#include "objects.h"
10
11const int WIDTH
= 1000;
12const int HEIGHT
= 800;
13const int FPS
= 60;
14
15bool controls
[] = {false,
false,
false,
false,
false,
false,
false};
16enum CONTROLS
{W, S, A, D, SPACE, LClick, RClick
};
17enum STATE
{TITLE, PLAYING, END
};
18
19void ChangeState
(int &state,
int newState
);
20
21void InitPlayer
(Player
&player,
ALLEGRO_BITMAP *image
);
22void DrawPlayer
(Player
&player
);
23void MovePlayerW
(Player
&player
);
24void MovePlayerS
(Player
&player
);
25void MovePlayerA
(Player
&player
);
26void MovePlayerD
(Player
&player
);
27
28void InitBandit
(Bandit
&bandit,
ALLEGRO_BITMAP *image
);
29void DrawBandit
(Bandit
&bandit
);
30void StartBandit
(Bandit
&bandit, Player
&player
);
31void UpdateBandit
(Bandit
&bandit
);
32
33Player player
;
34Bandit bandit
;
35
36int main
(int argc,
char **argv
)
37{
38 //==============================================
39 //SHELL VARIABLES
40 //==============================================
41 bool done
= false;
42 bool render
= false;
43 //bool wonTheGame;
44 bool AudioToggleBool
= false;
45 bool Pause
= false;
46
47 float gameTime
= 0;
48 int inaccurateGameTime
= 0;
49 int frames
= 0;
50 int gameFPS
= 0;
51
52 //==============================================
53 //PROJECT VARIABLES
54 //==============================================
55
56 int state
= -1;
57
58 int mousex
= 0;
59 int mousey
= 0;
60 int boundmousex
= 12;
61 int boundmousey
= 12;
62
63 //==============================================
64 //ALLEGRO VARIABLES
65 //==============================================
66
67 ALLEGRO_EVENT_QUEUE *event_queue
= NULL
;
68 ALLEGRO_TIMER *timer
;
69 ALLEGRO_FONT *vikingFont
;
70 ALLEGRO_BITMAP *pausePic
;
71 ALLEGRO_BITMAP *mousePic
;
72 ALLEGRO_BITMAP *mousePicAttack
;
73 ALLEGRO_BITMAP *audioToggle
;
74 ALLEGRO_SAMPLE_INSTANCE *songInstance
= NULL
;
75
76 //==============================================
77 //ALLEGRO INIT FUNCTIONS
78 //==============================================
79 if(!al_init()) //initialize Allegro
80 return -1;
81
82 al_set_new_display_flags(ALLEGRO_FULLSCREEN
);
83 ALLEGRO_DISPLAY *display
= al_create_display(1000,
800); //create our display object
84
85 if(!display
) //test display object
86 return -1;
87
88 //==============================================
89 //ADDON INSTALL
90 //==============================================
91 al_install_keyboard();
92 al_install_mouse();
93 al_init_image_addon();
94 al_init_font_addon();
95 al_init_ttf_addon();
96 al_init_primitives_addon();
97 al_install_audio();
98 al_init_acodec_addon();
99
100
101 //==============================================
102 //PROJECT INIT
103 //==============================================
104//Fonts
105 vikingFont
= al_load_font("VIKING-N.ttf",
18,
0);
106//Bitmaps
107 audioToggle
= al_load_bitmap("AudioToggle.png");
108 mousePic
= al_load_bitmap("mousePic.png");
109 pausePic
= al_load_bitmap("PausePic.png");
110 mousePicAttack
= al_load_bitmap("mousePicAttack.png");
111//Audio
112
113//Display
114
115//Other
116 srand(time(NULL
));
117
118 ChangeState
(state, TITLE
);
119
120 //==============================================
121 //TIMER INIT AND STARTUP
122 //==============================================
123 event_queue
= al_create_event_queue();
124 timer
= al_create_timer(1.
0 / FPS
);
125
126 al_register_event_source(event_queue,
al_get_mouse_event_source());
127 al_register_event_source(event_queue,
al_get_timer_event_source(timer
));
128 al_register_event_source(event_queue,
al_get_keyboard_event_source());
129 al_register_event_source(event_queue,
al_get_display_event_source(display
));
130
131 al_hide_mouse_cursor(display
);
132
133 al_start_timer(timer
);
134 gameTime
= al_current_time
();
135 inaccurateGameTime
= al_current_time
();
136 while(!done
)
137 {
138 ALLEGRO_EVENT ev
;
139 al_wait_for_event(event_queue,
&ev
);
140
141 //==============================================
142 //INPUT
143 //==============================================
144 if (ev.type
== ALLEGRO_EVENT_TIMER
)
145 {
146 render
= true;
147 //UPDATE FPS===========
148 frames
++;
149 if(al_current_time
() - gameTime
>= 1)
150 {
151 gameTime
= al_current_time
();
152 inaccurateGameTime
= al_current_time
();
153 gameFPS
= frames
;
154 frames
= 0;
155 }
156 //=====================
157 if(Pause
== false)
158 {
159 if(controls
[W
])
160 MovePlayerW
(player
);
161 if(controls
[S
])
162 MovePlayerS
(player
);
163 if(controls
[A
])
164 MovePlayerA
(player
);
165 if(controls
[D
])
166 MovePlayerD
(player
);
167
168 if(state
== TITLE
)
169 {
170 //if(AudioToggleBool == false)
171 //al_play_sample_instance(songInstance);
172 }
173 else if(state
== PLAYING
)
174 {
175 //UpdateBG(bg);
176 //al_stop_sample_instance(songInstance);
177 }
178 else if(state
== END
)
179 {}
180 }
181 }
182 else if(ev.type
== ALLEGRO_EVENT_DISPLAY_CLOSE
)
183 {
184 done
= true;
185 }
186 else if(ev.type
== ALLEGRO_EVENT_KEY_DOWN
)
187 {
188 switch(ev.keyboard.keycode
)
189 {
190 case ALLEGRO_KEY_ESCAPE:
191 done
= true;
192 break;
193 case ALLEGRO_KEY_W:
194 controls
[W
] = true;
195 break;
196 case ALLEGRO_KEY_S:
197 controls
[S
] = true;
198 break;
199 case ALLEGRO_KEY_A:
200 controls
[A
] = true;
201 break;
202 case ALLEGRO_KEY_D:
203 controls
[D
] = true;
204 break;
205 case ALLEGRO_KEY_SPACE:
206 controls
[SPACE
] = true;
207 if(state
== TITLE
)
208 {}
209 else if(state
== PLAYING
)
210 {
211 ChangeState
(state, END
);
212 }
213 else if(state
== END
)
214 {
215 ChangeState
(state, PLAYING
);
216 }
217 break;
218 }
219 }
220 else if(ev.type
== ALLEGRO_EVENT_KEY_UP
)
221 {
222 switch(ev.keyboard.keycode
)
223 {
224 case ALLEGRO_KEY_ESCAPE:
225 done
= true;
226 break;
227 case ALLEGRO_KEY_W:
228 controls
[W
] = false;
229 break;
230 case ALLEGRO_KEY_S:
231 controls
[S
] = false;
232 break;
233 case ALLEGRO_KEY_A:
234 controls
[A
] = false;
235 break;
236 case ALLEGRO_KEY_D:
237 controls
[D
] = false;
238 break;
239 case ALLEGRO_KEY_SPACE:
240 controls
[SPACE
] = false;
241 break;
242 }
243 }
244 else if(ev.type
== ALLEGRO_EVENT_MOUSE_BUTTON_DOWN
)
245 {
246 if(ev.mouse.button
& 1)
247 {
248 controls
[LClick
] = true;
249
250 if(AudioToggleBool
== false && mousex
>= 968 &&
251 mousex
<= 995 && mousey
>= 5 && mousey
<= 37 && state
== TITLE
)
252 {
253 //al_stop_sample_instance(songInstance);
254 AudioToggleBool
= true;
255 }
256 else if(AudioToggleBool
== true && mousex
>= 968 &&
257 mousex
<= 995 && mousey
>= 5 && mousey
<= 37 && state
== TITLE
)
258 {
259 AudioToggleBool
= false;
260 //al_play_sample_instance(songInstance);
261 }
262
263 if(Pause
== false && mousex
>= 0 && mousex
<= 32 &&
264 mousey
>= 5 && mousey
<= 37 && state
== PLAYING
)
265 {
266 Pause
= true;
267 }
268 else if(Pause
== true && mousex
>= 0 && mousex
<= 32 &&
269 mousey
>= 5 && mousey
<= 37 && state
== PLAYING
)
270 {
271 Pause
= false;
272 }
273 }
274 else if (ev.mouse.button
& 2)
275 {
276 controls
[RClick
] = true;
277 }
278 }
279 else if(ev.type
== ALLEGRO_EVENT_MOUSE_BUTTON_UP
)
280 {
281 if(ev.mouse.button
& 1)
282 {
283 controls
[LClick
] = false;
284 }
285 else if (ev.mouse.button
& 2)
286 {
287 controls
[RClick
] = false;
288 }
289 }
290 else if(ev.type
== ALLEGRO_EVENT_MOUSE_AXES
)
291 {
292 mousex
= ev.mouse.x
;
293 mousey
= ev.mouse.y
;
294 }
295
296 //==============================================
297 //RENDER
298 //==============================================
299 if(render
&& al_is_event_queue_empty(event_queue
))
300 {
301 render
= false;
302 if(state
== TITLE
)
303 {
304 al_draw_bitmap(audioToggle,
968,
5,
0);
305
306 if(AudioToggleBool
== true)
307 {
308 al_draw_ellipse(979,
21,
15,
15,
al_map_rgb(255,
0,
0),
4);
309 al_draw_line(994,
13,
964,
28,
al_map_rgb(255,
0,
0),
4);
310 }
311
312 al_draw_bitmap(mousePic, mousex
- 5, mousey
- 4,
0);
313 }
314 else if(state
== PLAYING
)
315 {
316 al_draw_textf(vikingFont,
al_map_rgb(255,
255,
255),
900,
5,
0,
"FPS: %i", gameFPS
); //display FPS on screen
317
318 al_draw_bitmap(pausePic,
0,
5,
0);
319
320 if(Pause
== true)
321 al_draw_text(vikingFont,
al_map_rgb(255,
255,
255), WIDTH
/ 2, HEIGHT
/ 2 - 100, ALLEGRO_ALIGN_CENTRE,
"Pause");
322
323 if(mousex
<= bandit.x
- 10 && mousex
>= bandit.x
+ 10 &&
324 mousey
<= bandit.y
- 10 && mousey
>= bandit.y
+ 10)
325 al_draw_bitmap(mousePicAttack, mousex, mousey,
0);
326 }
327 else if(state
== END
)
328 {
329 //if(wonTheGame)
330 //{
331 //al_draw_bitmap(gameOverWin, 0, 0, 0);
332 //}
333 //else if(!wonTheGame)
334 //{
335 //al_draw_bitmap(gameOverLose, 0, 0, 0);
336 //}
337 }
338
339 //al_draw_bitmap(mousePic, mousex - 6, mousey - 6, 0);
340
341 //al_draw_filled_rectangle(mousex + 1, mousey + 1, mousex + 3,mousey + 3, al_map_rgba(255,0,255,1)); // Bounding Box for The Mouse
342 //BEGIN PROJECT RENDER================
343
344
345 //FLIP BUFFERS========================
346 al_flip_display();
347 al_clear_to_color(al_map_rgb(0,
0,
0));
348 }
349 }
350
351 //==============================================
352 //DESTROY PROJECT OBJECTS
353 //==============================================
354
355 //SHELL OBJECTS=================================
356 al_destroy_font(vikingFont
);
357 al_destroy_bitmap(mousePic
);
358 al_destroy_bitmap(mousePicAttack
);
359 al_destroy_bitmap(pausePic
);
360 al_destroy_bitmap(audioToggle
);
361 al_destroy_timer(timer
);
362 al_destroy_event_queue(event_queue
);
363 al_destroy_display(display
);
364 al_destroy_sample_instance(songInstance
);
365
366 return 0;
367}
368
369void ChangeState
(int &state,
int newState
)
370{
371 if(state
== TITLE
)
372 {}
373 else if(state
== PLAYING
)
374 {
375 //al_stop_sample_instance(songInstance);
376 }
377 else if(state
== END
)
378 {}
379
380 state
= newState
;
381
382 if(state
== TITLE
)
383 {}
384 else if(state
== PLAYING
)
385 {
386 //al_play_sample_instance(songInstance);
387 }
388 else if(state
== END
)
389 {}
390}
391
392void InitPlayer
(Player
&player,
ALLEGRO_BITMAP *image
= NULL
)
393{
394 player.ID
= PLAYER
;
395 player.x
= 20;
396 player.y
= HEIGHT
/ 2;
397 player.health
= 100;
398 player.lives
= 3;
399 player.speed
= 1;
400 player.score
= 0;
401 player.boundx
= 10;
402 player.boundy
= 10;
403
404 if(player.image
!= NULL
)
405 player.image
= image
;
406}
407
408void DrawPlayer
(Player
&player
)
409{
410 al_draw_bitmap(player.image, player.x, player.y,
0);
411}
412
413void MovePlayerW
(Player
&player
)
414{
415 player.y
-= player.speed
;
416 if(player.y
< 0)
417 player.y
= 0;
418}
419void MovePlayerS
(Player
&player
)
420{
421 player.y
+= player.speed
;
422 if(player.y
> HEIGHT
)
423 player.y
= HEIGHT
;
424}
425
426void MovePlayerA
(Player
&player
)
427{
428 player.x
-= player.speed
;
429 if(player.x
< 0)
430 player.x
= 0;
431}
432
433void MovePlayerD
(Player
&player
)
434{
435 player.x
+= player.speed
;
436 if(player.x
> 350)
437 player.x
= 350;
438}
439
440void InitBandit
(Bandit
&bandit,
ALLEGRO_BITMAP *image
= NULL
)
441{
442 bandit.ID
= BANDIT
;
443 bandit.live
= false;
444 bandit.x
= WIDTH
/ 2;
445 bandit.y
= HEIGHT
/ 2;
446 bandit.speed
= 1;
447 bandit.boundx
= 10;
448 bandit.boundy
= 10;
449 bandit.health
= 20;
450
451 if(bandit.image
!= NULL
)
452 bandit.image
= image
;
453}
454
455void DrawBandit
(Bandit
&draugr
)
456{
457 if(bandit.live
)
458 al_draw_bitmap(bandit.image, bandit.x, bandit.y,
0);
459}
460
461void StartBandit
(Bandit
&bandit, Player
&player
)
462{
463 if(player.x
== 50)
464 bandit.live
= true;
465}
466
467void UpdateBandit
(Bandit
&bandit
)
468{
469
470}