载入中...
搜索中...
未找到
TileBuffer.cpp
浏览该文件的文档.
1#include "editor/TileBuffer.h"
2
3#include "common/Exception.h"
4
5#include <algorithm>
6
7namespace eve::editor {
8
10 if (width <= 0 || height <= 0) throw Exception("TileBuffer: width/height must be > 0");
11 width_ = width;
12 height_ = height;
13 gids_.assign(static_cast<size_t>(width_) * static_cast<size_t>(height_), 0);
14}
15
17 if (width <= 0 || height <= 0) throw Exception("TileBuffer::resize: width/height must be > 0");
18 std::vector<int> next(static_cast<size_t>(width) * static_cast<size_t>(height), 0);
19 int copyW = std::min(width, width_);
20 int copyH = std::min(height, height_);
21 for (int y = 0; y < copyH; ++y) {
22 for (int x = 0; x < copyW; ++x) {
23 next[static_cast<size_t>(y * width + x)] = gids_[static_cast<size_t>(y * width_ + x)];
24 }
25 }
26 width_ = width;
27 height_ = height;
28 gids_.swap(next);
29}
30
31void TileBuffer::clear() { std::fill(gids_.begin(), gids_.end(), 0); }
32
33void TileBuffer::fill(int gid) { std::fill(gids_.begin(), gids_.end(), gid); }
34
35bool TileBuffer::inBounds(int x, int y) const {
36 return x >= 0 && y >= 0 && x < width_ && y < height_;
37}
38
39void TileBuffer::setGid(int x, int y, int gid) {
40 if (!inBounds(x, y)) throw Exception("TileBuffer::setGid: out of bounds");
41 gids_[static_cast<size_t>(index(x, y))] = gid;
42}
43
44int TileBuffer::getGid(int x, int y) const {
45 if (!inBounds(x, y)) throw Exception("TileBuffer::getGid: out of bounds");
46 return gids_[static_cast<size_t>(index(x, y))];
47}
48
49} // namespace eve::editor
int y
Definition Grass.cpp:135
float height
Definition Grass.cpp:235
int x
Definition Grass.cpp:135
int width
int getGid(int x, int y) const
bool inBounds(int x, int y) const
void resize(int width, int height)
void setGid(int x, int y, int gid)
TileBuffer(int width, int height)
Definition TileBuffer.cpp:9