AST/arenaEngine.c
2018-06-23 08:41:52 +02:00

257 lines
4.3 KiB
C

#include "arenaEngine.h"
#include <stdlib.h>
#include <stdio.h>
#include "fileHandler.h"
//#include "logHelper.h"
/*
* Arena generate functions
*/
#define BLOC_SIZE 32 // Taille d'un bloc (carré) en pixels
#define NB_BLOCS_LARGEUR 20
#define NB_BLOCS_HAUTEUR 20
#define LARGEUR_FENETRE BLOC_SIZE * NB_BLOCS_LARGEUR
#define HAUTEUR_FENETRE BLOC_SIZE * NB_BLOCS_HAUTEUR
ARENA_H_TILE* genNewArena(int size_h, int size_w)
{
ARENA_H_TILE* arenaOrigin = NULL;
ARENA_H_TILE* TmpCursor_h = NULL;
ARENA_W_TILE* TmpCursor_w = NULL;
int i,j;
FILE* fichier = NULL;
char ligneFichier[NB_BLOCS_LARGEUR * NB_BLOCS_HAUTEUR + 1] = {0};
//int i = 0, j = 0;
printf("Chargement du fichier\n");
fichier = fopen("level2-20x20.lvl", "r");
if (fichier == NULL)
return 0;
fgets(ligneFichier, size_w * size_h + 1, fichier);
for(i=0;i<size_h;i++)
{
if (i==0)
{
arenaOrigin = createHTile(NULL);
TmpCursor_h = arenaOrigin;
}
else
{
TmpCursor_h = createHTile(TmpCursor_h);
}
for(j=0;j<size_w;j++)
{
if (j==0)
{
TmpCursor_w = createWTile(TmpCursor_h, NULL);
}
else
{
createWTile(NULL, TmpCursor_w);
}
switch (ligneFichier[(i * size_w) + j])
{
case '0': TmpCursor_w->type_id = 0;
//printf("herbe\n");
break;
case '1':
TmpCursor_w->type_id = 1;
// printf("caillou\n");
break;
case '2':
TmpCursor_w->type_id = 2;
//printf("arbre\n");
break;
}
if (j!=0) TmpCursor_w = TmpCursor_w->nextColumn;
}
}
return arenaOrigin;
}
ARENA_H_TILE* createHTile(ARENA_H_TILE* prevHTile)
{
ARENA_H_TILE* tile_h = NULL;
if (prevHTile == NULL)
{
tile_h = calloc(1,sizeof(ARENA_H_TILE)); //Using calloc because of resetting all memory allocated to 0
tile_h->type_id = 0;
//tile_h->playerOnTile = NULL;
tile_h->nextRow = NULL;
tile_h->nextColumn = NULL;
}
else if (prevHTile != NULL)
{
tile_h = calloc(1,sizeof(ARENA_H_TILE));
prevHTile->nextRow = tile_h;
tile_h->type_id = 0;
//tile_h->playerOnTile = NULL;
tile_h->nextRow = NULL;
tile_h->nextColumn = NULL;
}
return tile_h;
}
ARENA_W_TILE* createWTile(ARENA_H_TILE* prevHTile, ARENA_W_TILE* prevWTile)
{
ARENA_W_TILE* tile_w = NULL;
if (prevHTile != NULL && prevWTile == NULL)
{
tile_w = calloc(1,sizeof(ARENA_W_TILE));
prevHTile->nextColumn = tile_w;
tile_w->type_id = 0;
//tile_w->playerOnTile = NULL;
tile_w->nextColumn = NULL;
return tile_w;
}
else if (prevHTile == NULL && prevWTile != NULL)
{
tile_w = calloc(1,sizeof(ARENA_W_TILE));
prevWTile->nextColumn = tile_w;
tile_w->type_id = 0;
//tile_w->playerOnTile = NULL;
tile_w->nextColumn = NULL;
}
return NULL;
}
/*
ARENA_H_TILE* genNewArena(int size_h, int size_w)
{
ARENA_H_TILE* arenaOrigin = NULL;
ARENA_H_TILE* TmpCursor_h = NULL;
ARENA_W_TILE* TmpCursor_w = NULL;
int i,j;
for(i=0;i<size_h;i++)
{
if (i==0)
{
arenaOrigin = createHTile(NULL);
TmpCursor_h = arenaOrigin;
}
else
{
TmpCursor_h = createHTile(TmpCursor_h);
}
for(j=0;j<size_w;j++)
{
if (j==0)
{
TmpCursor_w = createWTile(TmpCursor_h, NULL);
}
else
{
createWTile(NULL, TmpCursor_w);
}
if (j!=0) TmpCursor_w = TmpCursor_w->nextColumn;
}
}
return arenaOrigin;
}
*/
/*
* Arena delete functions
*/
void deleteWTile(ARENA_W_TILE* WTile)
{
if (WTile->nextColumn != NULL) {
deleteWTile(WTile->nextColumn);
}
free(WTile);
}
void deleteHTile(ARENA_H_TILE* HTile)
{
if (HTile->nextRow != NULL)
{
deleteHTile(HTile->nextRow);
}
deleteWTile(HTile->nextColumn);
free(HTile);
}
void deleteArena(ARENA_H_TILE* arena)
{
deleteHTile(arena);
}
/*
* Arena status functions
*/
int getTileTypeID(ARENA_H_TILE* arena, int coord_x, int coord_y)
{
int type_id = -1;
int i;
if (coord_y == 0)
{
ARENA_H_TILE* tile_h = NULL;
tile_h = arena;
if (coord_x != 0)
{
for (i=0;i<coord_x;i++)
{
tile_h = arena->nextRow;
}
}
type_id = tile_h->type_id;
}
else
{
ARENA_W_TILE* tile_w = NULL;
ARENA_H_TILE* tile_h = NULL;
tile_h = arena;
if (coord_x != 0)
{
for (i=0;i<coord_x;i++)
{
tile_h = tile_h->nextRow;
}
}
tile_w = tile_h->nextColumn;
if (coord_y != 0)
{
for (i=0;i<coord_y;i++)
{
tile_w = tile_w->nextColumn;
}
}
type_id = tile_w->type_id;
}
return type_id;
}