From e3194bfce10d20e6b08043d7d45a6947559d6df0 Mon Sep 17 00:00:00 2001
From: Henri Montjoie <henri.montjoie@insa-rennes.fr>
Date: Wed, 11 May 2016 20:23:46 +0200
Subject: [PATCH] appel des fonctions de liberation de l'espace memoire

---
 src/affichage.c | 143 ++++++++++++++++++++++++++----------------------
 src/affichage.h |   1 +
 2 files changed, 78 insertions(+), 66 deletions(-)

diff --git a/src/affichage.c b/src/affichage.c
index 70560e6..fe339ff 100644
--- a/src/affichage.c
+++ b/src/affichage.c
@@ -24,15 +24,17 @@ char* nom_2;
  */
 int main_affichage()
 {
+    nom_1="Joueur 1";
+    nom_2="Joueur 2";
     etat=LANCEMENT;/*lance le jeu direct à l'ecran de lancement, sans rentrer les noms des joueurs*/
     //etat=SAISIE_JOUEURS; /*lance le jeu à l'ecran "entrer nom joueur 1"*/
 
-    if (SDL_Init(SDL_INIT_VIDEO)<0)
+    if (SDL_Init(SDL_INIT_VIDEO)<0) /*initialisation de la SDL*/
     {
         printf("Unable to init SDL: %s\n", SDL_GetError());
         exit(EXIT_FAILURE);
     }
-    if(TTF_Init()==-1)
+    if(TTF_Init()==-1)  /*initialisation de SDL_ttf (affichage de texte)*/
     {
         fprintf(stderr, "Erreur d'initialisation de TTF_Init : %s\n", TTF_GetError());
         exit(EXIT_FAILURE);
@@ -59,6 +61,7 @@ int main_affichage()
 
         }
     }
+    TTF_Quit();
     SDL_Quit();
     return 0;
 }
@@ -68,8 +71,8 @@ int main_affichage()
  * \fn char* afficherListeClasses(char** nomsClasses, char** skinsClasses, int nbChoix, TTF_Font* police,SDL_Color couleurTexte)
  * \brief Fonction permettant le choix de la classe d'un personnage par le joueur
  *
- * \param le tableau contenant les noms des classes, le tableau contenant les adresse de leurs images, le nombre de classes, et la police et la couleur dans lesquelles écrire les noms
- * \return le nom de la classe choisie
+ * \param le tableau contenant les noms des classes, le tableau contenant les adresses de leurs images, le nombre de classes, et la police et la couleur dans lesquelles écrire les noms
+ * \return le nom de la classe choisie (ou "fermer" si l'utilisateur ferme la fenetre)
  */
 char* afficherListeClasses(char** nomsClasses, char** skinsClasses, int nbChoix, TTF_Font* police,SDL_Color couleurTexte)
 {
@@ -131,6 +134,11 @@ char* afficherListeClasses(char** nomsClasses, char** skinsClasses, int nbChoix,
         }
         SDL_Flip(fenetre);
     }
+    for(i=0;i<nbChoix;i++)
+    {
+        SDL_FreeSurface(tabTextes[i]);
+        SDL_FreeSurface(tabSkins[i]);
+    }
     SDL_FreeSurface(fenetre);
     return classeChoisie;
 }
@@ -208,6 +216,10 @@ int saisie_noms_joueurs()
     nom_1=nom_joueur1;
     nom_2=nom_joueur2;
 
+    TTF_CloseFont(police);
+    SDL_FreeSurface(ecran);
+    SDL_FreeSurface(logo);
+    SDL_FreeSurface(texte);
     return 0;
 }
 
@@ -218,26 +230,7 @@ int saisie_noms_joueurs()
  */
 int affichage_ecran_accueil()
 {
-    if(etat!=LANCEMENT)
-    {
-        etat=LANCEMENT;
-        nom_1="Joueur 1";
-        nom_2="Joueur 2";
-        ajouterUnJoueur(nom_1,&etat);
-        ajouterUnJoueur(nom_2,&etat);
-    }
-
-    if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
-    {
-        printf( "Unable to init SDL: %s\n", SDL_GetError() );
-        exit(EXIT_FAILURE);
-    }
-    if(TTF_Init() == -1)
-    {
-        fprintf(stderr, "Erreur d'initialisation de TTF_Init : %s\n", TTF_GetError());
-        exit(EXIT_FAILURE);
-    }
-
+    etat=LANCEMENT;
     SDL_Surface* ecran = SDL_SetVideoMode(LARGEUR_CARTE*(LARGEUR_CASE+1), HAUTEUR_CARTE*(HAUTEUR_CASE+2), 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
     SDL_WM_SetCaption("Elder Internal Ignition",NULL);
     SDL_Event event;
@@ -290,6 +283,11 @@ int affichage_ecran_accueil()
         SDL_BlitSurface(rectangle,NULL,ecran,&pos_texte);
         SDL_Flip(ecran);
     }
+    TTF_CloseFont(police);
+    SDL_FreeSurface(ecran);
+    SDL_FreeSurface(logo);
+    SDL_FreeSurface(rectangle);
+    SDL_FreeSurface(texte);
     return 0;
 }
 
@@ -300,25 +298,30 @@ int affichage_ecran_accueil()
  */
 int affichage_menu_configuration()
 {
-    if(etat!=CONFIGURATION)
-    {
-        etat=CONFIGURATION;
-    }
+    etat=CONFIGURATION;
     SDL_Surface* ecran = SDL_SetVideoMode(LARGEUR_CARTE*(LARGEUR_CASE+1), HAUTEUR_CARTE*(HAUTEUR_CASE+2), 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
     SDL_WM_SetCaption("Elder Internal Ignition",NULL);
     SDL_Event event;
+    SDL_Surface* map_choisie = NULL;
     SDL_Surface* texte = NULL;
     SDL_Surface* texte2 = NULL;
     SDL_Surface* texte3 = NULL;
-    TTF_Font *police = NULL;
-    TTF_Font *petite_police = NULL;
+    SDL_Surface* previsualisation[5]={NULL};
+    SDL_Surface* textes[NB_JOUEURS*NB_PERSO]={NULL};
+    SDL_Surface* images_classes[nbClasses()];
+    SDL_Surface* images_persos[NB_PERSO][NB_JOUEURS]={{NULL}};
+    SDL_Rect positions_images[NB_PERSO][NB_JOUEURS];
+    SDL_Rect posTextes[NB_JOUEURS*NB_PERSO];
     SDL_Rect pos_texte;
     SDL_Rect pos_texte2;
     SDL_Rect pos_texte3;
     SDL_Rect pos_texte3_bis;
+    SDL_Rect position_carte[5];
+    TTF_Font *police = NULL;
+    TTF_Font *petite_police = NULL;
     SDL_Color couleurNoire = {0, 0, 0};
     SDL_Color couleurBlanche = {255, 255, 255};
-    int i;
+    int i,j,p,k,c;
 
     SDL_FillRect(ecran,NULL,SDL_MapRGB(ecran->format,0,0,0));
 
@@ -342,50 +345,40 @@ int affichage_menu_configuration()
     SDL_BlitSurface(texte3,NULL,ecran,&pos_texte3);
     SDL_BlitSurface(texte3,NULL,ecran,&pos_texte3_bis);
 
-    SDL_Surface textes[6];
-    SDL_Rect posTextes[6];
     police=TTF_OpenFont("../resources/fonts/arial.ttf",26);
 
     char src[50], dest[50];
-
     char buffer[3];
     for(i=0;i<3;i++)
     {
         strcpy(dest,  "personnage ");
         strcpy(src, itoa(i+1,buffer,10));
-        textes[i]= *TTF_RenderText_Shaded(police, strcat(dest, src), couleurBlanche,couleurNoire);
+        textes[i]= TTF_RenderText_Shaded(police, strcat(dest, src), couleurBlanche,couleurNoire);
     }
-
     for(i=3;i<6;i++)
     {
         strcpy(dest,  "personnage ");
         strcpy(src, itoa(i-2,buffer,10));
-        textes[i]= *TTF_RenderText_Shaded(police, strcat(dest, src), couleurBlanche,couleurNoire);
+        textes[i]= TTF_RenderText_Shaded(police, strcat(dest, src), couleurBlanche,couleurNoire);
     }
-
     for(i=0;i<3;i++)
     {
-        posTextes[i].x=ecran->w/4-textes[i].w/2;
+        posTextes[i].x=ecran->w/4-textes[i]->w/2;
         posTextes[i].y=(i+1)*ecran->h/6;
-        SDL_BlitSurface(&textes[i],NULL,ecran,&(posTextes[i]));
+        SDL_BlitSurface(textes[i],NULL,ecran,&(posTextes[i]));
     }
-
     for(i=3;i<6;i++)
     {
-        posTextes[i].x=(3*ecran->w)/4-textes[i].w/2;
+        posTextes[i].x=(3*ecran->w)/4-textes[i]->w/2;
         posTextes[i].y=posTextes[i-3].y;
-        SDL_BlitSurface(&textes[i],NULL,ecran,&posTextes[i]);
+        SDL_BlitSurface(textes[i],NULL,ecran,&posTextes[i]);
     }
     SDL_Flip(ecran);
 
     char** path_images=obtenirSkinsClasses();
-    SDL_Surface* images_classes[nbClasses()];
-    SDL_Surface* images_persos[3][2]={{NULL}};
-    SDL_Rect positions_images[3][2];
     int numClassePerso[3][2]={{-1,-1},{-1,-1},{-1,-1}};
-    int j,p,k;
-    j=0;
-    p=0;
+    j=0; /*numero du joeuur*/
+    p=0; /*numero du personnage*/
     char *classeChoisie;
     for(i=0;i<nbClasses();i++)
     {
@@ -398,7 +391,7 @@ int affichage_menu_configuration()
         SDL_SetColorKey(images_classes[i], SDL_SRCCOLORKEY, SDL_MapRGB(images_classes[i]->format, 0, 0, 255));
     }
 
-    SDL_Surface* map_choisie=SDL_LoadBMP("../resources/Skins/Map_select.bmp");
+    map_choisie=SDL_LoadBMP("../resources/Skins/Map_select.bmp");
     if (!map_choisie)
         {
             printf("Unable to load bitmap: %s\n", SDL_GetError());
@@ -406,17 +399,13 @@ int affichage_menu_configuration()
         }
     SDL_SetColorKey(map_choisie, SDL_SRCCOLORKEY, SDL_MapRGB(map_choisie->format, 0, 0, 255));
 
-    SDL_Surface* previsualisation[5]={NULL};
-    SDL_Rect position_carte[5];
     char* path[5];
-    path[0]="../resources/Skins/Map_1.bmp";
+    path[0]="../resources/Skins/Map_1.bmp";        //a revoir : cela peut se trouver dans le XML
     path[1]="../resources/Skins/Map_2.bmp";
     path[2]="../resources/Skins/Map_3.bmp";
     path[3]="../resources/Skins/Map_4.bmp";
     path[4]="../resources/Skins/autre_map.bmp";
 
-    int c=-1;
-
     for(i=0;i<5;i++)
     {
         previsualisation[i]=SDL_LoadBMP(path[i]);
@@ -429,6 +418,7 @@ int affichage_menu_configuration()
         position_carte[i].x=(ecran->w-previsualisation[i]->w)/2+(10+previsualisation[i]->w)*(i-2);
     }
 
+    c=-1; /*le numéro de la carte choisie*/
     while(etat==CONFIGURATION)
     {
         SDL_WaitEvent(&event);
@@ -446,35 +436,36 @@ int affichage_menu_configuration()
                         selectionnerCarte(obtenirNomsCartes()[c]);
                     }
                 }
-                if(event.button.x>posTextes[0].x&&event.button.x<posTextes[0].x+textes[0].w)
+                if(event.button.x>posTextes[0].x&&event.button.x<posTextes[0].x+textes[0]->w)
                     j=1;
-                if(event.button.x>posTextes[3].x&&event.button.x<posTextes[3].x+textes[3].w)
+                if(event.button.x>posTextes[3].x&&event.button.x<posTextes[3].x+textes[3]->w)
                     j=2;
-                for(i=0;i<3;i++){
-                    if(event.button.y>posTextes[i].y&&event.button.y<posTextes[i].y+textes[i].h)
+                for(i=0;i<3;i++)
+                {
+                    if(event.button.y>posTextes[i].y&&event.button.y<posTextes[i].y+textes[i]->h)
                         p=i+1;
                 }
-                if(j!=0&&p!=0){
+                if(j!=0&&p!=0)
+                {
                     classeChoisie=afficherListeClasses(obtenirNomsClasses(), obtenirSkinsClasses(), nbClasses(), police, couleurBlanche);
                     if(classeChoisie!="fermer")
                     {
                         numClassePerso[p-1][j-1]=ajouterPersonnage(classeChoisie, p-1, j);
                     }
                     ecran = SDL_SetVideoMode(LARGEUR_CARTE*(LARGEUR_CASE+1), HAUTEUR_CARTE*(HAUTEUR_CASE+2), 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
-
                 }
                 break;
             default:
                 break;
         }
-
-        for(i=0;i<6;i++){
-            SDL_BlitSurface(&textes[i],NULL,ecran,&(posTextes[i]));
+        for(i=0;i<6;i++)
+        {
+            SDL_BlitSurface(textes[i],NULL,ecran,&(posTextes[i]));
         }
         for(i=0;i<3;i++){
             for(k=0;k<2;k++){
-                positions_images[i][k].y=posTextes[i].y+textes[i].h;
-                positions_images[i][k].x=posTextes[3*k].x+(textes[3*k].w-images_classes[0]->w)/2;
+                positions_images[i][k].y=posTextes[i].y+textes[i]->h;
+                positions_images[i][k].x=posTextes[NB_PERSO*k].x+(textes[NB_PERSO*k]->w-images_classes[0]->w)/2;
                 images_persos[i][k]=images_classes[numClassePerso[i][k]];
                 if(numClassePerso[i][k]!=-1)
                 {
@@ -495,6 +486,26 @@ int affichage_menu_configuration()
         p=0;
         j=0;
     }
+
+    TTF_CloseFont(police);
+    TTF_CloseFont(petite_police);
+    SDL_FreeSurface(ecran);
+    SDL_FreeSurface(map_choisie);
+    SDL_FreeSurface(texte);
+    SDL_FreeSurface(texte2);
+    SDL_FreeSurface(texte3);
+    for(i=0;i<5;i++){
+        SDL_FreeSurface(previsualisation[i]);
+    }
+    for(i=0;i<NB_PERSO;i++){
+        for(k=0;k<NB_JOUEURS;k++){
+            SDL_FreeSurface(textes[i*k]);
+            SDL_FreeSurface(images_persos[i][k]);
+        }
+    }
+    for(i=0;i<nbClasses();i++){
+        SDL_FreeSurface(images_classes[i]);
+    }
     return 0;
 }
 
diff --git a/src/affichage.h b/src/affichage.h
index af59287..3978af6 100644
--- a/src/affichage.h
+++ b/src/affichage.h
@@ -12,6 +12,7 @@
 #define LARGEUR_CASE 53
 #define HAUTEUR_CASE 71
 #define NB_PERSO 3
+#define NB_JOUEURS 3
 #define HAUTEUR_BOUTON 20
 #define LARGEUR_BOUTON 115
 
-- 
GitLab