dungeon_crawler
view 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 source
1 #include <stdio.h>
2 #include "tileset.h"
3 #include "datapath.h"
5 static char *strip_space(char *ptr);
7 static TileSet *active_tileset;
10 TileSet::~TileSet()
11 {
12 for(auto iter : tiles) {
13 delete iter.second;
14 }
15 }
17 bool TileSet::load(const char *fname)
18 {
19 FILE *fp;
20 if(!(fp = fopen(fname, "r"))) {
21 fprintf(stderr, "failed to open tileset: %s\n", fname);
22 return false;
23 }
25 int linenum = 0;
26 char buf[512];
27 while(fgets(buf, sizeof buf, fp)) {
28 linenum++;
30 char *line = strip_space(buf);
31 if(!*line || *line == '#') {
32 continue;
33 }
35 char *tilefile = strchr(line, ':');
36 if(!tilefile) {
37 fprintf(stderr, "error parsing tileset %s, line %d: %s\n", fname,
38 linenum, line);
39 fclose(fp);
40 return false;
41 }
42 *tilefile++ = 0;
44 printf("Tileset %s, loading tile \"%s\" -> %s\n", fname, line, tilefile);
45 Tile *tile = new Tile;
46 if(!tile->load(datafile_path(tilefile))) {
47 fprintf(stderr, "failed to load tile: %s\n", tilefile);
48 delete tile;
49 continue;
50 }
52 tiles[line] = tile;
53 }
55 fclose(fp);
56 return true;
57 }
59 Tile *TileSet::get_tile(const char *name) const
60 {
61 auto res = tiles.find(name);
62 return res != tiles.end() ? res->second : 0;
63 }
65 void set_active_tileset(TileSet *set)
66 {
67 active_tileset = set;
68 }
70 TileSet *get_active_tileset()
71 {
72 return active_tileset;
73 }
75 static char *strip_space(char *ptr)
76 {
77 while(isspace(*ptr)) {
78 ptr++;
79 }
81 char *nl = strrchr(ptr, '\n');
82 if(nl) *nl = 0;
83 char *cr = strrchr(ptr, '\r');
84 if(cr) *cr = 0;
86 return ptr;
87 }