-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimatedTexture.cpp
More file actions
84 lines (59 loc) · 2.08 KB
/
AnimatedTexture.cpp
File metadata and controls
84 lines (59 loc) · 2.08 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
//-----------------------------------------------------------------//
// AnimatedTexture.cpp //
// Extends the Texture class' functionality to handle animations //
// Uses the Texture class' spritesheet constructor //
// //
// By: Ather Omar //
//-----------------------------------------------------------------//
#include "AnimatedTexture.h"
//---------------------------------------------------------------
// QuickSDL
//---------------------------------------------------------------
namespace QuickSDL {
AnimatedTexture::AnimatedTexture(std::string filename, int x, int y, int w, int h, int frameCount, float animationSpeed, ANIM_DIR animationDir)
: Texture(filename, x, y, w, h) {
mTimer = Timer::Instance();
mStartX = x;
mStartY = y;
mFrameCount = frameCount;
mAnimationSpeed = animationSpeed;
mTimePerFrame = mAnimationSpeed / mFrameCount;
mAnimationTimer = 0.0f;
mAnimationDirection = animationDir;
mAnimationDone = false;
mWrapMode = loop;
}
AnimatedTexture::~AnimatedTexture() {
}
void AnimatedTexture::WrapMode(WRAP_MODE mode) {
mWrapMode = mode;
}
void AnimatedTexture::ResetAnimation()
{
mAnimationTimer = 0.0f;
mAnimationDone = false;
}
bool AnimatedTexture::IsAnimating()
{
return !mAnimationDone;
}
void AnimatedTexture::Update() {
if(!mAnimationDone) {
mAnimationTimer += mTimer->DeltaTime();
if(mAnimationTimer >= mAnimationSpeed) {
//Only loop if the wrap mode is loop
if(mWrapMode == loop) {
mAnimationTimer -= mAnimationSpeed;
} else {
mAnimationDone = true;
mAnimationTimer = mAnimationSpeed - mTimePerFrame;
}
}
if(mAnimationDirection == horizontal) {
mClipRect.x = mStartX + (int)(mAnimationTimer / mTimePerFrame) * mWidth;
} else {
mClipRect.y = mStartY + (int)(mAnimationTimer / mTimePerFrame) * mHeight;
}
}
}
}