dungeon_crawler

diff prototype/src/tileset.cc @ 5:252a00508411

more stuff
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 12 Aug 2012 07:07:57 +0300
parents
children e5567ddbf2ef
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/prototype/src/tileset.cc	Sun Aug 12 07:07:57 2012 +0300
     1.3 @@ -0,0 +1,87 @@
     1.4 +#include <stdio.h>
     1.5 +#include "tileset.h"
     1.6 +#include "datapath.h"
     1.7 +
     1.8 +static char *strip_space(char *ptr);
     1.9 +
    1.10 +static TileSet *active_tileset;
    1.11 +
    1.12 +
    1.13 +TileSet::~TileSet()
    1.14 +{
    1.15 +	for(auto iter : tiles) {
    1.16 +		delete iter.second;
    1.17 +	}
    1.18 +}
    1.19 +
    1.20 +bool TileSet::load(const char *fname)
    1.21 +{
    1.22 +	FILE *fp;
    1.23 +	if(!(fp = fopen(fname, "r"))) {
    1.24 +		fprintf(stderr, "failed to open tileset: %s\n", fname);
    1.25 +		return false;
    1.26 +	}
    1.27 +
    1.28 +	int linenum = 0;
    1.29 +	char buf[512];
    1.30 +	while(fgets(buf, sizeof buf, fp)) {
    1.31 +		linenum++;
    1.32 +
    1.33 +		char *line = strip_space(buf);
    1.34 +		if(!*line || *line == '#') {
    1.35 +			continue;
    1.36 +		}
    1.37 +
    1.38 +		char *tilefile = strchr(line, ':');
    1.39 +		if(!tilefile) {
    1.40 +			fprintf(stderr, "error parsing tileset %s, line %d: %s\n", fname,
    1.41 +					linenum, line);
    1.42 +			fclose(fp);
    1.43 +			return false;
    1.44 +		}
    1.45 +		*tilefile++ = 0;
    1.46 +
    1.47 +		printf("Tileset %s, loading tile \"%s\" -> %s\n", fname, line, tilefile);
    1.48 +		Tile *tile = new Tile;
    1.49 +		if(!tile->load(datafile_path(tilefile))) {
    1.50 +			fprintf(stderr, "failed to load tile: %s\n", tilefile);
    1.51 +			delete tile;
    1.52 +			continue;
    1.53 +		}
    1.54 +
    1.55 +		tiles[line] = tile;
    1.56 +	}
    1.57 +
    1.58 +	fclose(fp);
    1.59 +	return true;
    1.60 +}
    1.61 +
    1.62 +Tile *TileSet::get_tile(const char *name) const
    1.63 +{
    1.64 +	auto res = tiles.find(name);
    1.65 +	return res != tiles.end() ? res->second : 0;
    1.66 +}
    1.67 +
    1.68 +void set_active_tileset(TileSet *set)
    1.69 +{
    1.70 +	active_tileset = set;
    1.71 +}
    1.72 +
    1.73 +TileSet *get_active_tileset()
    1.74 +{
    1.75 +	return active_tileset;
    1.76 +}
    1.77 +
    1.78 +static char *strip_space(char *ptr)
    1.79 +{
    1.80 +	while(isspace(*ptr)) {
    1.81 +		ptr++;
    1.82 +	}
    1.83 +
    1.84 +	char *nl = strrchr(ptr, '\n');
    1.85 +	if(nl) *nl = 0;
    1.86 +	char *cr = strrchr(ptr, '\r');
    1.87 +	if(cr) *cr = 0;
    1.88 +
    1.89 +	return ptr;
    1.90 +}