File: AnimatedSprite.cpp

package info (click to toggle)
jazz2-native 3.5.0-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 16,912 kB
  • sloc: cpp: 172,557; xml: 113; python: 36; makefile: 5; sh: 2
file content (142 lines) | stat: -rw-r--r-- 3,399 bytes parent folder | download
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
#include "AnimatedSprite.h"
#include "../../Main.h"

namespace nCine
{
	AnimatedSprite::AnimatedSprite()
		: AnimatedSprite(nullptr, nullptr, 0.0f, 0.0f)
	{
	}

	AnimatedSprite::AnimatedSprite(SceneNode* parent, Texture* texture)
		: AnimatedSprite(parent, texture, 0.0f, 0.0f)
	{
	}

	AnimatedSprite::AnimatedSprite(Texture* texture)
		: AnimatedSprite(nullptr, texture, 0.0f, 0.0f)
	{
	}

	AnimatedSprite::AnimatedSprite(SceneNode* parent, Texture* texture, float xx, float yy)
		: Sprite(parent, texture, xx, yy), anims_(4), currentAnimIndex_(0)
	{
		_type = ObjectType::AnimatedSprite;
	}

	AnimatedSprite::AnimatedSprite(SceneNode* parent, Texture* texture, Vector2f position)
		: AnimatedSprite(parent, texture, position.X, position.Y)
	{
	}

	AnimatedSprite::AnimatedSprite(Texture* texture, float xx, float yy)
		: AnimatedSprite(nullptr, texture, xx, yy)
	{
	}

	AnimatedSprite::AnimatedSprite(Texture* texture, Vector2f position)
		: AnimatedSprite(nullptr, texture, position.X, position.Y)
	{
	}

	bool AnimatedSprite::isPaused() const
	{
		bool isPaused = true;
		if (!anims_.empty()) {
			isPaused = anims_[currentAnimIndex_].isPaused();
		}
		return isPaused;
	}

	void AnimatedSprite::setPaused(bool isPaused)
	{
		if (!anims_.empty()) {
			anims_[currentAnimIndex_].setPaused(isPaused);
		}
	}

	void AnimatedSprite::OnUpdate(float timeMult)
	{
		if (!anims_.empty()) {
			const unsigned int previousFrame = anims_[currentAnimIndex_].frame();
			anims_[currentAnimIndex_].updateFrame(timeMult);

			// Updating sprite texture rectangle only on change
			if (previousFrame != anims_[currentAnimIndex_].frame()) {
				setTexRect(anims_[currentAnimIndex_].rect());
			}
		}

		Sprite::OnUpdate(timeMult);
	}

	void AnimatedSprite::addAnimation(const RectAnimation& anim)
	{
		anims_.push_back(anim);
		currentAnimIndex_ = (unsigned int)anims_.size() - 1;
		setTexRect(anims_[currentAnimIndex_].rect());
	}

	void AnimatedSprite::addAnimation(RectAnimation&& anim)
	{
		anims_.push_back(std::move(anim));
		currentAnimIndex_ = (unsigned int)anims_.size() - 1;
		setTexRect(anims_[currentAnimIndex_].rect());
	}

	void AnimatedSprite::clearAnimations()
	{
		anims_.clear();
	}

	void AnimatedSprite::setAnimationIndex(std::uint32_t animIndex)
	{
		if (!anims_.empty()) {
			DEATH_ASSERT(animIndex < anims_.size());
			currentAnimIndex_ = animIndex;
			setTexRect(anims_[currentAnimIndex_].rect());
		}
	}

	RectAnimation* AnimatedSprite::currentAnimation()
	{
		RectAnimation* currentAnim = nullptr;
		if (!anims_.empty()) {
			currentAnim = &anims_[currentAnimIndex_];
		}
		return currentAnim;
	}

	const RectAnimation* AnimatedSprite::currentAnimation() const
	{
		const RectAnimation* currentAnim = nullptr;
		if (!anims_.empty()) {
			currentAnim = &anims_[currentAnimIndex_];
		}
		return currentAnim;
	}

	std::uint32_t AnimatedSprite::frame() const
	{
		unsigned int frame = 0;
		if (!anims_.empty()) {
			frame = anims_[currentAnimIndex_].frame();
		}
		return frame;
	}

	void AnimatedSprite::setFrame(std::uint32_t frameNum)
	{
		if (!anims_.empty()) {
			anims_[currentAnimIndex_].setFrame(frameNum);
			setTexRect(anims_[currentAnimIndex_].rect());
		}
	}

	AnimatedSprite::AnimatedSprite(const AnimatedSprite& other)
		: Sprite(other), anims_(other.anims_), currentAnimIndex_(other.currentAnimIndex_)
	{
		_type = ObjectType::AnimatedSprite;
		setFrame(other.frame());
	}
}