dungeon_crawler

view prototype/src/tileset.cc @ 46:f3030df27110

debugging the particles
author John Tsiombikas <nuclear@mutantstargoat.com>
date Thu, 13 Sep 2012 06:33:51 +0300
parents e5567ddbf2ef
children aa9e28670ae2
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(this);
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 TextureSet *TileSet::get_textures()
60 {
61 return &texset;
62 }
64 const TextureSet *TileSet::get_textures() const
65 {
66 return &texset;
67 }
69 Tile *TileSet::get_tile(const char *name) const
70 {
71 auto res = tiles.find(name);
72 return res != tiles.end() ? res->second : 0;
73 }
75 void TileSet::update_tiles(unsigned long msec)
76 {
77 for(auto tilepair : tiles) {
78 tilepair.second->update(msec, 0.0f);
79 }
80 }
82 void set_active_tileset(TileSet *set)
83 {
84 active_tileset = set;
85 }
87 TileSet *get_active_tileset()
88 {
89 return active_tileset;
90 }
92 static char *strip_space(char *ptr)
93 {
94 while(isspace(*ptr)) {
95 ptr++;
96 }
98 char *nl = strrchr(ptr, '\n');
99 if(nl) *nl = 0;
100 char *cr = strrchr(ptr, '\r');
101 if(cr) *cr = 0;
103 return ptr;
104 }