/* * Example program for the Allegro library, by Shawn Hargreaves. * * This program demonstrates the use of triple buffering. Several * triangles are displayed rotating and bouncing on the screen * until you press a key. Note that on some platforms you * can't get real hardware triple buffering. The Allegro code * remains the same, but most likely the graphic driver will * emulate it. Unfortunately, in these cases you can't expect * the animation to be completely smooth and flicker free. */ #include #define NUM_SHAPES 16 typedef struct SHAPE { int color; /* color of the shape */ fixed x, y; /* centre of the shape */ fixed dir1, dir2, dir3; /* directions to the three corners */ fixed dist1, dist2, dist3; /* distances to the three corners */ fixed xc, yc, ac; /* position and angle change values */ } SHAPE; SHAPE shapes[NUM_SHAPES]; int triplebuffer_not_available = 0; /* randomly initialises a shape structure */ void init_shape(SHAPE *shape) { shape->color = 1+(AL_RAND()%15); /* randomly position the corners */ shape->dir1 = itofix(AL_RAND()%256); shape->dir2 = itofix(AL_RAND()%256); shape->dir3 = itofix(AL_RAND()%256); shape->dist1 = itofix(AL_RAND()%64); shape->dist2 = itofix(AL_RAND()%64); shape->dist3 = itofix(AL_RAND()%64); /* rand centre position and movement speed/direction */ shape->x = itofix(AL_RAND() % SCREEN_W); shape->y = itofix(AL_RAND() % SCREEN_H); shape->ac = itofix((AL_RAND()%9)-4); shape->xc = itofix((AL_RAND()%7)-2); shape->yc = itofix((AL_RAND()%7)-2); } /* updates the position of a shape structure */ void move_shape(SHAPE *shape) { shape->x += shape->xc; shape->y += shape->yc; shape->dir1 += shape->ac; shape->dir2 += shape->ac; shape->dir3 += shape->ac; if (((shape->x <= 0) && (shape->xc < 0)) || ((shape->x >= itofix(SCREEN_W)) && (shape->xc > 0))) { shape->xc = -shape->xc; shape->ac = itofix((AL_RAND()%9)-4); } if (((shape->y <= 0) && (shape->yc < 0)) || ((shape->y >= itofix(SCREEN_H)) && (shape->yc > 0))) { shape->yc = -shape->yc; shape->ac = itofix((AL_RAND()%9)-4); } } /* draws a frame of the animation */ void draw(BITMAP *b) { int c; char message[1024]; acquire_bitmap(b); clear_bitmap(b); for (c=0; c