From b30241a8a51be904b22459a1d0cc3322e0a505d9 Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Sat, 3 Dec 2016 05:50:36 +0200 Subject: [PATCH] datamap object passed around while loading --- src/datamap.cc | 31 ++++++++++--------------------- src/datamap.h | 24 ++++++++++++++++++------ src/metascene.cc | 22 ++++++++++++---------- src/metascene.h | 2 ++ src/scene.h | 2 ++ src/sceneload.cc | 2 +- src/texture.cc | 6 +++--- src/texture.h | 3 ++- 8 files changed, 50 insertions(+), 42 deletions(-) diff --git a/src/datamap.cc b/src/datamap.cc index ee7de51..e466082 100644 --- a/src/datamap.cc +++ b/src/datamap.cc @@ -1,10 +1,6 @@ #include #include #include -#include -#include -#include -//#include #include "datamap.h" #ifdef WIN32 @@ -15,23 +11,18 @@ static char *clean_line(char *s); -//static std::vector> dmap; -static std::vector> dmap; -static std::map cache; -static std::string root; - -void datamap_reset() +void DataMap::clear() { dmap.clear(); cache.clear(); } -void datamap_set_path(const char *path) +void DataMap::set_path(const char *path) { root = std::string(path); } -bool datamap_load_map(const char *fname) +bool DataMap::load_map(const char *fname) { std::string path = root.empty() ? fname : root + std::string("/") + fname; fname = path.c_str(); @@ -49,7 +40,7 @@ bool datamap_load_map(const char *fname) return false; } - datamap_reset(); + clear(); char *line; int nline = 0; @@ -83,22 +74,20 @@ bool datamap_load_map(const char *fname) err: fprintf(stderr, "error while parsing %s, invalid line %d: %s\n", fname, nline, line); - datamap_reset(); + clear(); fclose(fp); return false; } -void datamap_map(const char *re, const char *path) +void DataMap::map(const char *match, const char *path) { - //std::pair mapping; - //mapping.first = std::regex(re); std::pair mapping; - mapping.first = std::string(re); + mapping.first = std::string(match); mapping.second = std::string(path); dmap.push_back(std::move(mapping)); } -int datamap_lookup(const char *in, char *buf, int bsz) +int DataMap::lookup(const char *in, char *buf, int bsz) const { std::string res; @@ -135,9 +124,9 @@ int datamap_lookup(const char *in, char *buf, int bsz) return res.length() + 1; } -int datamap_path_size(const char *in) +int DataMap::path_size(const char *in) const { - return datamap_lookup(in, 0, 0); + return lookup(in, 0, 0); } static char *clean_line(char *s) diff --git a/src/datamap.h b/src/datamap.h index 47e7d49..137fdca 100644 --- a/src/datamap.h +++ b/src/datamap.h @@ -1,13 +1,25 @@ #ifndef DATAMAP_H_ #define DATAMAP_H_ -void datamap_reset(); -void datamap_set_path(const char *path); +#include +#include +#include -bool datamap_load_map(const char *fname); -void datamap_map(const char *re, const char *path); +class DataMap { + std::vector> dmap; + mutable std::map cache; + std::string root; -int datamap_lookup(const char *in, char *buf, int bsz); -int datamap_path_size(const char *in); +public: + void clear(); + + void set_path(const char *path); + + bool load_map(const char *fname); + void map(const char *match, const char *path); + + int lookup(const char *in, char *buf, int bsz) const; + int path_size(const char *in) const; +}; #endif // DATAMAP_H_ diff --git a/src/metascene.cc b/src/metascene.cc index 21e9093..66e0027 100644 --- a/src/metascene.cc +++ b/src/metascene.cc @@ -2,7 +2,6 @@ #include #include "metascene.h" #include "scene.h" -#include "datamap.h" #include "treestore.h" #include "logger.h" @@ -20,7 +19,7 @@ struct MaterialEdit { static bool proc_node(MetaScene *mscn, struct ts_node *node); static bool proc_scenefile(MetaScene *mscn, struct ts_node *node); -static bool proc_mtledit(MaterialEdit *med, struct ts_node *node, TextureSet *texset); +static bool proc_mtledit(MetaScene *mscn, MaterialEdit *med, struct ts_node *node, TextureSet *texset); static void apply_mtledit(Scene *scn, const MaterialEdit &med); static void apply_mtledit(Material *mtl, const MaterialEdit &med); static struct ts_attr *attr_inscope(struct ts_node *node, const char *name); @@ -94,7 +93,7 @@ static bool proc_node(MetaScene *mscn, struct ts_node *node) const char *match = ts_get_attr_str(node, "match"); const char *replace = ts_get_attr_str(node, "replace"); if(match && replace) { - datamap_map(match, replace); + mscn->datamap.map(match, replace); } } @@ -118,7 +117,7 @@ static bool proc_scenefile(MetaScene *mscn, struct ts_node *node) struct ts_attr *adpath = attr_inscope(node, "datapath"); if(adpath && adpath->val.type == TS_STRING) { info_log("adding data path: %s\n", adpath->val.str); - datamap_set_path(adpath->val.str); + mscn->datamap.set_path(adpath->val.str); } // walkmesh @@ -127,9 +126,9 @@ static bool proc_scenefile(MetaScene *mscn, struct ts_node *node) sdat->walkmesh_regexp = std::string(awmesh->val.str); } - int namesz = datamap_lookup(fname, 0, 0); + int namesz = mscn->datamap.lookup(fname, 0, 0); char *namebuf = (char*)alloca(namesz + 1); - if(datamap_lookup(fname, namebuf, namesz + 1)) { + if(mscn->datamap.lookup(fname, namebuf, namesz + 1)) { fname = namebuf; } @@ -137,17 +136,20 @@ static bool proc_scenefile(MetaScene *mscn, struct ts_node *node) struct ts_node *child = node->child_list; while(child) { MaterialEdit medit; - if(proc_mtledit(&medit, child, mscn->texman)) { + if(proc_mtledit(mscn, &medit, child, mscn->texman)) { sdat->mtledit.push_back(medit); } child = child->next; } Scene *newscn = mscn->sceneman->get(fname); - /* NOTE: setting this after get() is not a race condition, because + /* NOTE: setting all these after get() is not a race condition, because * scene_loaded() which uses this, will only run in our main loop during * SceneSet::update() on the main thread. */ + newscn->datamap = mscn->datamap; + mscn->datamap.clear(); + newscn->metascn = mscn; mscn->scndata[newscn] = sdat; } @@ -193,7 +195,7 @@ bool MetaScene::scene_loaded(Scene *newscn) return true; } -static bool proc_mtledit(MaterialEdit *med, struct ts_node *node, TextureSet *texset) +static bool proc_mtledit(MetaScene *mscn, MaterialEdit *med, struct ts_node *node, TextureSet *texset) { if(strcmp(node->name, "mtledit") != 0) { return false; @@ -239,7 +241,7 @@ static bool proc_mtledit(MaterialEdit *med, struct ts_node *node, TextureSet *te // remove med->tex = 0; } else { - med->tex = texset->get_texture(afile->val.str, TEX_2D); + med->tex = texset->get_texture(afile->val.str, TEX_2D, &mscn->datamap); } } // TODO add more edit modes diff --git a/src/metascene.h b/src/metascene.h index 5d57ee0..00960ca 100644 --- a/src/metascene.h +++ b/src/metascene.h @@ -4,11 +4,13 @@ #include #include "scene.h" #include "mesh.h" +#include "datamap.h" class MetaScene { public: SceneSet *sceneman; TextureSet *texman; + DataMap datamap; std::vector scenes; diff --git a/src/scene.h b/src/scene.h index 7489261..b9e338b 100644 --- a/src/scene.h +++ b/src/scene.h @@ -7,6 +7,7 @@ #include "snode.h" #include "texture.h" #include "dataset.h" +#include "datamap.h" enum { SCNLOAD_FLIPYZ = 1, @@ -24,6 +25,7 @@ private: public: MetaScene *metascn; + DataMap datamap; // meshes objects and nodes are owned by Scene std::vector meshes; diff --git a/src/sceneload.cc b/src/sceneload.cc index 7e9cade..ad6e61e 100644 --- a/src/sceneload.cc +++ b/src/sceneload.cc @@ -202,7 +202,7 @@ static bool load_material(Scene *scn, Material *mat, const aiMaterial *aimat) int textype = assimp_textype(aitype); info_log("loading %s texture: %s\n", assimp_textypestr(aitype), fname); - Texture *tex = scn->texset->get_texture(fname, TEX_2D); + Texture *tex = scn->texset->get_texture(fname, TEX_2D, &scn->datamap); assert(tex); mat->textures.push_back(tex); diff --git a/src/texture.cc b/src/texture.cc index b3c17fe..3aebec3 100644 --- a/src/texture.cc +++ b/src/texture.cc @@ -468,13 +468,13 @@ TextureSet::TextureSet() { } -Texture *TextureSet::get_texture(const char *name, TextureType type) const +Texture *TextureSet::get_texture(const char *name, TextureType type, const DataMap *dmap) const { char *fname; - int nsize = datamap_path_size(name); + int nsize = dmap ? dmap->path_size(name) : 0; if(nsize) { fname = (char*)alloca(nsize); - datamap_lookup(name, fname, nsize); + dmap->lookup(name, fname, nsize); } else { fname = (char*)name; } diff --git a/src/texture.h b/src/texture.h index 09b2c91..5e18e7a 100644 --- a/src/texture.h +++ b/src/texture.h @@ -2,6 +2,7 @@ #define TEXTURE_H_ #include "dataset.h" +#include "datamap.h" #include "opengl.h" class Image; @@ -68,7 +69,7 @@ private: public: TextureSet(); - Texture *get_texture(const char *name, TextureType type = TEX_2D) const; + Texture *get_texture(const char *name, TextureType type = TEX_2D, const DataMap *dmap = 0) const; }; #endif // TEXTURE_H_ -- 1.7.10.4