From bdedea6d43bd8656548fff341093146a0e131d32 Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Sun, 26 Oct 2014 18:36:55 +0000 Subject: fix segmentation fault issued at startup we used to try to open an image whose filename is wrong. Since we do not check the return of this function, the program crashes. Signed-off-by: Olivier Gayot --- main.c | 58 +++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 21 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index fa98bc4..8cbc3dc 100644 --- a/main.c +++ b/main.c @@ -45,7 +45,12 @@ int main (int argc, char *argv[]) TTF_Init(); SDL_WM_SetCaption ("WARCRAFT BATTLE",NULL); SDL_ShowCursor(SDL_DISABLE); - Fchargerimages (&surfaces); + if (Fchargerimages (&surfaces) < 0) { + TTF_Quit(); + SDL_Quit(); + return -1; + } + Finitialiserpositions (&positions,&surfaces); Fmenuprincipal (&surfaces, &positions); @@ -156,31 +161,40 @@ void Finitialiserpositions (POSITIONS *positions, SURFACES *surfaces) } } -void Fchargerimages (SURFACES *surfaces) +int Fchargerimages (SURFACES *surfaces) { +#define LOAD_IMAGE(_name, _path) \ + if ((surfaces->_name = IMG_Load(_path)) == NULL) { \ + fprintf(stderr, "Impossible d'ouvrir " _path); \ + return -1; \ + } + int i; - surfaces->Pgobelin = IMG_Load ("images/gobelin.bmp"); - surfaces->Ppaladin = IMG_Load ("images/paladin.bmp"); - surfaces->Ppretre = IMG_Load ("images/prêtre.bmp"); - surfaces->Pvoleur = IMG_Load ("images/voleur.bmp"); - surfaces->Pmenujouer = IMG_Load ("images/menu_principal/jouer.bmp"); - surfaces->Pmenuoptions = IMG_Load ("images/menu_principal/options.bmp"); - surfaces->Pmenuquitter = IMG_Load ("images/menu_principal/quitter.bmp"); - surfaces->Pchoixvoleur = IMG_Load ("images/menu_choix_classe/voleur.bmp"); - surfaces->Pchoixpretre = IMG_Load ("images/menu_choix_classe/pretre.bmp"); - surfaces->Pchoixpaladin = IMG_Load ("images/menu_choix_classe/paladin.bmp"); - surfaces->Pcurseurennemis = IMG_Load ("images/jeu/curseur.bmp"); - surfaces->Pcurseurallies = IMG_Load ("images/jeu/curseurpersos.bmp"); - surfaces->Pcadrecible = IMG_Load ("images/jeu/cadre_cible.bmp"); - surfaces->Pactive = IMG_Load ("images/jeu/active.bmp"); - surfaces->Pdesactive = IMG_Load ("images/jeu/desactive.bmp"); - surfaces->Pcadreactions = IMG_Load ("images/jeu/cadre_actions.bmp"); - surfaces->Pactionselectionnee=IMG_Load ("images/jeu/cadre_actions_select.bmp"); - surfaces->Pactiondesactivee=IMG_Load("images/jeu/cadre_actions_unactive.bmp"); - surfaces->Pmort=IMG_Load("images/jeu/mort.bmp"); + + LOAD_IMAGE(Pgobelin, "images/gobelin.bmp"); + LOAD_IMAGE(Ppaladin, "images/paladin.bmp"); + LOAD_IMAGE(Ppretre, "images/pretre.bmp"); + LOAD_IMAGE(Pvoleur, "images/voleur.bmp"); + LOAD_IMAGE(Pmenujouer, "images/menu_principal/jouer.bmp"); + LOAD_IMAGE(Pmenuoptions, "images/menu_principal/options.bmp"); + LOAD_IMAGE(Pmenuquitter, "images/menu_principal/quitter.bmp"); + LOAD_IMAGE(Pchoixvoleur, "images/menu_choix_classe/voleur.bmp"); + LOAD_IMAGE(Pchoixpretre, "images/menu_choix_classe/pretre.bmp"); + LOAD_IMAGE(Pchoixpaladin, "images/menu_choix_classe/paladin.bmp"); + LOAD_IMAGE(Pcurseurennemis, "images/jeu/curseur.bmp"); + LOAD_IMAGE(Pcurseurallies, "images/jeu/curseurpersos.bmp"); + LOAD_IMAGE(Pcadrecible, "images/jeu/cadre_cible.bmp"); + LOAD_IMAGE(Pactive, "images/jeu/active.bmp"); + LOAD_IMAGE(Pdesactive, "images/jeu/desactive.bmp"); + LOAD_IMAGE(Pcadreactions, "images/jeu/cadre_actions.bmp"); + LOAD_IMAGE(Pactionselectionnee, "images/jeu/cadre_actions_select.bmp"); + LOAD_IMAGE(Pactiondesactivee, "images/jeu/cadre_actions_unactive.bmp"); + LOAD_IMAGE(Pmort, "images/jeu/mort.bmp"); for (i = 0;i < 5;i++) surfaces->Tennemi[i] = surfaces->Pgobelin; +#undef LOAD_IMAGE + //définition de la transparence alpha dans les surfaces SDL_SetColorKey(surfaces->Pgobelin,SDL_SRCCOLORKEY,SDL_MapRGB(surfaces->Pecran->format,0,255,18)); SDL_SetColorKey(surfaces->Ppretre,SDL_SRCCOLORKEY,SDL_MapRGB(surfaces->Pecran->format,39,189,31)); @@ -191,6 +205,8 @@ void Fchargerimages (SURFACES *surfaces) SDL_SetColorKey(surfaces->Pactive,SDL_SRCCOLORKEY,SDL_MapRGB(surfaces->Pecran->format,255,255,255)); SDL_SetColorKey(surfaces->Pdesactive,SDL_SRCCOLORKEY,SDL_MapRGB(surfaces->Pecran->format,255,255,255)); SDL_SetColorKey(surfaces->Pmort,SDL_SRCCOLORKEY,SDL_MapRGB(surfaces->Pecran->format,255,255,255)); + + return 0; } void Fdechargerimages (SURFACES *surfaces)//dechargement des surfaces alouées -- cgit v1.2.3