Mercurial > hg > Game > Cerium
annotate Renderer/Test/dynamic_create.cc @ 580:ec9dd24c2dc8 draft
add all object in file in dynamic_create
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 24 Oct 2009 16:32:29 +0900 |
parents | d2183b72251f |
children | c34956de8276 |
rev | line source |
---|---|
566 | 1 #include <stdlib.h> |
2 #include <stdio.h> | |
3 #include <string.h> | |
4 #include <fcntl.h> | |
5 #include <sys/types.h> | |
6 #include <sys/mman.h> | |
7 #include <sys/stat.h> | |
8 #include <unistd.h> | |
9 #include <arpa/inet.h> | |
10 #include "SceneGraphRoot.h" | |
11 #include "lindaapi.h" | |
12 #include "dynamic_create.h" | |
13 | |
14 #define PORT 10000 | |
15 | |
16 #define SERIAL_REGIST_TUPLE_NO 1 | |
17 | |
18 /* | |
19 typedef struct { | |
20 caddr_t file_mmap; | |
21 off_t size; | |
22 } st_mmap_t; | |
23 */ | |
24 | |
25 typedef struct client_ { | |
26 int id; | |
27 SceneGraphPtr sgp; | |
28 struct client_ *next; | |
29 } client_t; | |
30 | |
31 typedef struct { | |
32 int tid; | |
33 int sid; | |
34 int read_id; | |
35 SceneGraphPtr node; | |
36 TaskManager *manager; | |
37 client_t *clist; | |
38 } callback_arg; | |
39 | |
40 | |
41 void | |
42 client_list_init(TaskManager* manager, client_t *clist) | |
43 { | |
44 clist->id = -1; | |
45 clist->next = clist; | |
46 } | |
47 | |
48 void | |
49 client_list_update(TaskManager *manager, client_t *clist, int id, SceneGraphPtr sgp) | |
50 { | |
51 } | |
52 | |
53 void | |
54 client_list_delete(TaskManager *manager, client_t *clist, int id) | |
55 { | |
56 client_t *c, *prev; | |
57 for (c = clist->next, prev = clist; c->next != clist; c = c->next) { | |
58 if (c->id == id) { | |
59 prev->next = c->next; | |
60 return; | |
61 } | |
62 prev = clist; | |
63 } | |
64 if (c->id == id) { | |
65 prev->next = c->next; | |
66 return; | |
67 } | |
68 } | |
69 | |
70 | |
71 static void | |
72 earth_collision(SceneGraphPtr node, int screen_w, int screen_h, | |
73 SceneGraphPtr tree) | |
74 { | |
75 } | |
76 | |
77 static void | |
78 moon_collision(SceneGraphPtr node, int screen_w, int screen_h, | |
79 SceneGraphPtr tree) | |
80 { | |
81 } | |
82 | |
83 static void | |
84 moon_move(SceneGraphPtr node, int screen_w, int screen_h) | |
85 { | |
86 //node->angle[0] += 3.0f; | |
87 node->xyz[1] += 1.0f; | |
88 } | |
89 | |
90 | |
91 static void | |
92 earth_move(SceneGraphPtr node, int screen_w, int screen_h) | |
93 { | |
575
341f1f881a9b
Linda API worked. (slightly unreliable)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
574
diff
changeset
|
94 // psx_sync_n(); in viewer::MainLoop |
566 | 95 } |
96 | |
97 SceneGraphPtr | |
98 create_sg(TaskManager *manager, SceneGraphPtr node, unsigned char *data, int len, int serial_id) | |
99 { | |
580
ec9dd24c2dc8
add all object in file in dynamic_create
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
579
diff
changeset
|
100 SceneGraphPtr child = sgroot->createSceneGraph(); |
ec9dd24c2dc8
add all object in file in dynamic_create
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
579
diff
changeset
|
101 node->addChild(child); |
ec9dd24c2dc8
add all object in file in dynamic_create
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
579
diff
changeset
|
102 child->set_move_collision(moon_move, moon_collision); |
ec9dd24c2dc8
add all object in file in dynamic_create
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
579
diff
changeset
|
103 // 読み込んだオブジェクトは、すべて、child の child になる。 |
ec9dd24c2dc8
add all object in file in dynamic_create
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
579
diff
changeset
|
104 sgroot->createFromXMLmemory(sgroot->tmanager, child, (char *)data, len); |
ec9dd24c2dc8
add all object in file in dynamic_create
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
579
diff
changeset
|
105 return node; |
566 | 106 } |
107 | |
108 | |
109 static void | |
110 callback_get_xml(unsigned char *xml_tuple, void *arg) { | |
111 int xml_len = psx_get_datalength(xml_tuple); | |
112 callback_arg *carg = (callback_arg *)arg; | |
113 unsigned char *xml_data = xml_tuple+LINDA_HEADER_SIZE; | |
114 SceneGraphPtr sgp; | |
115 // ここで create | |
574 | 116 fwrite(xml_data, 1, 10, stdout); |
566 | 117 sgp = create_sg(carg->manager, carg->node, xml_data, xml_len, carg->sid); |
118 client_list_update(carg->manager, carg->clist, carg->sid, sgp); | |
119 free(arg); | |
120 free(xml_tuple); | |
121 } | |
122 | |
123 static void | |
124 callbacker(unsigned char *tuple, void *arg) { | |
125 int serial_id, xml_id; | |
126 | |
127 unsigned char *data; | |
128 callback_arg *carg = (callback_arg *)arg; | |
129 | |
130 // 最初の4byteデータは使わない | |
131 data = tuple+LINDA_HEADER_SIZE; | |
132 // clientのSerialIDを取得 | |
133 serial_id = ntohl(*(int *)data); | |
574 | 134 |
135 printf("serial id = %d\n", serial_id); | |
566 | 136 // タプルを解放 |
574 | 137 //psx_del(carg->tid, SERIAL_REGIST_TUPLE_NO); |
566 | 138 |
139 // xml fileを取得する もうすでにxml fileが送信済みである事を期待 | |
140 // つまり、送信者がserial_idを送る前にxml fileを送信していなくてはならない | |
141 xml_id = serial_id * 10; | |
142 callback_arg *copy_arg = (callback_arg *)carg->manager->allocate(sizeof(callback_arg)); | |
143 *copy_arg = *carg; | |
144 copy_arg->sid = serial_id; | |
145 psx_callback_in(carg->tid, xml_id, callback_get_xml, (void *)copy_arg); | |
146 | |
147 /* dataは'\0'で終わっている事を期待 (writerで保証する) */ | |
148 //printf("get data[%d]: `%s'\n", len, data); | |
149 free(tuple); | |
150 | |
574 | 151 psx_callback_in(carg->tid, carg->read_id, callbacker, arg); |
566 | 152 } |
153 | |
577 | 154 static char *linda = "localhost"; |
155 | |
566 | 156 void |
157 linda_init(TaskManager *manager, client_t *clist, SceneGraphPtr node) | |
158 { | |
159 init_linda(); | |
160 callback_arg *carg = (callback_arg *)manager->allocate(sizeof(callback_arg)); | |
161 | |
579 | 162 carg->tid = open_linda_java(linda, PORT); |
566 | 163 carg->read_id = SERIAL_REGIST_TUPLE_NO; |
164 carg->node = node; | |
165 carg->manager = manager; | |
166 carg->clist = clist; | |
574 | 167 psx_callback_in(carg->tid, carg->read_id, callbacker, carg); |
566 | 168 } |
169 | |
170 MainLoopPtr | |
171 dynamic_create::init(Viewer *sgroot, int screen_w, int screen_h) | |
172 { | |
173 //SceneGraphPtr earth; | |
174 client_t *clist; | |
175 clist = (client_t *)sgroot->manager->allocate(sizeof(client_t)); | |
176 | |
177 client_list_init(sgroot->manager, clist); | |
178 | |
179 SceneGraphPtr parent; | |
180 parent = sgroot->createSceneGraph(); | |
181 parent->set_move_collision(earth_move, earth_collision); | |
182 parent->xyz[0] += 300; | |
183 | |
184 linda_init(sgroot->manager, clist, parent); | |
185 | |
186 // SceneGraphRoot に、使用する SceneGraph を設定する | |
187 // このとき、ユーザーが記述した SceneGraph の root を渡す。 | |
188 sgroot->setSceneData(parent); | |
189 return sgroot; | |
190 } | |
191 | |
192 extern Application * | |
193 application() { | |
194 return new dynamic_create(); | |
195 } | |
196 | |
197 const char *usr_help_str = "Usage: ./test_nogl [OPTION]\n"; | |
198 | |
199 extern int init(TaskManager *manager, int argc, char *argv[]); | |
200 extern void task_initialize(); | |
201 static void TMend(TaskManager *manager); | |
202 | |
203 int | |
204 TMmain(TaskManager *manager, int argc, char *argv[]) | |
205 { | |
206 task_initialize(); | |
207 manager->set_TMend(TMend); | |
577 | 208 |
209 for(int i=0;i<argc;i++) { | |
210 if (strcmp(argv[i],"-linda") == 0 && i+1<=argc) { | |
211 linda = argv[i+1]; | |
212 } | |
213 } | |
214 | |
215 | |
566 | 216 return init(manager, argc, argv); |
217 | |
218 } | |
219 | |
220 void | |
221 TMend(TaskManager *manager) | |
222 { | |
223 printf("test_nogl end\n"); | |
224 } | |
225 | |
226 /* end */ | |
227 |