SDL#4 Creation des elements de xMario + gestion de plusieurs evenements:
|
@ -11,7 +11,7 @@ int main(int argc, char* argv[]) {
|
||||||
|
|
||||||
/* [0] Initialisation du manager + Creation de la fenetre
|
/* [0] Initialisation du manager + Creation de la fenetre
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
mgr = new xManager("Ma fenetre SDL", WIN_WIDTH, WIN_HEIGHT);
|
mgr = new xManager("Ma fenetre SDL", BLOC_WIDTH*BLOC_SIZE, BLOC_HEIGHT*BLOC_SIZE);
|
||||||
|
|
||||||
// Gestion erreur
|
// Gestion erreur
|
||||||
if( !mgr->status() ){
|
if( !mgr->status() ){
|
||||||
|
@ -19,58 +19,54 @@ int main(int argc, char* argv[]) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Couleur de fond
|
||||||
|
mgr->setBackground(0, 96, 183);
|
||||||
|
mgr->setImage("src/bg1.png");
|
||||||
|
|
||||||
/* [2] On definit le background color
|
/* [2] On definit le terrain
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
mgr->setBackground(255, 0, 255, 150);
|
// On cree un bout du terrain
|
||||||
// mgr->setImage( "src/1.bmp" );
|
xMarioGrass btmleft(mgr, (SDL_Rect){-1, 20-2, 10, 3} );
|
||||||
|
btmleft.push();
|
||||||
|
|
||||||
|
|
||||||
/* [3] Gestion des animations
|
|
||||||
|
/* [3] Gestion des animations (blocs animes)
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
xSpriteAnimation mario_mystery_block(
|
// On cree une coquille verte
|
||||||
mgr,
|
// xMarioGreenShell gs(mgr, 5, 20-3);
|
||||||
"src/mario_crop.png",
|
// gs.start(100, SPRITE_ANIM_INFINITE);
|
||||||
(SDL_Rect){0, 0, 50, 50}
|
|
||||||
);
|
|
||||||
mario_mystery_block.addFrame( (SDL_Rect){4*19, 2*20, 20, 20} );
|
|
||||||
mario_mystery_block.addFrame( (SDL_Rect){5*19, 2*20, 20, 20} );
|
|
||||||
mario_mystery_block.addFrame( (SDL_Rect){6*19, 2*20, 20, 20} );
|
|
||||||
mario_mystery_block.addFrame( (SDL_Rect){7*19, 2*20, 20, 20} );
|
|
||||||
|
|
||||||
mario_mystery_block.start(200, SPRITE_ANIM_INFINITE);
|
// On cree un bloc mystere
|
||||||
|
xMarioMysteryBloc mb(mgr, 5, 20-5);
|
||||||
|
mb.start(150, SPRITE_ANIM_INFINITE);
|
||||||
|
|
||||||
|
|
||||||
xSpriteAnimation mario_green_shell(
|
|
||||||
mgr,
|
|
||||||
"src/mario_crop.png",
|
|
||||||
(SDL_Rect){50, 0, 50, 50}
|
|
||||||
);
|
|
||||||
mario_green_shell.addFrame( (SDL_Rect){4*19, 210, 20, 20} );
|
|
||||||
mario_green_shell.addFrame( (SDL_Rect){5*19, 210, 20, 20} );
|
|
||||||
mario_green_shell.addFrame( (SDL_Rect){6*19, 210, 20, 20} );
|
|
||||||
mario_green_shell.addFrame( (SDL_Rect){7*19, 210, 20, 20} );
|
|
||||||
|
|
||||||
mario_green_shell.start(200, SPRITE_ANIM_INFINITE);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* [n-1] Boucle infinie
|
/* [n-1] Boucle infinie
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
mgr->update();
|
|
||||||
|
// Gestion des evenements
|
||||||
|
SDL_Event event;
|
||||||
|
mgr->attachEvent(SDL_KEYDOWN, &keydownEventHandler);
|
||||||
|
mgr->attachEvent(SDL_QUIT, &quitEventHandler);
|
||||||
|
|
||||||
|
// Boucle de traitement
|
||||||
|
mgr->update(); mgr->update();
|
||||||
mgr->manageFps(FPS);
|
mgr->manageFps(FPS);
|
||||||
running = true;
|
|
||||||
while(running){
|
while(running){
|
||||||
|
|
||||||
// Gestion d'un evenement
|
|
||||||
mgr->waitEvent(SDL_QUIT, &quitEventHandler);
|
|
||||||
|
|
||||||
// cout << "Main loop" << endl;
|
// Gestion des evenements
|
||||||
|
while( SDL_PollEvent(&event) != 0 )
|
||||||
|
mgr->manageEvents(&event);
|
||||||
|
|
||||||
// Gestion des FPS (vitesse de la boucle)
|
|
||||||
mgr->manageFps();
|
|
||||||
|
mgr->manageFps(); // Gestion des FPS (speed)
|
||||||
mgr->update(); // Mise a jour du rendu
|
mgr->update(); // Mise a jour du rendu
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,7 +83,24 @@ int main(int argc, char* argv[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* GESTION DE QUAND ON QUITTE LA FENETRE
|
||||||
|
*
|
||||||
|
* @e<SDL_Event*> Pointeur sur l'evenement appelant
|
||||||
|
*
|
||||||
|
*/
|
||||||
void quitEventHandler(SDL_Event *e){
|
void quitEventHandler(SDL_Event *e){
|
||||||
cout << "Exiting program" << endl;
|
cout << "Exiting program" << endl;
|
||||||
running = false;
|
running = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* GESTION DE QUAND ON APPUIE SUR FLECHE BAS
|
||||||
|
*
|
||||||
|
* @e<SDL_Event*> Pointeur sur l'evenement appelant
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void keydownEventHandler(SDL_Event *e){
|
||||||
|
cout << "BAS" << endl;
|
||||||
}
|
}
|
|
@ -10,7 +10,8 @@
|
||||||
|
|
||||||
/* [LIB] Externes
|
/* [LIB] Externes
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
#include "xSDL.h" // Librairie perso
|
#include "xSDL.h" // Librairie perso
|
||||||
|
#include "xMario.h" // Elements utiles au jeu
|
||||||
|
|
||||||
/* [NS] Namespace
|
/* [NS] Namespace
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
|
@ -18,13 +19,14 @@
|
||||||
|
|
||||||
/* [CONST] Constantes et enumerations
|
/* [CONST] Constantes et enumerations
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
#define WIN_WIDTH 1200
|
#define BLOC_WIDTH 32
|
||||||
#define WIN_HEIGHT 800
|
#define BLOC_HEIGHT 20
|
||||||
|
|
||||||
#define FPS 60
|
#define FPS 60
|
||||||
|
|
||||||
/* [FONCTIONS] Fonctions du corps
|
/* [FONCTIONS] Fonctions du corps
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
void quitEventHandler(SDL_Event *e);
|
void quitEventHandler(SDL_Event *e);
|
||||||
|
void keydownEventHandler(SDL_Event *e);
|
||||||
|
|
||||||
#endif
|
#endif
|
BIN
SDL#4/main.o
After Width: | Height: | Size: 725 KiB |
After Width: | Height: | Size: 192 KiB |
Before Width: | Height: | Size: 13 KiB |
Before Width: | Height: | Size: 32 KiB |
After Width: | Height: | Size: 140 KiB |
Before Width: | Height: | Size: 194 KiB |
Before Width: | Height: | Size: 33 KiB |
After Width: | Height: | Size: 4.8 MiB |
After Width: | Height: | Size: 55 KiB |
Before Width: | Height: | Size: 142 KiB After Width: | Height: | Size: 213 KiB |
Before Width: | Height: | Size: 63 KiB After Width: | Height: | Size: 63 KiB |
After Width: | Height: | Size: 1.8 KiB |
|
@ -0,0 +1,8 @@
|
||||||
|
A FAIRE
|
||||||
|
=======
|
||||||
|
- [x] Auto-texture pour le sol (grass)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
FAIT
|
||||||
|
====
|
|
@ -0,0 +1,27 @@
|
||||||
|
#ifndef DEF_XMARIO_H
|
||||||
|
|
||||||
|
#define DEF_XMARIO_H
|
||||||
|
|
||||||
|
/* [LIBS] Internes
|
||||||
|
=========================================================*/
|
||||||
|
// #include "xSDL.h"
|
||||||
|
|
||||||
|
|
||||||
|
/* [DEF] Constantes et enumerations
|
||||||
|
=========================================================*/
|
||||||
|
#define BLOC_SIZE 32
|
||||||
|
|
||||||
|
/* [HEADERS] Inclusion des .h des sous-libs
|
||||||
|
=========================================================*/
|
||||||
|
#include "xMario/xMarioGrass.h"
|
||||||
|
#include "xMario/xMarioGreenShell.h"
|
||||||
|
#include "xMario/xMarioMysteryBloc.h"
|
||||||
|
|
||||||
|
|
||||||
|
/* [BODIES] Inclusion des .cpp des sous-libs
|
||||||
|
=========================================================*/
|
||||||
|
#include "xMario/xMarioGrass.cpp"
|
||||||
|
#include "xMario/xMarioGreenShell.cpp"
|
||||||
|
#include "xMario/xMarioMysteryBloc.cpp"
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,104 @@
|
||||||
|
/* [CONSTRUCTOR] Construction d'un xSMarioGrass
|
||||||
|
=========================================================*/
|
||||||
|
xMarioGrass::xMarioGrass(xManager *m, SDL_Rect rect){
|
||||||
|
_manager = m;
|
||||||
|
|
||||||
|
// Note: le rect correspond a un nombre de bloc
|
||||||
|
// On convertit le tout en blocs reels
|
||||||
|
int xMin = rect.x;
|
||||||
|
int xMax = rect.w + rect.x - 1;
|
||||||
|
|
||||||
|
int yMin = rect.y;
|
||||||
|
int yMax = rect.h + rect.y - 1;
|
||||||
|
|
||||||
|
|
||||||
|
/* (1) On charge la texture */
|
||||||
|
_spritesheet = IMG_LoadTexture(_manager->renderer(), "src/mario_ground.jpg");
|
||||||
|
|
||||||
|
|
||||||
|
int index = 0;
|
||||||
|
|
||||||
|
/* (2) On cree les plans (layers) */
|
||||||
|
for( int y = yMin ; y <= yMax ; y++ ){
|
||||||
|
for( int x = xMin ; x <= xMax ; x++ ){
|
||||||
|
// On cree une copie du spritesheet
|
||||||
|
this->add( new xSprite(_manager, _spritesheet) );
|
||||||
|
|
||||||
|
|
||||||
|
// TOP-LEFT
|
||||||
|
if( x == xMin && y == yMin ){
|
||||||
|
this->get(index)->dimensions(
|
||||||
|
(SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE},
|
||||||
|
(SDL_Rect){1, 0, 16, 16}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TOP RIGHT
|
||||||
|
else if( x == xMax && y == yMin ){
|
||||||
|
this->get(index)->dimensions(
|
||||||
|
(SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE},
|
||||||
|
(SDL_Rect){35, 0, 16, 16}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// BOTTOM LEFT
|
||||||
|
else if( x == xMin && y == yMax ){
|
||||||
|
this->get(index)->dimensions(
|
||||||
|
(SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE},
|
||||||
|
(SDL_Rect){1, 34, 16, 16}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// BOTTOM RIGHT
|
||||||
|
else if( x == xMax && y == yMax ){
|
||||||
|
this->get(index)->dimensions(
|
||||||
|
(SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE},
|
||||||
|
(SDL_Rect){35, 34, 16, 16}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// LEFT
|
||||||
|
else if( x == xMin ){
|
||||||
|
this->get(index)->dimensions(
|
||||||
|
(SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE},
|
||||||
|
(SDL_Rect){1, 17, 16, 16}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// RIGHT
|
||||||
|
else if( x == xMax ){
|
||||||
|
this->get(index)->dimensions(
|
||||||
|
(SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE},
|
||||||
|
(SDL_Rect){35, 17, 16, 16}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TOP
|
||||||
|
else if( y == yMin ){
|
||||||
|
this->get(index)->dimensions(
|
||||||
|
(SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE},
|
||||||
|
(SDL_Rect){18, 0, 16, 16}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// BOTTOM
|
||||||
|
else if( y == yMax ){
|
||||||
|
this->get(index)->dimensions(
|
||||||
|
(SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE},
|
||||||
|
(SDL_Rect){18, 34, 16, 16}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// INSIDE
|
||||||
|
else{
|
||||||
|
this->get(index)->dimensions(
|
||||||
|
(SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE},
|
||||||
|
(SDL_Rect){18, 17, 16, 16}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
#ifndef DEF_XMARIOGRASS_H
|
||||||
|
|
||||||
|
#define DEF_XMARIOGRASS_H
|
||||||
|
|
||||||
|
/* [DEF] Definition de la classe
|
||||||
|
=========================================================*/
|
||||||
|
class xMarioGrass : public xSpriteGroup{
|
||||||
|
|
||||||
|
public:
|
||||||
|
xMarioGrass(xManager *m, SDL_Rect rect);
|
||||||
|
|
||||||
|
private:
|
||||||
|
xManager *_manager;
|
||||||
|
SDL_Texture *_spritesheet; // Contiendra la texture
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,15 @@
|
||||||
|
/* [CONSTRUCTOR] Construction d'un xMarioGreenShell
|
||||||
|
=========================================================*/
|
||||||
|
xMarioGreenShell::xMarioGreenShell(xManager *m, int x, int y)
|
||||||
|
: xSpriteAnimation(
|
||||||
|
m,
|
||||||
|
"src/mario_crop.png",
|
||||||
|
(SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE}
|
||||||
|
){
|
||||||
|
|
||||||
|
/* (2) On definit les clip de chaque frame */
|
||||||
|
this->addFrame( (SDL_Rect){4*19, 210, 20, 20} );
|
||||||
|
this->addFrame( (SDL_Rect){5*19, 210, 20, 20} );
|
||||||
|
this->addFrame( (SDL_Rect){6*19, 210, 20, 20} );
|
||||||
|
this->addFrame( (SDL_Rect){7*19, 210, 20, 20} );
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
#ifndef DEF_XMARIOGREENSHELL_H
|
||||||
|
|
||||||
|
#define DEF_XMARIOGREENSHELL_H
|
||||||
|
|
||||||
|
class xMarioGreenShell : public xSpriteAnimation{
|
||||||
|
|
||||||
|
public:
|
||||||
|
xMarioGreenShell(xManager *manager, int x, int y); // Spritesheet avec taille de chaque sprite
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
|
@ -0,0 +1,15 @@
|
||||||
|
/* [CONSTRUCTOR] Construction d'un xMarioGreenShell
|
||||||
|
=========================================================*/
|
||||||
|
xMarioMysteryBloc::xMarioMysteryBloc(xManager *m, int x, int y)
|
||||||
|
: xSpriteAnimation(
|
||||||
|
m,
|
||||||
|
"src/myst_bloc.png",
|
||||||
|
(SDL_Rect){BLOC_SIZE*x, BLOC_SIZE*y, BLOC_SIZE, BLOC_SIZE}
|
||||||
|
){
|
||||||
|
|
||||||
|
/* (2) On definit les clip de chaque frame */
|
||||||
|
this->addFrame( (SDL_Rect){0, 0, 16, 16} );
|
||||||
|
this->addFrame( (SDL_Rect){0, 16, 16, 16} );
|
||||||
|
this->addFrame( (SDL_Rect){0, 32, 16, 16} );
|
||||||
|
this->addFrame( (SDL_Rect){0, 48, 16, 16} );
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
#ifndef DEF_XMARIOMYSTERYBLOC_H
|
||||||
|
|
||||||
|
#define DEF_XMARIOMYSTERYBLOC_H
|
||||||
|
|
||||||
|
class xMarioMysteryBloc : public xSpriteAnimation{
|
||||||
|
|
||||||
|
public:
|
||||||
|
xMarioMysteryBloc(xManager *manager, int x, int y); // Spritesheet avec taille de chaque sprite
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
|
@ -2,13 +2,12 @@
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
xManager::xManager(const char *t, int w, int h){
|
xManager::xManager(const char *t, int w, int h){
|
||||||
// default values
|
// default values
|
||||||
_lasttick = 0;
|
_lasttick = SDL_GetTicks();
|
||||||
_fpstime = 1000/60;
|
_fpstime = 1000/60;
|
||||||
_window = NULL;
|
_window = NULL;
|
||||||
_renderer = NULL;
|
_renderer = NULL;
|
||||||
_texture = NULL;
|
_texture = NULL;
|
||||||
|
|
||||||
|
|
||||||
// Initialisation des sous-sys. SDL
|
// Initialisation des sous-sys. SDL
|
||||||
SDL_Init( SDL_INIT_EVERYTHING );
|
SDL_Init( SDL_INIT_EVERYTHING );
|
||||||
|
|
||||||
|
@ -28,6 +27,10 @@ xManager::xManager(const char *t, int w, int h){
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|
||||||
|
// On enregistre les dimensions de la fenetre
|
||||||
|
_winrect.x = _winrect.y = 0;
|
||||||
|
SDL_GetWindowSize(_window, &_winrect.w, &_winrect.h);
|
||||||
|
|
||||||
// Creation du renderer
|
// Creation du renderer
|
||||||
_renderer = SDL_CreateRenderer(
|
_renderer = SDL_CreateRenderer(
|
||||||
_window,
|
_window,
|
||||||
|
@ -125,20 +128,45 @@ void xManager::pull(SDL_Texture *t){
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* [MANAGEFTP] Gestion de la vitesse de boucle
|
||||||
|
=========================================================*/
|
||||||
|
void xManager::manageFps(const int fps){
|
||||||
|
/* (1) Definition de fps */
|
||||||
|
if( fps != 0 )
|
||||||
|
_fpstime = 1000/fps;
|
||||||
|
|
||||||
|
if( _lasttick == 0 )
|
||||||
|
_lasttick = SDL_GetTicks()-_fpstime;
|
||||||
|
|
||||||
|
/* (2) Utilisation en fin de boucle */
|
||||||
|
|
||||||
|
// 1 > Si trop rapide, on attends
|
||||||
|
if( SDL_GetTicks()-_lasttick < _fpstime )
|
||||||
|
SDL_Delay( _fpstime - (SDL_GetTicks()-_lasttick) );
|
||||||
|
|
||||||
|
// On enregistre le temps actuel
|
||||||
|
_lasttick = SDL_GetTicks();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* [UPDATE] Mise a jour du rendu
|
/* [UPDATE] Mise a jour du rendu
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
void xManager::update(){
|
void xManager::update(){
|
||||||
// cout << "Update MAIN SPRITE +" << _sprites.size() << " added sprites.." << endl;
|
// cout << "Update MAIN SPRITE +" << _sprites.size() << " added sprites.." << endl;
|
||||||
|
// On bloque l'acces inter-thread
|
||||||
|
_mutex.lock();
|
||||||
|
|
||||||
/* (1) On efface le rendu */
|
/* (1) On efface le rendu */
|
||||||
SDL_RenderClear(_renderer);
|
SDL_RenderClear(_renderer);
|
||||||
|
|
||||||
|
|
||||||
/* (2) On applique la couleur de fond */
|
/* (2) On applique la couleur de fond */
|
||||||
SDL_Rect dRect; // On recupere les dimensions de la fenetre
|
SDL_RenderDrawRect(_renderer, &_winrect);
|
||||||
SDL_GetWindowSize(_window, &dRect.w, &dRect.h);
|
|
||||||
SDL_RenderDrawRect(_renderer, &dRect);
|
|
||||||
|
|
||||||
|
|
||||||
/* (3) On ajoute le rendu principal (si existe) */
|
/* (3) On ajoute le rendu principal (si existe) */
|
||||||
|
@ -154,44 +182,27 @@ void xManager::update(){
|
||||||
/* (n) On affiche le resultat */
|
/* (n) On affiche le resultat */
|
||||||
SDL_RenderPresent(_renderer);
|
SDL_RenderPresent(_renderer);
|
||||||
|
|
||||||
|
// On debloque l'acces
|
||||||
|
_mutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* [WAITEVENT] Attends un evenement pour executer
|
/* [ATTACHEVENT] Ajoute une fonction a un type d'evenement
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
void xManager::waitEvent(SDL_EventType t, void(*handler)(SDL_Event*)){
|
void xManager::attachEvent(SDL_EventType t, void(*handler)(SDL_Event*)){
|
||||||
SDL_Event event;
|
|
||||||
|
|
||||||
// On attends un evenement
|
// On attache le type d'evenement a la fonction
|
||||||
while( SDL_PollEvent(&event) ){
|
_events.push_back( t );
|
||||||
|
_handlers.push_back( handler );
|
||||||
// quand evenement, si type = t
|
|
||||||
if( event.type == t )
|
|
||||||
(*handler)(&event);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* [MANAGEFTP] Gestion de la vitesse de boucle
|
|
||||||
|
|
||||||
|
|
||||||
|
/* [MANAGEEVENTS] Gestion des evenements
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
void xManager::manageFps(const int fps){
|
void xManager::manageEvents(SDL_Event *event){
|
||||||
/* (1) Definition de fps */
|
// On lance les evenements en fonction de leur type
|
||||||
if( fps != 0 ) _fpstime = 1000/fps;
|
for( int i = 0 ; i < _events.size() ; i ++ )
|
||||||
|
if( event->type == _events[i] ) // si type ok
|
||||||
/* (2) Initialisation timer */
|
(*_handlers[i])(event); // on execute le handler
|
||||||
if( _lasttick == 0 )
|
|
||||||
_lasttick = SDL_GetTicks();
|
|
||||||
|
|
||||||
/* (3) Utilisation en fin de WHILE() */
|
|
||||||
|
|
||||||
// 1 > Si trop rapide, on attends + definit _lasttick
|
|
||||||
else if( SDL_GetTicks()-_lasttick < _fpstime ){
|
|
||||||
SDL_Delay( _fpstime - (SDL_GetTicks()-_lasttick) );
|
|
||||||
|
|
||||||
_lasttick = SDL_GetTicks() + _fpstime - (SDL_GetTicks()-_lasttick);
|
|
||||||
|
|
||||||
// 2 > Temps ok
|
|
||||||
}else
|
|
||||||
_lasttick = SDL_GetTicks();
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -11,7 +11,6 @@
|
||||||
bool status();
|
bool status();
|
||||||
bool setBackground(Uint8 r=0xff, Uint8 g=0xff, Uint8 b=0xff, Uint8 a=0xff);
|
bool setBackground(Uint8 r=0xff, Uint8 g=0xff, Uint8 b=0xff, Uint8 a=0xff);
|
||||||
bool setImage(const char *url);
|
bool setImage(const char *url);
|
||||||
void waitEvent(SDL_EventType t, void(*handler)(SDL_Event*) );
|
|
||||||
|
|
||||||
void push(SDL_Texture *t, SDL_Rect *origin, SDL_Rect *dest);
|
void push(SDL_Texture *t, SDL_Rect *origin, SDL_Rect *dest);
|
||||||
void pull(SDL_Texture *t);
|
void pull(SDL_Texture *t);
|
||||||
|
@ -20,16 +19,28 @@
|
||||||
|
|
||||||
void manageFps(const int fps=0);
|
void manageFps(const int fps=0);
|
||||||
|
|
||||||
|
|
||||||
|
// Gestion des evenements
|
||||||
|
void attachEvent(SDL_EventType t, void(*handler)(SDL_Event*) );
|
||||||
|
void manageEvents(SDL_Event* event);
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// gestion FPS
|
// gestion FPS
|
||||||
Uint32 _lasttick;
|
Uint32 _lasttick;
|
||||||
Uint32 _fpstime;
|
Uint32 _fpstime;
|
||||||
|
|
||||||
|
// Gestion evenements
|
||||||
|
vector<SDL_EventType> _events;
|
||||||
|
vector<void(*)(SDL_Event*)> _handlers;
|
||||||
|
|
||||||
// status de l'initialisation
|
// status de l'initialisation
|
||||||
bool _status;
|
bool _status;
|
||||||
|
|
||||||
// Elements utiles
|
// Elements utiles
|
||||||
SDL_Window *_window;
|
SDL_Window *_window;
|
||||||
|
SDL_Rect _winrect;
|
||||||
|
|
||||||
SDL_Renderer *_renderer;
|
SDL_Renderer *_renderer;
|
||||||
SDL_Texture *_texture;
|
SDL_Texture *_texture;
|
||||||
|
|
||||||
|
|
|
@ -21,9 +21,6 @@ xSprite::xSprite(xManager *m, const int rgb[]){
|
||||||
_manager = m;
|
_manager = m;
|
||||||
_texture = NULL;
|
_texture = NULL;
|
||||||
|
|
||||||
|
|
||||||
cout << "a: [" << (sizeof(rgb)/sizeof(*rgb)) << "]" << endl;
|
|
||||||
|
|
||||||
SDL_Surface *surf = SDL_CreateRGBSurface(0, 0, 0, 32, 0, 0, 0, rgb[3]);
|
SDL_Surface *surf = SDL_CreateRGBSurface(0, 0, 0, 32, 0, 0, 0, rgb[3]);
|
||||||
|
|
||||||
// On recupere la couleur
|
// On recupere la couleur
|
||||||
|
@ -52,14 +49,16 @@ xSprite::xSprite(xManager *m, const char *url){
|
||||||
/* (1) On cree la texture directement */
|
/* (1) On cree la texture directement */
|
||||||
_texture = IMG_LoadTexture(_manager->renderer(), url);
|
_texture = IMG_LoadTexture(_manager->renderer(), url);
|
||||||
|
|
||||||
|
// Gestion erreur
|
||||||
|
if( _texture == NULL )
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
/* (1bis) On cree la surface puis on la copie dans la texture */
|
/* [CONSTRUCTOR] Constructions avec copie de texture
|
||||||
// SDL_Surface *surf = IMG_Load( url );
|
=========================================================*/
|
||||||
|
xSprite::xSprite(xManager *m, SDL_Texture *t){
|
||||||
// _texture = SDL_CreateTextureFromSurface(_manager->renderer(), surf);
|
_manager = m;
|
||||||
|
_texture = t;
|
||||||
// SDL_FreeSurface(surf);
|
|
||||||
// surf = NULL;
|
|
||||||
|
|
||||||
// Gestion erreur
|
// Gestion erreur
|
||||||
if( _texture == NULL )
|
if( _texture == NULL )
|
||||||
|
@ -70,8 +69,6 @@ xSprite::xSprite(xManager *m, const char *url){
|
||||||
/* [DIMENSIONS] Definition des dimensions par defaut
|
/* [DIMENSIONS] Definition des dimensions par defaut
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
void xSprite::dimensions(){
|
void xSprite::dimensions(){
|
||||||
cout <<"A"<<endl;
|
|
||||||
|
|
||||||
/* (1) On recupere les informations de la texture */
|
/* (1) On recupere les informations de la texture */
|
||||||
int w, h;
|
int w, h;
|
||||||
SDL_QueryTexture(_texture, NULL, NULL, &w, &h);
|
SDL_QueryTexture(_texture, NULL, NULL, &w, &h);
|
||||||
|
@ -86,8 +83,6 @@ void xSprite::dimensions(){
|
||||||
/* [DIMENSIONS] Definition des dimensions de la texture
|
/* [DIMENSIONS] Definition des dimensions de la texture
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
void xSprite::dimensions(SDL_Rect r){
|
void xSprite::dimensions(SDL_Rect r){
|
||||||
cout <<"B"<<endl;
|
|
||||||
|
|
||||||
/* (1) On recupere les informations de la texture */
|
/* (1) On recupere les informations de la texture */
|
||||||
int w, h;
|
int w, h;
|
||||||
SDL_QueryTexture(_texture, NULL, NULL, &w, &h);
|
SDL_QueryTexture(_texture, NULL, NULL, &w, &h);
|
||||||
|
@ -100,9 +95,7 @@ void xSprite::dimensions(SDL_Rect r){
|
||||||
|
|
||||||
/* [DIMENSIONS] Definition des dimensions de la texture+clip
|
/* [DIMENSIONS] Definition des dimensions de la texture+clip
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
void xSprite::dimensions(SDL_Rect r, SDL_Rect clip){
|
void xSprite::dimensions(SDL_Rect r, SDL_Rect clip){
|
||||||
cout <<"C"<<endl;
|
|
||||||
|
|
||||||
/* (1) On definit les dimensions */
|
/* (1) On definit les dimensions */
|
||||||
_dst = (SDL_Rect){r.x, r.y, r.w, r.h};
|
_dst = (SDL_Rect){r.x, r.y, r.w, r.h};
|
||||||
_src = (SDL_Rect){clip.x, clip.y, clip.w, clip.h};
|
_src = (SDL_Rect){clip.x, clip.y, clip.w, clip.h};
|
||||||
|
@ -134,3 +127,12 @@ SDL_Texture *xSprite::texture(){ return _texture; }
|
||||||
/* [MANAGER] Retourne le manager
|
/* [MANAGER] Retourne le manager
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
xManager *xSprite::manager(){ return _manager; }
|
xManager *xSprite::manager(){ return _manager; }
|
||||||
|
|
||||||
|
|
||||||
|
/* [DST] Retourne le SDL_Rect de destination
|
||||||
|
=========================================================*/
|
||||||
|
SDL_Rect *xSprite::dst(){ return &_dst; }
|
||||||
|
|
||||||
|
/* [SRC] Retourne le SDL_Rect source
|
||||||
|
=========================================================*/
|
||||||
|
SDL_Rect *xSprite::src(){ return &_src; }
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
xSprite(xManager *m); // Sprite vide
|
xSprite(xManager *m); // Sprite vide
|
||||||
xSprite(xManager *m, const int rgb[]); // Sprite couleur
|
xSprite(xManager *m, const int rgb[]); // Sprite couleur
|
||||||
xSprite(xManager *m, const char *url); // Sprite image
|
xSprite(xManager *m, const char *url); // Sprite image
|
||||||
|
xSprite(xManager *m, SDL_Texture *t); // Sprite texture
|
||||||
~xSprite();
|
~xSprite();
|
||||||
|
|
||||||
void dimensions(); // Dimensions par defaut
|
void dimensions(); // Dimensions par defaut
|
||||||
|
@ -21,8 +22,10 @@
|
||||||
|
|
||||||
// GETTERS
|
// GETTERS
|
||||||
SDL_Texture *texture();
|
SDL_Texture *texture();
|
||||||
xManager *manager();
|
xManager *manager();
|
||||||
|
|
||||||
|
SDL_Rect *dst();
|
||||||
|
SDL_Rect *src();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
xManager *_manager;
|
xManager *_manager;
|
||||||
|
|
|
@ -22,9 +22,39 @@ xSpriteAnimation::xSpriteAnimation(xManager *manager, const char *url, SDL_Rect
|
||||||
// Gestion erreur
|
// Gestion erreur
|
||||||
if( _texture == NULL )
|
if( _texture == NULL )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* [CONSTRUCTOR] Construction de l'animation a partir d'une texture existante
|
||||||
|
=========================================================*/
|
||||||
|
xSpriteAnimation::xSpriteAnimation(xManager *manager, SDL_Texture *t, SDL_Rect viewport){
|
||||||
|
/* (1) Definition des attributs */
|
||||||
|
_manager = manager;
|
||||||
|
_texture = NULL;
|
||||||
|
|
||||||
|
_viewport = viewport;
|
||||||
|
|
||||||
|
|
||||||
|
/* (2) On charge le spritesheet */
|
||||||
|
_texture = t;
|
||||||
|
|
||||||
|
// Gestion erreur
|
||||||
|
if( _texture == NULL )
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* [MOVE] Modification de la position/taille du sprite
|
||||||
|
=========================================================*/
|
||||||
|
void xSpriteAnimation::move(SDL_Rect newpos){
|
||||||
|
_viewport.x = newpos.x;
|
||||||
|
_viewport.y = newpos.y;
|
||||||
|
_viewport.w = newpos.w;
|
||||||
|
_viewport.h = newpos.h;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* [ADDFRAME] Ajout d'une frame d'animation
|
/* [ADDFRAME] Ajout d'une frame d'animation
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
void xSpriteAnimation::addFrame(SDL_Rect clip){
|
void xSpriteAnimation::addFrame(SDL_Rect clip){
|
||||||
|
@ -65,7 +95,7 @@ void xSpriteAnimationProcess(xSpriteAnimation *xSA, int t, int flags){
|
||||||
// On met a jour la frame
|
// On met a jour la frame
|
||||||
xSA->_frame = xSA->_frames[i];
|
xSA->_frame = xSA->_frames[i];
|
||||||
|
|
||||||
// xSA->manager()->update();
|
xSA->manager()->update();
|
||||||
|
|
||||||
|
|
||||||
if( SDL_GetTicks()-timer < t )
|
if( SDL_GetTicks()-timer < t )
|
||||||
|
|
|
@ -6,8 +6,11 @@
|
||||||
|
|
||||||
public:
|
public:
|
||||||
xSpriteAnimation(xManager *manager, const char *url, SDL_Rect viewport); // Spritesheet avec taille de chaque sprite
|
xSpriteAnimation(xManager *manager, const char *url, SDL_Rect viewport); // Spritesheet avec taille de chaque sprite
|
||||||
|
xSpriteAnimation(xManager *manager, SDL_Texture *t, SDL_Rect viewport); // Spritesheet avec taille de chaque sprite
|
||||||
~xSpriteAnimation();
|
~xSpriteAnimation();
|
||||||
|
|
||||||
|
void move(SDL_Rect newpos); // Deplace l'animation
|
||||||
|
|
||||||
void addFrame(SDL_Rect clip); // Ajoute une frame en fonction des coordonnees
|
void addFrame(SDL_Rect clip); // Ajoute une frame en fonction des coordonnees
|
||||||
|
|
||||||
// GETTER
|
// GETTER
|
||||||
|
@ -19,7 +22,7 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private:
|
protected:
|
||||||
xManager *_manager;
|
xManager *_manager;
|
||||||
SDL_Texture *_texture;
|
SDL_Texture *_texture;
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,25 @@
|
||||||
/* [CONSTRUCTOR] Initialisation de la liste de xSprite
|
/* [CONSTRUCTOR] Initialisation de la liste de xSprite
|
||||||
=========================================================*/
|
=========================================================*/
|
||||||
xSpriteGroup::xSpriteGroup(){
|
xSpriteGroup::xSpriteGroup(){
|
||||||
// _sprites = new vector<xSprite*>(0);
|
}
|
||||||
|
|
||||||
|
/* [DESTRUCTOR] Efface la liste de xSprite
|
||||||
|
=========================================================*/
|
||||||
|
xSpriteGroup::~xSpriteGroup(){
|
||||||
|
for( int i = _sprites.size()-1 ; i >= 0 ; i-- )
|
||||||
|
_sprites.erase( _sprites.begin() + i );
|
||||||
|
}
|
||||||
|
|
||||||
|
/* [MOVE] Deplace toutes les sprites
|
||||||
|
=========================================================*/
|
||||||
|
void xSpriteGroup::move(int x, int y){
|
||||||
|
for( int i = 0 ; i < _sprites.size() ; i++ ){
|
||||||
|
|
||||||
|
SDL_Rect *dest = _sprites[i]->dst();
|
||||||
|
|
||||||
|
(*dest).x += x;
|
||||||
|
(*dest).y += y;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* [ADD] Ajoute un xSprite au groupe
|
/* [ADD] Ajoute un xSprite au groupe
|
||||||
|
|
|
@ -7,6 +7,10 @@
|
||||||
|
|
||||||
public:
|
public:
|
||||||
xSpriteGroup();
|
xSpriteGroup();
|
||||||
|
~xSpriteGroup();
|
||||||
|
|
||||||
|
void move(int x, int y); // Deplace toutes les sprites
|
||||||
|
|
||||||
void add(xSprite *s);
|
void add(xSprite *s);
|
||||||
void remove(xSprite *s);
|
void remove(xSprite *s);
|
||||||
xSprite* get(int i);
|
xSprite* get(int i);
|
||||||
|
@ -16,7 +20,7 @@
|
||||||
|
|
||||||
void update(); // Fait renmonter la mise a jour du manager
|
void update(); // Fait renmonter la mise a jour du manager
|
||||||
|
|
||||||
private:
|
protected:
|
||||||
vector<xSprite*> _sprites;
|
vector<xSprite*> _sprites;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|