Allegro bouncing balls

Started by
10 comments, last by AmolValmikPatil 12 years, 11 months ago
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();
Advertisement
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]
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!
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.rad = rand() % 10 + 1;
cir.x = rand() % 620 + 1;
cir.y = rand() % 470 + 1;
cir.xs = rand() % 3 + 1;
cir.ys = rand() % 3 + 1;
cir.r = rand() % 255 + 1;
cir.g = rand() % 255 + 1;
cir.b = rand() % 255 + 1;

cir.x += cir.xs;
cir.y += cir.ys;
}
}

void draw()
{
acquire_screen();

clear_bitmap(buffer);

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

draw_sprite(screen, buffer, 0, 0);

release_screen();
}

void input()
{
for(i = 0; i < 10; i++)
{
if( cir.x == 0 + cir.rad) cir.xs += ( cir.xs * 2);
else if( cir.x == Screen_Width - cir.rad) cir.xs -= ( cir.xs * 2);
else if( cir.y == 0 + cir.rad) cir.ys += ( cir.ys * 2);
else if( cir.y == Screen_Height - cir.rad) cir.ys -= (cir.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?
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.
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
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.
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}

Wanna help spread Firefox? http://www.spreadfirefox.com/?q=user/register&r=126078
okay thanks i'll try that now
That buffer thing worked! thanks sooo much.

This topic is closed to new replies.

Advertisement