Code:
/** Include Header Files! */
#include <stdlib.h>
#include "allegro.h"
/** Set Project Definitions */
// Create some colors
#define WHITE makecol(255,255,255)
#define RED makecol(255,0,0)
#define BLUE makecol(0,0,255)
#define GREEN makecol(0,255,0)
#define ORANGE makecol(255,165,0)
#define YELLOW makecol(255,255,0)
// define a constant for number of objects to spawn
#define NUM 5
// creating a box structure
struct tagBOX{
int ctrx,ctry;
int left,top,right,bottom;
int color;
}mybox[NUM];
// creating circle structure
struct tagCIRCLE{
int x,y,radius,color;
}mycircle[NUM];
// set bitmap images
BITMAP *buffer;
// initialize box function
void setup_box()
{
int n;
for(n=0;n<NUM;n++)
{
mybox[n].ctrx = 20 + rand()%SCREEN_W - 20;
mybox[n].ctry = 50 + rand()%SCREEN_H - 20;
mybox[n].left = 10;
mybox[n].top = 10;
mybox[n].right = 10;
mybox[n].bottom = 10;
mybox[n].color = makecol(rand()%255,rand()%255,rand()%255);
}
}
// draw the box
void draw_box(int n)
{
int x1 = mybox[n].ctrx - mybox[n].left;
int y1 = mybox[n].ctry - mybox[n].top;
int x2 = mybox[n].ctrx + mybox[n].right;
int y2 = mybox[n].ctry + mybox[n].bottom;
int col = mybox[n].color;
rectfill(buffer,x1,y1,x2,y2,col);
}
// initialize circle function
void setup_circle()
{
int n;
for(n=0;n<NUM;n++)
{
mycircle[n].radius = 5 + rand()%25;
mycircle[n].x = mycircle[n].radius + rand()%SCREEN_W - mycircle[n].radius;
mycircle[n].y = mycircle[n].radius + rand()%SCREEN_H - mycircle[n].radius;
mycircle[n].color = makecol(rand()%255,rand()%255,rand()%255);
}
}
// draw the circle
void draw_circle(int n)
{
int r = mycircle[n].radius;
int x = mycircle[n].x;
int y = mycircle[n].y;
int col = mycircle[n].color;
circle(buffer,x,y,r,col);
}
/** Our main function */
void main(void)
{
int n;
//initialize program
allegro_init();
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_WINDOWED,640,480,0,0);
install_keyboard();
install_mouse();
install_timer();
srand(time(NULL));
// initialize our functions
setup_box();
setup_circle();
//CREATE SECONDARY SCREEN BUFFER
buffer=create_bitmap(640,480);
//display title
textprintf(buffer,font,0,1,WHITE,
"Welcome to Allegro, press ESC to Quit!");
//draw border around screen
/** rect(BITMAP,x1,y1,x2,y2,color); */
rect(buffer,1,12,SCREEN_W-2,SCREEN_H-2,RED);
//load the mouse cursor
show_mouse(buffer);
// main game loop
while(!key[KEY_ESC])
{
// draw objects
for(n=0;n<NUM;n++)
{
// draw box
draw_box(n);
// draw circle
draw_circle(n);
// draw box and location
textprintf(buffer,font,10,20+n*10,mybox[n].color,
"Box[%d],x:%d,y:%d",n,mybox[n].ctrx,mybox[n].ctry);
// draw box and location to box
textprintf(buffer,font,mybox[n].ctrx - mybox[n].left,
mybox[n].ctry - mybox[n].top - 12,mybox[n].color,
"Box[%d],x:%d,y:%d",n,mybox[n].ctrx,mybox[n].ctry);
// draw circle and location
textprintf(buffer,font,180,20+n*10,mycircle[n].color,
"Cir[%d],x:%d,y:%d",n,mycircle[n].x,mycircle[n].y);
// draw circle and location to circle
textprintf(buffer,font,mycircle[n].x,mycircle[n].y,
mycircle[n].color,"Cir[%d],x:%d,y:%d",n,mycircle[n].x,
mycircle[n].y);
}
//update screen
/** blit(BITMAP,BITMAP,x1,y1,x2,y2,image_w,image_h); */
blit(buffer,screen,0,0,0,0,640,480);
//slow the game down a bit
rest(20);
}
//free memory before exiting
allegro_exit();
}
END_OF_MAIN()