-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCAnimate.cpp
480 lines (402 loc) · 7.54 KB
/
CAnimate.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#include "CAnimate.h"
CAnimate::CAnimate()
{
mView = true;
Setup();
}
void CAnimate::Setup()
{
int i;
mXProgress = 1.0; // Percentage of teh sprite to draw horizontally. From 0.0 to 1.0. Allows for progress bars.
for (i=0; i<10; i++)
{
mMaxFrames[i] = 0;
}
for (i = 0; i < 5; i++)
{
mAudio[i] = NULL;
}
mNumAudio = 0;
mTexture = NULL;
mFloor = 600;
mDelay = 0;
mTicks = 0;
mXPos = 0;
mYPos = 0;
W=0; H=0;
mStartFrame = 0;
mEndFrame = 0;
mCurFrame = 0;
mState = 0;
mAnimate = false;
mVelocity = 1;
mCurAnim = 0;
mActive = false;
mLoop = true;
mWrapAnimation = false;
mView = true;
Oscillate = false;
FrameInc = 1;
FrameRate = 50; //Milliseconds
OldTime = 0;
mShowHitBox = false;
mFade = false;
}
void CAnimate::ShowHitBox(bool b)
{
mShowHitBox = b;
}
void CAnimate::SetFloor(int f)
{
mFloor = f; // This is the lowest on the screen that you want the Move object to allow the sprite to go to.
}
int CAnimate::GetFloor()
{
return(mFloor);
}
int CAnimate::GetState()
{
return(mState);
}
//
// Set the current state of the sprite. The Transition() mehtod uses the state to determine
// what to do next.
//
void CAnimate::SetState(int s)
{
mState = s;
}
void CAnimate::SetActive(bool a)
{
mActive = a;
}
void CAnimate::MoveOffScreen()
{
SetPosX(-100 - GetWidth());
SetMoverState(false);
Stop();
}
void CAnimate::Reset()
{
Stop();
mCurAnim = 0;
mCurFrame = 0;
mState = 0;
MoveOffScreen();
mActive = false;
}
//
// Returns true if the sprite's hitbox is touching the specified rectangle.
//
bool CAnimate::Collide(CRectangle r)
{
CRectangle r2 = GetBounds();
if (r2.intersects(r))
return(true);
return(false);
}
//
// Return the hotbox for the sprite.
//
CRectangle CAnimate::GetBounds()
{
CRectangle r = {0,0,0,0};
if (mTexture != NULL)
{
r = mTexture->GetBounds();
r.x = r.x + mXPos;
r.y = r.y + mYPos;
}
return(r);
}
int CAnimate::GetWidth()
{
return(W);
}
int CAnimate::GetHeight()
{
return(H);
}
int CAnimate::GetVelocity()
{
return(mVelocity);
}
void CAnimate::SetVelocity(int v)
{
mVelocity = v;
}
//
// Turn teh sprite's mover object on and off.
//
void CAnimate::SetMoverState(bool b)
{
}
void CAnimate::SetTexture(CTexture *tex, int rows, int frames)
{
int i;
mMaxRows = rows;
for (i = 0; i < 10; i++)
{
mMaxFrames[i] = frames;
}
mTexture = tex;
if (tex != NULL)
{
W = mTexture->GetWidth() / frames;
H = mTexture->GetHeight() / rows;
}
}
//
// Set the number of frames for each row of the sprite's texture.
// Some animations will have the same number of frames.
//
void CAnimate::SetMaxFrames(int r0, int r1, int r2, int r3, int r4, int r5, int r6, int r7, int r8, int r9)
{
mMaxFrames[0] = r0;
mMaxFrames[1] = r1;
mMaxFrames[2] = r2;
mMaxFrames[3] = r3;
mMaxFrames[4] = r4;
mMaxFrames[5] = r5;
mMaxFrames[6] = r6;
mMaxFrames[7] = r7;
mMaxFrames[8] = r8;
mMaxFrames[9] = r9;
}
void CAnimate::SetMaxFrames(int row, int max)
{
mMaxFrames[row] = max;
}
void CAnimate::SetPos(int x, int y)
{
mXPos = x;
mYPos = y;
;
}
int CAnimate::GetPosX()
{
return(mXPos);
}
int CAnimate::GetPosY()
{
return(mYPos);
}
void CAnimate::SetPosX(int x)
{
mXPos = x;
}
void CAnimate::SetPosY(int y)
{
mYPos = y;
;
}
//
// Set the current visible frame for the animation.
//
void CAnimate::SetFrame(int n)
{
mCurFrame = n;
}
//
// Start the specified sprite animation.
//
void CAnimate::Start(int anim, bool loop)
{
mCurAnim = anim;
mStartFrame = 0;
mEndFrame = mMaxFrames[anim];
mCurFrame = 0;
mTicks = mDelay;
mAnimate = true;
mLoop = loop;
mActive = true;
mTexture->SetAlpha(0xFF);
PlayAudio(anim);
}
void CAnimate::Stop()
{
mAnimate = false;
}
//
// Default Transition() method. This jsut moves the sprote off teh screen at the end of the animation.
// A class can override this and transition to different animations based on what state they are in.
//
void CAnimate::Transition()
{
Reset();
MoveOffScreen();
}
//
// Performs thh animation by incrementing teh frame counter is the fame timer has elapsed.
// If the animation isn't repeating then the Transition() method is called when the last frame is reached.
//
bool CAnimate::Tick()
{
if (OldTime + FrameRate > SDL_GetTicks())
{
return(false);
}
OldTime = SDL_GetTicks();
if (mAnimate)
{
mCurFrame += FrameInc;
if (mFade)
{
Uint8 f = 0XFF - 128 * ((float)mCurFrame / (float)mMaxFrames[mCurAnim]);
if (mTexture!=NULL)
{
mTexture->SetAlpha(f);
}
}
if (Oscillate)
{
if (FrameInc > 0)
{
if (mCurFrame >= mMaxFrames[mCurAnim])
{
mCurFrame = mMaxFrames[mCurAnim] - 1;
FrameInc = -FrameInc;
}
}
else
{
if (mCurFrame <= 0)
{
mCurFrame = 0;
FrameInc = -FrameInc;
mAnimate = mLoop;
}
}
}
else
{
if (mCurFrame >= mMaxFrames[mCurAnim])
{
if (mWrapAnimation == true)
{
mCurFrame = 0;
mCurAnim++;
if (mCurAnim >= mMaxRows)
{
mCurAnim=0;
mAnimate = mLoop;
if (mAnimate == false)
{
Transition();
return(true);
}
}
}
else
{
mCurFrame = 0;
mAnimate = mLoop;
if (mAnimate == false)
{
Transition();
return(true);
}
}
}
}
}
return(false);
}
void CAnimate::OnAnimate()
{
//if (mAnimate == true)
//{
// mTicks--;
// if (mTicks > 0)
// return(false);
// else
// mTicks = mDelay;
// mCurFrame++;
// if (mCurFrame > mEndFrame)
// {
// if (mLoop)
// {
// mCurFrame = mStartFrame;
// }
// else
// {
// mCurFrame = mStartFrame;
// Stop();
// Transition();
// return(true);
// }
// }
//}
//return(false);
}
void CAnimate::SetFrameRate(int ms)
{
FrameRate = ms; // Set the number id milliseconds between frames.
}
void CAnimate::Move()
{
}
void CAnimate::Draw(SDL_Renderer *renderer)
{
// Draw the sprite to the screen.
if (mView == true)
{
if (mTexture != NULL)
{
SDL_Rect src;
// Determine if only part of the sprite needs to be draw. Used for progress bars.
int scaledW = W;
if (mXProgress != 1.0)
{
scaledW = (int)((float)W * mXProgress);
if (scaledW == 0)
{
return;
}
}
// Calculate the pixels to pull out of the texture.
src = { mCurFrame*W, mCurAnim*H, scaledW, H };
// SPecify where the pixels will go on the screen.
SDL_Rect dst = { mXPos, mYPos, scaledW, H };
// Send the pixels to the screen.
mTexture->Draw(renderer, &src, &dst, 0.0);
if (mShowHitBox)
{
// Draw the hitbox for debugging.
mTexture->DrawBounds(renderer, mXPos, mYPos);
}
}
}
}
void CAnimate::AddAudio(Mix_Chunk *audio)
{
if (mNumAudio < 5)
{
mAudio[mNumAudio] = audio;
mNumAudio++;
}
}
void CAnimate::PlayAudio(int i)
{
if (i < 5)
{
if (mAudio[i] != NULL)
{
Mix_PlayChannel(-1, mAudio[i], 0);
}
}
}
void CAnimate::SetAlpha(Uint8 alpha)
{
// Set the transparency level of the sprite from 0 - 255.
if (mTexture != NULL)
{
mTexture->SetAlpha(alpha);
}
}
Uint8 CAnimate::GetAlpha()
{
if (mTexture != NULL)
return(mTexture->GetAlpha());
return(0);
}