5.2.4 Drawing Circles and Ellipses
Allegro also provides functions for drawing circles and ellipses, as you will see.
The circle-drawing function is called circle, surprisingly enough. This function
takes a set of parameters very similar to those you have seen already—the destination bitmap, x, y, the radius, and the color.
Circles
The circle function has this declaration:
120 Chapter 3 n 2D Vector Graphics Programming
void circle(BITMAP *bmp, int x, int y, int radius, int color)
To demonstrate, the Circles program draws random circles on the screen, as
shown in Figure 3.6.
#include
#include
int main(void)
{
int x,y,radius;
int red,green,blue,color;
//initialize some stuff
allegro init();
install keyboard();
install timer();
srand(time(NULL));
//initialize video mode to 640x480
int ret = set gfx mode(GFX AUTODETECT WINDOWED, 640, 480, 0, 0);
if (ret != 0) {
Figure 3.6
The Circles program draws random circles on the screen.
allegro message(allegro error);
return 1;
}
//display screen resolution
textprintf ex(screen, font, 0, 0, 15, -1,
"Circles Program - %dx%d - Press ESC to quit",
SCREEN W, SCREEN H);
//wait for keypress
while(!key[KEY ESC])
{
//set a random location
x = 30 + rand() % (SCREEN W-60);
y = 30 + rand() % (SCREEN H-60);
radius = rand() % 30;
//set a random color
red = rand() % 255;
green = rand() % 255;
blue = rand() % 255;
color = makecol(red,green,blue);
//draw the circle
circle(screen, x, y, radius, color);
rest(25);
}
//end program
allegro exit();
return 0;
}
END OF MAIN()
Chia sẻ với bạn bè của bạn: |