|
Variable 'arrow' is being used without being initialized! But it is... |
DJLad16
Member #14,857
January 2013
|
I've got one those annoying errors when your display pops up and gives an error and no other information. 1//Prototpyes
2void initPlayer(Player &player);
3
4void initCoin(Coin &coin1);
5void drawCoin(Coin &coin1, ALLEGRO_BITMAP *image);
6bool coinCollide(Coin &coin1,Player &player ,int cX, int cY, int cWidth, int cHeight, int pWidth, int pHeight, bool live, int coinCount);
7
8void initCoin2(Coin &coin2);
9void drawCoin2(Coin &coin2, ALLEGRO_BITMAP *image);
10bool coinCollide2(Coin &coin2,Player &player ,int cX, int cY, int cWidth, int cHeight, int pWidth, int pHeight, bool live, int coinCount);
11
12void initArrow(Arrow arrow);
13void drawArrow(Arrow arrow, ALLEGRO_BITMAP *image, ALLEGRO_BITMAP *image2);
14void fireArrow(Arrow arrow, Player &player, ALLEGRO_SAMPLE *bowShot);
15void updateArrow(Arrow arrow);
16
17bool collision(Player &player, int ex, int ey, int eWidth, int eHeight, int pWidth, int pHeight);
18
19void cameraUpdate(float *cameraPosition,Player &player, float width, float height);
20
21//Global Variables
22enum direction {LEFT, RIGHT};
23int dir = LEFT;
24//int arrowCount = 10;
25//const int NUM_ARROW = 10;
26const int NUM_ENEMY = 10;
27int groundHeight = 545;
28static bool fired = false;
29bool coinLive = false;
30float cameraX, cameraY = 0;
31
32int main()
33{
34 ALLEGRO_DISPLAY *display;
35
36 if(!al_init())
37 {
38 al_show_native_message_box(0, 0, "Error", "Falied to initialize allegro", 0, 0);
39 return -1;
40 }
41
42 display = al_create_display(ScreenWidth, ScreenHeight);
43
44 if(!display)
45 {
46 al_show_native_message_box(0, 0, "Error", "Falied to initialize the display", 0, 0);
47 return -1;
48 }
49
50 Player player;
51 Coin coin1;
52 Coin coin2;
53 Arrow arrow;
54 bool done = false, active = false, draw = true;
55 bool jump = false;
56 int sourceX = 32, sourceY = 0;
57 int coinCount = 0;
58 const float FPS = 60.0;
59 float gameFPS = 0;
60 float jumpSpeed = 15.0;
61 float velX = 0, velY = 0;
62 const float gravity = 1;
63 float cameraPosition[2] = {0,0};
64
65
66
67 al_init_image_addon();
68 al_init_primitives_addon();
69 al_install_keyboard();
70 al_init_font_addon();
71 al_init_ttf_addon();
72 al_install_audio();
73 al_init_acodec_addon();
74
75 initPlayer(player);
76 initArrow(arrow);
77 initCoin(coin1);
78 initCoin2(coin2);
79
80 ALLEGRO_KEYBOARD_STATE keystate;
81 ALLEGRO_TRANSFORM camera;
82 ALLEGRO_SAMPLE *coinCollect = al_load_sample("coin collect sound.wav");
83 ALLEGRO_SAMPLE *bowShot = al_load_sample("bow sound effect.wav");
84 ALLEGRO_SAMPLE *bgSong = al_load_sample("Background song.wav");
85 ALLEGRO_SAMPLE_INSTANCE *songInstance = al_create_sample_instance(bgSong);
86 ALLEGRO_BITMAP *background2 = al_load_bitmap("Background.png");
87 ALLEGRO_BITMAP *character = al_load_bitmap("spritesheet(Bow & left + right).png");
88 ALLEGRO_BITMAP *arrow_r = al_load_bitmap("Arrow(RIGHT).png");
89 ALLEGRO_BITMAP *arrow_l = al_load_bitmap("Arrow(LEFT).png");
90 ALLEGRO_BITMAP *coin = al_load_bitmap("coin2.png");
91 //ALLEGRO_BITMAP *key = al_load_bitmap("key.png");
92 ALLEGRO_BITMAP *ground = al_load_bitmap("Ground.png");
93 ALLEGRO_FONT *numArrow = al_load_font("JUNGBN__.TTF", 23, 0);
94 ALLEGRO_FONT *fps = al_load_font("JUNGBN__.TTF", 23, 0);
95 ALLEGRO_FONT *numCoin = al_load_font("JUNGBN__.TTF", 23, 0);
96 ALLEGRO_FONT *random = al_load_font("JUNGBN__.TTF", 23, 0);
97 ALLEGRO_TIMER *timer = al_create_timer(1.0/FPS);
98 ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue();
99
100 al_reserve_samples(3); //Respresents how many audio clips I have in the game
101
102 al_set_sample_instance_playmode(songInstance, ALLEGRO_PLAYMODE_LOOP);
103 al_attach_sample_instance_to_mixer(songInstance, al_get_default_mixer());
104 al_set_window_title(display, "Australian Outback");
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_play_sample_instance(songInstance);
110
111 al_start_timer(timer);
112 while(!done)
113 {
114 ALLEGRO_EVENT event;
115 al_wait_for_event(event_queue, &event);
116 al_get_keyboard_state(&keystate);
117
118 if(al_key_down(&keystate, ALLEGRO_KEY_ESCAPE))
119 {
120 done = true;
121 }
122 else if(event.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
123 {
124 done = true;
125 }
126
127
128 else if(event.type == ALLEGRO_EVENT_TIMER)
129 {
130 active = true;
131 updateArrow(arrow);
132
133 if(al_key_down(&keystate, ALLEGRO_KEY_D))
134 {
135 velX = player.speed;
136 dir = RIGHT;
137
138 }
139 else if(al_key_down(&keystate, ALLEGRO_KEY_A))
140 {
141 velX = -player.speed;
142 dir = LEFT;
143 }
144 else
145 {
146 velX = 0;
147 active = false;
148 }
149 if(al_key_down(&keystate, ALLEGRO_KEY_W) && jump)
150 {
151 velY = -jumpSpeed;
152 jump = false;
153 }
154 if(active)
155 {
156 sourceX += al_get_bitmap_width(character) / 3;
157 }
158 else
159 {
160 sourceX = 32;
161 }
162 if(sourceX >= al_get_bitmap_width(character))
163 {
164 sourceX = 0;
165 }
166 sourceY = dir;
167 draw = true;
168 if(!jump)
169 velY += gravity;
170 else
171 velY = 0;
172 player.x += velX;
173 player.y += velY;
174
175 jump = (player.y >= groundHeight);
176
177 if(jump)
178 {
179 player.y = groundHeight;
180 }
181
182 cameraUpdate(cameraPosition, player, 32, 32);
183 al_identity_transform(&camera);
184 al_translate_transform(&camera, -cameraPosition[0], -cameraPosition[1]);
185 al_use_transform(&camera);
186
187 }
188
189 if(al_key_down(&keystate, ALLEGRO_KEY_SPACE))
190 {
191 fireArrow(arrow, player, bowShot);
192 }
193 else
194 {
195 fired = false;
196 }
197 if(collision(player, 371, 470, 325, 30, 32, 32))
198 {
199 groundHeight = 470;
200 player.y = groundHeight;
201 player.y += velY;
202
203 }
204 else if(collision(player, 70, 390, 214, 38, 32, 32))
205 {
206 groundHeight = 390;
207 player.y = groundHeight;
208 player.y += velY;
209 }
210 else if(collision(player, 79, 275, 214, 30, 32, 32))
211 {
212 groundHeight = 275;
213 player.y = groundHeight;
214 player.y += velY;
215
216 }
217 else if(collision(player, 313, 220, 261, 30, 32, 32))
218 {
219 groundHeight = 220;
220 player.y = groundHeight;
221 player.y += velY;
222 }
223 else if(collision(player, 655, 165, 36, 30, 32, 32))
224 {
225 groundHeight = 165;
226 player.y = groundHeight;
227 player.y += velY;
228 }
229 else if(collision(player, 582, 70, 36, 30, 32, 32))
230 {
231 groundHeight = 70;
232 player.y = groundHeight;
233 player.y += velY;
234 }
235 else if(collision(player, 763, 30, 36, 30, 32, 32))
236 {
237 groundHeight = 30;
238 player.y = groundHeight;
239 player.y += velY;
240 }
241 else if(collision(player, 926, 77, 214, 38, 32, 32))
242 {
243 groundHeight = 77;
244 player.y = groundHeight;
245 player.y += velY;
246 }
247 else if(collision(player, 926, 140, 214, 38, 32, 32))
248 {
249 groundHeight = 140;
250 player.y = groundHeight;
251 player.y += velY;
252 }
253 else if(collision(player, 1202, 120, 36, 30, 32, 32))
254 {
255 groundHeight = 120;
256 player.y = groundHeight;
257 player.y += velY;
258 }
259 else
260 {
261 groundHeight = 545;
262 }
263
264 if(coinCollide(coin1, player, coin1.x, coin1.y, coin1.width, coin1.height, 32, 32, coinLive, coinCount))
265 {
266 coin1.live = false;
267 ++coinCount;
268 coin1.x = 0;
269 coin1.y = 0;
270 al_play_sample(coinCollect, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0);
271 }
272 if(coinCollide2(coin2, player, coin2.x, coin2.y, coin2.width, coin1.height, 32, 32, coinLive, coinCount))
273 {
274 coin2.live = false;
275 ++coinCount;
276 coin2.x = 0;
277 coin2.y = 0;
278 al_play_sample(coinCollect, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0);
279 }
280
281 if(draw && al_is_event_queue_empty(event_queue))
282 {
283 draw = false;
284 al_draw_bitmap(background2, 0, 0, 0);
285 al_draw_bitmap_region(character, sourceX, sourceY * al_get_bitmap_height(character) / 2, 32, 32, player.x, player.y, 0);
286 drawArrow(arrow, arrow_r, arrow_l);
287 drawCoin(coin1, coin);
288 drawCoin2(coin2, coin);
289 al_draw_textf(numArrow, al_map_rgb(252,209, 22), 10, 10, 0, "Arrows: %i", arrow.arrowCount);
290 al_draw_textf(numCoin, al_map_rgb(252, 209, 22), 10, 50, 0, "Coins: %i", coinCount);
291 //al_draw_textf(fps, al_map_rgb(252, 209, 22), 120, 10, 0, "FPS: %i", gameFPS);
292 al_flip_display();
293 al_clear_to_color(al_map_rgb(255, 255, 255));
294
295 }
296 }
297
298 al_destroy_bitmap(character);
299 al_destroy_bitmap(ground);
300 al_destroy_bitmap(coin);
301 al_destroy_bitmap(background2);
302 al_destroy_bitmap(arrow_l);
303 al_destroy_bitmap(arrow_r);
304 al_destroy_sample(coinCollect);
305 al_destroy_sample(bowShot);
306 al_destroy_sample(bgSong);
307 al_destroy_sample_instance(songInstance);
308 al_destroy_font(numArrow);
309 al_destroy_font(numCoin);
310 al_destroy_font(fps);
311 al_destroy_display(display);
312 al_destroy_event_queue(event_queue);
313 al_destroy_timer(timer);
314 return 0;
315}
316
317void initArrow(Arrow arrow)
318{
319 arrow.ID = ARROW;
320 arrow.speed = 4.0;
321 arrow.live = false;
322 arrow.arrowCount = 10;
323}
324void drawArrow(Arrow arrow, ALLEGRO_BITMAP *image, ALLEGRO_BITMAP *image2)
325{
326
327 if(arrow.live && RIGHT)
328 {
329 al_draw_bitmap(image, arrow.x, arrow.y, 0);
330 }
331 if(arrow.live && LEFT)
332 {
333 al_draw_bitmap(image2, arrow.x, arrow.y, 0);
334 }
335}
336void fireArrow(Arrow arrow, Player &player, ALLEGRO_SAMPLE *bowShot)
337{
338 if(!arrow.live && !fired )
339 {
340 --arrow.arrowCount;
341 arrow.live = true;
342 arrow.x = player.x + 17;
343 arrow.y = player.y + 22;
344 fired = true;
345 al_play_sample(bowShot, 1.0, 0.0, 1.0, ALLEGRO_PLAYMODE_ONCE, 0);
346 }
347
348}
349void updateArrow(Arrow arrow)
350{
351 if(arrow.live)
352 {
353 /*if(dir == RIGHT)
354 {*/
355 arrow.x += arrow.speed;
356 if(arrow.x >= 2000 - 10)
357 {
358 arrow.live = false;
359 }
360 //}
361 /*else if(dir == LEFT)
362 {
363 arrow.x -= arrow.speed;
364 if(arrow.x >= 2000 - 10)
365 {
366 arrow.live = false;
367 }
368 }*/
369 }
370}
I have not included some stuff to save space. If you want it all, just ask |
André Silva
Member #11,991
May 2010
|
I can see you already know about arguments by value and by reference.
|
DJLad16
Member #14,857
January 2013
|
Ah! I should have know that, thanks anyway |
|