-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimator.c
More file actions
101 lines (101 loc) · 3.04 KB
/
Copy pathAnimator.c
File metadata and controls
101 lines (101 loc) · 3.04 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#ifndef ANIMATOR_C
#define ANIMATOR_C
#include "Library.h"
#include "Artist.c"
#include "Controller.c"
#include "Physics.c"
static int length = 0;
//Adds the sent frame of object's current animation into the FrameOrder
void addToOrder(GameObject* object, int frame)
{
static int dozen = 1;
struct line* _line = malloc(sizeof(struct line));
if (++length >= dozen * 10) realloc(order, sizeof(struct line) * 10 * (++dozen));
_line->msecs = object->animation[object->current_animation].msecs;
_line->frame = frame;
_line->object = object;
_line->sent_time = GetTickCount();
order[length - 1] = *_line;
}
//*Changes the object's current frame on the sent one.
//If animation doesn't have the sent frame and if the animation isn't looped then nothing adds into the FrameOrder
void updateSprite(GameObject* object, int frame)
{
object->animation[object->current_animation].frame = mainArray[object->animation[object->current_animation].id].data + mainArray[object->animation[object->current_animation].id].sizeX * mainArray[object->animation[object->current_animation].id].sizeY / object->animation[object->current_animation].frames * 3 * frame;
drawObject(object, object->x, object->y);
if (++frame != object->animation[object->current_animation].frames)
addToOrder(object, frame);
else
if (object->animation[object->current_animation].loop == 0)
{
object->busy = 0;
}
else addToOrder(object, 0);
}
//**Removes the frame from the FrameOrder via it's id
void removeFromOrder(int id)
{
register int i = id;
--length;
for (; i < length; i++)
order[i] = order[i + 1];
}
//Soft animation reset for the sent object
//Animations such as 'idle' or 'run' should be interruptable by 'jump'
int resetAnimation(GameObject* object)
{
register int i = 0;
if (object->animation[object->current_animation].interruptable == 1)
{
for (i; i < length; i++)
if (order[i].object == object)
{
removeFromOrder(i);
break;
}
return 1;
}
return 0;
}
//Global cycle which looks for the frame that should be updated
//Uses Ticks for checking frame's time
//If time has come:
//1. The function requests an update* for the frame
//2. The function requests the removal** of the frame
void iterate(int timer_id)
{
register int i = 0;
register long now = GetTickCount();
for (i; i < length; i++)
if (now - order[i].sent_time >= order[i].msecs)
{
updateSprite(order[i].object, order[i].frame);
removeFromOrder(i);
}
//playerController(0);
//i = 16 - now;
//if (i < 0) i = 0;
//else
//printf("---------\ANIM: %d\n---------", i);
glutTimerFunc(i, iterate, timer_id);
}
//Starts the sent id animation cycle for the sent object
//Just changes the current animation ID;
//Marks the object as 'busy';
//Requests the first animation frame's update*
void playAnimation(GameObject* object, int animation)
{
register int i = 0;
if (object->busy == 0)
{
for (; i < object->animations; i++)
if (object->animation[i].name == animation)
{
object->current_animation = animation;
object->busy = 1;
updateSprite(object, 0);
break;
}
}
}
#endif // !ANIMATOR_C