dungeon_crawler

annotate prototype/src/tileset.cc @ 63:7f52d6310317

fixed design issue with datafile_path
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 02 Oct 2012 04:52:59 +0300
parents aa9e28670ae2
children
rev   line source
nuclear@5 1 #include <stdio.h>
nuclear@63 2 #ifndef _MSC_VER
nuclear@63 3 #include <alloca.h>
nuclear@63 4 #else
nuclear@63 5 #include <malloc.h>
nuclear@63 6 #endif
nuclear@5 7 #include "tileset.h"
nuclear@5 8 #include "datapath.h"
nuclear@5 9
nuclear@5 10 static char *strip_space(char *ptr);
nuclear@5 11
nuclear@5 12 static TileSet *active_tileset;
nuclear@5 13
nuclear@48 14 TileSet::TileSet()
nuclear@48 15 : texset(load_texture, destroy_texture),
nuclear@48 16 sampleset(load_audio_sample, destroy_audio_sample)
nuclear@48 17 {
nuclear@48 18 }
nuclear@5 19
nuclear@5 20 TileSet::~TileSet()
nuclear@5 21 {
nuclear@5 22 for(auto iter : tiles) {
nuclear@5 23 delete iter.second;
nuclear@5 24 }
nuclear@5 25 }
nuclear@5 26
nuclear@5 27 bool TileSet::load(const char *fname)
nuclear@5 28 {
nuclear@5 29 FILE *fp;
nuclear@5 30 if(!(fp = fopen(fname, "r"))) {
nuclear@5 31 fprintf(stderr, "failed to open tileset: %s\n", fname);
nuclear@5 32 return false;
nuclear@5 33 }
nuclear@63 34 char *tmp = (char*)alloca(strlen(fname) + 1);
nuclear@63 35 strcpy(tmp, fname);
nuclear@63 36 fname = tmp;
nuclear@5 37
nuclear@5 38 int linenum = 0;
nuclear@5 39 char buf[512];
nuclear@5 40 while(fgets(buf, sizeof buf, fp)) {
nuclear@5 41 linenum++;
nuclear@5 42
nuclear@5 43 char *line = strip_space(buf);
nuclear@5 44 if(!*line || *line == '#') {
nuclear@5 45 continue;
nuclear@5 46 }
nuclear@5 47
nuclear@5 48 char *tilefile = strchr(line, ':');
nuclear@5 49 if(!tilefile) {
nuclear@5 50 fprintf(stderr, "error parsing tileset %s, line %d: %s\n", fname,
nuclear@5 51 linenum, line);
nuclear@5 52 fclose(fp);
nuclear@5 53 return false;
nuclear@5 54 }
nuclear@5 55 *tilefile++ = 0;
nuclear@5 56
nuclear@5 57 printf("Tileset %s, loading tile \"%s\" -> %s\n", fname, line, tilefile);
nuclear@11 58 Tile *tile = new Tile(this);
nuclear@63 59 if(!tile->load(datafile_path(tilefile).c_str())) {
nuclear@5 60 fprintf(stderr, "failed to load tile: %s\n", tilefile);
nuclear@5 61 delete tile;
nuclear@5 62 continue;
nuclear@5 63 }
nuclear@5 64
nuclear@5 65 tiles[line] = tile;
nuclear@5 66 }
nuclear@5 67
nuclear@5 68 fclose(fp);
nuclear@5 69 return true;
nuclear@5 70 }
nuclear@5 71
nuclear@11 72 TextureSet *TileSet::get_textures()
nuclear@11 73 {
nuclear@11 74 return &texset;
nuclear@11 75 }
nuclear@11 76
nuclear@11 77 const TextureSet *TileSet::get_textures() const
nuclear@11 78 {
nuclear@11 79 return &texset;
nuclear@11 80 }
nuclear@11 81
nuclear@48 82 SampleSet *TileSet::get_samples()
nuclear@48 83 {
nuclear@48 84 return &sampleset;
nuclear@48 85 }
nuclear@48 86
nuclear@48 87 const SampleSet *TileSet::get_samples() const
nuclear@48 88 {
nuclear@48 89 return &sampleset;
nuclear@48 90 }
nuclear@48 91
nuclear@5 92 Tile *TileSet::get_tile(const char *name) const
nuclear@5 93 {
nuclear@5 94 auto res = tiles.find(name);
nuclear@5 95 return res != tiles.end() ? res->second : 0;
nuclear@5 96 }
nuclear@5 97
nuclear@46 98 void TileSet::update_tiles(unsigned long msec)
nuclear@46 99 {
nuclear@46 100 for(auto tilepair : tiles) {
nuclear@46 101 tilepair.second->update(msec, 0.0f);
nuclear@46 102 }
nuclear@46 103 }
nuclear@46 104
nuclear@5 105 void set_active_tileset(TileSet *set)
nuclear@5 106 {
nuclear@5 107 active_tileset = set;
nuclear@5 108 }
nuclear@5 109
nuclear@5 110 TileSet *get_active_tileset()
nuclear@5 111 {
nuclear@5 112 return active_tileset;
nuclear@5 113 }
nuclear@5 114
nuclear@5 115 static char *strip_space(char *ptr)
nuclear@5 116 {
nuclear@5 117 while(isspace(*ptr)) {
nuclear@5 118 ptr++;
nuclear@5 119 }
nuclear@5 120
nuclear@5 121 char *nl = strrchr(ptr, '\n');
nuclear@5 122 if(nl) *nl = 0;
nuclear@5 123 char *cr = strrchr(ptr, '\r');
nuclear@5 124 if(cr) *cr = 0;
nuclear@5 125
nuclear@5 126 return ptr;
nuclear@5 127 }