Begginers Allegro Issue

So Hi I'm new to the forum and I've come across an issue, I'm trying to create a beginner pong game, but I'm getting this error after trying to add paddles or "bar" Unhandled exception at 0x00a517bd in Pongattempt1.exe: 0xC0000005: Access violation reading location 0x00000004.
Not really sure whats going on and have checked my code several times and even re-written a large part of it and can't seem to find what the issue is, thanks for any advice/help you can give.

#include <allegro.h>

#define DOWN_RIGHT 0
#define UP_RIGHT 1
#define DOWN_LEFT 2
#define UP_LEFT 3

void moveBall(void);
void respondToKeyboard(void);
void reverseVerticalDirection(void);
void reverseHorizontalDirection(void);

int ball_x;
int ball_y;
int direction;
int bar1;
int bar2;
BITMAP *ball;
BITMAP *bar;
BITMAP *buffer;
SAMPLE *boing;

int main(void)
{
allegro_init();
install_keyboard();
install_sound(DIGI_AUTODETECT,MIDI_AUTODETECT,NULL);
set_color_depth(16);
set_gfx_mode(GFX_AUTODETECT,640,480,0,0);
ball = load_bitmap("ball.bmp",NULL);
bar = load_bitmap("bar.bmp",NULL);
buffer = create_bitmap(SCREEN_W,SCREEN_H);
boing = load_sample("boing.wav");
ball_x = SCREEN_W/2;
ball_y = SCREEN_H/2;
bar1 = SCREEN_H/2;
bar2 = SCREEN_H/2;
srand(time(NULL));
direction = rand()%4;

while(!key[KEY_ESC])
{
moveBall();
respondToKeyboard();
clear_to_color(buffer,makecol(255,255,255));
blit(ball,buffer,0,0,ball_x,ball_y,ball->w,ball->h);
blit(bar,buffer,0,0,0,bar1,bar->w,bar->h);
blit(bar,buffer,0,0,620,bar2,bar->w,bar->h);
blit(buffer,screen,0,0,0,0,buffer->w,buffer->h);
clear_bitmap(buffer);
}

destroy_bitmap(ball);
destroy_bitmap(ball);
destroy_bitmap(buffer);
destroy_sample(boing);
return 0;
}
END_OF_MAIN()

void moveBall()
{
switch(direction){
case DOWN_RIGHT:
++ball_x;
++ball_y;
break;
case UP_RIGHT:
++ball_x;
--ball_y;
break;
case DOWN_LEFT:
--ball_x;
++ball_y;
break;
case UP_LEFT:
--ball_x;
--ball_y;
break;
}
if (ball_y <= 30 || ball_y >= 440)
reverseVerticalDirection();
if (ball_x <= 0 || ball_x >= 600)
reverseHorizontalDirection();
}

void respondToKeyboard()
{
if ( key[KEY_A])
bar1 -=3;
if ( key[KEY_Z])
bar1 +=3;

if ( key[KEY_UP])
bar2 -=3;
if ( key[KEY_DOWN])
bar2 +=3;

if ( bar1 < 30)
bar1 = 30;
else if ( bar1 > 380)
bar1 = 380;
if ( bar2 < 30)
bar2 = 30;
else if ( bar2 > 380)
bar2 = 380;
}
void reverseVerticalDirection()
{
if ((direction % 2) == 0)
++direction;
else
--direction;
play_sample(boing,255,128,1000,0);
}
void reverseHorizontalDirection()
{
direction = (direction + 2) %4;
play_sample(boing,255,128,1000,0);
}
Are you sure loading the images succeeded?
Well I had a similar issue with ball, as I typed pmp instead of bmp, but this time I'm sure it is spelt correctly, so I'm not sure what else could stop the file loading
First verify if it loads, if it doesn't that is likely the problem.
1
2
3
bar = load_bitmap("bar.bmp",NULL);
if(!bar)
     std::cout << "Error loading bar.bmp";
Thanks for the assistance, I modified that little bit of code with allegro_message and it did show that the image wasn't loading correctly, I checked the names carefully and they were a match, but after changing the name from bar to paddle and then renaming my variables it started to work.

Thank you very much for taking the time to help me with it.
Last edited on
Topic archived. No new replies allowed.