struct Animation
{
//int delta_ms;//每帧的间隔时间
double delta;
//int time_ms;//当前过去的时间
clock_t time;//上一帧开始时间
int frameIndex;//当前帧下标
int frameCount;//帧总数
IMAGE *frames;//帧数组
Animation(IMAGE *im,int n)
{
frames = im; frameCount = n;
}
void animation_init(double delt)
{
delta = delt;
time= clock();
frameIndex = 0;
}
//void animation_update();
IMAGE* animation_get_frame(bool &next)
{
double dt = (double)(clock() - time) / CLOCKS_PER_SEC;
if (dt > delta)
{
frameIndex++;
if (frameIndex == frameCount - 1)frameIndex = 0;
time = clock();
next = true;
}
else
next = false;
return frames + frameIndex;
}
};
int main(int argc, char *argv[])
{
Init();
loadResource();
BeginBatchDraw();
printf("行走:\n");
Animation a0(&mm[0], 6);Animation a1(&mm[0], 6);
a0.animation_init(0.2);a1.animation_init(.3);
XY xy0(0, 0, 20, 0, WIDTH, HEIGHT);
XY xy1(200, 200, 0, 30, WIDTH, HEIGHT);
point xyo0(1,1); point xyo1(1, 1);
while (1)
{
cleardevice();
//1
bool next = false;
IMAGE *im = a0.animation_get_frame(next);
if (next)
{
xy0.update();
point xy = xy0.getxy();
xyo0 = xy;
}
putAlphaImage(xyo0.x, xyo0.y, im, 0);
//2
im = a1.animation_get_frame(next);
if (next)
{
xy1.update();
point xy = xy1.getxy();
xyo1 = xy;
}
putAlphaImage(xyo1.x, xyo1.y, im, 0);
FlushBatchDraw();
Sleep(1);
}
EndBatchDraw();
return 0;
}
