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@11
|
45 Tile *tile = new Tile(this);
|
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@11
|
59 TextureSet *TileSet::get_textures()
|
nuclear@11
|
60 {
|
nuclear@11
|
61 return &texset;
|
nuclear@11
|
62 }
|
nuclear@11
|
63
|
nuclear@11
|
64 const TextureSet *TileSet::get_textures() const
|
nuclear@11
|
65 {
|
nuclear@11
|
66 return &texset;
|
nuclear@11
|
67 }
|
nuclear@11
|
68
|
nuclear@5
|
69 Tile *TileSet::get_tile(const char *name) const
|
nuclear@5
|
70 {
|
nuclear@5
|
71 auto res = tiles.find(name);
|
nuclear@5
|
72 return res != tiles.end() ? res->second : 0;
|
nuclear@5
|
73 }
|
nuclear@5
|
74
|
nuclear@46
|
75 void TileSet::update_tiles(unsigned long msec)
|
nuclear@46
|
76 {
|
nuclear@46
|
77 for(auto tilepair : tiles) {
|
nuclear@46
|
78 tilepair.second->update(msec, 0.0f);
|
nuclear@46
|
79 }
|
nuclear@46
|
80 }
|
nuclear@46
|
81
|
nuclear@5
|
82 void set_active_tileset(TileSet *set)
|
nuclear@5
|
83 {
|
nuclear@5
|
84 active_tileset = set;
|
nuclear@5
|
85 }
|
nuclear@5
|
86
|
nuclear@5
|
87 TileSet *get_active_tileset()
|
nuclear@5
|
88 {
|
nuclear@5
|
89 return active_tileset;
|
nuclear@5
|
90 }
|
nuclear@5
|
91
|
nuclear@5
|
92 static char *strip_space(char *ptr)
|
nuclear@5
|
93 {
|
nuclear@5
|
94 while(isspace(*ptr)) {
|
nuclear@5
|
95 ptr++;
|
nuclear@5
|
96 }
|
nuclear@5
|
97
|
nuclear@5
|
98 char *nl = strrchr(ptr, '\n');
|
nuclear@5
|
99 if(nl) *nl = 0;
|
nuclear@5
|
100 char *cr = strrchr(ptr, '\r');
|
nuclear@5
|
101 if(cr) *cr = 0;
|
nuclear@5
|
102
|
nuclear@5
|
103 return ptr;
|
nuclear@5
|
104 }
|