dungeon_crawler
changeset 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 | f71381c9e245 |
children | 0b130c6e534d |
files | prototype/src/cmdcon.cc prototype/src/datapath.cc prototype/src/datapath.h prototype/src/dataset.h prototype/src/main.cc prototype/src/renderer.cc prototype/src/renderer_deferred.cc prototype/src/renderer_multipass.cc prototype/src/tile.cc prototype/src/tileset.cc |
diffstat | 10 files changed, 52 insertions(+), 61 deletions(-) [+] |
line diff
1.1 --- a/prototype/src/cmdcon.cc Tue Sep 25 06:59:11 2012 +0300 1.2 +++ b/prototype/src/cmdcon.cc Tue Oct 02 04:52:59 2012 +0300 1.3 @@ -19,15 +19,15 @@ 1.4 1.5 bool init_cmdcon() 1.6 { 1.7 - const char *path = datafile_path(FONT_FILENAME); 1.8 - if(!path) { 1.9 + std::string path = datafile_path(FONT_FILENAME); 1.10 + if(path.empty()) { 1.11 fprintf(stderr, "failed to locate font file: %s\n", FONT_FILENAME); 1.12 return false; 1.13 } 1.14 1.15 - printf("loading font: %s\n", path); 1.16 - if(!(font = dtx_open_font(path, 14))) { 1.17 - fprintf(stderr, "failed to open font file: %s\n", path); 1.18 + printf("loading font: %s\n", path.c_str()); 1.19 + if(!(font = dtx_open_font(path.c_str(), 14))) { 1.20 + fprintf(stderr, "failed to open font file: %s\n", path.c_str()); 1.21 return false; 1.22 } 1.23 dtx_use_font(font, 14);
2.1 --- a/prototype/src/datapath.cc Tue Sep 25 06:59:11 2012 +0300 2.2 +++ b/prototype/src/datapath.cc Tue Oct 02 04:52:59 2012 +0300 2.3 @@ -10,12 +10,11 @@ 2.4 pathlist.push_back(path); 2.5 } 2.6 2.7 -const char *datafile_path(const char *fname) 2.8 +std::string datafile_path(const char *fname) 2.9 { 2.10 - static std::string res; 2.11 - 2.12 + std::string res; 2.13 if(!fname) { 2.14 - return 0; 2.15 + return res; 2.16 } 2.17 2.18 for(auto path : pathlist) { 2.19 @@ -23,8 +22,8 @@ 2.20 FILE *fp = fopen(res.c_str(), "r"); 2.21 if(fp) { 2.22 fclose(fp); 2.23 - return res.c_str(); 2.24 + return res; 2.25 } 2.26 } 2.27 - return 0; 2.28 + return std::string(fname); 2.29 }
3.1 --- a/prototype/src/datapath.h Tue Sep 25 06:59:11 2012 +0300 3.2 +++ b/prototype/src/datapath.h Tue Oct 02 04:52:59 2012 +0300 3.3 @@ -1,8 +1,10 @@ 3.4 #ifndef DATAPATH_H_ 3.5 #define DATAPATH_H_ 3.6 3.7 +#include <string> 3.8 + 3.9 void add_data_path(const char *path); 3.10 3.11 -const char *datafile_path(const char *fname); 3.12 +std::string datafile_path(const char *fname); 3.13 3.14 #endif // DATAPATH_H_
4.1 --- a/prototype/src/dataset.h Tue Sep 25 06:59:11 2012 +0300 4.2 +++ b/prototype/src/dataset.h Tue Oct 02 04:52:59 2012 +0300 4.3 @@ -53,7 +53,7 @@ 4.4 } else { 4.5 path = name; 4.6 } 4.7 - if(!(path = datafile_path(path))) { 4.8 + if(!(path = datafile_path(path).c_str()) || !*path) { 4.9 fprintf(stderr, "can't find data file: %s\n", name); 4.10 return 0; 4.11 }
5.1 --- a/prototype/src/main.cc Tue Sep 25 06:59:11 2012 +0300 5.2 +++ b/prototype/src/main.cc Tue Oct 02 04:52:59 2012 +0300 5.3 @@ -109,7 +109,7 @@ 5.4 move_sound->set_volume(0.4); 5.5 5.6 music = new OggVorbisStream; 5.7 - if(music->open(datafile_path("bgtrack.ogg"))) { 5.8 + if(music->open(datafile_path("bgtrack.ogg").c_str())) { 5.9 music->set_volume(0.6); 5.10 music->play(PlayMode::loop); 5.11 } else { 5.12 @@ -175,14 +175,14 @@ 5.13 // load a tileset 5.14 tileset = new TileSet; 5.15 printf("loading tileset: %s\n", cfg.tileset_file); 5.16 - if(!tileset->load(datafile_path(cfg.tileset_file))) { 5.17 + if(!tileset->load(datafile_path(cfg.tileset_file).c_str())) { 5.18 return false; 5.19 } 5.20 set_active_tileset(tileset); 5.21 5.22 level = new Level; 5.23 printf("loading level: %s\n", cfg.level_file); 5.24 - if(!level->load(datafile_path(cfg.level_file))) { 5.25 + if(!level->load(datafile_path(cfg.level_file).c_str())) { 5.26 return false; 5.27 } 5.28
6.1 --- a/prototype/src/renderer.cc Tue Sep 25 06:59:11 2012 +0300 6.2 +++ b/prototype/src/renderer.cc Tue Oct 02 04:52:59 2012 +0300 6.3 @@ -114,22 +114,16 @@ 6.4 6.5 static unsigned int load_sdr(const char *vfname, const char *pfname) 6.6 { 6.7 - char vsfile[PATH_MAX], psfile[PATH_MAX]; 6.8 - const char *fname; 6.9 unsigned int prog; 6.10 6.11 - if((fname = datafile_path(vfname))) { 6.12 - strcpy(vsfile, fname); 6.13 - } else { 6.14 - vsfile[0] = 0; 6.15 - } 6.16 - if((fname = datafile_path(pfname))) { 6.17 - strcpy(psfile, fname); 6.18 - } else { 6.19 - psfile[0] = 0; 6.20 - } 6.21 - if(!(prog = create_program_load(vsfile, psfile))) { 6.22 - fprintf(stderr, "failed to load shader program (%s, %s)\n", vsfile, psfile); 6.23 + std::string vsfile = datafile_path(vfname); 6.24 + std::string psfile = datafile_path(pfname); 6.25 + 6.26 + const char *vs = vsfile.empty() ? 0 : vsfile.c_str(); 6.27 + const char *ps = psfile.empty() ? 0 : psfile.c_str(); 6.28 + 6.29 + if(!(prog = create_program_load(vs, ps))) { 6.30 + fprintf(stderr, "failed to load shader program (%s, %s)\n", vs, ps); 6.31 return 0; 6.32 } 6.33 return prog;
7.1 --- a/prototype/src/renderer_deferred.cc Tue Sep 25 06:59:11 2012 +0300 7.2 +++ b/prototype/src/renderer_deferred.cc Tue Oct 02 04:52:59 2012 +0300 7.3 @@ -255,22 +255,16 @@ 7.4 7.5 static unsigned int load_sdr(const char *vfname, const char *pfname) 7.6 { 7.7 - char vsfile[PATH_MAX], psfile[PATH_MAX]; 7.8 - const char *fname; 7.9 unsigned int prog; 7.10 7.11 - if((fname = datafile_path(vfname))) { 7.12 - strcpy(vsfile, fname); 7.13 - } else { 7.14 - vsfile[0] = 0; 7.15 - } 7.16 - if((fname = datafile_path(pfname))) { 7.17 - strcpy(psfile, fname); 7.18 - } else { 7.19 - psfile[0] = 0; 7.20 - } 7.21 - if(!(prog = create_program_load(vsfile, psfile))) { 7.22 - fprintf(stderr, "failed to load shader program (%s, %s)\n", vsfile, psfile); 7.23 + std::string vsfile = datafile_path(vfname); 7.24 + std::string psfile = datafile_path(pfname); 7.25 + 7.26 + const char *vs = vsfile.empty() ? 0 : vsfile.c_str(); 7.27 + const char *ps = psfile.empty() ? 0 : psfile.c_str(); 7.28 + 7.29 + if(!(prog = create_program_load(vs, ps))) { 7.30 + fprintf(stderr, "failed to load shader program (%s, %s)\n", vs, ps); 7.31 return 0; 7.32 } 7.33 return prog;
8.1 --- a/prototype/src/renderer_multipass.cc Tue Sep 25 06:59:11 2012 +0300 8.2 +++ b/prototype/src/renderer_multipass.cc Tue Oct 02 04:52:59 2012 +0300 8.3 @@ -102,22 +102,16 @@ 8.4 8.5 static unsigned int load_sdr(const char *vfname, const char *pfname) 8.6 { 8.7 - char vsfile[PATH_MAX], psfile[PATH_MAX]; 8.8 - const char *fname; 8.9 unsigned int prog; 8.10 8.11 - if((fname = datafile_path(vfname))) { 8.12 - strcpy(vsfile, fname); 8.13 - } else { 8.14 - vsfile[0] = 0; 8.15 - } 8.16 - if((fname = datafile_path(pfname))) { 8.17 - strcpy(psfile, fname); 8.18 - } else { 8.19 - psfile[0] = 0; 8.20 - } 8.21 - if(!(prog = create_program_load(vsfile, psfile))) { 8.22 - fprintf(stderr, "failed to load shader program (%s, %s)\n", vsfile, psfile); 8.23 + std::string vsfile = datafile_path(vfname); 8.24 + std::string psfile = datafile_path(pfname); 8.25 + 8.26 + const char *vs = vsfile.empty() ? 0 : vsfile.c_str(); 8.27 + const char *ps = psfile.empty() ? 0 : psfile.c_str(); 8.28 + 8.29 + if(!(prog = create_program_load(vs, ps))) { 8.30 + fprintf(stderr, "failed to load shader program (%s, %s)\n", vs, ps); 8.31 return 0; 8.32 } 8.33 return prog;
9.1 --- a/prototype/src/tile.cc Tue Sep 25 06:59:11 2012 +0300 9.2 +++ b/prototype/src/tile.cc Tue Oct 02 04:52:59 2012 +0300 9.3 @@ -155,7 +155,7 @@ 9.4 { 9.5 for(size_t i=0; i<lights.size(); i++) { 9.6 if(light_side[i] & draw_mask) { 9.7 - printf("rendering light ...\n"); 9.8 + //printf("rendering light ...\n"); 9.9 lights[i]->draw(); 9.10 } 9.11 } 9.12 @@ -273,7 +273,7 @@ 9.13 9.14 // ... ALSO add a fire particle system :) save me jebus 9.15 struct psys_emitter *ps = psys_create(); 9.16 - if(ps && psys_load_attr(&ps->attr, datafile_path("fire.psys")) == 0) { 9.17 + if(ps && psys_load_attr(&ps->attr, datafile_path("fire.psys").c_str()) == 0) { 9.18 Vector3 lpos = lt->get_position(); 9.19 psys_set_pos(ps, v3_cons(lpos.x, lpos.y + 0.01, lpos.z), 0); 9.20 psys_global.push_back(ps);
10.1 --- a/prototype/src/tileset.cc Tue Sep 25 06:59:11 2012 +0300 10.2 +++ b/prototype/src/tileset.cc Tue Oct 02 04:52:59 2012 +0300 10.3 @@ -1,4 +1,9 @@ 10.4 #include <stdio.h> 10.5 +#ifndef _MSC_VER 10.6 +#include <alloca.h> 10.7 +#else 10.8 +#include <malloc.h> 10.9 +#endif 10.10 #include "tileset.h" 10.11 #include "datapath.h" 10.12 10.13 @@ -26,6 +31,9 @@ 10.14 fprintf(stderr, "failed to open tileset: %s\n", fname); 10.15 return false; 10.16 } 10.17 + char *tmp = (char*)alloca(strlen(fname) + 1); 10.18 + strcpy(tmp, fname); 10.19 + fname = tmp; 10.20 10.21 int linenum = 0; 10.22 char buf[512]; 10.23 @@ -48,7 +56,7 @@ 10.24 10.25 printf("Tileset %s, loading tile \"%s\" -> %s\n", fname, line, tilefile); 10.26 Tile *tile = new Tile(this); 10.27 - if(!tile->load(datafile_path(tilefile))) { 10.28 + if(!tile->load(datafile_path(tilefile).c_str())) { 10.29 fprintf(stderr, "failed to load tile: %s\n", tilefile); 10.30 delete tile; 10.31 continue;