14void SpriteClip::checkIndex(
int index)
const {
16 throw Exception(
"SpriteClip: frame index %d out of range (count=%d)",
index,
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});
29 if (!sheet)
throw Exception(
"SpriteClip.addFrameByName: sheet is null");
32 throw Exception(
"SpriteClip.addFrameByName: unknown frame '%s'", frameName.c_str());
37 if (firstSheetFrame < 0 || lastSheetFrame < firstSheetFrame)
38 throw Exception(
"SpriteClip.addRange: expected 0 <= first <= last");
39 if (fps <= 0.f)
throw Exception(
"SpriteClip.addRange: fps must be > 0");
41 for (
int frame = firstSheetFrame; frame <= lastSheetFrame; ++frame)
46 if (fps <= 0.f)
throw Exception(
"SpriteClip.setFPS: fps must be > 0");
48 for (Entry &entry : frames_) entry.duration =
duration;
52 if (frames_.empty())
return 0.f;
53 const float duration = frames_.front().duration;
55 for (
const Entry &entry : frames_) {
56 if (std::fabs(entry.duration -
duration) > 1e-6f)
return 0.f;
62 checkIndex(clipFrame);
63 events_[clipFrame] =
name;
67 auto it = events_.find(clipFrame);
68 return it == events_.end() ? std::string() : it->second;
75 return frames_[
static_cast<size_t>(
index)].sheetFrame;
85 for (
const Entry &e : frames_) sum += e.duration;
90 if (timeSeconds < 0.f) timeSeconds = 0.f;
92 if (dur <= 0.f)
return 0.f;
94 float t = std::fmod(timeSeconds, dur);
95 if (
t < 0.f)
t += dur;
98 return timeSeconds > dur ? dur : timeSeconds;
102 if (frames_.empty())
return -1;
105 if (!loop_ && timeSeconds >= dur)
return getFrameCount() - 1;
109 acc += frames_[
static_cast<size_t>(i)].
duration;
EVENGINE_API_FOUNDATION public API.
SpriteClip(const std::string &name="")
Sprite clip.
int getSheetFrame(int index) const
Returns the sheet frame.
void addEvent(int clipFrame, const std::string &name)
Attach a named event to a clip-frame index.
float wrapTime(float timeSeconds) const
Local time wrapped or clamped according to loop flag.
float getFPS() const
Uniform FPS, or 0 when empty/variable-duration.
void addFrame(int sheetFrameIndex, float duration=0.1f)
Append one cell: sheetFrameIndex + display duration (seconds).
float getDuration() const
Returns the duration.
void setName(const std::string &name)
Sets the name.
void addFrameByName(SpriteSheet *sheet, const std::string &frameName, float duration=0.1f)
Resolve name via sheet and append.
void setFPS(float fps)
Replace every existing frame duration with 1/fps.
int frameAtTime(float timeSeconds) const
Map absolute time (seconds) into a clip frame index. When loop is false and t >= duration,...
int getFrameCount() const
Returns the frame count.
void addRange(int firstSheetFrame, int lastSheetFrame, float fps)
Append inclusive sheet-frame range at a uniform frame rate.
std::string getEvent(int clipFrame) const
Return event name for a frame, or empty when none.
float getFrameDuration(int index) const
Returns the frame duration.
Sprite-sheet / texture-atlas frame table (pixel rects).
int findFrame(const std::string &name) const
Finds frame.