1 #include "nta/Animation2D.h"
2 #include "nta/Logger.h"
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"
8 SpriteSheet::SpriteSheet(ContextData& context, crstring file_path, crivec2 dims) : dims(dims) {
9 tex = context.getTexture(file_path).get_data_or(GLTexture::no_texture());
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();
16 const float top = dims.y * r;
17 const float left = dims.x * c;
20 glGenFramebuffers(1, &fbo);
21 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
22 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
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);
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) {
35 if (m_start_index >= m_sheet.num_rows*m_sheet.num_cols) {
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) {
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) {
45 glm::vec4 Animation2D::get_uv()
const {
48 glm::vec4 Animation2D::get_flipped_uv()
const {
51 glm::vec2 Animation2D::get_frame_dims()
const {
54 GLuint Animation2D::get_tex_id()
const {
57 std::size_t Animation2D::get_index()
const {
60 std::size_t Animation2D::get_start()
const {
63 std::size_t Animation2D::get_length()
const {
66 float Animation2D::get_speed()
const {
69 float Animation2D::get_time()
const {
72 void Animation2D::switch_animation(std::size_t start, std::size_t length,
float speed) {
85 void Animation2D::set_speed(
float speed) {
88 void Animation2D::step(
float dt) {