datamap object passed around while loading
authorJohn Tsiombikas <nuclear@mutantstargoat.com>
Sat, 3 Dec 2016 03:50:36 +0000 (05:50 +0200)
committerJohn Tsiombikas <nuclear@mutantstargoat.com>
Sat, 3 Dec 2016 03:50:36 +0000 (05:50 +0200)
src/datamap.cc
src/datamap.h
src/metascene.cc
src/metascene.h
src/scene.h
src/sceneload.cc
src/texture.cc
src/texture.h

index ee7de51..e466082 100644 (file)
@@ -1,10 +1,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <vector>
-#include <map>
-#include <string>
-//#include <regex>
 #include "datamap.h"
 
 #ifdef WIN32
 
 static char *clean_line(char *s);
 
-//static std::vector<std::pair<std::regex, std::string>> dmap;
-static std::vector<std::pair<std::string, std::string>> dmap;
-static std::map<std::string, std::string> 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<std::regex, std::string> mapping;
-       //mapping.first = std::regex(re);
        std::pair<std::string, std::string> 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)
index 47e7d49..137fdca 100644 (file)
@@ -1,13 +1,25 @@
 #ifndef DATAMAP_H_
 #define DATAMAP_H_
 
-void datamap_reset();
-void datamap_set_path(const char *path);
+#include <vector>
+#include <map>
+#include <string>
 
-bool datamap_load_map(const char *fname);
-void datamap_map(const char *re, const char *path);
+class DataMap {
+       std::vector<std::pair<std::string, std::string>> dmap;
+       mutable std::map<std::string, std::string> 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_
index 21e9093..66e0027 100644 (file)
@@ -2,7 +2,6 @@
 #include <regex>
 #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
index 5d57ee0..00960ca 100644 (file)
@@ -4,11 +4,13 @@
 #include <map>
 #include "scene.h"
 #include "mesh.h"
+#include "datamap.h"
 
 class MetaScene {
 public:
        SceneSet *sceneman;
        TextureSet *texman;
+       DataMap datamap;
 
        std::vector<Scene*> scenes;
 
index 7489261..b9e338b 100644 (file)
@@ -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<Mesh*> meshes;
index 7e9cade..ad6e61e 100644 (file)
@@ -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);
 
index b3c17fe..3aebec3 100644 (file)
@@ -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;
        }
index 09b2c91..5e18e7a 100644 (file)
@@ -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_