changeset 883:756f76293237

merge
author tkaito
date Mon, 12 Jul 2010 04:03:14 +0900
parents f525427ae30f (current diff) c81744aaa3ef (diff)
children 7c5a30983efa
files
diffstat 2 files changed, 94 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/Renderer/Test/aquarium.cc	Mon Jul 12 04:02:14 2010 +0900
+++ b/Renderer/Test/aquarium.cc	Mon Jul 12 04:03:14 2010 +0900
@@ -12,10 +12,9 @@
 
 #define GET_SERIAL_ID 65535
 
-int aquarium::last_player_id = 1;
+int aquarium::last_player_id = 0;
 
 Viewer *aquarium::sgroot;
-SceneGraphPtr aquarium::parent;
 linda_t aquarium::linda_addr = { "localhost", 10000 };
 int aquarium::linda;
 int aquarium::serial_id;
@@ -42,65 +41,73 @@
 {
 }
 
-static void
-set_position(SceneGraphPtr node, unsigned char *reply) {
+void
+aquarium::update_last_player_id() {
+	last_player_id++;
+	if (last_player_id == serial_id)
+		last_player_id++;
+}
+
+void
+aquarium::set_position(SceneGraphPtr node, unsigned char *reply) {
+	aqua::Position *pos = new aqua::Position();
+	pos->ParseFromArray(reply + LINDA_HEADER_SIZE, psx_get_datalength(reply));
+	node->stack_xyz[0] = node->xyz[0] = pos->x() - start_x;
+	node->stack_xyz[1] = node->xyz[1] = pos->y();
+
+	delete pos;
 }
 
 static void
 update_position_move(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h)
 {
-
-    SceneGraphRoot *sgroot = (SceneGraphRoot *)sgroot_;
 	// LindaServerから座標データを取得してオブジェクトに反映させる。
-	
 	if (!node->resend_flag || node->seq_rd != node->seq) {
 		unsigned char *reply_rd = psx_reply(node->seq_rd);
 		if (reply_rd != NULL) {
-			set_position(node, reply_rd);
-			free(reply_rd);
+			aquarium::set_position(node, reply_rd);
+			psx_free(reply_rd);
 			return;
 		}
 	}
 	unsigned char *reply = psx_reply(node->seq);
 	if (reply != NULL) {
-		set_position(node, reply);
-		free(reply);
-		node->seq = psx_wait_rd(sgroot->tid, node->id * 10 + 1);
+//		aquarium::set_position(node, reply);
+		psx_free(reply);
+		node->seq = psx_wait_rd(aquarium::linda, node->id * 10 + 1);
 		node->resend_flag = true;
 	} else if (node->resend_flag) {
-		node->seq_rd = psx_rd(sgroot->tid, node->id * 10 + 1);
+		node->seq_rd = psx_rd(aquarium::linda, node->id * 10 + 1);
 		node->resend_flag = false;
 	}
 }
 
 SceneGraphPtr
-create_sg(Viewer *viewer, SceneGraphPtr parent, unsigned char *data, int len, int serial_id)
+create_sg(Viewer *viewer, SceneGraphPtr par, unsigned char *data, int len, int serial_id)
 {
     SceneGraphPtr child = viewer->sgroot->createSceneGraph();
-    parent->addChild(child);
     viewer->sgroot->createFromXMLmemory(viewer->sgroot->tmanager, child, (char *)data, len);
-    child->set_move_collision(update_position_move, null_collision); // TODO:
+    child->set_move_collision(update_position_move, null_collision);
     child->id = serial_id;
 	child->seq = psx_wait_rd(aquarium::linda, serial_id * 10 + 1);
 	child->seq_rd = psx_rd(aquarium::linda, serial_id * 10 + 1);
 	child->resend_flag = false;
+    par->addChild(child);
     return child;
 }
 
-
 static void
 check_new_player_move(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h)
 {
 	unsigned char *reply_rd = psx_reply(node->seq_rd);
 	if (reply_rd != NULL) {
 		unsigned char *xml_data = reply_rd + LINDA_HEADER_SIZE;
-		//int xml_len = *(int *)(reply_rd + LINDA_DATA_LENGTH_OFFSET);
 		int xml_len = psx_get_datalength(reply_rd);
 		create_sg(aquarium::sgroot, node, xml_data, xml_len, aquarium::last_player_id);
 		psx_free(reply_rd);
-		aquarium::last_player_id++;
+		aquarium::update_last_player_id();
 		int tuple_id = aquarium::last_player_id * 10;
-		aquarium::parent->seq_rd = psx_rd(aquarium::linda, tuple_id);
+		node->seq_rd = psx_rd(aquarium::linda, tuple_id);
 	}
 	// printf("rd id: %d\n", aquarium::last_player_id);
 }
@@ -130,34 +137,80 @@
     return addr;
 }
 
+void callback_free(unsigned char *tuple, void *arg) {
+	psx_free(tuple);
+}
+
 void
-aquarium::create_my_sg(Viewer *sgroot, int screen_w, int screen_h)
+aquarium::send_position(SceneGraphPtr node) {
+	int pos_id = serial_id * 10 + 1;
+	psx_callback_in(linda, pos_id, callback_free, NULL);
+	aqua::Position *pos = new aqua::Position();
+	pos->set_x(node->xyz[0] + start_x);
+	pos->set_y(node->xyz[1]);
+	int size = pos->ByteSize();
+	unsigned char *msg = (unsigned char *) sgroot->manager->allocate(sizeof(char) * size);
+	pos->SerializeToArray(msg, size); // 更新したデータを再度シリアライズ
+	delete pos;
+	psx_out(linda, pos_id, msg, size);
+}
+
+void
+my_move(SceneGraphPtr node, void *sgroot_, int w, int h)
 {
-    SceneGraphPtr root = sgroot->createSceneGraph();
-    root->set_move_collision(null_move, null_collision); // TODO:
+    SceneGraphRoot *sgroot = (SceneGraphRoot *)sgroot_;
+    Pad *pad = sgroot->getController();
+	int flag = 0;
+    if (pad->right.isHold() || pad->left.isHold()) {
+		if (pad->right.isHold()) {
+			node->xyz[0] += 5.0f;
+			flag = 1;
+		} else if (pad->left.isHold()) {
+			node->xyz[0] -= 5.0f;
+			flag = 1;
+		}
+    }
+    
+    if (pad->down.isHold() || pad->up.isHold() ) {
+		if (pad->down.isHold()) {
+			node->xyz[1] += 5.0f;
+			flag = 1;
+		} else if (pad->up.isHold()) {
+			node->xyz[1] -= 5.0f;
+			flag = 1;
+		}
+    }
 
+	/* ここで座標を送信	*/
+	if (flag || node->resend_flag) {
+		aquarium::send_position(node);
+	}
+}
+
+void
+aquarium::create_my_sg(Viewer *viewer, SceneGraphPtr par, int screen_w, int screen_h)
+{
     int size;
     void *addr = file_map(xml_file_name, &size);
-    SceneGraphPtr sgp = sgroot->createSceneGraph();
-    sgroot->createFromXMLmemory(sgp, (char *)addr, size);
-    sgp->set_move_collision(null_move, null_collision);
+    SceneGraphPtr sgp = viewer->createSceneGraph();
+    viewer->createFromXMLmemory(sgp, (char *)addr, size);
+    sgp->set_move_collision(my_move, null_collision);
 
-    root->addChild(sgp);
+    par->addChild(sgp);
 
     int xml_id = serial_id * 10;
     psx_out(linda, xml_id, (unsigned char *)addr, size);
-	printf("%d", size);
 	int pos_id = serial_id * 10 + 1;
 
 	aqua::Position *pos = new aqua::Position();
-	pos->set_x(0.0f);
+	pos->set_x(0.0f + start_x);
 	pos->set_y(0.0f);
-	unsigned char *msg = (unsigned char *) sgroot->manager->allocate(sizeof(unsigned char *) * size);
+	unsigned char *msg = (unsigned char *) viewer->manager->allocate(sizeof(unsigned char *) * size);
 	pos->SerializeToArray(msg, size);
     psx_out(linda, pos_id, (unsigned char *)msg, pos->ByteSize());
 	delete pos;
-	root->seq = 0;
-	root->resend_flag = 0;
+	sgp->seq = 0;
+	sgp->resend_flag = 0;
 }
 
 MainLoopPtr
@@ -167,12 +220,12 @@
 	width = screen_w;
 	linda_connect(); // 接続に合わせて serial_id も取得
 	update_screen_scope();
-	parent = sgroot->createSceneGraph();
+	SceneGraphPtr parent = sgroot->createSceneGraph();
 	parent->set_move_collision(check_new_player_move, null_collision);
 	
-	create_my_sg(sgroot, screen_w, screen_h);
-	if (serial_id == last_player_id)
-		last_player_id++;
+	create_my_sg(sgroot, parent, screen_w, screen_h);
+	update_last_player_id();
+	
 	int tuple_id = aquarium::last_player_id * 10;
 	parent->seq_rd = psx_rd(linda, tuple_id);
 	sgroot->setSceneData(parent);
@@ -190,6 +243,7 @@
 		psx_sync_n();
 		data = psx_reply(seq);
 	} while (data == NULL);
+	data[LINDA_HEADER_SIZE + psx_get_datalength(data)] = '\0';
 	serial_id = atoi((char *)data + LINDA_HEADER_SIZE);
 	psx_free(data);
 	printf("Get serial_id: %d\n", serial_id);
--- a/Renderer/Test/aquarium.h	Mon Jul 12 04:02:14 2010 +0900
+++ b/Renderer/Test/aquarium.h	Mon Jul 12 04:03:14 2010 +0900
@@ -13,7 +13,6 @@
 	static int last_player_id;
 	
 	static Viewer *sgroot;
-	static SceneGraphPtr parent;
 	static linda_t linda_addr;
 	static int linda;
 	static int serial_id;
@@ -22,6 +21,9 @@
 	static char *xml_file_name;
 	static void linda_connect();
 	static void update_screen_scope();
-	void create_my_sg(Viewer *sgroot, int screen_w, int screen_h);
+	static void send_position(SceneGraphPtr node);
+	static void set_position(SceneGraphPtr node, unsigned char *reply);
+	static void update_last_player_id();
+	void create_my_sg(Viewer *sgroot, SceneGraphPtr parent, int screen_w, int screen_h);
     MainLoopPtr init(Viewer *viewer, int screen_w, int screen_h);
 };