dungeon_crawler
view prototype/src/tileset.cc @ 15:3a3236a4833c
adding shaders and renderer abstraction
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 19 Aug 2012 23:09:30 +0300 |
parents | 252a00508411 |
children | f3030df27110 |
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 set_active_tileset(TileSet *set)
76 {
77 active_tileset = set;
78 }
80 TileSet *get_active_tileset()
81 {
82 return active_tileset;
83 }
85 static char *strip_space(char *ptr)
86 {
87 while(isspace(*ptr)) {
88 ptr++;
89 }
91 char *nl = strrchr(ptr, '\n');
92 if(nl) *nl = 0;
93 char *cr = strrchr(ptr, '\r');
94 if(cr) *cr = 0;
96 return ptr;
97 }