-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathanimation.c
71 lines (63 loc) · 1.7 KB
/
animation.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* Twin - A Tiny Window System
* Copyright (c) 2024 National Cheng Kung University, Taiwan
* All rights reserved.
*/
#include <stdlib.h>
#include "twin.h"
twin_time_t twin_animation_get_current_delay(const twin_animation_t *anim)
{
if (!anim)
return 0;
return anim->iter->current_delay;
}
twin_pixmap_t *twin_animation_get_current_frame(const twin_animation_t *anim)
{
if (!anim)
return NULL;
return anim->iter->current_frame;
}
void twin_animation_advance_frame(twin_animation_t *anim)
{
if (!anim)
return;
twin_animation_iter_advance(anim->iter);
}
void twin_animation_destroy(twin_animation_t *anim)
{
if (!anim)
return;
free(anim->iter);
for (twin_count_t i = 0; i < anim->n_frames; i++) {
twin_pixmap_destroy(anim->frames[i]);
}
free(anim->frames);
free(anim->frame_delays);
free(anim);
}
twin_animation_iter_t *twin_animation_iter_init(twin_animation_t *anim)
{
twin_animation_iter_t *iter = malloc(sizeof(twin_animation_iter_t));
if (!iter || !anim)
return NULL;
iter->current_index = 0;
iter->current_frame = anim->frames[0];
iter->current_delay = anim->frame_delays[0];
anim->iter = iter;
iter->anim = anim;
return iter;
}
void twin_animation_iter_advance(twin_animation_iter_t *iter)
{
twin_animation_t *anim = iter->anim;
iter->current_index++;
if (iter->current_index >= anim->n_frames) {
if (anim->loop) {
iter->current_index = 0;
} else {
iter->current_index = anim->n_frames - 1;
}
}
iter->current_frame = anim->frames[iter->current_index];
iter->current_delay = anim->frame_delays[iter->current_index];
}