WoW Model Viewer
Your premiere tool for viewing, equipping and animating World of Warcraft models.
Loading...
Searching...
No Matches
AnimManager.cpp
Go to the documentation of this file.
1#include "AnimManager.h"
2#include "WoWModel.h"
3#include "Logger.h"
4
5#include "wow_enums.h"
6
8{
11 AnimIDMouth = -1;
12 Count = 1;
13 PlayIndex = 0;
14 CurLoop = 0;
15 animList[0].AnimID = 0;
16 animList[0].Loops = 0;
17
18 Frame = 0;
19 FrameSecondary = 0; // <-- Added initialization
20 FrameMouth = 0; // <-- Added initialization
21
22 if (model.anims.size() > 0)
23 {
24 TotalFrames = model.anims[0].length;
25 }
26 else
27 {
28 TotalFrames = 0;
29 }
30 Speed = 1.0f;
31 mouthSpeed = 1.0f;
32 Paused = false;
33}
34
36
37void AnimManager::SetCount(int count)
38{
39 Count = count;
40}
41
42void AnimManager::AddAnim(unsigned int id, short loops)
43{
44 if (Count > 3)
45 return;
46
47 animList[Count].AnimID = id;
48 animList[Count].Loops = loops;
49 Count++;
50}
51
52void AnimManager::SetAnim(short index, unsigned int id, short loops)
53{
54 // error check, we currently only support 4 animations.
55 if (index > 3)
56 return;
57
58 animList[index].AnimID = id;
59 animList[index].Loops = loops;
60
61 // Just an error check for our "auto animate"
62 if (index == 0)
63 {
64 Count = 1;
65 PlayIndex = index;
66 Frame = 0;
67 TotalFrames = model.anims[id].length;
68 }
69
70 if (index + 1 > Count)
71 Count = index + 1;
72}
73
75{
76 if (!Paused)
77 return;
78 Paused = false;
80 Paused = false;
81}
82
84{
85 Paused = true;
86 PlayIndex = 0;
87 SetFrame(0);
90}
91
92void AnimManager::Pause(bool force)
93{
94 if (Paused && force == false)
95 Paused = false;
96 else
97 Paused = true;
98}
99
101{
102 if (CurLoop == 1)
103 {
104 PlayIndex++;
105 if (PlayIndex >= Count)
106 {
107 Stop();
108 return;
109 }
111 }
112 else if (CurLoop > 1)
113 {
114 CurLoop--;
115 }
116 else if (CurLoop == 0)
117 {
118 PlayIndex++;
119 if (PlayIndex >= Count)
120 PlayIndex = 0;
121 }
122
123 Frame = 0;
125}
126
128{
129 if (CurLoop >= animList[PlayIndex].Loops)
130 {
131 PlayIndex--;
132
133 if (PlayIndex < 0)
134 {
135 Stop();
136 return;
137 }
139 }
140 else
141 {
142 CurLoop++;
143 }
144
147}
148
149int AnimManager::Tick(int time)
150{
151 if ((Count < PlayIndex))
152 return -1;
153
154 Frame += static_cast<int>(time * Speed);
155
156 // animate our mouth animation
157 if (AnimIDMouth > -1)
158 {
159 FrameMouth += (time * mouthSpeed);
160
161 if (FrameMouth >= model.anims[AnimIDMouth].length)
163 }
164
165 // animate our second (upper body) animation
166 if (AnimIDSecondary > -1)
167 {
168 FrameSecondary += (time * Speed);
169
172 }
173
174 if (Frame >= model.anims[animList[PlayIndex].AnimID].length)
175 {
176 Next();
177 return 1;
178 }
179
180 return 0;
181}
182
184{
185 return model.anims[animList[PlayIndex].AnimID].length;
186}
187
188void AnimManager::NextFrame() // Only called by the animation controls
189{
190 const ssize_t id = animList[PlayIndex].AnimID;
191 const ssize_t TimeDiff = (model.anims[id].length / 60);
192 Frame += TimeDiff;
193 ForceModelUpdate(TimeDiff);
194}
195
196void AnimManager::PrevFrame() // Only called by the animation controls
197{
198 const ssize_t id = animList[PlayIndex].AnimID;
199 const ssize_t TimeDiff = (model.anims[id].length / 60) * -1;
200 Frame += TimeDiff;
201 ForceModelUpdate(TimeDiff);
202}
203
204void AnimManager::SetFrame(size_t f) // Only called by the animation slider, or if Stop is called
205{
206 const ssize_t TimeDiff = f - Frame;
207 const ssize_t id = animList[PlayIndex].AnimID;
208
209 // ideal frame interval:
210 const int frameInterval = model.anims[id].length / 60;
211
212 uint i = 1;
213
214 // Update the model approx. ideal frame intervals, or rendering may not be smooth if slider is moved quickly...
215 if (frameInterval != 0)
216 i = round(abs(TimeDiff / frameInterval)); // Number of chunks to break it up into
217
218 if (i > 1)
219 {
220 for (uint j = 0; j < i; j++)
221 {
222 ForceModelUpdate(static_cast<float>(TimeDiff) / i);
223 }
224 }
225 else
226 {
227 ForceModelUpdate(TimeDiff);
228 }
229
230 Frame = f;
231}
232
234{
235 Stop();
236 Paused = true;
237 PlayIndex = 0;
238 Count = 0;
239 CurLoop = 0;
240 Frame = 0;
241}
242
244{
245 model.update(dt);
247 model.draw();
248}
size_t GetFrameCount()
void SetAnim(short index, unsigned int id, short loop)
size_t TotalFrames
Definition AnimManager.h:32
void Pause(bool force=false)
size_t SecondaryCount
Definition AnimManager.h:36
size_t FrameSecondary
Definition AnimManager.h:35
AnimInfo animList[4]
Definition AnimManager.h:29
ssize_t AnimIDMouth
Definition AnimManager.h:38
WoWModel & model
Definition AnimManager.h:25
void SetFrame(size_t f)
int Tick(int time)
short CurLoop
Definition AnimManager.h:43
void AddAnim(unsigned int id, short loop)
float mouthSpeed
Definition AnimManager.h:46
ssize_t AnimIDSecondary
Definition AnimManager.h:34
void SetCount(int count)
AnimManager(WoWModel &m)
size_t FrameMouth
Definition AnimManager.h:39
void ForceModelUpdate(float dt)
short PlayIndex
Definition AnimManager.h:42
size_t Frame
Definition AnimManager.h:31
Core WoW .m2 model: geometry, animation, textures, and character data.
Definition WoWModel.h:50
std::vector< ModelAnimation > anims
Definition WoWModel.h:178
void update(int dt)
void drawParticles()
void draw()
short Loops
Number of loops to play.
Definition AnimManager.h:15
size_t AnimID
Animation identifier.
Definition AnimManager.h:16
unsigned int uint
Definition types.h:36
@ UPPER_BODY_BONES
Definition wow_enums.h:75