jubilant-funicular
Animation2D.h
1 #ifndef NTA_ANIMATION2D_H_INCLUDED
2 #define NTA_ANIMATION2D_H_INCLUDED
3 
4 #include "nta/ContextData.h"
5 
6 namespace nta {
8  struct SpriteSheet {
9  SpriteSheet() {
10  tex.id = 0;
11  dims.x = dims.y = 0;
12  }
13  SpriteSheet(const GLTexture& tex, crivec2 dims) : tex(tex), dims(dims) {
14  }
15  SpriteSheet(ContextData& context, crstring file_path, crivec2 dims);
16  SpriteSheet(ContextData& context, crstring file_path, int num_cols) : SpriteSheet(context, file_path, glm::ivec2(1, num_cols)) {
17  }
19  std::size_t num_sprites() const {
20  return num_rows * num_cols;
21  }
22  glm::vec2 sprite_dims() const {
23  return glm::vec2(tex.width/num_cols, tex.height/num_rows);
24  }
25  // Not sure how I feel about this
26  std::size_t sprite_area() const {
27  auto dims = sprite_dims();
28  return dims.x*dims.y;
29  }
33  glm::vec4 get_uv(std::size_t index) const {
34  std::size_t r = index/num_cols;
35  std::size_t c = index%num_cols;
36  float w = 1.f/num_cols;
37  float h = 1.f/num_rows;
38  return glm::vec4(c*w, r*h, w, h);
39  }
41  glm::vec4 get_flipped_uv(std::size_t index) const {
42  auto coords = get_uv(index);
43  coords.x += coords[2];
44  coords[2] *= -1;
45  return coords;
46  }
48  glm::vec2 get_frame_dims() const {
49  return glm::vec2(tex.width/num_cols, tex.height/num_rows);
50  }
54  void read_sprite_pixels(GLubyte* pixels, std::size_t index) const;
58  union {
59  glm::ivec2 dims;
60  struct {
61  int num_rows;
62  int num_cols;
63  };
64  };
65  };
67  class Animation2D {
68  private:
73  float m_time;
75  float m_speed;
77  std::size_t m_start_index, m_length;
78  public:
79  Animation2D() {}
80  Animation2D(const SpriteSheet& sheet, std::size_t start = 0,
81  std::size_t length = 1, float speed = 1);
82  Animation2D(ContextData& context, crstring file_path, crivec2 dims,
83  std::size_t start = 0, std::size_t length = 1, float speed = 1);
84  Animation2D(ContextData& context, crstring file_path, int num_cols,
85  std::size_t start = 0, std::size_t length = 1, float speed = 1);
86  glm::vec4 get_uv() const;
87  glm::vec4 get_flipped_uv() const;
88  glm::vec2 get_frame_dims() const;
89  std::size_t get_index() const;
90  std::size_t get_start() const;
91  std::size_t get_length() const;
92  float get_speed() const;
93  GLuint get_tex_id() const;
94  float get_time() const;
95  void switch_animation(std::size_t start, std::size_t length, float speed = 1);
96  // ugh I have a setter and a getter (maybe I should just make m_speed public...)
97  void set_speed(float speed);
98  void reset_time() { m_time = 0.0f; }
99  void step(float dt);
100  };
101 }
102 
103 #endif // NTA_ANIMATION2D_H_INCLUDED
nta::SpriteSheet::num_sprites
std::size_t num_sprites() const
Returns the number of sprites in this sheet.
Definition: Animation2D.h:19
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::GLTexture::width
GLint width
the width and height, respectively, of the texture
Definition: GLTexture.h:64
nta::ContextData
Definition: ContextData.h:17
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::utils::format
std::string format(const std::string &fmt, Args &&... args)
Definition: format.h:88
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::GLTexture
represents a texture (tied to a specific GL context)
Definition: GLTexture.h:24
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::Animation2D
A 2D Animation made from a single SpriteSheet.
Definition: Animation2D.h:67
nta::SpriteSheet::read_sprite_pixels
void read_sprite_pixels(GLubyte *pixels, std::size_t index) const
Definition: Animation2D.cpp:11