Mercurial > hg > batmud > maputils
changeset 17:bbae33cf3113
Moved some mapblock handling in maputils lib
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 09 Dec 2006 20:05:19 +0000 |
parents | 4c880f80e972 |
children | f0196af28884 |
files | maputils.c maputils.h |
diffstat | 2 files changed, 134 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/maputils.c Sat Dec 09 01:46:14 2006 +0000 +++ b/maputils.c Sat Dec 09 20:05:19 2006 +0000 @@ -4,6 +4,7 @@ * (C) Copyright 2006 Tecnic Software productions (TNSP) */ #include "maputils.h" +#include "th_util.h" color_t mapColors[] = { { "#000000" }, @@ -74,7 +75,7 @@ const DINT nmapPieces = (sizeof(mapPieces) / sizeof(mappiece_t)); -DINT mcget(DINT c) +DINT mcGet(DINT c) { DINT i; for (i = 0; i < nmapPieces; i++) @@ -83,7 +84,7 @@ return -1; } -DINT mcgetc(DINT c) +DINT mcGetColor(DINT c) { DINT i; for (i = 0; i < nmapPieces; i++) @@ -116,3 +117,121 @@ if (title) fprintf(outFile, " <title>%s</title>\n", title); } + + +/* Map block handling + */ +mapblock_t * allocBlock(DINT blockW, DINT blockH) +{ + mapblock_t *blockRes; + + /* Check arguments */ + if ((blockW <= 0) || (blockH <= 0)) + return NULL; + + /* Allocate struct and data */ + blockRes = (mapblock_t *) th_calloc(1, sizeof(mapblock_t)); + if (!blockRes) return NULL; + + blockRes->d = (char *) th_calloc(blockH * blockW, sizeof(char)); + if (!blockRes->d) { + th_free(blockRes); + return NULL; + } + + blockRes->w = blockW; + blockRes->h = blockH; + + return blockRes; +} + + +void freeBlock(mapblock_t *b) +{ + if (b) { + if (b->d) + th_free(b->d); + b->d = NULL; + th_free(b); + } +} + +/* Parse single arbitrary sized block from given mapfile + */ +mapblock_t * parseFile(DCHAR *mapFilename) +{ + FILE *inFile; + mapblock_t *res; + DCHAR *o; + DINT x, y, resW, resH, maxW, c; + + /* Open file */ + if ((inFile = fopen(mapFilename, "rb")) == NULL) { + THERR("Could not open mapfile '%s' for reading.\n", + mapFilename); + return NULL; + } + + /* Probe map width */ + resH = 0; resW = -1; maxW = 0; + while ((c = fgetc(inFile)) != EOF) { + if (c == '\n' || c == '\r') { + if (maxW > resW) + resW = maxW; + maxW = 0; + resH++; + } else + maxW++; + } + + /* Seek back */ + if (fseek(inFile, 0L, SEEK_SET) == -1) { + fclose(inFile); + THERR("Could not rewind file '%s'.\n", + mapFilename); + return NULL; + } + + /* Allocate block */ + if ((res = allocBlock(resW, resH)) == NULL) { + fclose(inFile); + THERR("Could not allocate mapblock (%d, %d) for '%s'.\n", + resW, resH, mapFilename); + return NULL; + } + + /* Read data */ + o = res->d; + y = x = 0; + while ((c = fgetc(inFile)) != EOF && (y < res->h)) { + if (c == '\n' || c == '\r') { + if (x != res->w) { + THERR("Broken block, line width %d < %d!\n", + x, res->w); + fclose(inFile); + freeBlock(res); + return NULL; + } + + x = 0; + y++; + } else { + *o = c; + x++; + o++; + } + } + + /* Close file */ + fclose(inFile); + + if (y != res->h) { + THERR("Broken block, height %d < %d\n", + y, res->h); + freeBlock(res); + return NULL; + } + + return res; +} +
--- a/maputils.h Sat Dec 09 01:46:14 2006 +0000 +++ b/maputils.h Sat Dec 09 20:05:19 2006 +0000 @@ -14,6 +14,13 @@ typedef struct { + DCHAR *d; + DINT w, h; + DBOOL mark; +} mapblock_t; + + +typedef struct { DCHAR *css; } color_t; @@ -49,9 +56,13 @@ /* Functions */ -DINT mcget(DINT); -DINT mcgetc(DINT); +DINT mcGet(DINT); +DINT mcGetColor(DINT); void mcXHTMLcolors(FILE *); void mcXHTMLhead(FILE *, DCHAR *); +mapblock_t * allocBlock(DINT, DINT); +void freeBlock(mapblock_t *); +mapblock_t * parseFile(DCHAR *); + #endif