载入中...
搜索中...
未找到
SpriteSheet.cpp
浏览该文件的文档.
2
3#include "common/Exception.h"
4#include "graphics/Quad.h"
5
6namespace eve::animation {
7
8void SpriteSheet::checkIndex(int index) const {
9 if (index < 0 || index >= getFrameCount())
10 throw Exception("SpriteSheet: frame index %d out of range (count=%d)", index,
12}
13
14int SpriteSheet::addFrame(const std::string &name, int x, int y, int w, int h) {
15 return addFrameTrimmed(name, x, y, w, h, w, h, 0, 0);
16}
17
18int SpriteSheet::addFrameTrimmed(const std::string &name, int x, int y, int w, int h,
19 int sourceW, int sourceH, int offsetX, int offsetY) {
20 if (w <= 0 || h <= 0) throw Exception("SpriteSheet.addFrame: width/height must be > 0");
21 Frame f;
22 f.name = name.empty() ? ("frame" + std::to_string(frames_.size())) : name;
23 if (byName_.count(f.name))
24 throw Exception("SpriteSheet.addFrame: duplicate frame name '%s'", f.name.c_str());
25 f.x = x;
26 f.y = y;
27 f.w = w;
28 f.h = h;
29 f.sourceW = sourceW;
30 f.sourceH = sourceH;
31 f.offsetX = offsetX;
32 f.offsetY = offsetY;
33 int idx = static_cast<int>(frames_.size());
34 byName_[f.name] = idx;
35 frames_.push_back(std::move(f));
36 return idx;
37}
38
39int SpriteSheet::getFrameSourceWidth(int i) const { checkIndex(i); return frames_[size_t(i)].sourceW; }
40int SpriteSheet::getFrameSourceHeight(int i) const { checkIndex(i); return frames_[size_t(i)].sourceH; }
41int SpriteSheet::getFrameOffsetX(int i) const { checkIndex(i); return frames_[size_t(i)].offsetX; }
42int SpriteSheet::getFrameOffsetY(int i) const { checkIndex(i); return frames_[size_t(i)].offsetY; }
43
44int SpriteSheet::setGrid(int columns, int rows, int frameW, int frameH, int margin, int spacing,
45 int originX, int originY) {
46 if (columns <= 0 || rows <= 0)
47 throw Exception("SpriteSheet.setGrid: columns/rows must be > 0");
48 if (frameW <= 0 || frameH <= 0)
49 throw Exception("SpriteSheet.setGrid: frameW/frameH must be > 0");
50 if (margin < 0 || spacing < 0)
51 throw Exception("SpriteSheet.setGrid: margin/spacing must be >= 0");
52
53 clear();
54 int added = 0;
55 for (int row = 0; row < rows; ++row) {
56 for (int col = 0; col < columns; ++col) {
57 int x = originX + margin + col * (frameW + spacing);
58 int y = originY + margin + row * (frameH + spacing);
59 addFrame("", x, y, frameW, frameH);
60 ++added;
61 }
62 }
63 return added;
64}
65
67 frames_.clear();
68 byName_.clear();
69}
70
71int SpriteSheet::findFrame(const std::string &name) const {
72 auto it = byName_.find(name);
73 return it == byName_.end() ? -1 : it->second;
74}
75
76std::string SpriteSheet::getFrameName(int index) const {
77 checkIndex(index);
78 return frames_[static_cast<size_t>(index)].name;
79}
80
82 checkIndex(index);
83 return frames_[static_cast<size_t>(index)].x;
84}
85
87 checkIndex(index);
88 return frames_[static_cast<size_t>(index)].y;
89}
90
92 checkIndex(index);
93 return frames_[static_cast<size_t>(index)].w;
94}
95
97 checkIndex(index);
98 return frames_[static_cast<size_t>(index)].h;
99}
100
101void SpriteSheet::applyToQuad(graphics::Quad *quad, int frameIndex) const {
102 if (!quad) throw Exception("SpriteSheet.applyToQuad: quad is null");
103 checkIndex(frameIndex);
104 const Frame &f = frames_[static_cast<size_t>(frameIndex)];
105 quad->setViewport(f.x, f.y, f.w, f.h);
106}
107
109 auto *copy = new SpriteSheet();
110 copy->frames_ = frames_;
111 copy->byName_ = byName_;
112 copy->texture_ = texture_;
113 return copy;
114}
115
116} // namespace eve::animation
float w
Definition AnimClip.cpp:738
float y
Definition AnimClip.cpp:738
float x
Definition AnimClip.cpp:738
int rows
int h
std::string name
int idx
float f
int spacing
int columns
float offsetX
float offsetY
int margin
uint32_t index
EVENGINE_API_FOUNDATION public API.
Definition Exception.h:13
Sprite-sheet / texture-atlas frame table (pixel rects).
Definition SpriteSheet.h:23
SpriteSheet()=default
Sprite sheet.
int getFrameCount() const
Returns the frame count.
Definition SpriteSheet.h:56
int getFrameX(int index) const
Returns the frame x.
int getFrameOffsetX(int index) const
Returns the frame offset x.
int addFrame(const std::string &name, int x, int y, int w, int h)
Append a named frame. Returns frame index. Empty name → "frameN".
int getFrameSourceWidth(int index) const
Returns the frame source width.
int setGrid(int columns, int rows, int frameW, int frameH, int margin=0, int spacing=0, int originX=0, int originY=0)
Generate frames in row-major order from a grid. frameW/frameH are cell sizes; margin is outer padding...
std::string getFrameName(int index) const
Returns the frame name.
int getFrameWidth(int index) const
Returns the frame width.
SpriteSheet * clone() const
Duplicate frame metadata while sharing the borrowed atlas texture.
int getFrameHeight(int index) const
Returns the frame height.
int getFrameY(int index) const
Returns the frame y.
int getFrameOffsetY(int index) const
Returns the frame offset y.
int findFrame(const std::string &name) const
Finds frame.
void applyToQuad(graphics::Quad *quad, int frameIndex) const
Write pixel viewport into an existing Quad (does not allocate).
int getFrameSourceHeight(int index) const
Returns the frame source height.
int addFrameTrimmed(const std::string &name, int x, int y, int w, int h, int sourceW, int sourceH, int offsetX, int offsetY)
Append a trimmed frame with original canvas size and content offset.
Pixel-space sub-rectangle of a Texture (atlas cell / sprite frame). UV conversion uses the texture's ...
Definition Quad.h:11
void setViewport(int x, int y, int w, int h)
Sets the viewport.
Definition Quad.cpp:11