jubilant-funicular
Animation2D.cpp
1 #include "nta/Animation2D.h"
2 #include "nta/Logger.h"
3 
4 #define INVALID_LEN_MSG "Animation2D Error: Tried creating animation with 0 length"
5 #define INVALID_IDX_MSG "Animation2D Error: Tried creating animation start with nonexistent index"
6 
7 namespace nta {
8  SpriteSheet::SpriteSheet(ContextData& context, crstring file_path, crivec2 dims) : dims(dims) {
9  tex = context.getTexture(file_path).get_data_or(GLTexture::no_texture());
10  }
11  void SpriteSheet::read_sprite_pixels(GLubyte* pixels, std::size_t index) const {
12  const std::size_t r = index/num_cols;
13  const std::size_t c = index%num_cols;
14  const glm::ivec2 dims = sprite_dims();
15 
16  const float top = dims.y * r;
17  const float left = dims.x * c;
18 
19  GLuint fbo;
20  glGenFramebuffers(1, &fbo);
21  glBindFramebuffer(GL_FRAMEBUFFER, fbo);
22  glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
23  tex.id, 0);
24  glReadBuffer(GL_COLOR_ATTACHMENT0);
25  glReadPixels(left, top, dims.x, dims.y, GL_RGB, GL_UNSIGNED_BYTE, pixels);
26  glBindFramebuffer(GL_FRAMEBUFFER, 0);
27  glDeleteFramebuffers(1, &fbo);
28  }
29 
30  Animation2D::Animation2D(const SpriteSheet& sheet, std::size_t start, std::size_t length, float speed) :
31  m_sheet(sheet), m_time(0), m_speed(speed), m_start_index(start), m_length(length) {
32  if (m_length == 0) {
33  Logger::writeErrorToLog(INVALID_LEN_MSG, INVALID_VALUE);
34  }
35  if (m_start_index >= m_sheet.num_rows*m_sheet.num_cols) {
36  Logger::writeErrorToLog(INVALID_IDX_MSG, INVALID_VALUE);
37  }
38  }
39  Animation2D::Animation2D(ContextData& context, crstring file_path, crivec2 dims, std::size_t start, std::size_t length, float speed) :
40  Animation2D(SpriteSheet(context, file_path, dims), start, length, speed) {
41  }
42  Animation2D::Animation2D(ContextData& context, crstring file_path, int num_cols, std::size_t start, std::size_t length, float speed) :
43  Animation2D(SpriteSheet(context, file_path, num_cols), start, length, speed) {
44  }
45  glm::vec4 Animation2D::get_uv() const {
46  return m_sheet.get_uv(get_index());
47  }
48  glm::vec4 Animation2D::get_flipped_uv() const {
49  return m_sheet.get_flipped_uv(get_index());
50  }
51  glm::vec2 Animation2D::get_frame_dims() const {
52  return m_sheet.get_frame_dims();
53  }
54  GLuint Animation2D::get_tex_id() const {
55  return m_sheet.tex.id;
56  }
57  std::size_t Animation2D::get_index() const {
58  return m_start_index + ((std::size_t)m_time)%m_length;
59  }
60  std::size_t Animation2D::get_start() const {
61  return m_start_index;
62  }
63  std::size_t Animation2D::get_length() const {
64  return m_length;
65  }
66  float Animation2D::get_speed() const {
67  return m_speed;
68  }
69  float Animation2D::get_time() const {
70  return m_time;
71  }
72  void Animation2D::switch_animation(std::size_t start, std::size_t length, float speed) {
73  m_start_index = start;
74  m_length = length;
75  m_time = 0;
76  m_speed = speed;
77 
78  if (m_length == 0) {
79  Logger::writeErrorToLog(INVALID_LEN_MSG, INVALID_VALUE);
80  }
81  if (m_start_index >= m_sheet.num_rows*m_sheet.num_cols) {
82  Logger::writeErrorToLog(INVALID_IDX_MSG, INVALID_VALUE);
83  }
84  }
85  void Animation2D::set_speed(float speed) {
86  m_speed = speed;
87  }
88  void Animation2D::step(float dt) {
89  m_time += m_speed * dt;
90  }
91 }
nta::Animation2D::m_speed
float m_speed
how quickly the animation advances when step is called
Definition: Animation2D.h:75
nta::Animation2D::m_start_index
std::size_t m_start_index
A single animation spans m_length continuous sprite indices beginning with m_start_index.
Definition: Animation2D.h:77
nta::SpriteSheet::tex
GLTexture tex
The texture holding all the sprites.
Definition: Animation2D.h:56
nta::SpriteSheet
Multiple sprites (each the same size) in one texture.
Definition: Animation2D.h:8
nta::SpriteSheet::get_frame_dims
glm::vec2 get_frame_dims() const
Returns the dimensions of one sprite in this sheet.
Definition: Animation2D.h:48
nta::GLTexture::id
GLuint id
the id of the texture
Definition: GLTexture.h:62
nta
Definition: Animation2D.h:6
nta::Animation2D::m_time
float m_time
Definition: Animation2D.h:73
nta::SpriteSheet::get_uv
glm::vec4 get_uv(std::size_t index) const
Definition: Animation2D.h:33
nta::SpriteSheet::get_flipped_uv
glm::vec4 get_flipped_uv(std::size_t index) const
Like get_uv except it flips the sprite horizontally.
Definition: Animation2D.h:41
nta::Animation2D::m_sheet
SpriteSheet m_sheet
The sheet holding the sprites used by the animation.
Definition: Animation2D.h:70
nta::Logger::writeErrorToLog
static Error writeErrorToLog(crstring error, ErrorType type=OTHER)
writes entry in log and then notifies ErrorManager
Definition: Logger.cpp:31