added audio
[laserbrain_demo] / src / metascene.cc
index 2328f33..66be3ef 100644 (file)
@@ -22,6 +22,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(MetaScene *mscn, MaterialEdit *med, struct ts_node *node);
+static bool proc_music(MetaScene *mscn, struct ts_node *node);
 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);
@@ -32,11 +33,13 @@ static void print_scene_graph(SceneNode *n, int level);
 MetaScene::MetaScene()
 {
        walk_mesh = 0;
+       music = 0;
 }
 
 MetaScene::~MetaScene()
 {
        delete walk_mesh;
+       delete music;
 }
 
 
@@ -146,6 +149,9 @@ static bool proc_node(MetaScene *mscn, struct ts_node *node)
                if(match && replace) {
                        mscn->datamap.map(match, replace);
                }
+
+       } else if(strcmp(node->name, "music") == 0) {
+               return proc_music(mscn, node);
        }
 
        return true;
@@ -366,6 +372,37 @@ static void apply_mtledit(Scene *scn, const MaterialEdit &med)
        }
 }
 
+static bool proc_music(MetaScene *mscn, struct ts_node *node)
+{
+       const char *fname = ts_get_attr_str(node, "file");
+       if(fname) {
+               SceneData *sdat = new SceneData;
+               sdat->meta = mscn;
+
+               // datapath
+               struct ts_attr *adpath = attr_inscope(node, "datapath");
+               if(adpath && adpath->val.type == TS_STRING) {
+                       mscn->datamap.set_path(adpath->val.str);
+               }
+
+               int namesz = mscn->datamap.lookup(fname, 0, 0);
+               char *namebuf = (char*)alloca(namesz + 1);
+               if(mscn->datamap.lookup(fname, namebuf, namesz + 1)) {
+                       fname = namebuf;
+               }
+
+               OggVorbisStream *ovstream = new OggVorbisStream;
+               if(!ovstream->open(fname)) {
+                       delete ovstream;
+                       return false;
+               }
+
+               delete mscn->music;
+               mscn->music = ovstream;
+       }
+       return true;
+}
+
 static void apply_mtledit(Material *mtl, const MaterialEdit &med)
 {
        // TODO more edit modes...