Skip to content

Instantly share code, notes, and snippets.

@fkaa
Created January 29, 2014 19:15
Show Gist options
  • Select an option

  • Save fkaa/8694821 to your computer and use it in GitHub Desktop.

Select an option

Save fkaa/8694821 to your computer and use it in GitHub Desktop.
sprite_map[someid] = AnimatedSprite(Asset::load<Texture>("mychar.png"), glm::vec4(0, 48, 16, 16), 0.1f, 5, 16);
sprite_map[someid].update(delta); // calls Sprite::update(float);
#include "sprite.h"
Sprite::Sprite() {
}
Sprite::Sprite(const TexturePtr texture, const glm::vec4 source) :
tex(texture),
src(source) {
}
Sprite::~Sprite() {
}
AnimatedSprite::AnimatedSprite(const TexturePtr texture, const glm::vec4 source, float fps, int num, int size) :
Sprite(texture, source),
frame_time(fps),
num_frames(num),
tile_size(size),
current_frame(0) {
}
AnimatedSprite::~AnimatedSprite() {
}
void AnimatedSprite::update(float delta) {
printf("hierhjwoe");
time += delta;
if (time > frame_time) {
current_frame++;
time = 0;
if (current_frame > num_frames) {
current_frame = 0;
src.x -= (num_frames) * tile_size;
} else {
src.x += tile_size;
}
}
}
#ifndef CLOBAT_SPRITE_H_
#define CLOBAT_SPRITE_H_
#include "glutil.h"
#include "texture.h"
class Sprite {
public:
Sprite();
Sprite(const TexturePtr texture,
const glm::vec4 source);
~Sprite();
virtual void update(float delta) {}
TexturePtr tex;
glm::vec4 src;
bool x_flip;
};
class AnimatedSprite : public Sprite {
public:
AnimatedSprite(const TexturePtr texture, const glm::vec4 source, float fps, int num, int size);
~AnimatedSprite();
void update(float delta);
private:
const float frame_time;
const int num_frames;
int tile_size;
int current_frame;
float time;
};
#endif /* defined CLOBAT_SPRITE_H_ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment