File: RectAnimation.h

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 (129 lines) | stat: -rw-r--r-- 3,707 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
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
#pragma once

#include "../Primitives/Rect.h"

#include <Containers/SmallVector.h>

using namespace Death::Containers;

namespace nCine
{
	/// Contains data for a rectangles based animation
	class RectAnimation
	{
	public:
		/// Loop modes
		enum class LoopMode
		{
			NoRepeat,
			/// When the animation reaches the last frame it begins again from start
			FromStart,
			/// When the animation reaches the last frame it goes backward
			Backward
		};

		/// Default constructor
		RectAnimation()
			: RectAnimation(1.0f / 60.0f, LoopMode::NoRepeat) {}
		/// Constructor for an animation with a specified default frame duration, loop and rewind mode
		RectAnimation(float defaultFrameDuration, LoopMode loopMode);

		/// Returns `true` if the animation is paused
		inline bool isPaused() const {
			return isPaused_;
		}
		/// Sets the pause flag
		inline void setPaused(bool isPaused) {
			isPaused_ = isPaused;
		}

		/// Updates current frame based on time passed
		void updateFrame(float interval);

		/// Returns current frame
		inline std::uint32_t frame() const {
			return currentFrame_;
		}
		/// Sets current frame
		void setFrame(std::uint32_t frameNum);

		/// Returns the default frame duration in seconds
		float defaultFrameDuration() const {
			return defaultFrameDuration_;
		}
		/// Sets the default frame duration in seconds
		inline void setDefaultFrameDuration(float defaultFrameDuration) {
			defaultFrameDuration_ = defaultFrameDuration;
		}

		/// Returns the loop mode
		LoopMode loopMode() const {
			return loopMode_;
		}
		/// Sets the loop mode
		inline void setLoopMode(LoopMode loopMode) {
			loopMode_ = loopMode;
		}

		/// Adds a rectangle to the array specifying the frame duration
		void addRect(const Recti& rect, float frameTime);
		/// Creates a rectangle from origin and size and then adds it to the array, specifying the frame duration
		void addRect(std::int32_t x, std::int32_t y, std::int32_t w, std::int32_t h, float frameTime);

		/// Adds a rectangle to the array with the default frame duration
		inline void addRect(const Recti& rect) {
			addRect(rect, defaultFrameDuration_);
		}
		/// Creates a rectangle from origin and size and then adds it to the array, with the default frame duration
		inline void addRect(std::int32_t x, std::int32_t y, std::int32_t w, std::int32_t h) {
			addRect(x, y, w, h, defaultFrameDuration_);
		}

		/// Returns the current rectangle
		inline const Recti& rect() const {
			return rects_[currentFrame_];
		}
		/// Returns the current frame duration in seconds
		inline float frameDuration() const {
			return frameDurations_[currentFrame_];
		}

		/// Returns the array of all rectangles
		inline SmallVectorImpl<Recti>& rectangles() {
			return rects_;
		}
		/// Returns the constant array of all rectangles
		inline const SmallVectorImpl<Recti>& rectangles() const {
			return rects_;
		}

		/// Returns the array of all frame durations
		inline SmallVectorImpl<float>& frameDurations() {
			return frameDurations_;
		}
		/// Returns the constant array of all frame durations
		inline const SmallVectorImpl<float>& frameDurations() const {
			return frameDurations_;
		}

	private:
		/// The default frame duratin if a custom one is not specified when adding a rectangle
		float defaultFrameDuration_;
		/// The looping mode
		LoopMode loopMode_;

		/// The rectangles array
		SmallVector<Recti, 0> rects_;
		/// The frame durations array
		SmallVector<float, 0> frameDurations_;
		/// Current frame
		std::uint32_t currentFrame_;
		/// Elapsed time since the last frame change
		float elapsedFrameTime_;
		/// The flag about the frame advance direction
		bool goingForward_;
		/// The pause flag
		bool isPaused_;
	};

}