-
Notifications
You must be signed in to change notification settings - Fork 3
/
animationcontroller.cpp
106 lines (89 loc) · 1.94 KB
/
animationcontroller.cpp
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
102
103
104
105
106
#include "animationcontroller.h"
#include <cstdio>
AnimationController::AnimationController(){
speed = 1;
frameCount = 50;
currFrame = 0;
}
AnimationController::AnimationController(int numFrames){
speed = 1;
frameCount = numFrames;
currFrame = 0;
}
void AnimationController::setFrameCount(int numFrames){
if (currFrame >= numFrames){
currFrame = numFrames - 1;
}
frameCount = numFrames;
}
void AnimationController::setFrame(int frame)
{
if (frame < 0) {
currFrame = 0;
}
else if (frame > frameCount) {
currFrame = frameCount;
}
else {
currFrame = frame;
}
}
int AnimationController::getFrame(){
double currentTime = (double) clock();
if (playing){
currFrame++;
double time_passed =0;//TEMP: JUST ALWAYS GO TO NEXT FRAME (currentTime - startTime)/CLOCKS_PER_SEC;
if (time_passed > (1.0/speed)){
currFrame++;
printf("st %f, ct %f, ft %f\n", startTime, currentTime, 1.0/speed);
startTime = currentTime;
}
if (currFrame >= frameCount){
currFrame = 0;
}
}
//printf("Frame time : %f \n", currentTime - lastTime);
lastTime = currentTime;
return currFrame;
}
void AnimationController::setSpeed(float newSpeed){
speed = newSpeed;
ValidateSpeed();
}
float AnimationController::getSpeed(){
return speed;
}
void AnimationController::increaseSpeed(){
speed *= speedMultiplier;
ValidateSpeed();
}
void AnimationController::decreaseSpeed(){
speed /= speedMultiplier;
ValidateSpeed();
}
void AnimationController::ValidateSpeed(){
if (speed <= 0){
speed = 0.01;
}
}
void AnimationController::play(){
playing = true;
}
void AnimationController::pause(){
playing = false;
}
void AnimationController::togglePlay(){
playing = !playing;
}
void AnimationController::stepForward(){
currFrame++;
if (currFrame >= frameCount){
currFrame = 0;
}
}
void AnimationController::stepBackward(){
currFrame--;
if (currFrame < 0){
currFrame = frameCount - 1;
}
}