13 static bool proc_node(Scene *scn, struct ts_node *node);
14 static bool proc_scenefile(Scene *scn, struct ts_node *node);
15 static struct ts_attr *attr_inscope(struct ts_node *node, const char *name);
17 static void print_scene_graph(SceneNode *n, int level);
19 bool load_scene(Scene *scn, const char *fname)
21 struct ts_node *root = ts_load(fname);
22 if(!root || strcmp(root->name, "scene") != 0) {
24 error_log("failed to load scene metadata: %s\n", fname);
28 bool res = proc_node(scn, root);
31 info_log("loaded scene: %s\n", fname);
32 info_log("scene graph:\n");
33 print_scene_graph(scn->nodes, 0);
37 static bool proc_node(Scene *scn, struct ts_node *node)
39 struct ts_node *c = node->child_list;
41 if(!proc_node(scn, c)) {
47 // do this last to allow other contents of the node to do their thing
48 if(strcmp(node->name, "scenefile") == 0) {
49 return proc_scenefile(scn, node);
51 } else if(strcmp(node->name, "remap") == 0) {
52 const char *match = ts_get_attr_str(node, "match");
53 const char *replace = ts_get_attr_str(node, "replace");
54 if(match && replace) {
55 datamap_map(match, replace);
62 static bool proc_scenefile(Scene *scn, struct ts_node *node)
64 const char *fname = ts_get_attr_str(node, "file");
67 struct ts_attr *adpath = attr_inscope(node, "datapath");
68 if(adpath && adpath->val.type == TS_STRING) {
69 info_log("adding data path: %s\n", adpath->val.str);
70 datamap_set_path(adpath->val.str);
74 char *walkmesh_regexp = 0;
75 struct ts_attr *awmesh = attr_inscope(node, "walkmesh");
76 if(awmesh && awmesh->val.type == TS_STRING) {
77 walkmesh_regexp = awmesh->val.str;
80 int namesz = datamap_lookup(fname, 0, 0);
81 char *namebuf = (char*)alloca(namesz + 1);
82 if(datamap_lookup(fname, namebuf, namesz + 1)) {
86 if(!(scn->load(fname, SCNLOAD_FLIPTEX))) {
90 // extract the walk mesh if necessary
91 // XXX as written, this will match even objects loaded by previous scenefile blocks
93 if(walkmesh_regexp && (wscn = scn->extract_nodes(walkmesh_regexp))) {
94 // apply all transformations to the meshes
97 int nmeshes = wscn->meshes.size();
98 for(int i=0; i<nmeshes; i++) {
99 Mesh *m = wscn->meshes[i];
102 scn->walk_mesh->append(*m);
105 wscn->remove_mesh(m); // to save it from destruction
117 static struct ts_attr *attr_inscope(struct ts_node *node, const char *name)
119 struct ts_attr *attr = 0;
121 while(node && !(attr = ts_get_attr(node, name))) {
127 static void print_scene_graph(SceneNode *n, int level)
131 for(int i=0; i<level; i++) {
135 int nobj = n->get_num_objects();
137 info_log("%s - %d obj\n", n->get_name(), n->get_num_objects());
139 info_log("%s\n", n->get_name());
142 for(int i=0; i<n->get_num_children(); i++) {
143 print_scene_graph(n->get_child(i), level + 1);