Figure 6.5
A single sprite is flipped both vertically and horizontally.
6.5 Drawing Rotated Sprites
Figure 8.7
The tank sprite (courtesy of Ari Feldman) rotated in eight directions.
Figure 8.8
The tank sprite (courtesy of Ari Feldman) rotated in 16 directions.
#include
#include
#define WHITE makecol(255,255,255)
int main(void)
{
int x, y;
float angle = 0;
//initialize program
allegro init();
install keyboard();
set color depth(32);
set gfx mode(GFX AUTODETECT WINDOWED, 640, 480, 0, 0);
textout ex(screen, font, "Rotate: LEFT / RIGHT arrow keys",
0, 0, WHITE, 0);
//load tank sprite
BITMAP *tank = load bitmap("tank.bmp", NULL);
//calculate center of screen
x = SCREEN W/2 - tank->w/2;
y = SCREEN H/2 - tank->h/2;
//draw tank at starting location
rotate sprite(screen, tank, x, y, 0);
//main loop
while(!key[KEY ESC])
{
//wait for keypress
if (keypressed())
{
//left arrow rotates left
if (key[KEY LEFT])
{
angle -= 0.1;
if (angle < 0) angle = 256;
rotate sprite(screen, tank, x, y, itofix(angle));
}
//right arrow rotates right
if (key[KEY RIGHT])
{
angle þ= 0.1;
if (angle > 256) angle = 0;
rotate sprite(screen, tank, x, y, itofix(angle));
}
Basic Sprite Handling 285
//display angle
textprintf ex(screen,font,0,10,WHITE,0,"Angle = %f", angle);
}
}
allegro exit();
return 0;
}
END OF MAIN()
Additional Rotation Functions
void rotate sprite v flip(BITMAP *bmp, BITMAP *sprite,
int x, int y, fixed angle)
The preceding function rotates and also flips the image vertically. To flip
horizontally, add itofix(128) to the angle. To flip in both directions, use
rotate sprite() and add itofix(128) to its angle.
void rotate scaled sprite(BITMAP *bmp, BITMAP *sprite,
int x, int y, fixed angle, fixed scale)
The preceding function rotates an image and scales (stretches to fit) the image at
the same time.
void rotate scaled sprite v flip(BITMAP *bmp, BITMAP *sprite,
int x, int y, fixed angle, fixed scale)
The preceding function rotates the image while also scaling and flipping it
vertically, simply combining the functionality of the previous two functions.
Chia sẻ với bạn bè của bạn: |