动画是利用人类的视觉特点被创造出来的艺术品。图像在不同的时间和位置出现可以产生一定的视觉特效。SDL程序可以制作动画。想要制作出动画的效果,就需要把握好单位时间内出现的图片数量这一个因素。一般来说,一秒24帧可以得到流畅的动画场景。那么两张图像的时间间隔保持约为41666 microseconds.
下面例子中的GIF图片是GifCam.exe截屏制成。
- 制作一个简单动画:
红色的圆圈不断扩大,周而复始。整个窗口在一段时间后自动关闭。
可以使用alarm函数可以实现“计时”,或者SDL_GetTicks判断时间再终止。想要之前所画的圆消失,可以用不同的颜色再画一次,将其抹去。
#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
#include <string.h>
#include <math.h>
#include <SDL_draw.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(){
SDL_Surface *screen = SDL_SetVideoMode(240,240,16,SDL_SWSURFACE);
if(screen == NULL){
fprintf(stderr,"SDL_SetVideoMode error %s\n",SDL_GetError());
exit(1);
}
if(SDL_Init(SDL_INIT_VIDEO) < 0){
fprintf(stderr,"SDL_INIT_VIDEO error %s\n",SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
int us = 41666*2;
while(1){
if(SDL_GetTicks()>10000) exit(0);
int r;
for(r=0;r<=80;r+=