-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimateSpriteComponent.cpp
115 lines (99 loc) · 2.98 KB
/
AnimateSpriteComponent.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
#include "AnimateSpriteComponent.h"
#include <iostream>
#include "Actor.h"
AnimateSpriteComponent::AnimateSpriteComponent(Actor* actor, int drawOrder) : SpriteComponent(actor, drawOrder)
, mCurrentFrame(0.0f)
, mAnimFPS(10.0f)
, mCurrentAnimation()
, state(SDL_GetKeyboardState(NULL))
, flip(SDL_FLIP_NONE)
{
std::vector<Animation> mAnimations;
Animation tmp;
tmp.mAnimName = "tmp";
mCurrentAnimation = tmp;
}
void AnimateSpriteComponent::Update(float deltaTime)
{
//SpriteComponent::Update(deltaTime);
if (mAnimations.size() > 0)
{
// if the animation is non looping and has reached then last frame, do nothing
if (mCurrentFrame >= mCurrentAnimation.mEndPos && !mCurrentAnimation.mLoop)
{
return;
}
else {
// update current frame based on frame rate
// and delta time
mCurrentFrame += mAnimFPS * deltaTime;
}
// if the animation is looping and the last frame is reached, return to the first
// frame of the animation
if (static_cast<int>(mCurrentFrame) >= mCurrentAnimation.mEndPos && mCurrentAnimation.mLoop)
{
mCurrentFrame = static_cast<float>(mCurrentAnimation.mStartPos);
}
// set the current texture
// setTexture(mAnimSpriteSheets[static_cast<int>(mCurrentFrame)]);
}
}
void AnimateSpriteComponent::SetAnimationTextures(SDL_Texture* spriteSheet
, const std::string animName
, const bool looping
, const Vector2 frameSize)
{
// creating a vector of animations to draw from
int sheetWidth, sheetHeight;
Animation anim;
SDL_QueryTexture(spriteSheet, nullptr, nullptr, &sheetWidth, &sheetHeight);
anim.mAnimName = animName;
anim.mLoop = looping;
anim.flip = false;
anim.mStartPos = 1;
anim.mEndPos = sheetWidth / static_cast<int>(frameSize.x);
anim.mSpriteSheet = spriteSheet;
anim.mFrameSize = frameSize;
mAnimations.push_back(anim);
std::cout << "Added " << animName << " to the animation vector" << std::endl;
}
void AnimateSpriteComponent::Draw(SDL_Renderer* renderer)
{
if (mCurrentAnimation.mAnimName == "tmp")
{
return;
}
SDL_Rect rDest;
SDL_Rect rSrc;
// Scale the width and height of the tile on the screen
rDest.w = 90;
rDest.h = 90;
rDest.x = static_cast<int>(mOwner->GetPosition().x - rDest.w / 2);
rDest.y = static_cast<int>(mOwner->GetPosition().y - rDest.h / 2);
// The width and height of the tile on the tile set
rSrc.w = static_cast<int>(mCurrentAnimation.mFrameSize.x);
rSrc.h = static_cast<int>(mCurrentAnimation.mFrameSize.y);
rSrc.x = static_cast<int>(mCurrentFrame) % static_cast<int>(mCurrentAnimation.mFrameSize.x) * mCurrentAnimation.mFrameSize.x;
rSrc.y = 0;
// Draw
SDL_RenderCopyEx(renderer,
mCurrentAnimation.mSpriteSheet,
&rSrc,
&rDest,
-Math::ToDegrees(mOwner->GetRotation()),
nullptr,
flip);
}
void AnimateSpriteComponent::SetCurrentAnimation(const std::string animationName)
{
std::cout << "Setting current animation to " << animationName << "\n";
for (const auto& a : mAnimations)
{
if (a.mAnimName == animationName)
{
mCurrentAnimation = a;
mCurrentFrame = 1.0f;
return;
}
}
}