515
|
1 #include <stdlib.h>
|
518
|
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>
|
515
|
9 #include "SceneGraphRoot.h"
|
524
|
10 #include "lindaapi/lindaapi.h"
|
|
11
|
|
12 #define PORT 10000
|
515
|
13
|
518
|
14 /*
|
|
15 typedef struct {
|
|
16 caddr_t file_mmap;
|
|
17 off_t size;
|
|
18 } st_mmap_t;
|
|
19 */
|
|
20
|
524
|
21 typedef struct {
|
|
22 int tid;
|
|
23 int read_id;
|
|
24 SceneGraphPtr node;
|
|
25 } callback_arg;
|
|
26
|
518
|
27 int
|
|
28 fix_byte(int size,int fix_byte_size)
|
|
29 {
|
|
30 size = (size/fix_byte_size)*fix_byte_size + ((size%fix_byte_size)!= 0)*fix_byte_size;
|
|
31
|
|
32 return size;
|
|
33 }
|
|
34
|
|
35 st_mmap_t
|
|
36 my_mmap(char *filename)
|
|
37 {
|
|
38 int fd = -1;
|
|
39 int map = MAP_PRIVATE;
|
|
40 st_mmap_t st_mmap;
|
|
41 struct stat sb;
|
|
42
|
|
43 if ((fd = open(filename, O_RDONLY, 0666)) == 0 ) {
|
|
44 fprintf(stderr, "Can't open %s\n", filename);
|
|
45 }
|
|
46
|
|
47 if (fstat(fd, &sb)) {
|
|
48 fprintf(stderr, "Can't fstat %s\n", filename);
|
|
49 }
|
|
50
|
|
51 printf("file size %d\n", (int)sb.st_size);
|
|
52
|
|
53 st_mmap.size = fix_byte(sb.st_size, 4096);
|
|
54
|
|
55 printf("fix 4096byte file size %d\n", (int)st_mmap.size);
|
|
56
|
|
57 st_mmap.file_mmap = (char *)mmap(NULL, st_mmap.size, PROT_READ, map, fd, (off_t)0);
|
|
58 if (st_mmap.file_mmap == (caddr_t)-1) {
|
|
59 fprintf(stderr, "Can't mmap file\n");
|
|
60 perror(NULL);
|
|
61 exit(0);
|
|
62 }
|
|
63
|
|
64 return st_mmap;
|
|
65 }
|
|
66
|
515
|
67 static void
|
|
68 earth_collision(SceneGraphPtr node, int screen_w, int screen_h,
|
|
69 SceneGraphPtr tree)
|
|
70 {
|
|
71 }
|
|
72
|
|
73 static void
|
|
74 moon_collision(SceneGraphPtr node, int screen_w, int screen_h,
|
|
75 SceneGraphPtr tree)
|
|
76 {
|
|
77 }
|
|
78
|
|
79 static void
|
|
80 moon_move(SceneGraphPtr node, int screen_w, int screen_h)
|
|
81 {
|
|
82 node->angle[0] += 3.0f;
|
|
83 node->xyz[1] += 1.0f;
|
|
84 }
|
|
85
|
|
86
|
|
87 static void
|
|
88 earth_move(SceneGraphPtr node, int screen_w, int screen_h)
|
|
89 {
|
|
90 node->angle[1] += 1.0f;
|
|
91 if (node->angle[1] > 360.0f) {
|
|
92 node->angle[1] = 0.0f;
|
|
93 }
|
|
94
|
|
95 node->xyz[0] += node->stack_xyz[0];
|
|
96 if ((int)node->xyz[0] > screen_w || (int)node->xyz[0] < 0) {
|
|
97 node->stack_xyz[0] = -node->stack_xyz[0];
|
|
98 }
|
|
99
|
|
100 node->xyz[1] += node->stack_xyz[1];
|
|
101 if ((int)node->xyz[1] > screen_h || (int)node->xyz[1] < 0) {
|
|
102 node->stack_xyz[1] = -node->stack_xyz[1];
|
|
103 }
|
|
104
|
524
|
105 //Pad *pad = sgroot->getController();
|
526
|
106
|
|
107 /*
|
520
|
108 if (pad->circle.isPush()) {
|
515
|
109 SceneGraphPtr earth;
|
|
110 sgroot->createFromXMLmemory(sgroot->tmanager, "xml_file/universe.xml");
|
|
111 if (pad->right.isPush()) {
|
524
|
112 SceneGraphPtr earth;
|
518
|
113
|
|
114 st_mmap_t m = my_mmap("xml_file/universe.xml");
|
|
115
|
|
116 //sgroot->createFromXMLmemory(sgroot->tmanager, "xml_file/universe.xml");
|
|
117 sgroot->createFromXMLmemory(sgroot->tmanager, m);
|
515
|
118 earth = sgroot->createSceneGraph("Earth");
|
|
119 earth->set_move_collision(moon_move, moon_collision);
|
|
120 node->addChild(earth);
|
|
121 }
|
524
|
122 */
|
|
123
|
|
124 psx_sync_n();
|
|
125 }
|
|
126
|
|
127 void
|
|
128 create_sg(SceneGraphPtr node, unsigned char *data, int len)
|
|
129 {
|
|
130 SceneGraphPtr earth;
|
|
131
|
|
132 //sgroot->createFromXMLmemory(sgroot->tmanager, "xml_file/universe.xml");
|
|
133 const char *objname = sgroot->createFromXMLmemory(sgroot->tmanager, (char *)data, len);
|
|
134 earth = sgroot->createSceneGraph(objname);
|
|
135 earth->set_move_collision(moon_move, moon_collision);
|
|
136 node->addChild(earth);
|
|
137
|
515
|
138 }
|
|
139
|
524
|
140 static void
|
|
141 callbacker(unsigned char *tuple, void *arg) {
|
|
142 int len;
|
|
143 unsigned char *data;
|
|
144 callback_arg *carg = (callback_arg *)arg;
|
|
145
|
|
146 len = psx_get_datalength(tuple);
|
|
147 // 最初の4byteデータは使わない
|
|
148 data = tuple+LINDA_HEADER_SIZE;
|
|
149
|
|
150 // ここで create
|
|
151 create_sg(carg->node, data, len);
|
|
152
|
|
153 /* dataは'\0'で終わっている事を期待 (writerで保証する) */
|
|
154 //printf("get data[%d]: `%s'\n", len, data);
|
|
155 free(tuple);
|
|
156
|
|
157 psx_callback_wait_rd(carg->tid, carg->read_id, callbacker, arg);
|
|
158 }
|
|
159
|
|
160
|
518
|
161 void
|
524
|
162 linda_init(TaskManager *manager, SceneGraphPtr node)
|
518
|
163 {
|
|
164 init_linda();
|
524
|
165 callback_arg *carg = (callback_arg *)manager->allocate(sizeof(callback_arg));
|
526
|
166 carg->tid = open_linda_java("133.13.59.231", PORT);
|
524
|
167 carg->read_id = 10;
|
|
168 carg->node = node;
|
|
169 psx_callback_wait_rd(carg->tid, carg->read_id, callbacker, carg);
|
518
|
170 }
|
|
171
|
515
|
172 void
|
|
173 dynamic_init(TaskManager *manager)
|
|
174 {
|
520
|
175 //SceneGraphPtr earth;
|
515
|
176 sgroot->tmanager = manager;
|
|
177
|
|
178 #if 0
|
524
|
179 // テスト用に mmap したデータを第2引数に渡す予定でした
|
515
|
180 sgroot->createFromXMLmemory(manager, "xml_file/universe.xml");
|
|
181
|
|
182 // sglist に登録されている name から sgid を引き、sg_src[sgid] からコピーして返す
|
|
183 earth = sgroot->createSceneGraph("Earth");
|
|
184 #else
|
|
185 SceneGraphPtr parent;
|
|
186 parent = sgroot->createSceneGraph();
|
|
187 parent->set_move_collision(earth_move, earth_collision);
|
|
188 #endif
|
524
|
189
|
|
190 linda_init(manager, parent);
|
515
|
191
|
|
192 // SceneGraphRoot に、使用する SceneGraph を設定する
|
|
193 // このとき、ユーザーが記述した SceneGraph の root を渡す。
|
|
194 sgroot->setSceneData(parent);
|
|
195 }
|