#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();
return false;
}
- datamap_reset();
+ clear();
char *line;
int nline = 0;
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;
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)
#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_
#include <regex>
#include "metascene.h"
#include "scene.h"
-#include "datamap.h"
#include "treestore.h"
#include "logger.h"
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);
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);
}
}
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
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;
}
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;
}
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;
// 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
#include <map>
#include "scene.h"
#include "mesh.h"
+#include "datamap.h"
class MetaScene {
public:
SceneSet *sceneman;
TextureSet *texman;
+ DataMap datamap;
std::vector<Scene*> scenes;
#include "snode.h"
#include "texture.h"
#include "dataset.h"
+#include "datamap.h"
enum {
SCNLOAD_FLIPYZ = 1,
public:
MetaScene *metascn;
+ DataMap datamap;
// meshes objects and nodes are owned by Scene
std::vector<Mesh*> meshes;
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);
{
}
-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;
}
#define TEXTURE_H_
#include "dataset.h"
+#include "datamap.h"
#include "opengl.h"
class Image;
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_