载入中...
搜索中...
未找到
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 if (w <= 0 || h <= 0) throw Exception("SpriteSheet.addFrame: width/height must be > 0");
16 Frame f;
17 f.name = name.empty() ? ("frame" + std::to_string(frames_.size())) : name;
18 if (byName_.count(f.name))
19 throw Exception("SpriteSheet.addFrame: duplicate frame name '%s'", f.name.c_str());
20 f.x = x;
21 f.y = y;
22 f.w = w;
23 f.h = h;
24 int idx = static_cast<int>(frames_.size());
25 byName_[f.name] = idx;
26 frames_.push_back(std::move(f));
27 return idx;
28}
29
30int SpriteSheet::setGrid(int columns, int rows, int frameW, int frameH, int margin, int spacing,
31 int originX, int originY) {
32 if (columns <= 0 || rows <= 0)
33 throw Exception("SpriteSheet.setGrid: columns/rows must be > 0");
34 if (frameW <= 0 || frameH <= 0)
35 throw Exception("SpriteSheet.setGrid: frameW/frameH must be > 0");
36 if (margin < 0 || spacing < 0)
37 throw Exception("SpriteSheet.setGrid: margin/spacing must be >= 0");
38
39 clear();
40 int added = 0;
41 for (int row = 0; row < rows; ++row) {
42 for (int col = 0; col < columns; ++col) {
43 int x = originX + margin + col * (frameW + spacing);
44 int y = originY + margin + row * (frameH + spacing);
45 addFrame("", x, y, frameW, frameH);
46 ++added;
47 }
48 }
49 return added;
50}
51
53 frames_.clear();
54 byName_.clear();
55}
56
57int SpriteSheet::findFrame(const std::string &name) const {
58 auto it = byName_.find(name);
59 return it == byName_.end() ? -1 : it->second;
60}
61
62std::string SpriteSheet::getFrameName(int index) const {
63 checkIndex(index);
64 return frames_[static_cast<size_t>(index)].name;
65}
66
67int SpriteSheet::getFrameX(int index) const {
68 checkIndex(index);
69 return frames_[static_cast<size_t>(index)].x;
70}
71
72int SpriteSheet::getFrameY(int index) const {
73 checkIndex(index);
74 return frames_[static_cast<size_t>(index)].y;
75}
76
77int SpriteSheet::getFrameWidth(int index) const {
78 checkIndex(index);
79 return frames_[static_cast<size_t>(index)].w;
80}
81
82int SpriteSheet::getFrameHeight(int index) const {
83 checkIndex(index);
84 return frames_[static_cast<size_t>(index)].h;
85}
86
87void SpriteSheet::applyToQuad(graphics::Quad *quad, int frameIndex) const {
88 if (!quad) throw Exception("SpriteSheet.applyToQuad: quad is null");
89 checkIndex(frameIndex);
90 const Frame &f = frames_[static_cast<size_t>(frameIndex)];
91 quad->setViewport(f.x, f.y, f.w, f.h);
92}
93
94} // namespace eve::animation
int y
Definition Grass.cpp:135
int x
Definition Grass.cpp:135
int h
int w
int idx
float f
const char * name
Definition RockMesh.cpp:21
int spacing
int columns
int margin
int getFrameX(int index) const
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 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
int getFrameWidth(int index) const
int getFrameHeight(int index) const
int getFrameY(int index) const
int findFrame(const std::string &name) const
void applyToQuad(graphics::Quad *quad, int frameIndex) const
Write pixel viewport into an existing Quad (does not allocate).
Pixel-space sub-rectangle of a Texture (atlas cell / sprite frame). UV conversion uses the texture's ...
Definition Quad.h:9
void setViewport(int x, int y, int w, int h)
Definition Quad.cpp:11