89 lines
1.8 KiB
C
89 lines
1.8 KiB
C
|
#ifndef DEF_XENGINE_H
|
||
|
#define DEF_XENGINE_H
|
||
|
|
||
|
#include "SDL.h"
|
||
|
#include "SDL_image.h"
|
||
|
#include <vector>
|
||
|
#include <map>
|
||
|
#include <string>
|
||
|
#include <mutex>
|
||
|
#include <thread>
|
||
|
#include <stdexcept>
|
||
|
#include "xElement.h"
|
||
|
#include "xController.h"
|
||
|
using namespace std;
|
||
|
|
||
|
enum xAppState {
|
||
|
RUNNING,
|
||
|
STOPPING,
|
||
|
STOPPED
|
||
|
};
|
||
|
|
||
|
#define EventListener void(*)(SDL_Event*)
|
||
|
|
||
|
class xApplication{
|
||
|
|
||
|
public:
|
||
|
/** Singleton */
|
||
|
static xApplication* create(const char *t, int w, int h);
|
||
|
static xApplication* get();
|
||
|
|
||
|
~xApplication();
|
||
|
|
||
|
SDL_Window *window();
|
||
|
SDL_Renderer *renderer();
|
||
|
|
||
|
void setBackground(Uint8 r=0xff, Uint8 g=0xff, Uint8 b=0xff, Uint8 a=0xff);
|
||
|
bool setImage(const char *url);
|
||
|
|
||
|
|
||
|
// bool collide(SDL_Rect a, SDL_Rect b, vector<bool>& cols); // Collision entre 2 SDL_Rect
|
||
|
// bool hit(xSprite *current, int movex=0, int movey=0); // Gestion des collisions
|
||
|
// bool hit(string current, int movex=0, int movey=0); // Gestion des collisions
|
||
|
|
||
|
/** scene */
|
||
|
void push(string id, xElement* sprite);
|
||
|
void pull(string id);
|
||
|
void pull(xElement *sprite);
|
||
|
void clear();
|
||
|
|
||
|
void render();
|
||
|
|
||
|
// manage main loop and thread loops
|
||
|
void setFps(const uint32_t fps);
|
||
|
void schedule();
|
||
|
|
||
|
xAppState state;
|
||
|
private:
|
||
|
/** Singleton */
|
||
|
xApplication(const char *t, int w, int h);
|
||
|
static xApplication* _instance;
|
||
|
|
||
|
void syncFps(const int fps=0);
|
||
|
|
||
|
// fps management
|
||
|
uint32_t _fps = 60;
|
||
|
uint32_t _lasttick;
|
||
|
uint32_t _fpstime;
|
||
|
|
||
|
// event management
|
||
|
xController _controller;
|
||
|
|
||
|
// sdl objects
|
||
|
SDL_Window *_window;
|
||
|
SDL_Rect _winrect;
|
||
|
|
||
|
SDL_Renderer *_renderer;
|
||
|
SDL_Texture *_texture;
|
||
|
|
||
|
// sprites
|
||
|
map<string, xElement*> _sprites;
|
||
|
|
||
|
// shared threads
|
||
|
map<string, thread*> _pool;
|
||
|
|
||
|
// thread safety
|
||
|
mutex _mutex_draw;
|
||
|
};
|
||
|
|
||
|
#endif
|