Here's my code:
#include <allegro5.h>
#include <allegro_native_dialog.h>
#include <allegro_primitives.h>
#include "objects.h"
// *** GLOBALS ***
const int WIDTH = 800, HEIGHT = 400, FPS = 60, NUM_BULLETS = 5;
enum KEYS { UP, DOWN, LEFT, RIGHT, SPACE };
bool keys[5] = { false, false, false, false, false };
// *** PROTOTYPES ***
void initShip(Spaceship & ship);
void drawShip(Spaceship & ship);
void moveShipUp(Spaceship & ship);
void moveShipDown(Spaceship & ship);
void moveShipLeft(Spaceship & ship);
void moveShipRight(Spaceship & ship);
void initBullet(Bullet bullet[], int size);
void drawBullet(Bullet bullet[], int size);
void fireBullet(Bullet bullet[], int size, Spaceship &ship);
void updateBullet(Bullet bullet[], int size);
int main(void)
{
// Init Allegro
if (!al_init()) {
al_show_native_message_box(NULL, "Error", "Error", "Failed to initialise allegro!", NULL, ALLEGRO_MESSAGEBOX_ERROR);
return -1;
}
// Primitive Variables
bool done = false, redraw = true;
// Object Variables
Spaceship ship;
Bullet bullets[5];
// Allegro Variables
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_TIMER *timer = NULL;
display = al_create_display(WIDTH, HEIGHT);
if (!display) {
al_show_native_message_box(NULL, "Error", "Error", "Failed to create display!", NULL, ALLEGRO_MESSAGEBOX_ERROR);
return -1;
}
al_install_keyboard();
al_init_primitives_addon();
event_queue = al_create_event_queue();
timer = al_create_timer(1.0 / FPS);
initShip(ship);
initBullet(bullets, NUM_BULLETS);
al_register_event_source(event_queue, al_get_keyboard_event_source());
al_register_event_source(event_queue, al_get_timer_event_source(timer));
al_register_event_source(event_queue, al_get_display_event_source(display));
al_start_timer(timer);
while (!done) {
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);
if (ev.type == ALLEGRO_EVENT_TIMER) {
redraw = true;
if (keys[UP])
moveShipUp(ship);
if (keys[DOWN])
moveShipDown(ship);
if (keys)
moveShipLeft(ship);
if (keys
)
moveShipRight(ship);
updateBullet(bullets, NUM_BULLETS);
} else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
done = true;
} else if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
switch (ev.keyboard.keycode) {
case ALLEGRO_KEY_ESCAPE:
done = true;
break;
case ALLEGRO_KEY_UP:
keys[UP] = true;
break;
case ALLEGRO_KEY_DOWN:
keys[DOWN] = true;
break;
case ALLEGRO_KEY_LEFT:
keys
= true;
break;
case ALLEGRO_KEY_RIGHT:
keys
= true;
break;
case ALLEGRO_KEY_SPACE:
keys[SPACE] = true;
fireBullet(bullets, NUM_BULLETS, ship);
break;
}
} else if (ev.type == ALLEGRO_EVENT_KEY_UP) {
switch (ev.keyboard.keycode) {
case ALLEGRO_KEY_ESCAPE:
done = true;
break;
case ALLEGRO_KEY_UP:
keys[UP] = false;
break;
case ALLEGRO_KEY_DOWN:
keys[DOWN] = false;
break;
case ALLEGRO_KEY_LEFT:
keys
= false;
break;
case ALLEGRO_KEY_RIGHT:
keys
= false;
break;
case ALLEGRO_KEY_SPACE:
keys[SPACE] = false;
break;
}
}
if (redraw && al_is_event_queue_empty(event_queue)) {
redraw = false;
drawShip(ship);
drawBullet(bullets, NUM_BULLETS);
al_flip_display();
al_clear_to_color(al_map_rgb(0, 0, 0));
}
}
al_destroy_display(display);
return 0;
}
void initShip(Spaceship & ship)
{
ship.x = 20;
ship.y = HEIGHT / 2;
ship.ID = PLAYER;
ship.lives = 3;
ship.speed = 7;
ship.boundsX = 6;
ship.boundsY = 7;
ship.score = 0;
}
void drawShip(Spaceship & ship)
{
al_draw_filled_rectangle(ship.x, ship.y - 9, ship.x + 10, ship.y - 7, al_map_rgb(255, 0, 0));
al_draw_filled_rectangle(ship.x, ship.y + 9, ship.x + 10, ship.y + 7, al_map_rgb(255, 0, 0));
al_draw_filled_triangle(ship.x - 12, ship.y - 17, ship.x + 12, ship.y, ship.x - 12, ship.y + 17, al_map_rgb(0, 255, 0));
al_draw_filled_rectangle(ship.x - 12, ship.y - 2, ship.x + 15, ship.y + 2, al_map_rgb(0, 0, 255));
}
void moveShipUp(Spaceship & ship)
{
ship.y -= ship.speed;
if (ship.y < 0)
ship.y = 0;
}
void moveShipDown(Spaceship & ship)
{
ship.y += ship.speed;
if (ship.y > HEIGHT)
ship.y = HEIGHT;
}
void moveShipLeft(Spaceship & ship)
{
ship.x -= ship.speed;
if (ship.x < 0)
ship.x = 0;
}
void moveShipRight(Spaceship & ship)
{
ship.x += ship.speed;
if (ship.x > 300)
ship.x = 300;
}
void initBullet(Bullet bullet[], int size)
{
for (int i = 0; i < 0; i++)
{
bullet.ID = BULLET;
bullet.speed = 10;
bullet.live = false;
}
}
void drawBullet(Bullet bullet[], int size)
{
for (int i = 0; i < size; i++)
{
if(bullet.live)
al_draw_filled_circle(bullet.x, bullet.y, 2, al_map_rgb(255, 255, 255));
}
}
void fireBullet(Bullet bullet[], int size, Spaceship &ship)
{
for (int i = 0; i < size; i++)
{
bullet.x = ship.x + 17;
bullet.y = ship.y;
bullet.live = true;
break;
}
}
void updateBullet(Bullet bullet[], int size)
{
for (int i = 0; i < size; i++)
{
if(bullet.live)
{
bullet.x += bullet.speed;
if(bullet.x > WIDTH)
bullet.live = false;
}
}
}
If anyone could tell me why this is happening that'd be greatly appreciated.