Mercurial > hg > Game > Cerium
annotate Renderer/Test/network.cc @ 1846:a0cb52163e57 draft
simplify GPU flip. Do not use output argments in flip. flip requires different kernel.
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 21 Dec 2013 08:38:13 +0900 |
parents | 16c795222988 |
children |
rev | line source |
---|---|
974 | 1 #include <stdio.h> |
2 #include <string.h> | |
3 #include <fcntl.h> | |
4 #include <sys/types.h> | |
5 #include <sys/mman.h> | |
6 #include <sys/stat.h> | |
7 #include <unistd.h> | |
8 #include "SceneGraphRoot.h" | |
9 #include "lindaapi.h" | |
10 #include "network.h" | |
11 #include "network_game.pb.h" | |
12 | |
13 #define GET_SERIAL_ID 65535 | |
14 | |
15 int NetworkGame::last_player_id = 0; | |
16 | |
17 Viewer *NetworkGame::sgroot; | |
18 linda_t NetworkGame::linda_addr = { "localhost", 10000 }; | |
19 int NetworkGame::linda; | |
20 int NetworkGame::serial_id; | |
21 int NetworkGame::width; | |
22 int NetworkGame::start_x; | |
23 char *NetworkGame::xml_file_name; | |
24 | |
1153
16c795222988
fix Makefile of Renderer/Test
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
1011
diff
changeset
|
25 const char *usr_help_str = "Usage: ./network [-linda LINDA_SERVER_NAME [-port PORT_NUM]] -xml XML_OBJECT_FILE_NAME\na"; |
974 | 26 void TMend(TaskManager *manager); |
27 | |
28 extern void task_initialize(); | |
29 extern int init(TaskManager *manager, int argc, char *argv[]); | |
30 extern Application * | |
31 application() { | |
32 return new NetworkGame(); | |
33 } | |
34 | |
1011 | 35 /* |
974 | 36 static void |
37 null_move(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h) | |
38 { | |
39 } | |
1011 | 40 */ |
41 | |
974 | 42 static void |
43 null_collision(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h, SceneGraphPtr tree) | |
44 { | |
45 } | |
46 | |
47 void | |
48 NetworkGame::update_last_player_id() { | |
49 last_player_id++; | |
50 if (last_player_id == serial_id) | |
51 last_player_id++; | |
52 } | |
53 | |
54 void | |
55 NetworkGame::set_position(SceneGraphPtr node, unsigned char *reply) { | |
56 network_game::Position *pos = new network_game::Position(); | |
57 pos->ParseFromArray(reply + LINDA_HEADER_SIZE, psx_get_datalength(reply)); | |
58 node->xyz[0] = pos->x(); | |
59 node->xyz[1] = pos->y(); | |
60 node->angle[0] = pos->angle_x(); | |
61 node->angle[1] = pos->angle_y(); | |
62 delete pos; | |
63 } | |
64 | |
65 static void | |
66 update_position_move(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h) | |
67 { | |
68 // LindaServerから座標データを取得してオブジェクトに反映させる。 | |
69 if (!node->resend_flag || node->seq_rd != node->seq) { | |
70 unsigned char *reply_rd = psx_reply(node->seq_rd); | |
71 if (reply_rd != NULL) { | |
72 NetworkGame::set_position(node, reply_rd); | |
73 psx_free(reply_rd); | |
74 return; | |
75 } | |
76 } | |
77 unsigned char *reply = psx_reply(node->seq); | |
78 if (reply != NULL) { | |
79 // NetworkGame::set_position(node, reply); | |
80 psx_free(reply); | |
81 node->seq = psx_wait_rd(NetworkGame::linda, node->id * 10 + 1); | |
82 node->resend_flag = true; | |
83 } else if (node->resend_flag) { | |
84 node->seq_rd = psx_rd(NetworkGame::linda, node->id * 10 + 1); | |
85 node->resend_flag = false; | |
86 } | |
87 } | |
88 | |
89 SceneGraphPtr | |
90 create_sg(Viewer *viewer, SceneGraphPtr par, unsigned char *data, int len, int serial_id) | |
91 { | |
92 SceneGraphPtr child = viewer->sgroot->createSceneGraph(); | |
93 viewer->sgroot->createFromXMLmemory(viewer->sgroot->tmanager, child, (char *)data, len); | |
94 child->set_move_collision(update_position_move, null_collision); | |
95 child->id = serial_id; | |
96 child->seq = psx_wait_rd(NetworkGame::linda, serial_id * 10 + 1); | |
97 child->seq_rd = psx_rd(NetworkGame::linda, serial_id * 10 + 1); | |
98 child->resend_flag = false; | |
99 par->addChild(child); | |
100 return child; | |
101 } | |
102 | |
103 static void | |
104 check_new_player_move(SceneGraphPtr node, void *sgroot_, int screen_w, int screen_h) | |
105 { | |
106 unsigned char *reply_rd = psx_reply(node->seq_rd); | |
107 if (reply_rd != NULL) { | |
108 unsigned char *xml_data = reply_rd + LINDA_HEADER_SIZE; | |
109 int xml_len = psx_get_datalength(reply_rd); | |
110 create_sg(NetworkGame::sgroot, node, xml_data, xml_len, NetworkGame::last_player_id); | |
111 psx_free(reply_rd); | |
112 NetworkGame::update_last_player_id(); | |
113 int tuple_id = NetworkGame::last_player_id * 10; | |
114 node->seq_rd = psx_rd(NetworkGame::linda, tuple_id); | |
115 } | |
116 // printf("rd id: %d\n", NetworkGame::last_player_id); | |
117 } | |
118 | |
119 void * | |
120 file_map(const char *filename, int *size) { | |
121 int fd; | |
122 void *addr; | |
123 struct stat sb; | |
124 | |
125 if ((fd = open(filename, O_RDONLY)) == -1) { | |
126 fprintf(stderr, "Can't open %s\n", filename); | |
127 perror(NULL); | |
128 } | |
129 if (fstat(fd, &sb) == -1) { | |
130 fprintf(stderr, "Can't fstat %s\n", filename); | |
131 perror(NULL); | |
132 } | |
133 *size = sb.st_size; | |
134 addr = mmap(NULL, *size, PROT_READ, MAP_PRIVATE, fd, 0); | |
135 if (addr == MAP_FAILED) { | |
136 perror("mmap error\n"); | |
137 exit(EXIT_FAILURE); | |
138 } | |
139 close(fd); | |
140 | |
141 return addr; | |
142 } | |
143 | |
144 void callback_free(unsigned char *tuple, void *arg) { | |
145 psx_free(tuple); | |
146 } | |
147 | |
148 void | |
149 NetworkGame::send_position(SceneGraphPtr node) { | |
150 int pos_id = serial_id * 10 + 1; | |
151 psx_callback_in(linda, pos_id, callback_free, NULL); | |
152 network_game::Position *pos = new network_game::Position(); | |
153 pos->set_x(node->xyz[0]); | |
154 pos->set_y(node->xyz[1]); | |
155 pos->set_angle_x(node->angle[0]); | |
156 pos->set_angle_y(node->angle[1]); | |
157 int size = pos->ByteSize(); | |
158 unsigned char *msg = (unsigned char *) sgroot->manager->allocate(sizeof(char) * size); | |
159 pos->SerializeToArray(msg, size); // 更新したデータを再度シリアライズ | |
160 delete pos; | |
161 psx_out(linda, pos_id, msg, size); | |
162 } | |
163 | |
164 void | |
165 my_move(SceneGraphPtr node, void *sgroot_, int w, int h) | |
166 { | |
167 SceneGraphRoot *sgroot = (SceneGraphRoot *)sgroot_; | |
168 Pad *pad = sgroot->getController(); | |
169 int flag = 0; | |
170 if (pad->right.isHold() || pad->left.isHold()) { | |
171 if (pad->right.isHold()) { | |
172 node->xyz[0] += 5.0f; | |
975
c48e3866f85f
player is center in camera
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
974
diff
changeset
|
173 sgroot->camera->xyz[0] = node->xyz[0]; |
974 | 174 // node->angle[1] = 0.0f; |
175 flag = 1; | |
176 } else if (pad->left.isHold()) { | |
177 node->xyz[0] -= 5.0f; | |
975
c48e3866f85f
player is center in camera
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
974
diff
changeset
|
178 sgroot->camera->xyz[0] = node->xyz[0]; |
974 | 179 // node->angle[1] = 180.0f; |
180 flag = 1; | |
181 } | |
182 } | |
183 | |
184 if (pad->down.isHold() || pad->up.isHold() ) { | |
185 if (pad->down.isHold()) { | |
186 node->xyz[1] += 5.0f; | |
975
c48e3866f85f
player is center in camera
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
974
diff
changeset
|
187 sgroot->camera->xyz[1] = node->xyz[1]; |
974 | 188 flag = 1; |
189 } else if (pad->up.isHold()) { | |
190 node->xyz[1] -= 5.0f; | |
975
c48e3866f85f
player is center in camera
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
974
diff
changeset
|
191 sgroot->camera->xyz[1] = node->xyz[1]; |
974 | 192 flag = 1; |
193 } | |
194 } | |
195 | |
196 /* ここで座標を送信 */ | |
197 if (flag || node->resend_flag) { | |
198 NetworkGame::send_position(node); | |
199 } | |
200 } | |
201 | |
202 void | |
203 NetworkGame::create_my_sg(Viewer *viewer, SceneGraphPtr par, int screen_w, int screen_h) | |
204 { | |
205 int size; | |
206 void *addr = file_map(xml_file_name, &size); | |
207 SceneGraphPtr sgp = viewer->createSceneGraph(); | |
208 viewer->createFromXMLmemory(sgp, (char *)addr, size); | |
209 sgp->set_move_collision(my_move, null_collision); | |
975
c48e3866f85f
player is center in camera
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
974
diff
changeset
|
210 Camera *camera = viewer->sgroot->camera; |
c48e3866f85f
player is center in camera
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
974
diff
changeset
|
211 camera->xyz[0] = 0.0; |
c48e3866f85f
player is center in camera
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
974
diff
changeset
|
212 camera->xyz[1] = 0.0; |
c48e3866f85f
player is center in camera
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
974
diff
changeset
|
213 |
974 | 214 par->addChild(sgp); |
215 sgp->c_xyz[0] = 0.0f; | |
216 sgp->c_xyz[1] = 0.0f; | |
217 sgp->c_xyz[2] = 0.0f; | |
218 | |
219 int xml_id = serial_id * 10; | |
220 psx_out(linda, xml_id, (unsigned char *)addr, size); | |
221 int pos_id = serial_id * 10 + 1; | |
222 | |
223 network_game::Position *pos = new network_game::Position(); | |
224 pos->set_x(0.0f); | |
225 pos->set_y(0.0f); | |
226 pos->set_angle_x(0.0f); | |
227 pos->set_angle_y(0.0f); | |
228 unsigned char *msg = (unsigned char *) viewer->manager->allocate(sizeof(unsigned char *) * size); | |
229 pos->SerializeToArray(msg, size); | |
230 psx_out(linda, pos_id, (unsigned char *)msg, pos->ByteSize()); | |
231 delete pos; | |
232 sgp->seq = 0; | |
233 sgp->resend_flag = 0; | |
234 } | |
235 | |
236 MainLoopPtr | |
237 NetworkGame::init(Viewer *sgroot, int screen_w, int screen_h) | |
238 { | |
239 this->sgroot = sgroot; | |
240 width = screen_w; | |
241 linda_connect(); // 接続に合わせて serial_id も取得 | |
242 // update_screen_scope(); | |
243 SceneGraphPtr parent = sgroot->createSceneGraph(); | |
244 parent->set_move_collision(check_new_player_move, null_collision); | |
245 | |
246 create_my_sg(sgroot, parent, screen_w, screen_h); | |
247 update_last_player_id(); | |
248 | |
249 int tuple_id = NetworkGame::last_player_id * 10; | |
250 parent->seq_rd = psx_rd(linda, tuple_id); | |
251 sgroot->setSceneData(parent); | |
252 return sgroot; | |
253 } | |
254 | |
255 void | |
256 NetworkGame::linda_connect() { | |
257 init_linda(); // セレクタの初期化 | |
258 linda = open_linda_java(linda_addr.hostname, linda_addr.port); | |
259 // serial_id の取得 | |
260 int seq = psx_in(linda, GET_SERIAL_ID); | |
261 unsigned char *data = NULL; | |
262 do { | |
263 psx_sync_n(); | |
264 data = psx_reply(seq); | |
265 } while (data == NULL); | |
266 // data[LINDA_HEADER_SIZE + psx_get_datalength(data)] = '\0'; | |
267 serial_id = atoi((char *)data + LINDA_HEADER_SIZE); | |
268 psx_free(data); | |
269 printf("Get serial_id: %d\n", serial_id); | |
270 } | |
271 | |
272 int | |
273 TMmain(TaskManager *manager, int argc, char *argv[]) | |
274 { | |
275 task_initialize(); | |
276 manager->set_TMend(TMend); | |
1153
16c795222988
fix Makefile of Renderer/Test
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
1011
diff
changeset
|
277 int xml_flag = false; |
16c795222988
fix Makefile of Renderer/Test
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
1011
diff
changeset
|
278 |
974 | 279 for (int i = 0; i < argc; i++) { |
280 if (strcmp(argv[i],"-linda") == 0 && i + 1 <= argc) { | |
281 NetworkGame::linda_addr.hostname = argv[i+1]; | |
282 } else if (strcmp(argv[i],"-port") == 0 && i + 1 <= argc) { | |
283 NetworkGame::linda_addr.port = atoi(argv[i+1]); | |
284 } else if (strcmp(argv[i],"-xml") == 0 && i + 1 <= argc) { | |
285 NetworkGame::xml_file_name = argv[i+1]; | |
1153
16c795222988
fix Makefile of Renderer/Test
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
1011
diff
changeset
|
286 xml_flag = true; |
974 | 287 } |
288 } | |
1153
16c795222988
fix Makefile of Renderer/Test
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
1011
diff
changeset
|
289 if (xml_flag) { |
16c795222988
fix Makefile of Renderer/Test
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
1011
diff
changeset
|
290 return init(manager, argc, argv); |
16c795222988
fix Makefile of Renderer/Test
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
1011
diff
changeset
|
291 } else { |
16c795222988
fix Makefile of Renderer/Test
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
1011
diff
changeset
|
292 perror(usr_help_str); |
16c795222988
fix Makefile of Renderer/Test
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
1011
diff
changeset
|
293 exit(1); |
16c795222988
fix Makefile of Renderer/Test
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
1011
diff
changeset
|
294 } |
16c795222988
fix Makefile of Renderer/Test
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
1011
diff
changeset
|
295 |
16c795222988
fix Makefile of Renderer/Test
kazz <kazz@cr.ie.u-ryukyu.ac.jp>
parents:
1011
diff
changeset
|
296 return 0; |
974 | 297 } |
298 | |
299 void | |
300 TMend(TaskManager *manager) | |
301 { | |
302 printf("NetworkGame end\n"); | |
303 } | |
304 | |
305 /* end */ |