SDL#4 avancement general
This commit is contained in:
parent
a5f3faba73
commit
8671bdd9a5
|
@ -95,9 +95,10 @@ int main(int argc, char* argv[]) {
|
|||
if( !mgr->hit("mario", 0, 5) )
|
||||
mario->move(0, 5);
|
||||
|
||||
if( mgr->hit("mystery-bloc", 0, 8) )
|
||||
mb.stop();
|
||||
|
||||
if( mgr->hit("mystery-bloc", 0, 7) )
|
||||
mb.jump();
|
||||
else
|
||||
mb.unjump();
|
||||
|
||||
|
||||
mgr->manageFps(); // Gestion des FPS (speed)
|
||||
|
@ -170,8 +171,8 @@ void keydownEventHandler(SDL_Event *e){
|
|||
break;
|
||||
|
||||
case SDLK_DOWN:
|
||||
down_move = true;
|
||||
mario->moveDown(&down_move);
|
||||
// down_move = true;
|
||||
// mario->moveDown(&down_move);
|
||||
break;
|
||||
|
||||
default:
|
||||
|
@ -218,7 +219,7 @@ void keyupEventHandler(SDL_Event *e){
|
|||
break;
|
||||
|
||||
case SDLK_DOWN:
|
||||
down_move = false;
|
||||
// down_move = false;
|
||||
|
||||
break;
|
||||
|
||||
|
|
BIN
SDL#4/main.o
BIN
SDL#4/main.o
Binary file not shown.
Binary file not shown.
Before Width: | Height: | Size: 1.8 KiB After Width: | Height: | Size: 2.1 KiB |
|
@ -1,5 +1,6 @@
|
|||
A FAIRE
|
||||
=======
|
||||
- [ ] Gestion du saut unique ou double (limitation)
|
||||
- [ ] Erreur a corriger pour xSpriteAnimation on doit faire start() mais push() puis start() (qui est equivalent) ne marche pas
|
||||
- [ ] Gestion de l'acceleration
|
||||
- [ ] Gestion de la gravite
|
||||
|
|
|
@ -47,13 +47,13 @@ xMarioMario::xMarioMario(xManager *m, int x, int y)
|
|||
=========================================================*/
|
||||
void xLeftMoveProcess(xMarioMario *m, bool *run){
|
||||
bool hasMoved = false;
|
||||
int step = 5;
|
||||
int step = 3;
|
||||
|
||||
|
||||
while( *run ){
|
||||
|
||||
hasMoved = false;
|
||||
step = 5;
|
||||
step = 3;
|
||||
|
||||
// Tant qu'on a pas bouge et qu'on peut se deplacer
|
||||
while( !hasMoved && step > 0 ){
|
||||
|
@ -99,13 +99,13 @@ void xMarioMario::moveLeft(bool *run){
|
|||
=========================================================*/
|
||||
void xRightMoveProcess(xMarioMario *m, bool *run){
|
||||
bool hasMoved = false;
|
||||
int step = 5;
|
||||
int step = 3;
|
||||
|
||||
|
||||
while( *run ){
|
||||
|
||||
hasMoved = false;
|
||||
step = 5;
|
||||
step = 3;
|
||||
|
||||
// Tant qu'on a pas bouge et qu'on peut se deplacer
|
||||
while( !hasMoved && step > 0 ){
|
||||
|
@ -155,13 +155,13 @@ void xMarioMario::moveRight(bool *run){
|
|||
=========================================================*/
|
||||
void xUpMoveProcess(xMarioMario *m, bool *run){
|
||||
bool hasMoved = false;
|
||||
int step = 5;
|
||||
int step = 7;
|
||||
|
||||
|
||||
while( *run ){
|
||||
|
||||
hasMoved = false;
|
||||
step = 5;
|
||||
step = 7;
|
||||
|
||||
// Tant qu'on a pas bouge et qu'on peut se deplacer
|
||||
while( !hasMoved && step > 0 ){
|
||||
|
|
|
@ -12,4 +12,99 @@ xMarioMysteryBloc::xMarioMysteryBloc(xManager *m, int x, int y)
|
|||
this->addFrame( (SDL_Rect){0, 16, 16, 16} );
|
||||
this->addFrame( (SDL_Rect){0, 32, 16, 16} );
|
||||
this->addFrame( (SDL_Rect){0, 48, 16, 16} );
|
||||
|
||||
_defaultrect = _viewport;
|
||||
_lastjump = SDL_GetTicks();
|
||||
_jumps = 0;
|
||||
_active = true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* [JUMP] Animation d'activation (quand saut)
|
||||
=========================================================*/
|
||||
void xMarioMysteryBloc::jump(){
|
||||
if( !this->active() )
|
||||
return;
|
||||
|
||||
if( _defaultrect.y == _viewport.y && SDL_GetTicks()-_lastjump > 300 ){
|
||||
|
||||
_viewport.x = _defaultrect.x - _defaultrect.w*.1/2;
|
||||
_viewport.y = _defaultrect.y-10 - _defaultrect.h*.1/2;
|
||||
_viewport.w = _defaultrect.w * 1.1;
|
||||
_viewport.h = _defaultrect.h * 1.1;
|
||||
|
||||
this->move(_viewport);
|
||||
|
||||
_lastjump = SDL_GetTicks();
|
||||
|
||||
_jumps++;
|
||||
|
||||
if( _jumps >= 6 )
|
||||
this->active(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* [UNJUMP] Animation d'activation (quand saut)
|
||||
=========================================================*/
|
||||
void xMarioMysteryBloc::unjump(){
|
||||
// if( !this->active() )
|
||||
// return;
|
||||
|
||||
if( _defaultrect.y != _viewport.y && SDL_GetTicks()-_lastjump > 100 ){
|
||||
|
||||
_viewport.x = _defaultrect.x;
|
||||
_viewport.y = _defaultrect.y;
|
||||
_viewport.w = _defaultrect.w;
|
||||
_viewport.h = _defaultrect.h;
|
||||
|
||||
this->move(_viewport);
|
||||
|
||||
_lastjump = SDL_GetTicks();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* [ACTIVE] Retourne l'etat du bouton
|
||||
=========================================================*/
|
||||
bool xMarioMysteryBloc::active(){
|
||||
return _active;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* [ACTIVE] Gestion du caractere "actif" du bouton
|
||||
=========================================================*/
|
||||
void xMarioMysteryBloc::active(bool active){
|
||||
_active = active;
|
||||
|
||||
this->stop();
|
||||
|
||||
if( active ){
|
||||
|
||||
_frames.clear();
|
||||
// for( int i = 0 ; i < _frames.size() ; i++ )
|
||||
// _frames.erase(_frames.begin()+i);
|
||||
|
||||
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} );
|
||||
|
||||
}else{
|
||||
|
||||
_frames.clear();
|
||||
// for( int i = 0 ; i < _frames.size() ; i++ )
|
||||
// _frames.erase(_frames.begin()+i);
|
||||
|
||||
this->addFrame( (SDL_Rect){0, 64, 16, 16} );
|
||||
|
||||
}
|
||||
|
||||
this->start(_index, _timeout, _flags);
|
||||
|
||||
}
|
|
@ -6,6 +6,19 @@
|
|||
|
||||
public:
|
||||
xMarioMysteryBloc(xManager *manager, int x, int y); // Spritesheet avec taille de chaque sprite
|
||||
|
||||
void jump(); // Effectue l'animation d'activation
|
||||
void unjump(); // Effectue l'animation d'activation
|
||||
|
||||
bool active(); // Retourne si le bloc est actif ou non
|
||||
void active(bool active); // Active ou non le bloc
|
||||
|
||||
private:
|
||||
bool _active;
|
||||
int _jumps;
|
||||
Uint32 _lastjump;
|
||||
SDL_Rect _defaultrect;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
|
@ -89,12 +89,17 @@ bool xManager::setImage(const char *url){
|
|||
/* [HIT] Retourne si une texture est en collision avec une autre
|
||||
=========================================================*/
|
||||
bool xManager::hit(string current, int movex, int movey){
|
||||
// Anti conflit inter-thread
|
||||
_mutex_hit.lock();
|
||||
|
||||
/* (1) On recupere le SDL_Rect destination du sprite courant */
|
||||
SDL_Rect *cRect = this->getDst(current);
|
||||
|
||||
// Gestion erreur
|
||||
if( cRect == NULL )
|
||||
if( cRect == NULL ){
|
||||
_mutex_hit.unlock();
|
||||
return false;
|
||||
}
|
||||
|
||||
SDL_Rect r = (SDL_Rect){
|
||||
(*cRect).x+movex,
|
||||
|
@ -107,8 +112,11 @@ bool xManager::hit(string current, int movex, int movey){
|
|||
/* (2) On regarde si en dehors de la fenetre */
|
||||
for( int y = r.y ; y < r.y+r.h ; y++ )
|
||||
for( int x = r.x ; x < r.x+r.w ; x++ )
|
||||
if( x < _winrect.x || x > _winrect.x+_winrect.w || y < _winrect.y || y>_winrect.y+_winrect.h )
|
||||
if( x < _winrect.x || x > _winrect.x+_winrect.w || y < _winrect.y || y>_winrect.y+_winrect.h ){
|
||||
// On debloque la ressource
|
||||
_mutex_hit.unlock();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/* (3) On compare avec toutes les autres textures */
|
||||
|
@ -125,14 +133,22 @@ bool xManager::hit(string current, int movex, int movey){
|
|||
|
||||
for( int y = r.y ; y < r.y+r.h ; y++ )
|
||||
for( int x = r.x ; x < r.x+r.w ; x++ )
|
||||
if( x>=c.x && x<=c.x+c.w && y>=c.y && y<=c.y+c.h )
|
||||
if( x>=c.x && x<=c.x+c.w && y>=c.y && y<=c.y+c.h ){
|
||||
// On debloque la ressource
|
||||
_mutex_hit.unlock();
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// On debloque la ressource
|
||||
_mutex_hit.unlock();
|
||||
|
||||
return false;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -140,14 +156,20 @@ bool xManager::hit(string current, int movex, int movey){
|
|||
/* [HIT] Retourne si une texture est en collision avec une autre
|
||||
=========================================================*/
|
||||
bool xManager::hit(SDL_Texture *current, int movex, int movey){
|
||||
// Anti conflit inter-thread
|
||||
_mutex_hit.lock();
|
||||
|
||||
|
||||
/* (1) On recupere le SDL_Rect destination du sprite courant */
|
||||
int xIndex = -1;
|
||||
for( int i = 0 ; i < _sprites.size() ; i++ )
|
||||
if( _sprites[i] == current )
|
||||
xIndex = i;
|
||||
|
||||
if( xIndex == -1 )
|
||||
if( xIndex == -1 ){
|
||||
_mutex_hit.unlock();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
SDL_Rect r = (SDL_Rect){
|
||||
|
@ -161,8 +183,11 @@ bool xManager::hit(SDL_Texture *current, int movex, int movey){
|
|||
/* (2) On regarde si en dehors de la fenetre */
|
||||
for( int y = r.y ; y < r.y+r.h ; y++ )
|
||||
for( int x = r.x ; x < r.x+r.w ; x++ )
|
||||
if( x < _winrect.x || x > _winrect.x+_winrect.w || y < _winrect.y || y>_winrect.y+_winrect.h )
|
||||
if( x < _winrect.x || x > _winrect.x+_winrect.w || y < _winrect.y || y>_winrect.y+_winrect.h ){
|
||||
// On debloque la ressource
|
||||
_mutex_hit.unlock();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/* (3) On compare avec toutes les autres textures */
|
||||
|
@ -179,13 +204,18 @@ bool xManager::hit(SDL_Texture *current, int movex, int movey){
|
|||
|
||||
for( int y = r.y ; y < r.y+r.h ; y++ )
|
||||
for( int x = r.x ; x < r.x+r.w ; x++ )
|
||||
if( x>=c.x && x<=c.x+c.w && y>=c.y && y<=c.y+c.h )
|
||||
if( x>=c.x && x<=c.x+c.w && y>=c.y && y<=c.y+c.h ){
|
||||
// On debloque la ressource
|
||||
_mutex_hit.unlock();
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// On debloque la ressource
|
||||
_mutex_hit.unlock();
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -236,7 +266,7 @@ SDL_Rect *xManager::getDst(string index){
|
|||
=========================================================*/
|
||||
void xManager::push(string index, SDL_Texture *t, SDL_Rect *src, SDL_Rect *dst){
|
||||
// On bloque l'acces inter-thread
|
||||
_mutex.lock();
|
||||
_mutex_push.lock();
|
||||
|
||||
_indexes.push_back( index );
|
||||
_sprites.push_back( t );
|
||||
|
@ -244,7 +274,7 @@ void xManager::push(string index, SDL_Texture *t, SDL_Rect *src, SDL_Rect *dst){
|
|||
_dst.push_back( dst );
|
||||
|
||||
// On debloque l'acces
|
||||
_mutex.unlock();
|
||||
_mutex_push.unlock();
|
||||
}
|
||||
|
||||
|
||||
|
@ -252,7 +282,7 @@ void xManager::push(string index, SDL_Texture *t, SDL_Rect *src, SDL_Rect *dst){
|
|||
=========================================================*/
|
||||
void xManager::pull(string index){
|
||||
// On bloque l'acces inter-thread
|
||||
_mutex.lock();
|
||||
_mutex_pull.lock();
|
||||
|
||||
// On cherche l'indice de la texture
|
||||
int xIndex = -1;
|
||||
|
@ -271,7 +301,7 @@ void xManager::pull(string index){
|
|||
_dst.erase( _dst.begin() + xIndex );
|
||||
|
||||
// On debloque l'acces
|
||||
_mutex.unlock();
|
||||
_mutex_pull.unlock();
|
||||
}
|
||||
|
||||
|
||||
|
@ -279,7 +309,7 @@ void xManager::pull(string index){
|
|||
=========================================================*/
|
||||
void xManager::pull(SDL_Texture *t){
|
||||
// On bloque l'acces inter-thread
|
||||
_mutex.lock();
|
||||
_mutex_pull.lock();
|
||||
|
||||
// On cherche l'indice de la texture
|
||||
int xIndex = -1;
|
||||
|
@ -298,7 +328,7 @@ void xManager::pull(SDL_Texture *t){
|
|||
_dst.erase( _dst.begin() + xIndex );
|
||||
|
||||
// On debloque l'acces
|
||||
_mutex.unlock();
|
||||
_mutex_pull.unlock();
|
||||
}
|
||||
|
||||
|
||||
|
@ -334,7 +364,7 @@ void xManager::manageFps(const int fps){
|
|||
void xManager::update(){
|
||||
// cout << "Update MAIN SPRITE +" << _sprites.size() << " added sprites.." << endl;
|
||||
// On bloque l'acces inter-thread
|
||||
_mutex.lock();
|
||||
_mutex_update.lock();
|
||||
|
||||
/* (1) On efface le rendu */
|
||||
SDL_RenderClear(_renderer);
|
||||
|
@ -358,7 +388,7 @@ void xManager::update(){
|
|||
SDL_RenderPresent(_renderer);
|
||||
|
||||
// On debloque l'acces
|
||||
_mutex.unlock();
|
||||
_mutex_update.unlock();
|
||||
}
|
||||
|
||||
/* [ATTACHEVENT] Ajoute une fonction a un type d'evenement
|
||||
|
|
|
@ -59,7 +59,10 @@
|
|||
vector<SDL_Rect*> _dst;
|
||||
|
||||
// Protection thread-safe
|
||||
mutex _mutex;
|
||||
mutex _mutex_push;
|
||||
mutex _mutex_pull;
|
||||
mutex _mutex_update;
|
||||
mutex _mutex_hit;
|
||||
|
||||
|
||||
};
|
||||
|
|
|
@ -113,6 +113,8 @@ SDL_Rect *xSpriteAnimation::viewport(){ return &_viewport; }
|
|||
/* [PUSH] Ajoute au rendu
|
||||
=========================================================*/
|
||||
void xSpriteAnimation::push(string index){
|
||||
_index = index;
|
||||
|
||||
/* (1) On ajoute le sprite au rendu */
|
||||
_manager->push(index, _texture, &_frame, &_viewport);
|
||||
}
|
||||
|
@ -121,6 +123,8 @@ void xSpriteAnimation::push(string index){
|
|||
/* [PULL] Retire du rendu
|
||||
=========================================================*/
|
||||
void xSpriteAnimation::pull(string index){
|
||||
_index = index;
|
||||
|
||||
/* (2) On retire le sprite du rendu */
|
||||
_manager->pull(index);
|
||||
}
|
||||
|
@ -142,16 +146,15 @@ void xSpriteAnimation::pull(){
|
|||
/* [TRHEAD] Process de l'animation
|
||||
=========================================================*/
|
||||
void xSpriteAnimationProcess(xSpriteAnimation *xSA, int t, int flags){
|
||||
int length = xSA->_frames.size();
|
||||
int timer = 0;
|
||||
int step = 1;
|
||||
int start = 0;
|
||||
int stop = length;
|
||||
int stop = xSA->_frames.size();
|
||||
|
||||
while( flags&SPRITE_ANIM_INFINITE ){
|
||||
|
||||
/* (1) Pour chaque sprite */
|
||||
for( int i = start ; i != stop ; i+=step ){
|
||||
for( int i = start ; i != xSA->_frames.size() ; i+=step ){
|
||||
timer = SDL_GetTicks();
|
||||
|
||||
|
||||
|
@ -170,8 +173,8 @@ void xSpriteAnimationProcess(xSpriteAnimation *xSA, int t, int flags){
|
|||
// SPRITE_ANIM_REVERSE
|
||||
if( flags&SPRITE_ANIM_REVERSE ){
|
||||
step *= -1;
|
||||
start = (step==1) ? 0 : length-1;
|
||||
stop = (step==1) ? length-1 : 0;
|
||||
start = (step==1) ? 0 : xSA->_frames.size()-1;
|
||||
stop = (step==1) ? xSA->_frames.size()-1 : 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -188,6 +191,10 @@ void xSpriteAnimationProcess(xSpriteAnimation *xSA, int t, int flags){
|
|||
/* [START] Ajoute l'animation au rendu
|
||||
=========================================================*/
|
||||
void xSpriteAnimation::start(string index, int t, int flags){
|
||||
_index = index;
|
||||
_timeout = t;
|
||||
_flags = flags;
|
||||
|
||||
this->push(index);
|
||||
|
||||
/* (1) On lance l'animation */
|
||||
|
|
|
@ -41,6 +41,11 @@
|
|||
vector<SDL_Rect> _frames;
|
||||
SDL_Rect _frame; // Frame courante
|
||||
|
||||
// Nom
|
||||
string _index;
|
||||
int _timeout;
|
||||
int _flags;
|
||||
|
||||
// Contiendra le thread de l'animation
|
||||
thread *_animation;
|
||||
friend void xSpriteAnimationProcess(xSpriteAnimation *xSA, int t, int flags );
|
||||
|
|
|
@ -48,6 +48,8 @@ void xSpriteGroup::remove(xSprite *s){
|
|||
/* [PUSH] Ajoute tous les xSprite du groupe a une surface parente
|
||||
=========================================================*/
|
||||
void xSpriteGroup::push(string index){
|
||||
_index = index;
|
||||
|
||||
string newIndex;
|
||||
|
||||
for( int i = 0 ; i < _sprites.size() ; i++ ){
|
||||
|
@ -60,6 +62,8 @@ void xSpriteGroup::push(string index){
|
|||
/* [PULL] Retire une sprite de la surface parents
|
||||
=========================================================*/
|
||||
void xSpriteGroup::pull(string index){
|
||||
_index = index;
|
||||
|
||||
string newIndex;
|
||||
|
||||
for( int i = 0 ; i < _sprites.size() ; i++ ){
|
||||
|
|
|
@ -24,6 +24,8 @@
|
|||
protected:
|
||||
vector<xSprite*> _sprites;
|
||||
|
||||
string _index;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue