diff TaskManager/Test/test_render/SceneGraphRoot.cpp @ 196:932a05a7a1db

add SceneGraphRoot, tools/create_sglist.pl
author gongo@gendarme.cr.ie.u-ryukyu.ac.jp
date Sun, 25 Jan 2009 17:46:37 +0900
parents
children eb20274baa7c
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TaskManager/Test/test_render/SceneGraphRoot.cpp	Sun Jan 25 17:46:37 2009 +0900
@@ -0,0 +1,191 @@
+#include <SDL.h>
+#include <SDL_image.h>
+#include <libxml/parser.h>
+#include "SceneGraphRoot.h"
+#include "xml.h"
+#include "sys.h"
+#include "TextureHash.h"
+#include "texture.h"
+#include "SGList.h"
+
+SceneGraphRoot::SceneGraphRoot(void)
+{
+    sg_src = new SceneGraphPtr[SGLIST_LENGTH];
+    sg_exec_list = NULL;
+    sg_draw_list = NULL;
+    sg_available_list = NULL;
+}
+
+SceneGraphRoot::~SceneGraphRoot(void)
+{
+    SceneGraphPtr p = sg_available_list;
+
+    while (p) {
+	SceneGraphPtr tmp = p->next;
+	delete p;
+	p = tmp;
+    }
+
+    delete [] sg_src;
+}
+
+void
+SceneGraphRoot::registSceneGraph(SceneGraphPtr sg)
+{
+    for (int i = 0; i < SGLIST_LENGTH; i++) {
+	if (strcmp(sg->name, sglist_table[i]) == 0) {
+	    sg_src[i] = sg;
+	    return;
+	}
+    }
+
+    fprintf(stderr, "error: (%s:%3d) Can't find Scene \"%s\"\n",
+	    __FUNCTION__, __LINE__, sg->name);
+}
+
+void
+SceneGraphRoot::addNext(SceneGraphPtr sg)
+{
+    SceneGraphPtr last = sg_available_list;
+
+    if (!last) {
+	sg_available_list = sg;
+    } else {
+	while (last->next) {
+	    last = last->next;
+	}
+	last->next = sg;
+	sg->prev = last;
+    }
+}
+
+/* XMLファイルからポリゴンを作成  */
+void
+SceneGraphRoot::createFromXMLfile(const char *xmlfile)
+{
+    xmlDocPtr doc;
+    xmlNodePtr cur;
+    SceneGraphPtr tmp;
+    
+    /* パース DOM生成 */
+    doc = xmlParseFile(xmlfile);
+    cur = xmlDocGetRootElement(doc);
+
+    /* ??  */
+    xmlStrcmp(cur->name,(xmlChar*)"OBJECT-3D");
+
+    /* XMLのノードを一つずつ解析  */
+    for (cur=cur->children; cur; cur=cur->next) {
+	/* 扱うのはsurfaceオンリー  */
+	if (xmlStrcmp(cur->name,(xmlChar*)"surface") != 0) {
+	    continue;
+	}
+
+	/* ポリゴン(SceneGraph)生成  */
+	tmp = new SceneGraph(cur);
+
+	registSceneGraph(tmp);
+    }
+
+    xmlFreeDoc(doc);
+}
+
+SceneGraphPtr
+SceneGraphRoot::createSceneGraph(int id)
+{
+    SceneGraphPtr src;
+    SceneGraphPtr p;
+
+    if (id < 0 || id > SGLIST_LENGTH) {
+	return NULL;
+    }
+
+    /* オリジナルの SceneGraph */
+    src = sg_src[id];
+
+    /* ユーザーにはオリジナルの clone を返す */
+    p = src->clone();
+
+    addNext(p);
+
+    return p;
+}
+
+void
+SceneGraphRoot::allExecute(int screen_w, int screen_h)
+{
+    SceneGraphPtr top = sg_exec_list;
+    SceneGraphPtr t = top;
+
+    while (t) {
+	t->move_execute(screen_w, screen_h);
+	t->collision_check(screen_w, screen_h, top);
+
+	t->frame++;
+
+	if (t->parent != NULL) {
+	    get_matrix(t->matrix, t->angle, t->xyz, t->parent->matrix);
+	} else {
+	    get_matrix(t->matrix, t->angle, t->xyz, NULL);
+	}
+
+	if (t->children != NULL) {
+	    t = t->children;
+	} else if (t->brother != NULL) {
+	    t = t->brother;
+	} else {
+	    while (t) {
+		if (t->brother != NULL) {
+		    t = t->brother;
+		    break;
+		} else {
+		    if (t->parent == NULL) {
+			t = NULL;
+			break;
+		    } else {
+			t = t->parent;
+		    }
+		}
+	    }
+	}
+    }
+}
+
+void
+SceneGraphRoot::checkRemove(void)
+{
+    SceneGraphPtr p = sg_available_list;
+    SceneGraphPtr p1;
+
+    while (p) {
+	p1 = p->next;
+	if (p->isRemoved()) {
+	    p->realRemove();
+	}
+	p = p1;
+    }
+}
+
+SceneGraphPtr
+SceneGraphRoot::getExecuteSceneGraph(void)
+{
+    return sg_exec_list;
+}
+
+SceneGraphPtr
+SceneGraphRoot::getDrawSceneGraph(void)
+{
+    return sg_draw_list;
+}
+
+void
+SceneGraphRoot::updateControllerState(void)
+{
+    controller->check();
+}
+
+void
+SceneGraphRoot::setSceneData(SceneGraphPtr sg)
+{
+    sg_draw_list = sg_exec_list = sg;
+}