dungeon_crawler

annotate prototype/src/tileset.cc @ 48:aa9e28670ae2

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