WHY_CPP  0.1
sprite.cpp
1 #include "sprite.h"
2 #include <whycpp/log.h>
3 #include <whycpp/types.h>
4 #include "int_utils.h"
5 
6 Sprite::Sprite(i32 width, i32 height)
7  : width_(width),
8  height_(height),
9  texture(std::vector<std::vector<RGBA>>(static_cast<u64>(width),
10  std::vector<RGBA>(static_cast<u64>(height), {0, 0, 0, 0}))) {
11  LOG_DEBUG("Sprite [%d, %d] created", width, height);
12 }
13 Sprite::~Sprite() {
14  LOG_DEBUG("Sprite [%d, %d] destroyed", width_, height_);
15 }
16 void Sprite::Set(i32 x, i32 y, const RGBA &color) {
17  texture.at(CheckX(x)).at(CheckY(y)) = color;
18 }
19 const RGBA &Sprite::Get(i32 x, i32 y) const {
20  return texture.at(static_cast<u64>(CheckX(x))).at(static_cast<u64>(CheckY(y)));
21 }
22 i32 Sprite::GetHeight() const {
23  return height_;
24 }
25 i32 Sprite::GetWidth() const {
26  return width_;
27 }
28 size_t Sprite::CheckX(i32 x) const {
29  if (x >= width_ || x < 0) {
30  LOG_VERBOSE("Sprite: X is out of bound, should be [0, %d) but %d", width_, x);
31  return static_cast<size_t>(clamp(x, 0, width_ - 1));
32  }
33  return static_cast<size_t>(x);
34 }
35 size_t Sprite::CheckY(i32 y) const {
36  if (y >= height_ || y < 0) {
37  LOG_VERBOSE("Sprite: Y is out of bound, should be [0, %d) but %d", height_, y);
38  return static_cast<size_t>(clamp(y, 0, height_ - 1));
39  }
40  return static_cast<size_t>(y);
41 }
Definition: color.h:11