Sign in to follow this  
pi_man

Allegro bouncing balls

Recommended Posts

hi. i am very new to c++ and allegro so i am sorry if the answer to this is very simple. i have been trying to create a program to make balls bounce around the screen. it is not dinished yet but every time i try to run it i just get one of those error report messages from windows. any help will be greatly appreciated. here is my code so far: #include <allegro.h> #include <cstdlib> int Screen_Height = 480; int Screen_Width = 640; BITMAP* buffer = create_bitmap(Screen_Width, Screen_Height); int game_running = 0; int num = 9; struct circle { int rad; int x; int y; int xs; int ys; int r; int g; int b; } cir[9]; void init() { for(num = 0; num < 10; num++) { cir[num].rad = rand() % 10 + 1; cir[num].x = rand() % 620 + 1; cir[num].y = rand() % 470 + 1; cir[num].xs = rand() % 3 + 1; cir[num].ys = rand() % 3 + 1; cir[num].r = rand() % 255 + 1; cir[num].g = rand() % 255 + 1; cir[num].b = rand() % 255 + 1; cir[num].x += cir[num].xs; cir[num].y += cir[num].ys; } } void draw() { acquire_screen(); clear_bitmap(buffer); for(num = 0; num < 10; num++) { circlefill(buffer, cir[num].x, cir[num].y, cir[num].rad, makecol( cir[num].r, cir[num].g, cir[num].b)); } draw_sprite(screen, buffer, 0, 0); release_screen(); } void input() { for(num = 0; num < 10; num++) { if( cir[num].x == 0 + cir[num].rad) cir[num].xs += ( cir[num].xs * 2); else if( cir[num].x == Screen_Width - cir[num].rad) cir[num].xs -= ( cir[num].xs * 2); else if( cir[num].y == 0 + cir[num].rad) cir[num].ys += ( cir[num].ys * 2); else if( cir[num].y == Screen_Height - cir[num].rad) cir[num].ys -= (cir[num].ys * 2); } } int main(int argc, char** argv) { allegro_init(); install_keyboard(); set_color_depth(16); set_gfx_mode(GFX_AUTODETECT, Screen_Width, Screen_Height, 0, 0); init(); do{ input(); draw(); } while(game_running == 0); allegro_exit(); return 0; } END_OF_MAIN();

Share this post


Link to post
Share on other sites
Have not looked at all the code, but I spotted this right away...

Your array of structures has a size of 9, so you can only access elements 0 - 8, which means your init(), draw(), and input() functions are going out of bounds in their loops. Try using a constant for the size of the structure array, and use that constant in your loop conditions as well. If you ever need to change the size, you don't need to do it at every location of 10, or 9.

Maybe try something like this....



const unsigned short _num = 10;

....

cir[ _num];

....

for(int i=0;i < _num;i++) {
.....
}







Hope that helps! If the error comes back, post what that error reports and edit your post to use source tags. It makes viewing it a little easier on the eyes.

[Edited by - progek on May 8, 2007 4:50:02 AM]

Share this post


Link to post
Share on other sites
thank you for your help i have not done the alterations yet because i do not have time but i thank you for taking the time to help me. you have made my life sooo much easier!

Share this post


Link to post
Share on other sites
it still came up with an error report from windows. my code so far is:

#include <allegro.h>
#include <cstdlib>

int Screen_Height = 480;
int Screen_Width = 640;

BITMAP* buffer = create_bitmap(Screen_Width, Screen_Height);

int game_running = 0;
const unsigned short _num = 10;
int i;

struct circle
{
int rad;
int x;
int y;
int xs;
int ys;
int r;
int g;
int b;
} cir[_num];

void init()
{
for(i = 0; i < _num; i++)
{
cir[i].rad = rand() % 10 + 1;
cir[i].x = rand() % 620 + 1;
cir[i].y = rand() % 470 + 1;
cir[i].xs = rand() % 3 + 1;
cir[i].ys = rand() % 3 + 1;
cir[i].r = rand() % 255 + 1;
cir[i].g = rand() % 255 + 1;
cir[i].b = rand() % 255 + 1;

cir[i].x += cir[i].xs;
cir[i].y += cir[i].ys;
}
}

void draw()
{
acquire_screen();

clear_bitmap(buffer);

for(i = 0; i < 10; i++)
{
circlefill(buffer, cir[i].x, cir[i].y, cir[i].rad, makecol( cir[i].r, cir[i].g, cir[i].b));
}

draw_sprite(screen, buffer, 0, 0);

release_screen();
}

void input()
{
for(i = 0; i < 10; i++)
{
if( cir[i].x == 0 + cir[i].rad) cir[i].xs += ( cir[i].xs * 2);
else if( cir[i].x == Screen_Width - cir[i].rad) cir[i].xs -= ( cir[i].xs * 2);
else if( cir[i].y == 0 + cir[i].rad) cir[i].ys += ( cir[i].ys * 2);
else if( cir[i].y == Screen_Height - cir[i].rad) cir[i].ys -= (cir[i].ys * 2);
}
}

int main(int argc, char** argv)
{
allegro_init();
install_keyboard();
set_color_depth(16);
set_gfx_mode(GFX_AUTODETECT, Screen_Width, Screen_Height, 0, 0);

init();
do{
input();
draw();
} while(game_running == 0);
allegro_exit();
return 0;
}
END_OF_MAIN();


is this what you ment?

Share this post


Link to post
Share on other sites
Exactly what error are you getting?

A couple things, though. I don't think you necessarily need to call allegro_exit(). Thats just me, though, may be good style, but I thought the END_OF_MAIN() macro is all that is necessary.

Also, what he meant about using constants is that you should reuse them in things like loop invariants. For example, consider how you have multiple for loops like:

for(int i = 0; i < 10; i++)

What if you suddenly wanted to loop to run while i < 20? You'd have to go back and make that change for every corresponding for loop in your code. If you use a constant like _num in its placed, then all you'd have to do is change the value to _num at compile time and voila, the effect will automatically trickle down to dependant for loops.

Nice to see another Allegro programmer on here, though! I feel like i'm one of very few. Everyone seems to love SDL.

Share this post


Link to post
Share on other sites
thanks for clearing that up for me.

the error i am getting is that windows one that asks you to send an error report because the program wasn't responding

if it helps recently i have been getting the message

.drectve `/DEFAULTLIB:"uuid.lib" /DEFAULTLIB:"uuid.lib" ' unrecognized

in all of my alleg progs but they still run

Share this post


Link to post
Share on other sites
Hmm...seems one of two things may be happening then.

1. Your getting a segmentation fault. (Trying to access memory that doesn't belong to you). Check all your array indexes to make sure they're within the bounds.

2. Your program is running in an infinite loop. Which I believe it is seeing as your do..while loop looks like it will never break.

Share this post


Link to post
Share on other sites
BITMAP* buffer = create_bitmap(Screen_Width, Screen_Height);

I may be wrong, but I think that you can only define a bitmap after you have set the graphics mode.


BITMAP *buffer;

int main() {
allegro_init();
install_keyboard();
set_color_depth(16);
set_gfx_mode(GFX_AUTODETECT, Screen_Width, Screen_Height, 0, 0);

buffer = create_bitmap(Screen_Width, Screen_Height);

//do stuff
}


Share this post


Link to post
Share on other sites
sorry bout this but i just encountered a bug on my still unfinished program. i seems that only three of my balls will actually stay within the bounds of the screen and all the others simply fly off. if anyone could help that would be great.

my source so far is:

#include <allegro.h>
#include <cstdlib>

int Screen_Height = 480;
int Screen_Width = 640;
int radius = 5;

BITMAP* buffer;

int game_running = 0;
const unsigned short _num = 10;
int i;

struct circle
{
int rad;
int x;
int y;
int xs;
int ys;
int r;
int g;
int b;
} cir[_num];

void init()
{
for(i = 0; i < _num; i++)
{
cir[i].rad = radius;
cir[i].x = rand() % 640 + 1;
cir[i].y = rand() % 480 + 1;
cir[i].xs = rand() % -2 + 1;
cir[i].ys = rand() % -2 + 1;
cir[i].r = rand() % 255 + 1;
cir[i].g = rand() % 255 + 1;
cir[i].b = rand() % 255 + 1;
}
}

void draw()
{
acquire_screen();

clear_bitmap(buffer);

for(i = 0; i < _num; i++)
{
circlefill(buffer, cir[i].x, cir[i].y, cir[i].rad, makecol( cir[i].r, cir[i].g, cir[i].b));
}

draw_sprite(screen, buffer, 0, 0);

release_screen();
}

void input()
{
for(i = 0; i < _num; i++)
{
if( cir[i].x == 0 + cir[i].rad) cir[i].xs += 1;
else if( cir[i].x == Screen_Width - cir[i].rad) cir[i].xs -= ( cir[i].xs * 2);
else if( cir[i].y == 0 + cir[i].rad) cir[i].ys += 1;
else if( cir[i].y == Screen_Height - cir[i].rad) cir[i].ys -= (cir[i].ys * 2);

cir[i].x += cir[i].xs;
cir[i].y += cir[i].ys;
}
}

int main(int argc, char** argv)
{
allegro_init();
install_keyboard();
set_color_depth(16);
set_gfx_mode(GFX_AUTODETECT, Screen_Width, Screen_Height, 0, 0);
buffer = create_bitmap(Screen_Width, Screen_Height);

init();
do{
input();
draw();
if(key[KEY_ESC]) game_running = 1;
} while(game_running == 0);
allegro_exit();
return 0;
}
END_OF_MAIN();

Share this post


Link to post
Share on other sites
This one is a logic probelm. You can't expect ball to touch texactly the boundary .. Try out following code

void input()
{
for(i = 0; i < _num; i++)
{
if( cir[i].x <= 0 + cir[i].rad) cir[i].xs += 1;
else if( cir[i].x >= Screen_Width - cir[i].rad) cir[i].xs -= ( cir[i].xs * 2);
else if( cir[i].y <= 0 + cir[i].rad) cir[i].ys += 1;
else if( cir[i].y >= Screen_Height - cir[i].rad) cir[i].ys -= (cir[i].ys * 2);

cir[i].x += cir[i].xs;
cir[i].y += cir[i].ys;
}
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this