Classe

cs_ToToL Messages postés 170 Date d'inscription vendredi 25 avril 2003 Statut Membre Dernière intervention 9 juillet 2008 - 1 juil. 2004 à 20:47
cs_ToToL Messages postés 170 Date d'inscription vendredi 25 avril 2003 Statut Membre Dernière intervention 9 juillet 2008 - 2 juil. 2004 à 19:18
Bonjour j'ai un eptit souci de classe. tout d'abor voici mon code source :

#include <SDL.h>
#include <stdlib.h>
#include <stdio.h>

int InitSDL( void );
void Analise( void );
int DrawScreen ( void );
int Load_IMG( void );
void Evenement( void );
void Mouvement( void );

class Sprite {
public:
Sprite(int x, int y);
void Movex ( int xx );
void Movey ( int yy );
void img_select( int img );
SDL_Rect pos,select;
};
Sprite::Sprite(int x, int y )
{
pos.x = x;
pos.y = y;
pos.w = 38;
pos.h = 67;
select.x = 0;
select.y = 0;
select.w = 38;
select.h = 67;
}
void Sprite::Movex ( int xx )
{
if ( pos.x > 2 && pos.x < 630 ) { pos.x = pos.x + xx;}
}
void Sprite::Movey ( int yy )
{
if ( pos.y > 2 && pos.y < 470 ) { pos.y = pos.y + yy;}
}
void Sprite::img_select ( int img )
{if ( img 1 ) { select.x 0; select.y = 0; }if ( img 2 ) { select.x 38; select.y = 0; }if ( img 3 ) { select.x 76; select.y = 0; }if ( img 4 ) { select.x 114; select.y = 0; }if ( img 5 ) { select.x 0; select.y = 67; }if ( img 6 ) { select.x 38; select.y = 67; }if ( img 7 ) { select.x 76; select.y = 67; }if ( img 8 ) { select.x 114; select.y = 67; }
}
Sprite * Firen = NULL;
Uint8 *keystate;

SDL_Surface * Screen,*img_firen;
int breakprog =0;

int main ( int argc , char * argv[] )
{
Firen = new Sprite(100,100);
InitSDL();
Analise();
delete Firen;
return 0;
}

int DrawScreen ( void )
{
SDL_Flip( Screen );
return 1;
}

void Analise( void )
{
if ( DrawScreen() == 0) return;
if ( Load_IMG() == 0 ) return;

while (!breakprog)
{
Evenement();
Mouvement();
}
}

int InitSDL( void )
{
if ( SDL_Init( SDL_INIT_VIDEO ) == -1 )
{
printf("Impossible d'initialiser SDL");
SDL_Quit();
}else{
if ( Screen = SDL_SetVideoMode( 640, 480, 16, SDL_HWSURFACE | SDL_DOUBLEBUF ) ) {
SDL_WM_SetCaption ("By ToToL", NULL);
SDL_EnableKeyRepeat(1,SDL_DEFAULT_REPEAT_INTERVAL);
SDL_FillRect(Screen, NULL, SDL_MapRGB(Screen->format, 0, 0, 255));
return 1;
}else{printf("\nImpossible de charger l'écran");SDL_Quit();}
}
return 0;
}

int Load_IMG( void )
{
img_firen = SDL_LoadBMP( "firen.bmp" );
if (!img_firen) return 0;
return 1;
}

void Evenement ( void )
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_VIDEOEXPOSE:
DrawScreen();
break;
case SDL_KEYDOWN:
keystate = SDL_GetKeyState(NULL);break;
case SDL_KEYUP:
keystate = SDL_GetKeyState(NULL);break;
case SDL_QUIT:
SDL_Quit();
breakprog=1;
return;
}
}
}

void Mouvement( void )
{
if ( keystate[SDLK_UP] ) { Firen->Movey(1); }
return;
}

en fait quand je compile et lance ce code la fenetre SDL s'affiche et s'enleve tout de suite, le programme a quitter. alors que si pour ma fonction Mouvement au lieu de mettre if ( keystate[SDLK_UP] ) { Firen->Movey(1); } je met if ( keystate[SDLK_UP] ) { } et bien la le programme ne quitte pas et je n'arrive pas a savoir d'ou sa vient.

vous n'auriez pas une idée ?

Merci

Thomas

3 réponses

cs_ToToL Messages postés 170 Date d'inscription vendredi 25 avril 2003 Statut Membre Dernière intervention 9 juillet 2008
2 juil. 2004 à 10:59
Personne n'a de solution?
0
cs_Chouchou182 Messages postés 252 Date d'inscription vendredi 13 juin 2003 Statut Membre Dernière intervention 25 avril 2011 1
2 juil. 2004 à 12:08
Salut

J'ai un peu modifié le code et il semble fonctionner.

Tout d'abord j'ai dû changer main en WinMain puisque j'utilise Visual C++.

J'ai ensuite rencontré ton problème (fermeture instantannée du prog) car je n'avais pas d'image firen.bmp mais je pense que ton problème n'était pas si simple.

En effet après avoir créé une bmp, j'ai rencontée une Access Violation vers ton tableau keystate. Ton code semble correspondre avec celui de la documentation mais le méthode que j'utilise a l'avantage de fonctionner : j'utilise une variable qui contient l'état des touches qui m'interressent mais que je renseigne à l'aide de switch plutot qu'avec GetKeyState().

Voici le code modifié (qui ne se ferme pas tout seul).
#include <SDL.h>
#pragma comment (lib, "sdl")
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>

int InitSDL( void );
void Analise( void );
int DrawScreen ( void );
int Load_IMG( void );
void Evenement( void );
void Mouvement( void );

class Sprite {
public:
Sprite(int x, int y);
void Movex ( int xx );
void Movey ( int yy );
void img_select( int img );
SDL_Rect pos,select;
};

Sprite::Sprite(int x, int y )
{
pos.x = x;
pos.y = y;
pos.w = 38;
pos.h = 67;
select.x = 0;
select.y = 0;
select.w = 38;
select.h = 67;
}

void Sprite::Movex ( int xx )
{
if ( pos.x > 2 && pos.x < 630 ) { pos.x = pos.x + xx;}
}

void Sprite::Movey ( int yy )
{
if ( pos.y > 2 && pos.y < 470 ) { pos.y = pos.y + yy;}
}

void Sprite::img_select ( int img )
{if ( img 1 ) { select.x 0; select.y = 0; }if ( img 2 ) { select.x 38; select.y = 0; }if ( img 3 ) { select.x 76; select.y = 0; }if ( img 4 ) { select.x 114; select.y = 0; }if ( img 5 ) { select.x 0; select.y = 67; }if ( img 6 ) { select.x 38; select.y = 67; }if ( img 7 ) { select.x 76; select.y = 67; }if ( img 8 ) { select.x 114; select.y = 67; }
}

// Globals
Sprite * Firen = NULL;

SDL_Surface * Screen,*img_firen;
int breakprog =0;
bool bUp = false ;

// WinMain
int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, char* szCmdLine, int nShowCmd)
{
Firen = new Sprite(100,100);
InitSDL();
Analise();
delete Firen;
return 0;
}

inline int DrawScreen ( void )
{
return !SDL_Flip( Screen );
}

void Analise( void )
{
if ( DrawScreen() == 0) return;
if ( Load_IMG() == 0 ) return;

while (!breakprog)
{
Evenement();
Mouvement();
}
}

int InitSDL( void )
{
if ( SDL_Init( SDL_INIT_VIDEO ) == -1 )
{
printf("Impossible d'initialiser SDL");
SDL_Quit();
}
else
{
if ( Screen = SDL_SetVideoMode( 640, 480, 16, SDL_HWSURFACE | SDL_DOUBLEBUF ) ) {
SDL_WM_SetCaption ("By ToToL", NULL);
SDL_FillRect(Screen, NULL, SDL_MapRGB(Screen->format, 0, 0, 255));
return 1;
}
else
{
printf("\nImpossible de charger l'écran");
SDL_Quit();
}
}
return 0;
}

int Load_IMG( void )
{
img_firen = SDL_LoadBMP( "firen.bmp" );
if (!img_firen) return 0;
return 1;
}

void Evenement ( void )
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_VIDEOEXPOSE:
DrawScreen();
break;

case SDL_KEYDOWN:
switch ( event.key.keysym.sym )
{
case SDLK_UP:
bUp = true ;
break ;
}
break ;

case SDL_KEYUP:
switch ( event.key.keysym.sym )
{
case SDLK_UP:
bUp = false ;
break ;
}
break ;

case SDL_QUIT:
SDL_Quit();
breakprog=1;
return;
}
}
}

void Mouvement( void )
{
if ( bUp )
{
Firen->Movey(1);
}
return;
}


J'espère que cela résout ton problème

Bonne continuation

Chouchou
0
cs_ToToL Messages postés 170 Date d'inscription vendredi 25 avril 2003 Statut Membre Dernière intervention 9 juillet 2008
2 juil. 2004 à 19:18
deja j'ai plusieurs question ...
1) moi aussi je suis sous VC++ et je ne voit pas pourquoi il faut utiliser winmain car winmain c pour les application win32 et main pour les fenetre ms dos mais la fenetre sdl est indépendante de cela
2)sa m'étonne qu'il ne faut pas utiliser getkeystate car sur un otre programme sa marchait .... bizar

@+ je teste le code ce soir
0
Rejoignez-nous