comparison 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
comparison
equal deleted inserted replaced
878:6f0797d596bb 879:596009c66065
40 static void 40 static void
41 null_collision(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h, SceneGraphPtr tree) 41 null_collision(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h, SceneGraphPtr tree)
42 { 42 {
43 } 43 }
44 44
45 static void 45 void
46 set_position(SceneGraphPtr node, unsigned char *reply) { 46 aquarium::set_position(SceneGraphPtr node, unsigned char *reply) {
47 aqua::Position *pos = new aqua::Position();
48 pos->ParseFromArray(reply + LINDA_HEADER_SIZE, psx_get_datalength(reply));
49 node->xyz[0] = pos->x();
50 node->xyz[1] = pos->y();
51 delete pos;
47 } 52 }
48 53
49 static void 54 static void
50 update_position_move(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h) 55 update_position_move(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h)
51 { 56 {
52 57 //SceneGraphRoot *sgroot = (SceneGraphRoot *)sgroot_;
53 SceneGraphRoot *sgroot = (SceneGraphRoot *)sgroot_;
54 // LindaServerから座標データを取得してオブジェクトに反映させる。 58 // LindaServerから座標データを取得してオブジェクトに反映させる。
55
56 if (!node->resend_flag || node->seq_rd != node->seq) { 59 if (!node->resend_flag || node->seq_rd != node->seq) {
57 unsigned char *reply_rd = psx_reply(node->seq_rd); 60 unsigned char *reply_rd = psx_reply(node->seq_rd);
58 if (reply_rd != NULL) { 61 if (reply_rd != NULL) {
59 set_position(node, reply_rd); 62 aquarium::set_position(node, reply_rd);
60 free(reply_rd); 63 psx_free(reply_rd);
64 printf("rd get\n");
61 return; 65 return;
62 } 66 }
63 } 67 }
64 unsigned char *reply = psx_reply(node->seq); 68 unsigned char *reply = psx_reply(node->seq);
65 if (reply != NULL) { 69 if (reply != NULL) {
66 set_position(node, reply); 70 aquarium::set_position(node, reply);
67 free(reply); 71 psx_free(reply);
68 node->seq = psx_wait_rd(sgroot->tid, node->id * 10 + 1); 72 node->seq = psx_wait_rd(aquarium::linda, node->id * 10 + 1);
69 node->resend_flag = true; 73 node->resend_flag = true;
74 printf("wait_rd get\n");
70 } else if (node->resend_flag) { 75 } else if (node->resend_flag) {
71 node->seq_rd = psx_rd(sgroot->tid, node->id * 10 + 1); 76 node->seq_rd = psx_rd(aquarium::linda, node->id * 10 + 1);
72 node->resend_flag = false; 77 node->resend_flag = false;
78 printf("re rd get\n");
73 } 79 }
74 } 80 }
75 81
76 SceneGraphPtr 82 SceneGraphPtr
77 create_sg(Viewer *viewer, SceneGraphPtr parent, unsigned char *data, int len, int serial_id) 83 create_sg(Viewer *viewer, SceneGraphPtr parent, unsigned char *data, int len, int serial_id)
85 child->seq_rd = psx_rd(aquarium::linda, serial_id * 10 + 1); 91 child->seq_rd = psx_rd(aquarium::linda, serial_id * 10 + 1);
86 child->resend_flag = false; 92 child->resend_flag = false;
87 return child; 93 return child;
88 } 94 }
89 95
90
91 static void 96 static void
92 check_new_player_move(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h) 97 check_new_player_move(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h)
93 { 98 {
94 unsigned char *reply_rd = psx_reply(node->seq_rd); 99 unsigned char *reply_rd = psx_reply(node->seq_rd);
95 if (reply_rd != NULL) { 100 if (reply_rd != NULL) {
96 unsigned char *xml_data = reply_rd + LINDA_HEADER_SIZE; 101 unsigned char *xml_data = reply_rd + LINDA_HEADER_SIZE;
97 //int xml_len = *(int *)(reply_rd + LINDA_DATA_LENGTH_OFFSET);
98 int xml_len = psx_get_datalength(reply_rd); 102 int xml_len = psx_get_datalength(reply_rd);
99 create_sg(aquarium::sgroot, node, xml_data, xml_len, aquarium::last_player_id); 103 create_sg(aquarium::sgroot, node, xml_data, xml_len, aquarium::last_player_id);
100 psx_free(reply_rd); 104 psx_free(reply_rd);
101 aquarium::last_player_id++; 105 aquarium::last_player_id++;
102 int tuple_id = aquarium::last_player_id * 10; 106 int tuple_id = aquarium::last_player_id * 10;
128 close(fd); 132 close(fd);
129 133
130 return addr; 134 return addr;
131 } 135 }
132 136
133 void 137 void callback_free(unsigned char *tuple, void *arg) {
134 aquarium::create_my_sg(Viewer *sgroot, int screen_w, int screen_h) 138 psx_free(tuple);
135 { 139 }
136 SceneGraphPtr root = sgroot->createSceneGraph(); 140
137 root->set_move_collision(null_move, null_collision); // TODO: 141 void
138 142 aquarium::send_position(SceneGraphPtr node) {
143 int pos_id = serial_id * 10 + 1;
144 psx_callback_in(linda, pos_id, callback_free, NULL);
145 aqua::Position *pos = new aqua::Position();
146 pos->set_x(node->xyz[0]);
147 pos->set_y(node->xyz[1]);
148 int size = pos->ByteSize();
149 unsigned char *msg = (unsigned char *) sgroot->manager->allocate(sizeof(char) * size);
150 pos->SerializeToArray(msg, size); // 更新したデータを再度シリアライズ
151 delete pos;
152 psx_out(linda, pos_id, msg, size);
153 }
154
155 void
156 my_move(SceneGraphPtr node, void *sgroot_, int w, int h)
157 {
158 SceneGraphRoot *sgroot = (SceneGraphRoot *)sgroot_;
159 Pad *pad = sgroot->getController();
160 int flag = 0;
161 if (pad->right.isHold() || pad->left.isHold()) {
162 if (pad->right.isHold()) {
163 node->xyz[0] += 5.0f;
164 flag = 1;
165 } else if (pad->left.isHold()) {
166 node->xyz[0] -= 5.0f;
167 flag = 1;
168 }
169 }
170
171 if (pad->down.isHold() || pad->up.isHold() ) {
172 if (pad->down.isHold()) {
173 node->xyz[1] += 5.0f;
174 flag = 1;
175 } else if (pad->up.isHold()) {
176 node->xyz[1] -= 5.0f;
177 flag = 1;
178 }
179 }
180
181 /* ここで座標を送信 */
182 if (flag || node->resend_flag) {
183 aquarium::send_position(node);
184 }
185 }
186
187 void
188 aquarium::create_my_sg(Viewer *viewer, SceneGraphPtr parent, int screen_w, int screen_h)
189 {
139 int size; 190 int size;
140 void *addr = file_map(xml_file_name, &size); 191 void *addr = file_map(xml_file_name, &size);
141 SceneGraphPtr sgp = sgroot->createSceneGraph(); 192 SceneGraphPtr sgp = viewer->createSceneGraph();
142 sgroot->createFromXMLmemory(sgp, (char *)addr, size); 193 viewer->createFromXMLmemory(sgp, (char *)addr, size);
143 sgp->set_move_collision(null_move, null_collision); 194 sgp->set_move_collision(my_move, null_collision); // TODO: input from the controller
144 195
145 root->addChild(sgp); 196 parent->addChild(sgp);
146 197
147 int xml_id = serial_id * 10; 198 int xml_id = serial_id * 10;
148 psx_out(linda, xml_id, (unsigned char *)addr, size); 199 psx_out(linda, xml_id, (unsigned char *)addr, size);
149 printf("%d", size);
150 int pos_id = serial_id * 10 + 1; 200 int pos_id = serial_id * 10 + 1;
151 201
152 aqua::Position *pos = new aqua::Position(); 202 aqua::Position *pos = new aqua::Position();
153 pos->set_x(0.0f); 203 pos->set_x(0.0f);
154 pos->set_y(0.0f); 204 pos->set_y(0.0f);
155 unsigned char *msg = (unsigned char *) sgroot->manager->allocate(sizeof(unsigned char *) * size); 205 unsigned char *msg = (unsigned char *) viewer->manager->allocate(sizeof(unsigned char *) * size);
156 pos->SerializeToArray(msg, size); 206 pos->SerializeToArray(msg, size);
157 psx_out(linda, pos_id, (unsigned char *)msg, pos->ByteSize()); 207 psx_out(linda, pos_id, (unsigned char *)msg, pos->ByteSize());
158 delete pos; 208 delete pos;
159 root->seq = 0; 209 sgp->seq = 0;
160 root->resend_flag = 0; 210 sgp->resend_flag = 0;
161 } 211 }
162 212
163 MainLoopPtr 213 MainLoopPtr
164 aquarium::init(Viewer *sgroot, int screen_w, int screen_h) 214 aquarium::init(Viewer *sgroot, int screen_w, int screen_h)
165 { 215 {
168 linda_connect(); // 接続に合わせて serial_id も取得 218 linda_connect(); // 接続に合わせて serial_id も取得
169 update_screen_scope(); 219 update_screen_scope();
170 parent = sgroot->createSceneGraph(); 220 parent = sgroot->createSceneGraph();
171 parent->set_move_collision(check_new_player_move, null_collision); 221 parent->set_move_collision(check_new_player_move, null_collision);
172 222
173 create_my_sg(sgroot, screen_w, screen_h); 223 create_my_sg(sgroot, parent, screen_w, screen_h);
174 if (serial_id == last_player_id) 224 if (serial_id == last_player_id)
175 last_player_id++; 225 last_player_id++;
176 int tuple_id = aquarium::last_player_id * 10; 226 int tuple_id = aquarium::last_player_id * 10;
177 parent->seq_rd = psx_rd(linda, tuple_id); 227 parent->seq_rd = psx_rd(linda, tuple_id);
178 sgroot->setSceneData(parent); 228 sgroot->setSceneData(parent);
188 unsigned char *data = NULL; 238 unsigned char *data = NULL;
189 do { 239 do {
190 psx_sync_n(); 240 psx_sync_n();
191 data = psx_reply(seq); 241 data = psx_reply(seq);
192 } while (data == NULL); 242 } while (data == NULL);
243 data[LINDA_HEADER_SIZE + psx_get_datalength(data)] = '\0';
193 serial_id = atoi((char *)data + LINDA_HEADER_SIZE); 244 serial_id = atoi((char *)data + LINDA_HEADER_SIZE);
194 psx_free(data); 245 psx_free(data);
195 printf("Get serial_id: %d\n", serial_id); 246 printf("Get serial_id: %d\n", serial_id);
196 } 247 }
197 248