241 lines
6.7 KiB
C
241 lines
6.7 KiB
C
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include "wall.h"
|
|
#include "map.h"
|
|
#include "tile.h"
|
|
#include "common.h"
|
|
|
|
// FIXME: I think this is broken on XCode SDL - check valgrind for hints
|
|
/*int allocateMapTiles(struct Tile*** memory, unsigned int rows, unsigned int cols) {
|
|
int col_span = cols * sizeof(struct Tile);
|
|
int row_span = rows * sizeof(struct Tile*);
|
|
struct Tile ***row_pointers = *memory = malloc(row_span + rows * col_span);
|
|
if (memory == NULL) {
|
|
printf("ERROR: could not malloc tile matrix");
|
|
return ERROR;
|
|
}
|
|
struct Tile *payload = ((struct Tile *)row_pointers) + row_span;
|
|
int i;
|
|
for (i = 0; i < rows; payload += col_span, i++) {
|
|
row_pointers[i] = payload;
|
|
}
|
|
return SUCCESS;
|
|
}*/
|
|
|
|
/*int allocateMapTiles(struct Tile*** memory, unsigned int rows, unsigned int cols) {
|
|
*memory = (struct Tile**) malloc(rows * sizeof(struct Tile*));
|
|
struct Tile *temp = (struct Tile*)malloc(rows * cols * sizeof(struct Tile));
|
|
int i;
|
|
for (i=0;i<rows;i++) {
|
|
(*memory)[i] = temp + (i * cols);
|
|
}
|
|
int x=0;
|
|
int y=0;
|
|
while(x != cols) {
|
|
while(y != rows) {
|
|
memory[x][y]->tid = 0;
|
|
y++;
|
|
}
|
|
x++;
|
|
}
|
|
return SUCCESS;
|
|
}*/
|
|
/*int allocateMapTiles(struct Tile*** memory, unsigned int rows, unsigned int cols) {
|
|
struct Tile *tile;
|
|
tile = malloc(rows * cols * sizeof(*tile));
|
|
*memory = malloc(rows * sizeof(**memory));
|
|
int i=0;
|
|
for(i=0;i<rows;i++) {
|
|
*memory[i] = &tile[i*cols];
|
|
}
|
|
return SUCCESS;
|
|
}*/
|
|
|
|
/*int allocateMapTiles(struct Tile*** memory, unsigned int rows, unsigned int cols) {
|
|
*memory = malloc(rows*sizeof(struct Tile *));
|
|
|
|
int i;
|
|
for(i=0;i<rows;i++)
|
|
(*memory)[i] = malloc(cols * sizeof(struct Tile));
|
|
return SUCCESS;
|
|
} */
|
|
|
|
int allocateMapTiles(struct Tile*** memory, unsigned int rows, unsigned int cols) {
|
|
int i;
|
|
//*memory = calloc(rows, sizeof(struct Tile));
|
|
*memory = calloc(rows, sizeof *memory);
|
|
for(i=0;i<rows;i++)
|
|
(*memory)[i] = calloc(cols, sizeof(struct Tile));
|
|
return SUCCESS;
|
|
}
|
|
|
|
|
|
void freeMapTiles(struct Tile*** memory, unsigned int rows, unsigned int cols) {
|
|
int i;
|
|
for (i=0;i<rows;i++) {
|
|
free((*memory)[i]);
|
|
}
|
|
free(*memory);
|
|
}
|
|
|
|
void freeMap(struct Map **map) {
|
|
int x, y;
|
|
for(x=0;x<(*map)->width;x++) {
|
|
for(y=0;y<(*map)->height;y++) {
|
|
struct Tile* tile;
|
|
struct Tile* next_tile;
|
|
if ((*map)->matrix[x][y].next);
|
|
next_tile = (*map)->matrix[x][y].next;
|
|
tile = (*map)->matrix[x][y].next;
|
|
while(next_tile) {
|
|
// TODO: test, does this actually free properly?
|
|
//freeTile(&*tile);
|
|
tile = next_tile->next;
|
|
next_tile = next_tile->next;
|
|
freeTile(tile);
|
|
//free(tile);
|
|
//tile = tile->next;
|
|
}
|
|
}
|
|
}
|
|
freeMapTiles(&(*map)->matrix, (*map)->width, (*map)->height);
|
|
//free(map);
|
|
//map = NULL;
|
|
}
|
|
|
|
int allocateMap(struct Map** map, unsigned int width, unsigned int height) {
|
|
*map = malloc(sizeof(struct Map));
|
|
if (map == NULL) {
|
|
printf("ERROR: could not malloc Map");
|
|
return ERROR;
|
|
}
|
|
(*map)->width = width;
|
|
(*map)->height = height;
|
|
|
|
allocateMapTiles(&(*map)->matrix, width, height);
|
|
int x, y;
|
|
for (x=0;x<width;x++) {
|
|
for (y=0;y<height;y++) {
|
|
(*map)->matrix[x][y].next = NULL;
|
|
(*map)->matrix[x][y].prev = NULL;
|
|
(*map)->matrix[x][y].x = x;
|
|
(*map)->matrix[x][y].y = y;
|
|
}
|
|
}
|
|
return SUCCESS;
|
|
}
|
|
|
|
void replaceTile(struct Tile *tile, unsigned int tid, short id) {
|
|
tile->tid = tid;
|
|
tile->id = id;
|
|
// @@ CHECK IT OUT
|
|
//free(tile->data); // free old data, just in case
|
|
//tile->data = NULL;
|
|
if (tid == FLOOR) {
|
|
tile->data = malloc(sizeof(FloorTile));
|
|
memcpy(tile->data, &floors[id], sizeof(FloorTile)); // copy default properties in
|
|
} else if (tid == WALL) {
|
|
tile->data = malloc(sizeof(WallTile));
|
|
memcpy(tile->data, &walls[id], sizeof(WallTile));
|
|
} else if (tid == DOOR) {
|
|
tile->data = malloc(sizeof(struct DoorTile));
|
|
memcpy(tile->data, &doors[id], sizeof(struct DoorTile));
|
|
}
|
|
}
|
|
|
|
void appendTile(struct Tile *tile, unsigned int tid, short id) {
|
|
struct Tile *new_tile = newTile(tid, id, tile->x, tile->y);
|
|
new_tile->prev = tile;
|
|
tile->next = new_tile;
|
|
}
|
|
|
|
void floodMap(struct Map** map, unsigned int operation, unsigned int tid, short id) {
|
|
int x, y;
|
|
for(x=0;x < (*map)->width;x++) {
|
|
for(y=0;y < (*map)->height;y++) {
|
|
if (operation == TILE_REPLACE) {
|
|
replaceTile(&((*map)->matrix[x][y]), tid, id);
|
|
(*map)->matrix[x][y].x = x;
|
|
(*map)->matrix[x][y].y = y;
|
|
} else if (operation == TILE_APPEND) {
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void drawPath(struct Map** map, unsigned int operation, unsigned int tid, short id, unsigned int from_x, unsigned int from_y, unsigned int to_x, unsigned int to_y) {
|
|
int diff_x = from_x-to_x;
|
|
int diff_y = from_y-to_y;
|
|
int x_inc = (diff_x < 0 ? 1 : -1);
|
|
int y_inc = (diff_y < 0 ? 1 : -1);
|
|
int total = abs(diff_x)+abs(diff_y)*2; // FIXME: this is stupid
|
|
int toggle = 0, step = 0;
|
|
int x_mod, y_mod;
|
|
for(step=0;step < total;step++) {
|
|
if (operation == TILE_REPLACE) {
|
|
replaceTile(&(*map)->matrix[from_x][from_y], tid, id);
|
|
} else if (operation == TILE_APPEND) {
|
|
appendTile(&(*map)->matrix[from_x][from_y], tid, id);
|
|
}
|
|
if (toggle == 1) {
|
|
y_mod = 0;
|
|
x_mod = x_inc;
|
|
toggle = 0;
|
|
} else {
|
|
y_mod = y_inc;
|
|
x_mod = 0;
|
|
toggle = 1;
|
|
}
|
|
if (from_y != to_y)
|
|
from_y += y_mod;
|
|
if (from_x != to_x)
|
|
from_x += x_mod;
|
|
}
|
|
}
|
|
|
|
void drawLine(struct Map** map, unsigned int operation, unsigned int tid, short id, unsigned int from_x, unsigned int from_y, unsigned int to_x, unsigned int to_y) {
|
|
int x_inc = (from_x-to_x < 0 ? -1 : 1);
|
|
int y_inc = (from_y-to_y < 0 ? -1 : 1);
|
|
while(1) {
|
|
if (operation == TILE_REPLACE) {
|
|
replaceTile(&(*map)->matrix[from_x][from_y], tid, id);
|
|
} else if (operation == TILE_APPEND) {
|
|
appendTile(&(*map)->matrix[from_x][from_y], tid, id);
|
|
}
|
|
if (from_x == to_x && from_y == to_y)
|
|
break;
|
|
if (from_x != to_x)
|
|
from_x += x_inc;
|
|
if (from_y != to_y)
|
|
from_y += y_inc;
|
|
}
|
|
}
|
|
|
|
struct Tile *findTile(int start_x, int start_y, int width, int height, int tid) {
|
|
return NULL;
|
|
}
|
|
|
|
struct Tile *getTopTile(struct Map *target_map, int x, int y) {
|
|
struct Tile* target_tile;
|
|
target_tile = getTile(target_map, x, y);
|
|
if (target_tile != NULL) {
|
|
while(target_tile->next != NULL) {
|
|
target_tile = target_tile->next;
|
|
}
|
|
}
|
|
return target_tile;
|
|
}
|
|
|
|
struct Tile *getTile(struct Map *target_map, int x, int y) {
|
|
struct Tile* target_tile;
|
|
target_tile = NULL;
|
|
if ((x < target_map->width && x >= 0) && (y < target_map->height && y >= 0)) {
|
|
if (target_map->matrix[x][y].tid) {
|
|
target_tile = &target_map->matrix[x][y];
|
|
}
|
|
}
|
|
return target_tile;
|
|
}
|