载入中...
搜索中...
未找到
SpriteClip.cpp
浏览该文件的文档.
2
4#include "common/Exception.h"
5
6#include <cmath>
7
8namespace eve::animation {
9
10SpriteClip::SpriteClip(const std::string &name) : name_(name) {}
11
12void SpriteClip::setName(const std::string &name) { name_ = name; }
13
14void SpriteClip::checkIndex(int index) const {
15 if (index < 0 || index >= getFrameCount())
16 throw Exception("SpriteClip: frame index %d out of range (count=%d)", index,
18}
19
20void SpriteClip::addFrame(int sheetFrameIndex, float duration) {
21 if (sheetFrameIndex < 0)
22 throw Exception("SpriteClip.addFrame: sheetFrameIndex must be >= 0");
23 if (duration < 0.f) throw Exception("SpriteClip.addFrame: duration must be >= 0");
24 frames_.push_back(Entry{sheetFrameIndex, duration});
25}
26
27void SpriteClip::addFrameByName(SpriteSheet *sheet, const std::string &frameName,
28 float duration) {
29 if (!sheet) throw Exception("SpriteClip.addFrameByName: sheet is null");
30 int idx = sheet->findFrame(frameName);
31 if (idx < 0)
32 throw Exception("SpriteClip.addFrameByName: unknown frame '%s'", frameName.c_str());
33 addFrame(idx, duration);
34}
35
36void SpriteClip::clear() { frames_.clear(); }
37
38int SpriteClip::getSheetFrame(int index) const {
39 checkIndex(index);
40 return frames_[static_cast<size_t>(index)].sheetFrame;
41}
42
43float SpriteClip::getFrameDuration(int index) const {
44 checkIndex(index);
45 return frames_[static_cast<size_t>(index)].duration;
46}
47
49 float sum = 0.f;
50 for (const Entry &e : frames_) sum += e.duration;
51 return sum;
52}
53
54float SpriteClip::wrapTime(float timeSeconds) const {
55 if (timeSeconds < 0.f) timeSeconds = 0.f;
56 const float dur = getDuration();
57 if (dur <= 0.f) return 0.f;
58 if (loop_) {
59 float t = std::fmod(timeSeconds, dur);
60 if (t < 0.f) t += dur;
61 return t;
62 }
63 return timeSeconds > dur ? dur : timeSeconds;
64}
65
66int SpriteClip::frameAtTime(float timeSeconds) const {
67 if (frames_.empty()) return -1;
68 float t = wrapTime(timeSeconds);
69 const float dur = getDuration();
70 if (!loop_ && timeSeconds >= dur) return getFrameCount() - 1;
71
72 float acc = 0.f;
73 for (int i = 0; i < getFrameCount(); ++i) {
74 acc += frames_[static_cast<size_t>(i)].duration;
75 if (t < acc || i == getFrameCount() - 1) return i;
76 }
77 return getFrameCount() - 1;
78}
79
80} // namespace eve::animation
int idx
const char * name
Definition RockMesh.cpp:21
SpriteClip(const std::string &name="")
int getSheetFrame(int index) const
float wrapTime(float timeSeconds) const
Local time wrapped or clamped according to loop flag.
void addFrame(int sheetFrameIndex, float duration=0.1f)
Append one cell: sheetFrameIndex + display duration (seconds).
void setName(const std::string &name)
void addFrameByName(SpriteSheet *sheet, const std::string &frameName, float duration=0.1f)
Resolve name via sheet and append.
int frameAtTime(float timeSeconds) const
Map absolute time (seconds) into a clip frame index. When loop is false and t >= duration,...
float getFrameDuration(int index) const
Sprite-sheet / texture-atlas frame table (pixel rects).
Definition SpriteSheet.h:20
int findFrame(const std::string &name) const