File: RectAnimation.cpp

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

namespace nCine
{
	RectAnimation::RectAnimation(float defaultFrameDuration, LoopMode loopMode)
		: defaultFrameDuration_(defaultFrameDuration), loopMode_(loopMode), rects_(4), frameDurations_(4),
			currentFrame_(0), elapsedFrameTime_(0.0f), goingForward_(true), isPaused_(true)
	{
	}

	void RectAnimation::updateFrame(float timeMult)
	{
		// No frame calculation if the animation is paused or has only one rect
		if (isPaused_ || rects_.size() < 2) {
			return;
		}

		elapsedFrameTime_ += timeMult;
		// Determine the next frame rectangle
		while (elapsedFrameTime_ >= frameDurations_[currentFrame_]) {
			elapsedFrameTime_ -= frameDurations_[currentFrame_];

			if (goingForward_) {
				if (currentFrame_ == rects_.size() - 1) {
					if (loopMode_ == LoopMode::Backward) {
						goingForward_ = false;
						currentFrame_--;
					} else {
						if (loopMode_ == LoopMode::NoRepeat) {
							isPaused_ = true;
						} else {
							currentFrame_ = 0;
						}
					}
				} else {
					currentFrame_++;
				}
			} else {
				if (currentFrame_ == 0) {
					if (loopMode_ == LoopMode::NoRepeat) {
						isPaused_ = true;
					} else {
						goingForward_ = true;
						currentFrame_++;
					}
				} else {
					currentFrame_--;
				}
			}
		}
	}

	void RectAnimation::setFrame(std::uint32_t frameNum)
	{
		DEATH_ASSERT(frameNum < rects_.size());
		currentFrame_ = frameNum;
	}

	void RectAnimation::addRect(const Recti& rect, float frameDuration)
	{
		rects_.push_back(rect);
		frameDurations_.push_back(frameDuration);
	}

	void RectAnimation::addRect(std::int32_t x, std::int32_t y, std::int32_t w, std::int32_t h, float frameDuration)
	{
		rects_.push_back(Recti(x, y, w, h));
		frameDurations_.push_back(frameDuration);
	}
}