|
All bullets firing when I press my fire key |
ObscurePaulie
Member #15,956
May 2015
|
1#include <allegro5\allegro.h>
2#include <allegro5\allegro_native_dialog.h>
3#include <allegro5\allegro_primitives.h>
4#include <allegro5\allegro_font.h>
5#include <allegro5\allegro_ttf.h>
6#include <allegro5\allegro_image.h>
7#include <allegro5\allegro_audio.h>
8#include <allegro5\allegro_acodec.h>
9#include <string>
10#include <algorithm>
11#include <cmath>
12#include <fstream>
13#include "objects.h"
14
15const int ScreenWidth = 800;
16const int ScreenHeight = 600;
17const int NUM_ARROWS = 5;
18
19void initPlayer(Player &player);
20
21void initArrow(Arrow arrow[],int size, ALLEGRO_BITMAP *image);
22void drawArrow(Arrow arrow[],int size, ALLEGRO_BITMAP *image);
23void fireArrow(Arrow arrow[],int size, Player &player);
24void updateArrow(Arrow arrow[], int size);
25
26void CameraUpdate(float *cameraPosition, float x, float y, int width, int height)
27{
28 cameraPosition[0] = -(ScreenWidth / 2) + (x + width / 2);
29 cameraPosition[1] = -(ScreenHeight) + (y + height / 2);
30
31 if (cameraPosition[0] < 0)
32 cameraPosition[0] = 0;
33 if (cameraPosition[1] < 0)
34 cameraPosition[1] = 0;
35}
36
37int main()
38{
39 //primitive variable
40 bool done = false;
41 bool draw = true;
42 bool active = false;
43 bool jump = false;
44 const float FPS = 60;
45 const float frameFPS = 15;
46 float velX, velY;
47 velX = 0, velY = 0;
48 float jumpSpeed = 15;
49 const float gravity = 1;
50 int moveSpeed = 5;
51 enum Direction { DOWN, LEFT, RIGHT, UP };
52 int dir = DOWN, sourceX = 32, sourceY = 0;
53
54 //Allegro variables
55 ALLEGRO_DISPLAY *display = NULL;
56 ALLEGRO_EVENT_QUEUE *event_queue = NULL;
57 ALLEGRO_TIMER *timer = NULL;
58 ALLEGRO_TIMER *frameTimer = NULL;
59 ALLEGRO_KEYBOARD_STATE keyState;
60
61 //Initialization Functions
62 if (!al_init())
63 {
64 al_show_native_message_box(NULL, NULL, NULL, "Could not initialize ALLEGRO 5", NULL, NULL);
65 return -1;
66 }
67 display = al_create_display(ScreenWidth, ScreenHeight); //create our display object
68
69 if (!display) //test display object
70 return -1;
71
72 float cameraPosition[2] = { 0, 0 };
73
74 Arrow arrow[NUM_ARROWS];
75 Player player;
76
77 al_init_primitives_addon();
78 al_install_keyboard();
79 /*al_install_audio();
80 al_init_acodec_addon();*/
81 al_init_image_addon();
82
83 ALLEGRO_BITMAP *background = al_load_bitmap("dark background.png");
84 ALLEGRO_BITMAP *arrowImage = al_load_bitmap("Arrow.png");
85 ALLEGRO_BITMAP *player1 = al_load_bitmap("player.png");
86 al_convert_mask_to_alpha(player1, al_map_rgb(255, 0, 76));
87
88 initPlayer(player);
89 initArrow(arrow, NUM_ARROWS, arrowImage);
90
91 ALLEGRO_TRANSFORM camera;
92
93 event_queue = al_create_event_queue();
94 timer = al_create_timer(1.0 / FPS);
95 frameTimer = al_create_timer(1.0 / frameFPS);
96
97 al_register_event_source(event_queue, al_get_keyboard_event_source());
98 al_register_event_source(event_queue, al_get_timer_event_source(timer));
99 al_register_event_source(event_queue, al_get_timer_event_source(frameTimer));
100 al_register_event_source(event_queue, al_get_display_event_source(display));
101
102 al_start_timer(timer);
103 al_start_timer(frameTimer);
104
105 while (!done)
106 {
107 ALLEGRO_EVENT ev;
108 al_wait_for_event(event_queue, &ev);
109 al_get_keyboard_state(&keyState);
110
111 if (ev.type == ALLEGRO_EVENT_TIMER)
112 {
113 if (ev.timer.source == timer)
114 {
115 active = true;
116 if (al_key_down(&keyState, ALLEGRO_KEY_LEFT))
117 {
118 if (player.x <= 0)
119 player.x = 0;
120 else
121 {
122 velX = -player.speed;
123 dir = LEFT;
124 }
125 }
126
127 else if (al_key_down(&keyState, ALLEGRO_KEY_RIGHT))
128 {
129 velX = player.speed;
130 dir = RIGHT;
131 }
132
133 else if (al_key_down(&keyState, ALLEGRO_KEY_SPACE))
134 fireArrow(arrow, NUM_ARROWS, player);
135
136 else
137 {
138 velX = 0;
139 active = false;
140 }
141 if (al_key_down(&keyState, ALLEGRO_KEY_UP) && jump)
142 {
143 velY = -jumpSpeed;
144 jump = false;
145 }
146
147 else if (ev.timer.source = frameTimer)
148 {
149 if (active)
150 sourceX += al_get_bitmap_width(player1) / 3;
151 else
152 sourceX = 32;
153
154 if (sourceX >= al_get_bitmap_width(player1))
155 sourceX = 0;
156
157 sourceY = dir;
158 }
159
160 if (!jump)
161 velY += gravity;
162 else
163 velY = 0;
164
165 player.x += velX;
166 player.y += velY;
167
168 jump = (player.y + 32 >= 560);
169
170 if (jump)
171 player.y = 560 - 32;
172
173 draw = true;
174 updateArrow(arrow, NUM_ARROWS);
175 }
176 CameraUpdate(cameraPosition, player.x, player.y, 32, 32);
177
178 al_identity_transform(&camera);
179 al_translate_transform(&camera, -cameraPosition[0], -cameraPosition[1]);
180 al_use_transform(&camera);
181
182
183 else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
184 {
185 done = true;
186 }
187 else if (ev.type == ALLEGRO_EVENT_KEY_UP)
188 {
189 switch (ev.keyboard.keycode)
190 {
191 case ALLEGRO_KEY_ESCAPE:
192 done = true;
193 break;
194 }
195 }
196 if (draw)
197 {
198 al_draw_bitmap(background, 0, ScreenHeight - 800, 0);
199 drawArrow(arrow, NUM_ARROWS, arrowImage);
200 al_draw_bitmap_region(player1,sourceX, sourceY * al_get_bitmap_height(player1) / 4, 32, 32, player.x, player.y, 0);
201 al_draw_filled_rectangle(0, ScreenHeight, ScreenWidth * 2, ScreenHeight - 40, al_map_rgb(255, 0, 255));
202 al_flip_display();
203 al_clear_to_color(al_map_rgb(0, 0, 0));
204 }
205
206 }
207
208 al_destroy_display(display);
209 al_destroy_timer(timer);
210 al_destroy_event_queue(event_queue);
211 al_destroy_bitmap(player1);
212 al_destroy_bitmap(arrowImage);
213 al_destroy_bitmap(background);
214
215 return 0;
216}
217void initPlayer(Player &player)
218{
219 player.ID = PLAYER;
220 player.x = 10;
221 player.y = ScreenHeight - 25;
222 player.lives = 5;
223 player.score = 0;
224 player.speed = 5;
225 player.boundx = 5;
226 player.boundy = 5;
227
228}
229void initArrow(Arrow arrow[],int size, ALLEGRO_BITMAP *image)
230{
231 for (int i = 0; i < size; i++)
232 {
233 arrow[i].ID = ARROW;
234 arrow[i].speed = 4.0;
235 arrow[i].live = false;
236
237 arrow[i].image = image;
238 }
239}
240void drawArrow(Arrow arrow[],int size, ALLEGRO_BITMAP *image)
241{
242 for (int i = 0; i < size; i++)
243 {
244 if (arrow[i].live)
245 {
246 al_draw_bitmap(image, arrow[i].x, arrow[i].y, 0);
247 }
248 }
249}
250void fireArrow(Arrow arrow[],int size, Player &player)
251{
252 for (int i = 0; i < size; i++)
253 {
254 if (!arrow[i].live)
255 {
256 arrow[i].live = true;
257 arrow[i].x = player.x + 17;
258 arrow[i].y = player.y - 5;
259 break;
260 }
261 }
262}
263void updateArrow(Arrow arrow[], int size)
264{
265 for (int i = 0; i < size; i++)
266 {
267 if (arrow[i].live)
268 {
269 arrow[i].x += arrow[i].speed;
270 if (arrow[i].x >= ScreenWidth)
271 {
272 arrow[i].live = false;
273 }
274 }
275 }
276}
This is the code used in my fire function, I have a const int called NUM_ARROWS set to 5. I also have my arrows loading in from a bitmap and think this may be causing the problem. But I have followed a guide by Mike Geig and I used the same coding as him to get my arrows to fire. However, all my arrows fire at once even though I have the break in the loop. Which he says prevents this. Any help would be appreciated. PS. This is my first time using ALLEGRO 5 to create a 2D game and I'm trying to make a side scrolling RPG, if anyone has any tips or guides they could point me too that would also be a big help Edit: Thanks for the heads up on the code tags, sorry for not using them initially. This is my full code as of yet. |
bamccaig
Member #7,536
July 2006
|
Use <code> tags to get syntax highlighting and proper formatting of code on the forums (feel free to edit the OP to fix it if you wish). I suspect that the bug is outside of this function and instead in the caller. Programs execute very fast, and if you don't have the proper program structure in place to regulate when things happen and how quickly then a single key press from the user could result in a hundred "fire" events being triggered. You'd have to post more code to be sure. I don't see a particular problem here... -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
Edgar Reynaldo
Major Reynaldo
May 2007
|
Yes, you're checking once per timer whether to fire, and you fire one arrow per timer event, so in 10/60 second, you've fired all 10 of your arrows. You need to check for ALLEGRO_EVENT_KEY_DOWN or ALLEGRO_EVENT_KEY_UP, or use a timer variable to countdown until you can fire again and set it to the cooldown period when first fired. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
ObscurePaulie
Member #15,956
May 2015
|
Thanks for the help guys, I'll try it again in the morning, using the ALLEGRO_EVENT_KEY_DOWN and ALLEGRO_EVENT_KEY_UP and hopefully it will work. |
l j
Member #10,584
January 2009
|
else if (ev.timer.source = frameTimer) That's a pretty major bug as it will always evaluate to true unless frameTimer = 0. Minor nitpick, you loose arrows, not fire them.
|
|