MCPcopy Create free account
hub / github.com/OneLoneCoder/olcPixelGameEngine / FrameSequence

Class FrameSequence

utilities/olcUTIL_Animate2D.h:109–176  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

107 };
108
109 class FrameSequence
110 {
111 public:
112 // Constructs a sequence of frames with a duration and a traversal style
113 inline FrameSequence(const float fFrameDuration = 0.1f, const Style nStyle = Style::Repeat)
114 {
115 SetFrameDuration(fFrameDuration);
116 m_nStyle = nStyle;
117 }
118
119 inline void SetFrameDuration(const float fFrameDuration = 0.1f)
120 {
121 m_fFrameDuration = fFrameDuration;
122 m_fFrameRate = 1.0f / m_fFrameDuration;
123 }
124
125 // Adds a frame to this sequence
126 inline void AddFrame(const Frame& frame)
127 {
128 m_vFrames.emplace_back(frame);
129 }
130
131 // Returns a Frame Object for a given time into an animation
132 inline const Frame& GetFrame(const float fTime) const
133 {
134 size_t frame = ConvertTimeToFrame(fTime);
135 return m_vFrames[frame];
136 }
137
138 inline const bool Complete(const float fTime) const
139 {
140 return std::abs(fTime - (m_vFrames.size() * m_fFrameDuration)) < 0.01f;
141 }
142
143 private:
144 Style m_nStyle;
145 std::vector<Frame> m_vFrames;
146 float m_fFrameDuration = 0.1f;
147 float m_fFrameRate = 10.0f;
148
149 inline const size_t ConvertTimeToFrame(const float fTime) const
150 {
151 switch (m_nStyle)
152 {
153 case Style::Repeat:
154 return size_t(fTime * m_fFrameRate) % m_vFrames.size();
155 break;
156 case Style::OneShot:
157 {
158 size_t frame = std::clamp(size_t(fTime * m_fFrameRate), size_t(0), m_vFrames.size() - 1);
159 return frame;
160 }
161 break;
162 case Style::PingPong:
163 {
164 // Thanks @sigonasr2 (discord)
165 size_t frame = size_t(m_fFrameRate * fTime) % (m_vFrames.size() * 2 - 1);
166 return frame >= m_vFrames.size() ? m_vFrames.size() - frame % m_vFrames.size() - 1 : frame;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected