dungeon_crawler
annotate prototype/src/tileset.cc @ 10:22562582c82d
foo
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 18 Aug 2012 03:47:13 +0300 |
parents | |
children | e5567ddbf2ef |
rev | line source |
---|---|
nuclear@5 | 1 #include <stdio.h> |
nuclear@5 | 2 #include "tileset.h" |
nuclear@5 | 3 #include "datapath.h" |
nuclear@5 | 4 |
nuclear@5 | 5 static char *strip_space(char *ptr); |
nuclear@5 | 6 |
nuclear@5 | 7 static TileSet *active_tileset; |
nuclear@5 | 8 |
nuclear@5 | 9 |
nuclear@5 | 10 TileSet::~TileSet() |
nuclear@5 | 11 { |
nuclear@5 | 12 for(auto iter : tiles) { |
nuclear@5 | 13 delete iter.second; |
nuclear@5 | 14 } |
nuclear@5 | 15 } |
nuclear@5 | 16 |
nuclear@5 | 17 bool TileSet::load(const char *fname) |
nuclear@5 | 18 { |
nuclear@5 | 19 FILE *fp; |
nuclear@5 | 20 if(!(fp = fopen(fname, "r"))) { |
nuclear@5 | 21 fprintf(stderr, "failed to open tileset: %s\n", fname); |
nuclear@5 | 22 return false; |
nuclear@5 | 23 } |
nuclear@5 | 24 |
nuclear@5 | 25 int linenum = 0; |
nuclear@5 | 26 char buf[512]; |
nuclear@5 | 27 while(fgets(buf, sizeof buf, fp)) { |
nuclear@5 | 28 linenum++; |
nuclear@5 | 29 |
nuclear@5 | 30 char *line = strip_space(buf); |
nuclear@5 | 31 if(!*line || *line == '#') { |
nuclear@5 | 32 continue; |
nuclear@5 | 33 } |
nuclear@5 | 34 |
nuclear@5 | 35 char *tilefile = strchr(line, ':'); |
nuclear@5 | 36 if(!tilefile) { |
nuclear@5 | 37 fprintf(stderr, "error parsing tileset %s, line %d: %s\n", fname, |
nuclear@5 | 38 linenum, line); |
nuclear@5 | 39 fclose(fp); |
nuclear@5 | 40 return false; |
nuclear@5 | 41 } |
nuclear@5 | 42 *tilefile++ = 0; |
nuclear@5 | 43 |
nuclear@5 | 44 printf("Tileset %s, loading tile \"%s\" -> %s\n", fname, line, tilefile); |
nuclear@5 | 45 Tile *tile = new Tile; |
nuclear@5 | 46 if(!tile->load(datafile_path(tilefile))) { |
nuclear@5 | 47 fprintf(stderr, "failed to load tile: %s\n", tilefile); |
nuclear@5 | 48 delete tile; |
nuclear@5 | 49 continue; |
nuclear@5 | 50 } |
nuclear@5 | 51 |
nuclear@5 | 52 tiles[line] = tile; |
nuclear@5 | 53 } |
nuclear@5 | 54 |
nuclear@5 | 55 fclose(fp); |
nuclear@5 | 56 return true; |
nuclear@5 | 57 } |
nuclear@5 | 58 |
nuclear@5 | 59 Tile *TileSet::get_tile(const char *name) const |
nuclear@5 | 60 { |
nuclear@5 | 61 auto res = tiles.find(name); |
nuclear@5 | 62 return res != tiles.end() ? res->second : 0; |
nuclear@5 | 63 } |
nuclear@5 | 64 |
nuclear@5 | 65 void set_active_tileset(TileSet *set) |
nuclear@5 | 66 { |
nuclear@5 | 67 active_tileset = set; |
nuclear@5 | 68 } |
nuclear@5 | 69 |
nuclear@5 | 70 TileSet *get_active_tileset() |
nuclear@5 | 71 { |
nuclear@5 | 72 return active_tileset; |
nuclear@5 | 73 } |
nuclear@5 | 74 |
nuclear@5 | 75 static char *strip_space(char *ptr) |
nuclear@5 | 76 { |
nuclear@5 | 77 while(isspace(*ptr)) { |
nuclear@5 | 78 ptr++; |
nuclear@5 | 79 } |
nuclear@5 | 80 |
nuclear@5 | 81 char *nl = strrchr(ptr, '\n'); |
nuclear@5 | 82 if(nl) *nl = 0; |
nuclear@5 | 83 char *cr = strrchr(ptr, '\r'); |
nuclear@5 | 84 if(cr) *cr = 0; |
nuclear@5 | 85 |
nuclear@5 | 86 return ptr; |
nuclear@5 | 87 } |