dungeon_crawler

view prototype/src/tileset.cc @ 61:4472ef41a209

forgot an extra mrt uniform in deferred_omni.p.glsl
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 25 Sep 2012 06:37:56 +0300
parents f3030df27110
children 7f52d6310317
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;
9 TileSet::TileSet()
10 : texset(load_texture, destroy_texture),
11 sampleset(load_audio_sample, destroy_audio_sample)
12 {
13 }
15 TileSet::~TileSet()
16 {
17 for(auto iter : tiles) {
18 delete iter.second;
19 }
20 }
22 bool TileSet::load(const char *fname)
23 {
24 FILE *fp;
25 if(!(fp = fopen(fname, "r"))) {
26 fprintf(stderr, "failed to open tileset: %s\n", fname);
27 return false;
28 }
30 int linenum = 0;
31 char buf[512];
32 while(fgets(buf, sizeof buf, fp)) {
33 linenum++;
35 char *line = strip_space(buf);
36 if(!*line || *line == '#') {
37 continue;
38 }
40 char *tilefile = strchr(line, ':');
41 if(!tilefile) {
42 fprintf(stderr, "error parsing tileset %s, line %d: %s\n", fname,
43 linenum, line);
44 fclose(fp);
45 return false;
46 }
47 *tilefile++ = 0;
49 printf("Tileset %s, loading tile \"%s\" -> %s\n", fname, line, tilefile);
50 Tile *tile = new Tile(this);
51 if(!tile->load(datafile_path(tilefile))) {
52 fprintf(stderr, "failed to load tile: %s\n", tilefile);
53 delete tile;
54 continue;
55 }
57 tiles[line] = tile;
58 }
60 fclose(fp);
61 return true;
62 }
64 TextureSet *TileSet::get_textures()
65 {
66 return &texset;
67 }
69 const TextureSet *TileSet::get_textures() const
70 {
71 return &texset;
72 }
74 SampleSet *TileSet::get_samples()
75 {
76 return &sampleset;
77 }
79 const SampleSet *TileSet::get_samples() const
80 {
81 return &sampleset;
82 }
84 Tile *TileSet::get_tile(const char *name) const
85 {
86 auto res = tiles.find(name);
87 return res != tiles.end() ? res->second : 0;
88 }
90 void TileSet::update_tiles(unsigned long msec)
91 {
92 for(auto tilepair : tiles) {
93 tilepair.second->update(msec, 0.0f);
94 }
95 }
97 void set_active_tileset(TileSet *set)
98 {
99 active_tileset = set;
100 }
102 TileSet *get_active_tileset()
103 {
104 return active_tileset;
105 }
107 static char *strip_space(char *ptr)
108 {
109 while(isspace(*ptr)) {
110 ptr++;
111 }
113 char *nl = strrchr(ptr, '\n');
114 if(nl) *nl = 0;
115 char *cr = strrchr(ptr, '\r');
116 if(cr) *cr = 0;
118 return ptr;
119 }