xmario/xSDL/xSprite.h

69 lines
1.6 KiB
C
Raw Normal View History

#ifndef DEF_XSPRITE_H
#define DEF_XSPRITE_H
#include <string>
#include <vector>
#include <mutex>
#include "SDL.h"
#include "SDL_image.h"
#include "xElement.h"
#include "xOrchestrable.h"
#include "xApplication.h"
using namespace std;
class xSprite : public xElement, public xOrchestrable {
public:
// color sprite
xSprite(const int rgba[]);
// image sprite
xSprite(const char *url);
// image with default clip
xSprite(const char *url, SDL_Rect clip);
virtual ~xSprite();
// replace surface
void setSurface(const int rgba[]); // color sprite
void setSurface(const char *url); // image sprite
void setSurface(SDL_Surface *t); // copy surface
// animation
void addFrame(SDL_Rect clip);
void clearFrames();
// sets the sprite clip
// + clears animation frames
void setClip(SDL_Rect clip);
// set projection into scene
void project(SDL_Rect dest);
// implement xElement
virtual void draw(SDL_Renderer* renderer) override;
// orchestrable process
void tick(const uint32_t ticks);
// animation handles
void animate(uint32_t interval);
void freeze();
protected:
SDL_Surface* _surface = NULL;
SDL_Rect _projection;
// surface clips
// [0] : no clip, use default surface dimensions
// [1] : global clip, use it every time
// [2+]: clips are used for animation
vector<SDL_Rect> _frames;
// active clip for animation ; equal to 0 if not an animated sprite
size_t _active_clip = 0;
uint32_t _animation_interval;
mutex _mutex;
};
#endif