2019-11-05 20:45:41 +00:00
|
|
|
#ifndef DEF_XSPRITE_H
|
|
|
|
#define DEF_XSPRITE_H
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
#include <mutex>
|
|
|
|
#include "SDL.h"
|
|
|
|
#include "SDL_image.h"
|
|
|
|
#include "xElement.h"
|
2019-11-08 20:28:36 +00:00
|
|
|
#include "xOrchestrable.h"
|
|
|
|
#include "xApplication.h"
|
2019-11-05 20:45:41 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2019-11-08 20:28:36 +00:00
|
|
|
class xSprite : public xElement, public xOrchestrable {
|
2019-11-05 20:45:41 +00:00
|
|
|
|
|
|
|
public:
|
2019-11-08 20:28:36 +00:00
|
|
|
// 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();
|
2019-11-05 20:45:41 +00:00
|
|
|
|
|
|
|
// replace surface
|
|
|
|
void setSurface(const int rgba[]); // color sprite
|
|
|
|
void setSurface(const char *url); // image sprite
|
|
|
|
void setSurface(SDL_Surface *t); // copy surface
|
|
|
|
|
2019-11-08 20:28:36 +00:00
|
|
|
// animation
|
|
|
|
void addFrame(SDL_Rect clip);
|
|
|
|
void clearFrames();
|
2019-11-05 20:45:41 +00:00
|
|
|
|
2019-11-08 20:28:36 +00:00
|
|
|
// sets the sprite clip
|
|
|
|
// + clears animation frames
|
|
|
|
void setClip(SDL_Rect clip);
|
2019-11-05 20:45:41 +00:00
|
|
|
|
2019-11-08 20:28:36 +00:00
|
|
|
// set projection into scene
|
|
|
|
void project(SDL_Rect dest);
|
2019-11-05 20:45:41 +00:00
|
|
|
|
|
|
|
// implement xElement
|
|
|
|
virtual void draw(SDL_Renderer* renderer) override;
|
|
|
|
|
2019-11-08 20:28:36 +00:00
|
|
|
// orchestrable process
|
|
|
|
void tick(const uint32_t ticks);
|
|
|
|
|
|
|
|
// animation handles
|
|
|
|
void animate(uint32_t interval);
|
|
|
|
void freeze();
|
|
|
|
|
2019-11-05 20:45:41 +00:00
|
|
|
protected:
|
2019-11-08 20:28:36 +00:00
|
|
|
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;
|
2019-11-05 20:45:41 +00:00
|
|
|
|
2019-11-08 20:28:36 +00:00
|
|
|
// active clip for animation ; equal to 0 if not an animated sprite
|
|
|
|
size_t _active_clip = 0;
|
|
|
|
uint32_t _animation_interval;
|
2019-11-05 20:45:41 +00:00
|
|
|
|
2019-11-08 20:28:36 +00:00
|
|
|
mutex _mutex;
|
2019-11-05 20:45:41 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|