timesynk/tile.c

52 lines
1.3 KiB
C

#include <stdlib.h>
#include <stdio.h>
#include <string.h> // memcpy
#include "tile.h"
#include "wall.h"
#include "common.h"
int allocateTile(struct Tile** tile, unsigned int tid, short id) {
*tile = (struct Tile *) malloc(sizeof(struct Tile));
if (tile == NULL) {
printf("ERROR: could not malloc Tile");
return ERROR;
}
(*tile)->tid = tid;
(*tile)->id = id;
return SUCCESS;
}
void freeTile(struct Tile* tile) {
// @@ FIXME
free(tile->data);
free(tile);
}
struct Tile *newTile(unsigned int type_id, short id, short x, short y) {
struct Tile *new_tile;
new_tile = malloc(sizeof(struct Tile));
if (new_tile == NULL) {
printf("ERROR: could not malloc Tile");
return NULL;
}
new_tile->tid = type_id;
new_tile->id = id;
new_tile->x = x;
new_tile->y = y;
switch(type_id) {
case WALL:
new_tile->data = (WallTile *) malloc(sizeof(WallTile));
memcpy(new_tile->data, &walls[id], sizeof(WallTile));
break;
case FLOOR:
new_tile->data = (FloorTile *) malloc(sizeof(FloorTile));
memcpy(new_tile->data, &floors[id], sizeof(FloorTile));
break;
default:
new_tile->data = (struct BasicTile *) malloc(sizeof(struct BasicTile));
memcpy(new_tile->data, &walls[id], sizeof(struct BasicTile));
break;
}
return (new_tile);
}