diff Renderer/Test/aquarium.cc @ 879:596009c66065

[Renderer/Test/aquarium] sync object
author kazz <kazz@cr.ie.u-ryukyu.ac.jp>
date Tue, 06 Jul 2010 19:25:52 +0900
parents 65f0c4c7f381
children f4bdf2b0d7da
line wrap: on
line diff
--- a/Renderer/Test/aquarium.cc	Tue Jul 06 10:39:41 2010 +0900
+++ b/Renderer/Test/aquarium.cc	Tue Jul 06 19:25:52 2010 +0900
@@ -42,34 +42,40 @@
 {
 }
 
-static void
-set_position(SceneGraphPtr node, unsigned char *reply) {
+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->xyz[0] = pos->x();
+	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_;
+    //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);
+			printf("rd get\n");
 			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;
+		printf("wait_rd get\n");
 	} 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;
+		printf("re rd get\n");
 	}
 }
 
@@ -87,14 +93,12 @@
     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);
@@ -130,34 +134,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]);
+	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 parent, 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); // TODO: input from the controller
 
-    root->addChild(sgp);
+    parent->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_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
@@ -170,7 +220,7 @@
 	parent = sgroot->createSceneGraph();
 	parent->set_move_collision(check_new_player_move, null_collision);
 	
-	create_my_sg(sgroot, screen_w, screen_h);
+	create_my_sg(sgroot, parent, screen_w, screen_h);
 	if (serial_id == last_player_id)
 		last_player_id++;
 	int tuple_id = aquarium::last_player_id * 10;
@@ -190,6 +240,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);