Commit 92c24ce7 authored by LibertAntoine's avatar LibertAntoine

Add check allocation malloc

parent 19c16f7a
......@@ -11,7 +11,7 @@ out 0 0 255
construct 0 0 0
# Construction Data (DEGAT ALIM CAD PORTEE RANGE VAL VAL)
laser 1 1 5 2 0 200 100
missile 2 1 1 1 0 300 150
missile 2 1 2 1 0 300 150
radar 0 0 0 1 2 200 100
armement 1 0 0 0 2 400 200
centrale 0 1 0 2 2 400 200
......
......@@ -8,6 +8,10 @@ TypeCase* case_loadFromPPM()
TypeCase* cases = calloc(mapData->Xsplit*mapData->Ysplit, sizeof(int));
RGBcolor* pixel_ppm = malloc(sizeof(RGBcolor));
if (!pixel_ppm) {
printf("ERROR ALLOC : mapData");
exit(EXIT_FAILURE);
}
int nbSortie = 0;
......@@ -40,6 +44,10 @@ TypeCase* case_loadFromPPM()
void case_gameData_init() {
gameData = malloc(sizeof(GameData));
if (!gameData) {
printf("ERROR ALLOC : mapData");
exit(EXIT_FAILURE);
}
gameData->gameState = MAINMENU;
gameData->default_list = NULL;
};
......@@ -48,9 +56,9 @@ void case_initPlateau()
{
plateau = malloc(sizeof(Plateau));
if (!plateau) {
return EXIT_FAILURE;
printf("ERROR ALLOC : mapData");
exit(EXIT_FAILURE);
}
int argent = 2000;
plateau->idListInfos = GL_INVALID_VALUE;
......@@ -63,6 +71,10 @@ void case_initPlateau()
plateau->listProjectiles = projectile_initListProjectiles();
plateau->currentWave = *mapData->listWaves->next;
TypeMonster* tmp = malloc(sizeof(TypeMonster)*mapData->listWaves->next->monster_total);
if (!tmp) {
printf("ERROR ALLOC : mapData");
exit(EXIT_FAILURE);
}
memcpy(tmp , mapData->listWaves->next->monsters, sizeof(TypeMonster)*mapData->listWaves->next->monster_total);
plateau->currentWave.monsters = tmp;
......
......@@ -23,6 +23,10 @@ TextureText* display_loadTextureText(char text[])
TTF_Quit();
TextureText* texture_texte = malloc(sizeof(TextureText));
if (!texture_texte) {
printf("ERROR ALLOC : mapData");
exit(EXIT_FAILURE);
}
texture_texte->texture_id = texture_id;
texture_texte->ratio = texture_surface->w / (float) texture_surface->h;
......
......@@ -3,9 +3,10 @@
void itd_initMapData()
{
mapData = malloc(sizeof(MapData));
if (!mapData) {
printf("ERROR ALLOC : mapData");
} else {
if (!mapData) {
printf("ERROR ALLOC : mapData");
exit(CHK_ERROR_ALLOC);
} else {
mapData->contentState = 0;
return mapData;
}
......@@ -95,7 +96,10 @@ int itd_getInfosNodes(FILE* file)
else {
itd_gotoEndOfLine(file);
Node* nodes = malloc(sizeof(Node)*nbNoeud);
// TODO: Checker l'allocation.
if (!nodes) {
printf("ERROR ALLOC : nodes");
exit(CHK_ERROR_ALLOC);
}
int entrance_total = 0;
int idOut = -1;
for (int i = 0; i < nbNoeud; i++)
......@@ -141,7 +145,10 @@ int itd_getInfosNodes(FILE* file)
return CHK_ERROR_FILE;
}
InfosNodes* infosNodes = malloc(sizeof(InfosNodes));
//TODO: Checker allocation.
if (!infosNodes) {
printf("ERROR ALLOC : infosNodes");
exit(CHK_ERROR_ALLOC);
}
infosNodes->nbNoeud = nbNoeud;
infosNodes->nodes = nodes;
......@@ -206,7 +213,10 @@ int itd_getConstructionData(FILE* file, TypeCase type)
int getIdEntrees()
{
int* idEntrees = malloc(sizeof(int)*mapData->infosNodes->entrance_total);
//TODO: Checker allocation.
if (!idEntrees) {
printf("ERROR ALLOC : idEntrees");
exit(CHK_ERROR_ALLOC);
}
int j = 0;
for (int i = 0; i < mapData->infosNodes->nbNoeud; i++)
......@@ -233,7 +243,10 @@ int itd_getInfosWaves(FILE* file)
} else {
itd_gotoEndOfLine(file);
ListWaves* listWaves = malloc(sizeof(ListWaves));
// TODO: Checker allocation.
if (!listWaves) {
printf("ERROR ALLOC : listWaves");
exit(CHK_ERROR_ALLOC);
}
listWaves->wave_total = wave_total;
listWaves->next = NULL;
......@@ -242,7 +255,8 @@ int itd_getInfosWaves(FILE* file)
Wave* wave = NULL;
wave = malloc(sizeof(Wave));
if (!wave) {
printf("ERROR ALLOC : mapData");
printf("ERROR ALLOC : wave");
exit(CHK_ERROR_ALLOC);
}
int wave_id;
......@@ -263,8 +277,10 @@ int itd_getInfosWaves(FILE* file)
wave->monster_total = nbSolder + nbHugeSolder + nbGeneral + nbBoss;
wave->next = NULL;
TypeMonster* monsters = malloc(sizeof(TypeMonster) * wave->monster_total);
// TODO: Checker allocation.
if (!monsters) {
printf("ERROR ALLOC : monsters");
exit(CHK_ERROR_ALLOC);
}
int i = 0;
for (i ; i < nbSolder ; i++) {
......@@ -339,7 +355,8 @@ int itd_getImageFilePath(FILE* file)
// NOTE: ici le chiffre 6 correspond à la longueure de "level/"
char* filePath = malloc(sizeof(char) * (letterCount + 6));
if (!filePath) {
return CHK_ERROR_ALLOC;
printf("ERROR ALLOC : filePath");
exit(CHK_ERROR_ALLOC);
}
int i;
......@@ -379,6 +396,10 @@ int itd_checkForMapData(FILE* file)
if (fscanf(file, "%s", label)) {
RGBcolor* color = malloc(sizeof(RGBcolor));
if (!color) {
printf("ERROR ALLOC : color");
exit(CHK_ERROR_ALLOC);
}
if (strcmp("carte", label) == 0) {
if (itd_getImageFilePath(file) == CHK_SUCCESS) {
mapData->contentState |= MDATA_IMG;
......
......@@ -12,8 +12,10 @@ int itineraire_findShortestPath()
distances[mapData->infosNodes->idOut] = 0;
Node** previous = malloc(sizeof(Node*) * mapData->infosNodes->nbNoeud);
// TODO: Vérifier l'allocation dynamique
if (!previous) {
printf("ERROR ALLOC : previous");
exit(CHK_ERROR_ALLOC);
}
int i = mapData->infosNodes->idOut;
int distance;
while (i != -1) {
......@@ -50,6 +52,10 @@ int itineraire_findShortestPath()
int *itineraire_initVisitedArray(int size)
{
int *array = malloc(sizeof(int) * size);
if (!array) {
printf("ERROR ALLOC : array");
exit(CHK_ERROR_ALLOC);
}
itineraire_fillArray(array, size, 0);
return array;
......@@ -58,6 +64,10 @@ int *itineraire_initVisitedArray(int size)
int *itineraire_initDistanceArray(int size)
{
int *array = malloc(sizeof(int) * size);
if (!array) {
printf("ERROR ALLOC : array");
exit(CHK_ERROR_ALLOC);
}
int total_cases = mapData->Xsplit * mapData->Ysplit;
itineraire_fillArray(array, size, total_cases);
......@@ -105,6 +115,10 @@ int itineraire_initMonster(Monster* monster)
{
itineraire_findShortestPath();
Itineraire* itineraire = malloc(sizeof(Itineraire));
if (!itineraire) {
printf("ERROR ALLOC : itineraire");
exit(CHK_ERROR_ALLOC);
}
itineraire->next = NULL;
itineraire->nbEtape = 1;
itineraire_addEtape(itineraire, &mapData->infosNodes->nodes[monster->idIn]);
......@@ -120,6 +134,10 @@ int itineraire_initMonster(Monster* monster)
void itineraire_addEtape(Itineraire* itineraire, Node* node) {
Etape* etape = malloc(sizeof(Etape));
if (!etape) {
printf("ERROR ALLOC : etape");
exit(CHK_ERROR_ALLOC);
}
etape->node = node;
etape->next = NULL;
if(itineraire->next == NULL) {
......@@ -137,6 +155,10 @@ void itineraire_addEtape(Itineraire* itineraire, Node* node) {
ListChemins* itineraire_initListChemins()
{
ListChemins* listChemins = malloc(sizeof(listChemins));
if (!listChemins) {
printf("ERROR ALLOC : listChemins");
exit(CHK_ERROR_ALLOC);
}
listChemins->nbChemin = 0;
listChemins->next = NULL;
for(int i = 0; i < mapData->infosNodes->nbNoeud; i++) {
......@@ -175,6 +197,10 @@ void itineraire_addChemin(ListChemins* listChemins, Node* node_in, Node* node_ou
{
itineraire_checkValidChemin(node_in, node_out);
Chemin* new_chemin = malloc(sizeof(Chemin));
if (!new_chemin) {
printf("ERROR ALLOC : new_chemin");
exit(CHK_ERROR_ALLOC);
}
new_chemin->node_in = node_in;
new_chemin->node_out = node_out;
new_chemin->dead_monsters = 0;
......
#include "monster.h"
/*
DataMonsters** monster_initDataMonster()
{
DataMonsters** dataMonsters = malloc(sizeof(DataMonsters*) * 4);
dataMonsters[SOLDER] = malloc(sizeof(DataMonsters));
dataMonsters[HUGE_SOLDER] = malloc(sizeof(DataMonsters));
dataMonsters[GERERAL] = malloc(sizeof(DataMonsters));
dataMonsters[BOSS] = malloc(sizeof(DataMonsters));
dataMonsters[SOLDER]->PDV = 1;
dataMonsters[SOLDER]->strength = 1;
dataMonsters[SOLDER]->mass = 1.0;
dataMonsters[SOLDER]->value = 1.0;
dataMonsters[HUGE_SOLDER]->PDV = 2;
dataMonsters[HUGE_SOLDER]->strength = 2;
dataMonsters[HUGE_SOLDER]->mass = 2.0;
dataMonsters[HUGE_SOLDER]->value = 2.0;
dataMonsters[GERERAL]->PDV = 3;
dataMonsters[GERERAL]->strength = 1;
dataMonsters[GERERAL]->mass = 1.0;
dataMonsters[GERERAL]->value = 1.0;
dataMonsters[BOSS]->PDV = 4;
dataMonsters[BOSS]->strength = 1;
dataMonsters[BOSS]->mass = 1.0;
dataMonsters[BOSS]->value = 1.0;
return dataMonsters;
}
*/
ListMonsters* monster_initListMonster()
{
ListMonsters* listMonsters = malloc(sizeof(ListMonsters));
// TODO: Checker l'allocation
if (!listMonsters) {
printf("ERROR ALLOC : listMonsters");
exit(CHK_ERROR_ALLOC);
}
listMonsters->monster_total = 0;
listMonsters->firstMonster = NULL;
......@@ -85,7 +56,10 @@ void monster_printInfos(Monster *monster)
int monster_popMonster(TypeMonster type, int idIn)
{
Monster* monster = malloc(sizeof(Monster));
// TODO: Checker l'allocation
if (!monster) {
printf("ERROR ALLOC : monster");
exit(CHK_ERROR_ALLOC);
}
if (type == SOLDER) {
monster->sprite_texture = sprite_loadSprite(SOLDER_TEX, 900, TRUE);
......
......@@ -3,6 +3,10 @@
Etat* player_init(int argent)
{
Etat *joueur = malloc(sizeof(Etat));
if (!joueur) {
printf("ERROR ALLOC : joueur");
exit(CHK_ERROR_ALLOC);
}
joueur->argent = argent;
joueur->action = ADD;
......
......@@ -69,8 +69,8 @@ char* ppm_load()
//Read actual image data
unsigned char* pixel_data = malloc(sizeof(unsigned char)*3*width*height);
if (!pixel_data) {
printf("Couldn't initialize array of pixels\n");
return EXIT_FAILURE;
printf("ERROR ALLOC : pixel_data");
exit(CHK_ERROR_ALLOC);
}
ppm_gotoEndOfLine(image);
......
......@@ -3,6 +3,10 @@
ListProjectiles* projectile_initListProjectiles()
{
ListProjectiles* listProjectiles = malloc(sizeof(ListProjectiles));
if (!listProjectiles) {
printf("ERROR ALLOC : listProjectiles");
exit(CHK_ERROR_ALLOC);
}
listProjectiles->nbProjectile = 0;
listProjectiles->next = NULL;
......@@ -11,6 +15,10 @@ ListProjectiles* projectile_initListProjectiles()
int create_projectile(Tour* tour, Monster* monster, float degats) {
Projectile* projectile = malloc(sizeof(Projectile));
if (!projectile) {
printf("ERROR ALLOC : projectile");
exit(CHK_ERROR_ALLOC);
}
projectile->cible = monster;
projectile->x = tour->x;
projectile->y = tour->y;
......
......@@ -7,6 +7,10 @@ void sound_init()
}
gameData->sound = malloc(sizeof(Mix_Music*)*10);
if (!gameData->sound) {
printf("ERROR ALLOC : sound");
exit(CHK_ERROR_ALLOC);
}
Mix_Music *musique;
musique = Mix_LoadMUS("sound/musique.wav");
......
......@@ -41,6 +41,10 @@ TextureName sprite_getTextureNameFromButtonName(ButtonName button_name)
Texture* sprite_importTexture(char image_path[], int totalX, int totalY)
{
Texture* new_texture = malloc(sizeof(Texture));
if (!new_texture) {
printf("ERROR ALLOC : new_texture");
exit(CHK_ERROR_ALLOC);
}
SDL_Surface* texture_surface = IMG_Load(image_path);
if(!texture_surface) {
printf("IMG_Load: %s\n", IMG_GetError());
......
......@@ -3,6 +3,10 @@
ListTours* tour_initListTours()
{
ListTours* listTours = malloc(sizeof(ListTours));
if (!listTours) {
printf("ERROR ALLOC : listTours");
exit(CHK_ERROR_ALLOC);
}
listTours->nbTours = 0;
listTours->next = NULL;
return listTours;
......@@ -41,7 +45,8 @@ Tour *tour_create(TypeCase type, int index_case)
{
Tour *new = malloc(sizeof(Tour));
if (!new) {
// TODO: Throw allocation error
printf("ERROR ALLOC : new");
exit(CHK_ERROR_ALLOC);
}
int caseX, caseY;
case_getCasePosition(index_case, &caseX, &caseY);
......
......@@ -37,6 +37,10 @@ int moveWave()
}
} else if(plateau->currentWave.next != NULL) {
TypeMonster* tmp = malloc(sizeof(TypeMonster)*plateau->currentWave.next->monster_total);
if (!tmp) {
printf("ERROR ALLOC : tmp");
exit(CHK_ERROR_ALLOC);
}
memcpy(tmp , plateau->currentWave.next->monsters, sizeof(TypeMonster)*plateau->currentWave.next->monster_total);
plateau->currentWave = *plateau->currentWave.next;
plateau->currentWave.monsters = tmp;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment