From fc53b2f6fd06dbcd4805ec58a3865cb8b5f60a76 Mon Sep 17 00:00:00 2001
From: suck5u <suck5u@etu.univ-lorraine.fr>
Date: Mon, 28 Dec 2020 22:29:38 +0100
Subject: [PATCH] =?UTF-8?q?Impl=C3=A9mentation=20fonctionnel=20de=20la=20s?=
 =?UTF-8?q?auvegarde=20(charger=20et=20sauvegarder)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 code/constant.h         | 15 ++++++++++++
 code/file_management.c  |  1 +
 code/main.c             | 13 +----------
 code/round_management.c | 17 +++-----------
 code/round_management.h |  1 -
 code/world_data.c       | 51 ++++++++++++++++++++++++++++++++++++-----
 code/world_data.h       |  7 +++++-
 data/save.txt           |  8 +++----
 8 files changed, 75 insertions(+), 38 deletions(-)

diff --git a/code/constant.h b/code/constant.h
index d539ef6..4d29b91 100644
--- a/code/constant.h
+++ b/code/constant.h
@@ -29,6 +29,21 @@
 //Largeur image : 39
 #define SOLDIER_WIDTH 78 
 
+/**
+ * \brief Point de vie par défaut du joueur
+ */
+#define SOLDIER_HP 10
+
+/**
+ * \brief Valeur de l'inrémentation des ennemis à chauqe manche
+ */
+#define INCREMENTATION_ENEMIES 3
+
+/**
+ * \brief Nombre d'ennemis à la première manche
+ */
+#define NUMBER_ENEMIES_START 5
+
 /**
  * \brief Hauteur de l'ennemie
  */
diff --git a/code/file_management.c b/code/file_management.c
index 6d83ba4..18770b7 100644
--- a/code/file_management.c
+++ b/code/file_management.c
@@ -16,6 +16,7 @@ int* getDataSave(const char *name)
     //Deplacement : nombre de caractere du commentaire +2 (Sauf premier +1)
     int deplacement[4] = {24,28,43,19};
     int *data = malloc(sizeof(int));
+    //int data[4];
     for(int i = 0; i < nb_lines/2; i++)
     {
         fseek(file, deplacement[i],SEEK_CUR);
diff --git a/code/main.c b/code/main.c
index 632e07c..6e93021 100644
--- a/code/main.c
+++ b/code/main.c
@@ -18,7 +18,6 @@ void init(SDL_Window **window, SDL_Renderer ** renderer, resources_t *resources,
 int main(void)
 {
     SDL_Event event; // Événements liés à la fenêtre
-    //bool terminer = false;
     resources_t resources;
     world_t world;
     SDL_Window *window;  // Déclaration de la fenêtre
@@ -26,7 +25,6 @@ int main(void)
     srand(time(NULL));
     double time;
 
-
     // Initialisation de la SDL
     if(SDL_Init(SDL_INIT_VIDEO) < 0) 
     {
@@ -48,18 +46,9 @@ int main(void)
     
     renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);;
     
+    //Initialiser les données du monde et les ressources d'affichage
     init(&window,&renderer,&resources,&world);
     
-    /*int* dataSave;
-    dataSave = getDataSave("../data/save.txt");
-    for(int i = 0; i < 4; i++)
-    {
-        dataSave[i] += 5;
-    }
-    setDataSave("../data/save.txt",dataSave);*/
-    
-    //load_save();
-    
     //Timer
     Uint64 t1 = SDL_GetPerformanceCounter();
     Uint64 t2 = 0;
diff --git a/code/round_management.c b/code/round_management.c
index c04d7f7..be2e185 100644
--- a/code/round_management.c
+++ b/code/round_management.c
@@ -2,18 +2,6 @@
 #include <stdlib.h>
 #include <stdio.h>
 
-/*void load_save()
-{
-    int* dataSave;
-    dataSave = getDataSave("../data/save.txt");
-}
-
-void save_game()
-{
-    
-}*/
-
-
 int isRoundEnded(round_t *round)
 {
 	for (int i = 0; i < round->number_enemies; ++i)
@@ -29,13 +17,14 @@ int isRoundEnded(round_t *round)
 
 void initFirstRound(round_t *round, int nbEnemies, int incrementation)
 {
-	round->round_number = 0;
+	round->round_number = 1;
 	round->number_enemies = nbEnemies;
 	round->incrementation = incrementation;
 	round->counter = 0;
 	round->index_to_spawn = 0;
 	round->is_new_round = 0;
 	createEnemies(round);
+	printf("Ennemis : %d Incrementation : %d\n",round->number_enemies,round->incrementation);
 }
 
 //Actualiser qui se déplace
@@ -47,7 +36,7 @@ void updateRound(round_t *round)
 		round->index_to_spawn++;
 	}
 	round->counter++;
-	if(round->counter == 500)
+	if(round->counter == 50)
 	{
 		round->counter = 0;
 	}
diff --git a/code/round_management.h b/code/round_management.h
index 2c7766b..ae748b8 100644
--- a/code/round_management.h
+++ b/code/round_management.h
@@ -18,7 +18,6 @@ struct round_s
 */
 typedef struct round_s round_t;
 
-
 int isRoundEnded(round_t *round);
 
 void initFirstRound(round_t *round, int nbEnemies, int incrementation);
diff --git a/code/world_data.c b/code/world_data.c
index 67a0687..acba8a1 100644
--- a/code/world_data.c
+++ b/code/world_data.c
@@ -19,23 +19,61 @@ void print_sprite(sprite_t*sprite)
     printf("HP: %d \n",sprite->hp);      
 }
 
+int* load_save()
+{
+  //Récupérer les données de la sauvegarde
+  int* dataSave;
+  dataSave = getDataSave("../data/save.txt");
+
+  int *dataToAdd = malloc(sizeof(int));
+  //Initialiser les valeurs par défaut
+  dataToAdd[0] = SOLDIER_HP;
+  dataToAdd[1] = NUMBER_ENEMIES_START;
+  dataToAdd[2] = INCREMENTATION_ENEMIES;
+
+  //PV du joueur
+  if(dataSave[0] > 0 && dataSave[0] < 31)
+  {
+    dataToAdd[0] = dataSave[0];
+  }
+
+  //Nombre d'ennemis
+  if(dataSave[1] > 0 && dataSave[1] < 21)
+  {
+    dataToAdd[1] = dataSave[1];
+  }
+
+  //Incrémentation du nombre d'ennemis
+  if(dataSave[2] > 0 && dataSave[2] < 11)
+  {
+    dataToAdd[2] = dataSave[2];
+  }
+  return dataToAdd;
+}
+
+void save_game(world_t *world)
+{
+  int dataSave[4];
+  dataSave[0] = world->soldier.sprite.hp;
+  dataSave[1] = world->rounds.number_enemies;
+  dataSave[2] = world->rounds.incrementation;
+  dataSave[3] = 30;
+  setDataSave("../data/save.txt",dataSave);
+}
 
 void init_data(world_t *world)
 {
   
   //Chargement de la sauvegarde
-  int incrementation = 1;
-  int nbEnemis = 4;
+  int* data = load_save();
 
   //Chargement de la première manche
-  initFirstRound(&world->rounds,nbEnemis,incrementation);
+  initFirstRound(&world->rounds,data[1],data[2]);
 
   //Initialisation du soldat
-  init_soldier(&world->soldier,10);
+  init_soldier(&world->soldier,data[0]);
   setBulletD(&world->soldier);
   
-  //init_enemy(&world->enemy);
-
   init_supplying(&world->supplying,1);
   
   //world->DestBullet = malloc(sizeof(SDL_Rect)*NB_BULLET);
@@ -172,6 +210,7 @@ void handle_events(SDL_Event *event,world_t *world)
     if( event->type == SDL_QUIT || event->key.keysym.sym == SDLK_ESCAPE)
     {
       //On indique la fin du jeu
+      save_game(world);
       //Libère la mémoire du tableau DestBullet pour l'affichage
       free(world->soldier.DestBullet);
       clearEnemies(&world->rounds);
diff --git a/code/world_data.h b/code/world_data.h
index ea980c1..71d503e 100644
--- a/code/world_data.h
+++ b/code/world_data.h
@@ -2,8 +2,9 @@
 #define __WORLDDATA_H__
 
 #include <time.h>
-#include "constant.h"
 #include "round_management.h"
+#include "file_management.h"
+#include "constant.h"
 #include "powerUp.h"
 #include <SDL2/SDL.h>
 #include "math.h" //Nécessaire pour la collision entre deux sprites
@@ -27,6 +28,10 @@ typedef struct world_s world_t;
 
 void init_data(world_t * world);
 
+int* load_save();
+
+void save_game(world_t *world);
+
 void handle_events(SDL_Event *event,world_t *world);
 
 void refresh_data(world_t *world);
diff --git a/data/save.txt b/data/save.txt
index 8acea06..b6216b9 100644
--- a/data/save.txt
+++ b/data/save.txt
@@ -1,8 +1,8 @@
 /Vie du joueur au début
-275
+10
 /Nombre d'ennemis au début
-305
+15
 /Nombre d'ennemis en plus à chaque manche
-267
+5
 /Nombre de balles
-295
+30
-- 
GitLab