1int main
(void)
2{
3 //primitive variable*********************************************************************
4 bool done
= false;
5 bool redraw
= true;
6 const int FPS
= 60;
7 int state
= -1;
8 int level
= -1;
9 int imageRad
= 20;
10
11 int imageWidth
= 0;
12 int imageHeight
= 0;
13
14 //object variables***********************************************************************
15 Background BG
;
16 Background MG
;
17 Background FG
;
18
19 //Allegro variables**********************************************************************
20 ALLEGRO_DISPLAY *display
= NULL
;
21 ALLEGRO_EVENT_QUEUE *event_queue
= NULL
;
22 ALLEGRO_TIMER *timer
= NULL
;
23 ALLEGRO_FONT *font18
= NULL
;
24 ALLEGRO_BITMAP *shipImage
;
25 ALLEGRO_BITMAP *bulletImage
;
26 ALLEGRO_BITMAP *cometImage
;
27 ALLEGRO_BITMAP *expImage
;
28 ALLEGRO_BITMAP *title
= NULL
;
29 ALLEGRO_BITMAP *lost
= NULL
;
30 ALLEGRO_BITMAP *bgImage
= NULL
;
31 ALLEGRO_BITMAP *mgImage
= NULL
;
32 ALLEGRO_BITMAP *fgImage
= NULL
;
33 ALLEGRO_BITMAP *bossImage
;
34
35 //Initialization functions*********************************************************************
36 if(!al_init())
37 return -1;
38
39 display
= al_create_display(WIDTH, HEIGHT
);
40
41 if(!display
)
42 return -1;
43
44 al_init_primitives_addon();
45 al_install_keyboard();
46 al_init_font_addon();
47 al_init_ttf_addon();
48 al_init_image_addon();
49 al_install_audio();
50 al_init_acodec_addon();
51
52
53
54 event_queue
= al_create_event_queue();
55 timer
= al_create_timer(1.
0 / FPS
);
56
57 shipImage
= al_load_bitmap("Ship.png");
58 al_convert_mask_to_alpha(shipImage,
al_map_rgb(255,
0,
255));
59
60 bulletImage
= al_load_bitmap("Bullet.png");
61
62 cometImage
= al_load_bitmap("asteroid-1-96.png");
63
64 expImage
= al_load_bitmap("explosion_3_40_128-300x300.png");
65
66 bossImage
= al_load_bitmap("Boss.png");
67
68 title
= al_load_bitmap("Shooter_Title.png");
69 lost
= al_load_bitmap("Shooter_Lose.png");
70
71 bgImage
= al_load_bitmap("starBG.png");
72 mgImage
= al_load_bitmap("starMG.png");
73 fgImage
= al_load_bitmap("starFG.png");
74
75 al_reserve_samples(10);
76
77 shot
= al_load_sample("shot.wma");
78 boom
= al_load_sample("boom.wma");
79 song
= al_load_sample("song.mp3");
80
81 songInstance
= al_create_sample_instance(song
);
82 al_set_sample_instance_playmode(songInstance, ALLEGRO_PLAYMODE_LOOP
);
83
84 al_attach_sample_instance_to_mixer(songInstance,
al_get_default_mixer());
85
86
87 srand(time(NULL
));
88
89 ChangeState
(state, TITLE
);
90 ChangeLevel
(level, FIRST
);
91
92
93 InitShip
(ship, shipImage
);
94 InitBullet
(bullets, NUM_BULLETS, bulletImage
);
95 InitComet
(comets, NUM_COMETS, cometImage
);
96 InitExplosions
(explosions, NUM_EXPLOSIONS, expImage
);
97 InitBoss
(boss, NUM_BOSS, bossImage
);
98
99 InitBackground
(BG,
0,
0,
1,
0,
800,
400,
-1,
1, bgImage
);
100 InitBackground
(MG,
0,
0,
3,
0,
3200,
400,
-1,
1, mgImage
);
101 InitBackground
(FG,
0,
0,
10,
0,
800,
400,
-1,
1, fgImage
);
102
103 font18
= al_load_font("ariblk.ttf",
18,
0);
104
105 al_register_event_source(event_queue,
al_get_keyboard_event_source());
106 al_register_event_source(event_queue,
al_get_timer_event_source(timer
));
107 al_register_event_source(event_queue,
al_get_display_event_source(display
));
108
109 al_start_timer(timer
);
110
111
112 while(!done
)
113 {
114 ALLEGRO_EVENT ev
;
115 al_wait_for_event(event_queue,
&ev
);
116
117 if(ev.type
== ALLEGRO_EVENT_TIMER
)
118 {
119 redraw
= true;
120 if(keys
[UP
])
121 MoveShipUp
(ship
);
122 else if(keys
[DOWN
])
123 MoveShipDown
(ship
);
124 else
125 ResetShipAnimation
(ship,
1);
126 if(keys
[LEFT
])
127 MoveShipLeft
(ship
);
128 else if(keys
[RIGHT
])
129 MoveShipRight
(ship
);
130 else ResetShipAnimation
(ship,
2);
131
132 if(state
== TITLE
)
133 {}
134
135 //Entering the playing mode ===================================================================
136 else if(state
== PLAYING
)
137 {
138 if(ship.lives
>= 0)
139 {
140 UpdateBackground
(BG
);
141 UpdateBackground
(MG
);
142 UpdateBackground
(FG
);
143
144 //first phase==========================================================================
145 if(level
== FIRST
)
146 {
147 UpdateExplosions
(explosions, NUM_EXPLOSIONS
);
148 UpdateBullet
(bullets, NUM_BULLETS
);
149 StartComet
(comets, NUM_COMETS
);
150 UpdateComet
(comets, NUM_COMETS
);
151 CollideBullet
(bullets, NUM_BULLETS, comets, NUM_COMETS, boss, NUM_BOSS, ship,explosions, NUM_EXPLOSIONS
);
152 CollideComet
(comets, NUM_COMETS, ship, explosions, NUM_EXPLOSIONS
);
153
154 if(ship.score
== 5)
155 {
156 ChangeLevel
(level, ABOSS
); //when the score reaches 5, the game goes to the next phase
157 }
158 }
159
160 //first phase (boss)=====================================================================
161 else if(level
== ABOSS
)
162 {
163 if(boss
[1].health
>= 1)
164 {
165 UpdateExplosions
(explosions, NUM_EXPLOSIONS
);
166 UpdateBullet
(bullets, NUM_BULLETS
);
167 UpdateComet
(comets, NUM_COMETS
);
168 CollideBullet
(bullets, NUM_BULLETS, comets, NUM_COMETS, boss, NUM_BOSS, ship,explosions, NUM_EXPLOSIONS
);
169 CollideComet
(comets, NUM_COMETS, ship, explosions, NUM_EXPLOSIONS
);
170
171 StartBoss
(boss, NUM_BOSS
);
172 UpdateBoss
(boss, NUM_BOSS
);
173 }
174 else
175 ChangeLevel
(level, SECOND
); //once the boss dies, the second phase starts
176 }
177
178 //second phase ==========================================================================
179 else if(level
== SECOND
)
180 {
181 std::cout
<< "initial level loaded\n"; // <- This is not showing up in console
182 UpdateExplosions
(explosions, NUM_EXPLOSIONS
);
183 UpdateBullet
(bullets, NUM_BULLETS
);
184 StartComet
(comets, NUM_COMETS
);
185 UpdateComet
(comets, NUM_COMETS
);
186 CollideBullet
(bullets, NUM_BULLETS, comets, NUM_COMETS, boss, NUM_BOSS, ship,explosions, NUM_EXPLOSIONS
);
187 CollideComet
(comets, NUM_COMETS, ship, explosions, NUM_EXPLOSIONS
);
188 }
189 }
190
191 else if(ship.lives
<= 0)
192 ChangeState
(state, LOST
);
193 }
194
195 else if(state
== LOST
)
196 {}
197
198 }
199 else if(ev.type
== ALLEGRO_EVENT_DISPLAY_CLOSE
)
200 {
201 done
= true;
202 }
203 else if(ev.type
== ALLEGRO_EVENT_KEY_DOWN
)
204 {
205 switch(ev.keyboard.keycode
)
206 {
207 case ALLEGRO_KEY_ESCAPE:
208 done
= true;
209 break;
210 case ALLEGRO_KEY_UP:
211 keys
[UP
] = true;
212 break;
213 case ALLEGRO_KEY_DOWN:
214 keys
[DOWN
] = true;
215 break;
216 case ALLEGRO_KEY_LEFT:
217 keys
[LEFT
] = true;
218 break;
219 case ALLEGRO_KEY_RIGHT:
220 keys
[RIGHT
] = true;
221 break;
222 case ALLEGRO_KEY_SPACE:
223 keys
[SPACE
] = true;
224 if(state
== TITLE
)
225 ChangeState
(state, PLAYING
);
226 else if(state
== PLAYING
)
227 FireBullet
(bullets, NUM_BULLETS, ship
);
228 else if(state
== LOST
)
229 ChangeState
(state, PLAYING
);
230 break;
231 }
232 }
233 else if(ev.type
== ALLEGRO_EVENT_KEY_UP
)
234 {
235 switch(ev.keyboard.keycode
)
236 {
237 case ALLEGRO_KEY_UP:
238 keys
[UP
] = false;
239 break;
240 case ALLEGRO_KEY_DOWN:
241 keys
[DOWN
] = false;
242 break;
243 case ALLEGRO_KEY_LEFT:
244 keys
[LEFT
] = false;
245 break;
246 case ALLEGRO_KEY_RIGHT:
247 keys
[RIGHT
] = false;
248 break;
249 case ALLEGRO_KEY_SPACE:
250 keys
[SPACE
] = false;
251 break;
252 }
253 }
254
255 if(redraw
&& al_is_event_queue_empty(event_queue
))
256 {
257 redraw
= false;
258
259 if(state
== TITLE
)
260 {
261 al_draw_bitmap(title,
0,
0,
0);
262 }
263 else if(state
== PLAYING
)
264 {
265 DrawBackground
(BG
);
266 DrawBackground
(MG
);
267 DrawBackground
(FG
);
268
269 DrawShip
(ship
);
270 DrawBullet
(bullets, NUM_BULLETS
);
271 DrawComet
(comets, NUM_COMETS
);
272 DrawExplosions
(explosions, NUM_EXPLOSIONS
);
273 DrawBoss
(boss, NUM_BOSS
);
274
275 al_draw_textf(font18,
al_map_rgb(255,
0,
255),
5,
5,
0,
"%i lives left score: %i", ship.lives, ship.score
);
276 al_draw_textf(font18,
al_map_rgb(0,
255,
255),
10,
10,
0,
"%i lives left", boss
[1].health
);
277 }
278 else if(state
== LOST
)
279 {
280 al_draw_bitmap(lost,
0,
0,
0);
281
282 al_draw_textf(font18,
al_map_rgb(0,
0,
0), WIDTH
- 10,
20, ALLEGRO_ALIGN_RIGHT,
"FINAL SCORE: %i", ship.score
);
283 }
284
285
286 al_flip_display();
287 al_clear_to_color(al_map_rgb(0,
0,
0));
288
289 }
290 }
291
292 al_destroy_sample(shot
);
293 al_destroy_sample(boom
);
294 al_destroy_sample(song
);
295 al_destroy_sample_instance(songInstance
);
296 al_destroy_bitmap(bgImage
);
297 al_destroy_bitmap(mgImage
);
298 al_destroy_bitmap(fgImage
);
299 al_destroy_bitmap(title
);
300 al_destroy_bitmap(lost
);
301 al_destroy_bitmap(expImage
);
302 al_destroy_bitmap(cometImage
);
303 al_destroy_bitmap(shipImage
);
304 al_destroy_bitmap(bossImage
);
305 al_destroy_event_queue(event_queue
);
306 al_destroy_timer(timer
);
307 al_destroy_font(font18
);
308 al_destroy_display(display
);
309
310 return 0;
311}
312
313
314
315
316;