PDA

Voir la version complète : Segment Fault


tupapau
28/09/2005, 17h45
Je reposte ce code venant du forum OpenGL. Il produit un segment fault et je ne sais pas pourquoi?

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


/* Définition des types */
typedef struct {

SDL_Surface * surface;
SDL_Rect rect;
int x;
int y;
int width;
int height;

} sprite;

typedef struct {

Uint32 red;
Uint32 green;
Uint32 blue;
Uint32 alpha;
SDL_PixelFormat * fmt;

} RGBA_color;


/* Fonction [ create_sprite ]
* Créer un sprite de longueur
* width , de hauteur height
* et de couleur color
*/

sprite * create_sprite( int width, int height, RGBA_color color ){

sprite * object = malloc(sizeof(sprite));

object->surface = SDL_CreateRGBSurface(SDL_SWSURFACE , width, height, color.fmt->BytesPerPixel, color.red, color.green, color.blue, color.alpha);
SDL_FillRect( object->surface, NULL, SDL_MapRGB( color.fmt, color.red, color.green, color.blue ));

object->width = width;
object->height = height;
object->rect.w = width;
object->rect.h = height;

return object;
}


/* Fonction [ pos_sprite ]
* Position un sprite définit
* par create_sprite.
*/
int pos_sprite( sprite * object, int x, int y ){

object->rect.x = x;
object->rect.y = y;

return 0;
}


void player_move( SDL_Surface * screen, SDL_Surface * player, SDL_Rect * rect, int speed, SDL_Event event ){

switch(event.key.keysym.sym){

case SDLK_UP:{
if( rect->y-speed <= 0 )
rect->y = 0;

else
rect->y -= speed;
break;
}

case SDLK_DOWN:{

if( rect->y+speed >= (screen->h)-(player->h) )
rect->y = (screen->h)-(player->h);

else
rect->y += speed;
break;
}

default:break;

}

return;
}



int main( void ){

/* Declaration des pointeurs et variables */
SDL_Surface *screen = NULL;
SDL_Event event;

/* Couleur blanche */
RGBA_color color;
color.red = 255;
color.green = 255;
color.blue = 255;
color.alpha = 0;

/* Sprite */
sprite * playerA = NULL;
sprite * playerB = NULL;
sprite * filet = NULL;
sprite * balle = NULL;

int speed = 10;

/* Initialisation de SDL */
if( SDL_Init( SDL_INIT_AUDIO | SDL_INIT_VIDEO ) < 0 ) {
printf( "L'initialisation de SDL à échoué: %s\n", SDL_GetError() );
exit(EXIT_FAILURE);
}
/* Appeler la fonction SDL_Quit en cas de fermeture du programme par exit() ou return; */
atexit(SDL_Quit);

/* Mode vidéo */
screen = SDL_SetVideoMode( 640, 480, 16, SDL_SWSURFACE | SDL_DOUBLEBUF );
if( screen == NULL ){
printf( "Mode vidéo 640x480 en 16bpp refusé: %s\n", SDL_GetError() );
exit(EXIT_FAILURE);
}

SDL_WM_SetCaption( "Pong -- Next Language .NET" , NULL );

color.fmt = screen->format;
playerA = create_sprite( 15, 100, color );
playerB = create_sprite( 15, 100, color );
filet = create_sprite( 1, screen->h, color );
balle = create_sprite( 15, 15, color );

pos_sprite( playerA, ((screen->w/2)-(playerB->width/2))-(screen->w)/2.3, (screen->h/2)-(playerB->height/2));
pos_sprite( playerB, ((screen->w/2)-(playerB->width/2))+(screen->w)/2.3, (screen->h/2)-(playerB->height/2));
pos_sprite( filet, (screen->w/2)-(filet->width/2), 0);
pos_sprite( balle, ((screen->w/2)-(balle->width/2))+(screen->w)/2.5, (screen->h/2)-(balle->height/2));

/*int MouseY;*/

while( SDL_PollEvent( &event ) >= 0 ){

int Last_MouseY;
Last_MouseY = event.motion.y;

switch ( event.type ) {

/*
case SDL_MOUSEMOTION:{
MouseY = event.motion.y;

if( MouseY-decal < 0 )
rect.y = 0;

else if( MouseY > (screen->h)-50 )
rect.y = (screen->h)-100;

else
rect.y = MouseY-decal;

break;
}*/

case SDL_KEYDOWN:
player_move( screen, playerA->surface, &playerA->rect, speed, event );
break;

case SDL_QUIT:
exit(0);

}

SDL_FillRect(screen, 0, 0);
SDL_BlitSurface(playerA->surface, NULL,screen, &(playerA->rect));
SDL_BlitSurface(playerB->surface,NULL,screen,&(playerB->rect));
SDL_BlitSurface(filet->surface, NULL, screen, &(filet->rect));
SDL_BlitSurface(balle->surface, NULL, screen, &(balle->rect));
SDL_Flip(screen);
}

/* Nettoyer */
SDL_FreeSurface(playerA->surface);
SDL_FreeSurface(playerB->surface);
SDL_FreeSurface(filet->surface);
SDL_FreeSurface(balle->surface);
free(playerA);
free(playerB);
free(filet);
free(balle);

/* Retourne EXIT_SUCCESS */
return EXIT_SUCCESS;
}

compiler avec: gcc -W -Wall -O2 -o pong main.c `sdl-config --cflags --libs` -pedantic

et le débuggage
gdb pong
>run

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread -1213233920 (LWP 9051)]
0xb7f5c199 in SDL_ApplyGamma () from /usr/lib/libSDL-1.2.so.0

>frame 4
#4 0x08048ee3 in main () at main.c:179
179 SDL_BlitSurface(playerA->surface, NULL,screen, &(playerA->rect));

Merci d'avance :)

Seby
28/09/2005, 18h08
J'ai rapidement essayé d'executer ton prog.

J'ai pas trop le temps de voir mais en faisant ca :

//object->surface = SDL_CreateRGBSurface(SDL_SWSURFACE , width, height, color.fmt->BytesPerPixel, color.red, color.green, color.blue, color.alpha);
object->surface = SDL_LoadBMP( "Media/guy.bmp" ) ;
SDL_FillRect( object->surface, NULL, SDL_MapRGB( color.fmt, color.red, color.green, color.blue ));donc en remplacant la création de la surface, je n'ai plus de plantage, donc je pense que c'est par la que tu dois chercher...

tupapau
28/09/2005, 18h31
merci pour ton aide c'était le quatrieme argument qui bloqué