Mercurial > hg > Game > Cerium
annotate TaskManager/Test/test_render/viewer.cpp @ 266:8c9cae96404b draft
merge 151 263
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 03 Jun 2009 23:57:38 +0900 |
parents | 3f467ad8c40d 08e47bbca4f5 |
children | 943c2ebe4483 |
rev | line source |
---|---|
140 | 1 #include <SDL.h> |
109 | 2 #include "viewer.h" |
120 | 3 #include "viewer_types.h" |
137
91c74dbc32e4
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
4 #include "SceneGraph.h" |
91c74dbc32e4
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
5 #include "scene_graph_pack.h" |
109 | 6 #include "sys.h" |
7 #include "Func.h" | |
8 #include "error.h" | |
9 #include "TaskManager.h" | |
140 | 10 #include "Keyboard.h" |
11 #include "Joystick.h" | |
109 | 12 |
13 extern void post2runLoop(void *); | |
14 extern void post2runDraw(void *); | |
15 | |
16 /* measure for FPS (Frame Per Second) */ | |
17 int start_time; | |
18 int this_time; | |
19 int frames; | |
20 | |
140 | 21 extern SceneGraphPtr scene_graph; |
22 extern SceneGraphPtr scene_graph_viewer; | |
109 | 23 |
24 /* Data Pack */ | |
25 SceneGraphPack *sgpack; | |
26 PolygonPack *ppack; | |
27 SpanPackPtr spackList; | |
28 SpanPackPtr *spackList_ptr; | |
29 int spackList_length; | |
30 int spackList_length_align; | |
31 | |
32 void *__texture; | |
33 | |
140 | 34 |
35 /** | |
36 * Joystick があればそれを使い、 | |
37 * 無ければキーボードを返す | |
38 */ | |
39 static Pad* | |
40 create_controller(void) | |
41 { | |
42 if (SDL_NumJoysticks()) { | |
43 SDL_Joystick *joy = SDL_JoystickOpen(0); | |
44 if (!joy) { | |
45 fprintf(stderr, "%s: failed to open joystick", __FUNCTION__); | |
46 return new Keyboard; | |
47 } else { | |
48 printf("Use Joystick\n"); | |
49 return new Joystick(joy); | |
50 } | |
51 } else { | |
52 printf("Use Keyboard\n"); | |
53 return new Keyboard; | |
54 } | |
55 } | |
56 | |
57 | |
109 | 58 Viewer::Viewer(int b, int w, int h, int _num) |
59 { | |
60 bpp = b; | |
61 width = w; | |
62 height = h; | |
63 spe_num = _num; | |
64 } | |
65 | |
66 int | |
67 Viewer::get_ticks() | |
68 { | |
69 int time; | |
70 time = SDL_GetTicks(); | |
71 return time; | |
72 } | |
73 | |
74 bool | |
75 Viewer::quit_check() | |
76 { | |
77 SDL_Event event; | |
78 | |
79 while(SDL_PollEvent(&event)) { | |
80 if (event.type==SDL_QUIT) { | |
81 return true; | |
82 } | |
83 } | |
84 | |
85 Uint8 *keys=SDL_GetKeyState(NULL); | |
86 | |
87 if (keys[SDLK_q] == SDL_PRESSED) { | |
88 return true; | |
89 } | |
90 | |
91 return false; | |
92 } | |
93 | |
94 void | |
95 Viewer::quit() | |
96 { | |
97 SDL_Quit(); | |
98 } | |
99 | |
100 void | |
101 Viewer::swap_buffers() | |
102 { | |
103 SDL_GL_SwapBuffers(); | |
104 } | |
105 | |
137
91c74dbc32e4
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
106 extern void node_init(void); |
144 | 107 extern void create_cube_split(int); |
143 | 108 extern void create_snake_bg(void); |
142 | 109 |
109 | 110 void |
144 | 111 Viewer::run_init(char *xml, int sg_number) |
109 | 112 { |
113 HTaskPtr task_next; | |
114 HTaskPtr task_init_tex; | |
115 | |
116 start_time = get_ticks(); | |
117 this_time = 0; | |
118 frames = 0; | |
119 | |
140 | 120 //scene_graph = SceneGraph::createFromXMLfile(xml); |
121 /** | |
122 * これを実行すると、 | |
123 * scene_graph にオリジナルの SceneGraph Object のリストが、 | |
124 * scene_graph_viewer に描画用の SceneGraph が生成される | |
125 */ | |
142 | 126 //SceneGraph::createFromXMLfile(xml); |
137
91c74dbc32e4
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
127 //polygon->viewer = this; |
140 | 128 |
144 | 129 switch (sg_number) { |
130 case 0: | |
131 case 1: | |
132 create_cube_split(sg_number); | |
133 break; | |
134 case 2: | |
135 create_snake_bg(); | |
136 break; | |
137 case 3: | |
138 node_init(); | |
139 break; | |
140 default: | |
141 node_init(); | |
142 break; | |
143 } | |
142 | 144 |
140 | 145 scene_graph->controller = create_controller(); |
137
91c74dbc32e4
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
146 |
109 | 147 |
148 sgpack = (SceneGraphPack*)manager->malloc(sizeof(SceneGraphPack)); | |
149 sgpack->init(); | |
150 ppack = (PolygonPack*)manager->malloc(sizeof(PolygonPack)); | |
151 | |
152 spackList_length = (this->height + split_screen_h - 1) / split_screen_h; | |
153 spackList = (SpanPack*)manager->malloc(sizeof(SpanPack)*spackList_length); | |
154 | |
155 // SPU に送る address list は 16 倍数でないといけない。 | |
156 // spackList_length*sizeof(SpanPack*) が 16 倍数になるような | |
157 // length_align を求めている。 | |
158 spackList_length_align = (spackList_length + 3)&(~3); | |
159 | |
160 /* 各 SPU が持つ、SpanPack の address list */ | |
161 spackList_ptr = (SpanPack**)manager->malloc(sizeof(SpanPack*)*spackList_length_align); | |
162 | |
163 for (int i = 0; i < spackList_length; i++) { | |
164 spackList_ptr[i] = &spackList[i]; | |
165 } | |
166 | |
126
74d0a70f60e9
fix RGBA mask (bgr -> rgba)
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
123
diff
changeset
|
167 for (int i = 1; i <= spackList_length; i++) { |
74d0a70f60e9
fix RGBA mask (bgr -> rgba)
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
123
diff
changeset
|
168 spackList[i-1].init(i*split_screen_h); |
74d0a70f60e9
fix RGBA mask (bgr -> rgba)
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
123
diff
changeset
|
169 } |
74d0a70f60e9
fix RGBA mask (bgr -> rgba)
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
123
diff
changeset
|
170 |
109 | 171 task_next = manager->create_task(TASK_DUMMY); |
172 task_next->set_post(&post2runLoop, NULL); | |
173 | |
137
91c74dbc32e4
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
174 #if 0 |
91c74dbc32e4
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
175 // 諸事情で、今は SceneGraphPack を作らずに |
91c74dbc32e4
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
176 // そのまま SceneGraph でやっています |
91c74dbc32e4
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
177 HTaskPtr task_sgp; |
109 | 178 task_sgp = manager->create_task(TASK_CREATE_SGP); |
137
91c74dbc32e4
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
179 task_sgp->add_param((uint32)scene_graph); |
109 | 180 task_sgp->add_param((uint32)sgpack); |
181 task_next->wait_for(task_sgp); | |
182 task_sgp->spawn(); | |
137
91c74dbc32e4
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
183 #endif |
109 | 184 |
137
91c74dbc32e4
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
185 int tex_width = scene_graph->texture_image->w; |
91c74dbc32e4
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
186 int tex_height = scene_graph->texture_image->h; |
128
95e2046eb46f
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
187 int tex_blocksize = tex_width*tex_height*4; |
120 | 188 |
128
95e2046eb46f
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
189 __texture = (void*)manager->malloc(tex_blocksize); |
137
91c74dbc32e4
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
190 memcpy(__texture, scene_graph->texture_image->pixels, tex_blocksize); |
120 | 191 |
150 | 192 #if 0 |
137
91c74dbc32e4
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
193 uint32 *tex_src = (uint32*)scene_graph->texture_image->pixels; |
128
95e2046eb46f
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
194 uint32 *tex_dest = (uint32*)manager->malloc(tex_blocksize); |
120 | 195 int tile_size = TEXTURE_SPLIT_PIXEL*TEXTURE_SPLIT_PIXEL; |
128
95e2046eb46f
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
196 int tile_num = tex_width*tex_height/tile_size; |
95e2046eb46f
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
197 tile_num = (tile_num + 15)&(~15); |
120 | 198 void **tex_addrList = (void**)manager->malloc(sizeof(void*)*tile_num); |
199 | |
200 { | |
201 int t = 0; | |
202 | |
128
95e2046eb46f
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
203 for (int y = 0; y < tex_height; y += TEXTURE_SPLIT_PIXEL) { |
95e2046eb46f
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
204 for (int x = 0; x < tex_width; x += TEXTURE_SPLIT_PIXEL) { |
120 | 205 for (int j = 0; j < TEXTURE_SPLIT_PIXEL; j++) { |
206 for (int i = 0; i < TEXTURE_SPLIT_PIXEL; i++) { | |
128
95e2046eb46f
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
207 tex_dest[t++] = tex_src[(x+i) + tex_width*(y+j)]; |
120 | 208 } |
209 } | |
210 } | |
211 } | |
212 | |
213 t = 0; | |
214 | |
128
95e2046eb46f
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
215 for (int i = 0, t = 0; i < tex_width*tex_height; i += tile_size, t++) { |
120 | 216 tex_addrList[t] = (void*)&tex_dest[i]; |
217 } | |
218 } | |
219 | |
109 | 220 for (int i = 0; i < spe_num; i++) { |
221 task_init_tex = manager->create_task(TASK_INIT_TEXTURE); | |
120 | 222 task_init_tex->add_inData(tex_addrList, sizeof(void*)*tile_num); |
223 task_init_tex->add_param(tile_num); | |
109 | 224 task_init_tex->set_cpu(SPE_ANY); |
225 task_next->wait_for(task_init_tex); | |
226 task_init_tex->spawn(); | |
227 } | |
150 | 228 #endif |
109 | 229 |
230 task_next->spawn(); | |
231 } | |
232 | |
233 void | |
234 Viewer::run_loop(void) | |
235 { | |
236 HTaskPtr task_create_pp = NULL; | |
237 HTaskPtr task_create_sp = NULL; | |
238 HTaskPtr task_next; | |
239 bool quit_flg; | |
240 | |
241 quit_flg = quit_check(); | |
242 | |
243 if (quit_flg == true) { | |
244 this_time = get_ticks(); | |
245 run_finish(); | |
246 return; | |
247 } | |
248 | |
249 clean_pixels(); | |
250 | |
251 for (int i = 1; i <= spackList_length; i++) { | |
126
74d0a70f60e9
fix RGBA mask (bgr -> rgba)
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
123
diff
changeset
|
252 spackList[i-1].reinit(i*split_screen_h); |
109 | 253 } |
254 | |
255 task_next = manager->create_task(TASK_DUMMY); | |
256 task_next->set_post(post2runDraw, NULL); | |
257 | |
137
91c74dbc32e4
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
258 #if 0 |
91c74dbc32e4
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
259 HTaskPtr task_update_sgp = NULL; |
109 | 260 task_update_sgp = manager->create_task(TASK_UPDATE_SGP); |
261 task_update_sgp->add_inData(sgpack, sizeof(SceneGraphPack)); | |
262 task_update_sgp->add_outData(sgpack, sizeof(SceneGraphPack)); | |
263 task_update_sgp->add_param(width); | |
264 task_update_sgp->add_param(height); | |
265 task_next->wait_for(task_update_sgp); | |
137
91c74dbc32e4
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
266 task_update_sgp->spawn(); |
91c74dbc32e4
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
267 #else |
140 | 268 scene_graph->controller->check(); |
269 scene_graph_viewer->all_execute(width, height); | |
137
91c74dbc32e4
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
270 #endif |
109 | 271 |
137
91c74dbc32e4
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
272 #if 0 |
109 | 273 task_create_pp = manager->create_task(TASK_CREATE_PP); |
274 task_create_pp->add_inData(sgpack, sizeof(SceneGraphPack)); | |
275 task_create_pp->add_param((uint32)ppack); | |
137
91c74dbc32e4
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
276 #else |
91c74dbc32e4
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
277 task_create_pp = manager->create_task(TASK_CREATE_PP2); |
140 | 278 task_create_pp->add_param((uint32)scene_graph_viewer); |
137
91c74dbc32e4
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
279 task_create_pp->add_param((uint32)ppack); |
91c74dbc32e4
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
280 #endif |
109 | 281 task_next->wait_for(task_create_pp); |
282 | |
283 int range_base = spe_num; | |
284 // 切り上げのつもり | |
285 int range = (spackList_length + range_base - 1) / range_base; | |
286 | |
287 for (int i = 0; i < range_base; i++) { | |
288 int index_start = range*i; | |
289 int index_end = (index_start + range >= spackList_length) | |
290 ? spackList_length : index_start + range; | |
291 | |
292 task_create_sp = manager->create_task(TASK_CREATE_SPAN); | |
293 task_create_sp->add_inData(ppack, sizeof(PolygonPack)); | |
294 task_create_sp->add_inData(spackList_ptr, | |
295 sizeof(SpanPack*)*spackList_length_align); | |
296 task_create_sp->add_inData(&spackList[index_start], sizeof(SpanPack)); | |
297 | |
298 task_create_sp->add_param(index_start); | |
299 | |
300 /** | |
133
8f1419174cdf
DrawSpan のテクスチャ座標を求める部分で、width と height が逆に使われてた。
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
130
diff
changeset
|
301 * ex. screen_height が 480, spenum が 6 の場合、各SPEのy担当範囲 |
109 | 302 * [ 1.. 80] [ 81..160] [161..240] |
303 * [241..320] [321..400] [401..480] | |
304 * | |
305 * ex. screen_height が 1080, spenum が 5 の場合、 | |
306 * [ 1..216] [217..432] [433..648] | |
307 * [649..864] [865..1080] | |
308 */ | |
309 task_create_sp->add_param(index_start*split_screen_h + 1); | |
310 task_create_sp->add_param(index_end*split_screen_h); | |
311 | |
312 task_next->wait_for(task_create_sp); | |
313 task_create_sp->wait_for(task_create_pp); | |
314 | |
315 task_create_sp->set_cpu(SPE_ANY); | |
316 task_create_sp->spawn(); | |
317 } | |
318 | |
319 task_create_pp->spawn(); | |
320 task_next->spawn(); | |
321 } | |
322 | |
323 void | |
324 Viewer::run_draw(void) | |
325 { | |
326 HTaskPtr task_next; | |
327 HTaskPtr task_draw; | |
328 | |
329 task_next = manager->create_task(TASK_DUMMY); | |
330 task_next->set_post(post2runLoop, NULL); | |
331 | |
332 ppack->clear(); | |
333 | |
334 for (int i = 0; i < spackList_length; i++) { | |
335 SpanPack *spack = &spackList[i]; | |
336 int startx = 1; | |
337 int endx = split_screen_w; | |
338 | |
339 int start_y = spack->info.y_top - split_screen_h + 1; | |
137
91c74dbc32e4
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
340 //int end_y = spack->info.y_top; |
109 | 341 int rangey = (start_y + split_screen_h - 1 > this->height) |
342 ? this->height - start_y + 1 : split_screen_h; | |
343 | |
344 while (startx < this->width) { | |
345 if (spack->info.size > 0) { | |
346 // Draw SpanPack | |
347 task_draw = manager->create_task(TASK_DRAW_SPAN); | |
348 task_draw->add_inData(spack, sizeof(SpanPack)); | |
349 } else { | |
350 // Draw Background (現在は塗りつぶし) | |
351 task_draw = manager->create_task(TASK_DRAW_BACK); | |
266 | 352 <<<<<<< local |
133
8f1419174cdf
DrawSpan のテクスチャ座標を求める部分で、width と height が逆に使われてた。
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
130
diff
changeset
|
353 task_draw->add_param(0xffffff); |
266 | 354 ======= |
122 | 355 //task_draw->add_param(0x00ffcc55); |
356 task_draw->add_param(0); | |
357 //task_draw->add_param(st_rgb); | |
266 | 358 >>>>>>> other |
109 | 359 } |
360 | |
361 for (int k = 0; k < rangey; k++) { | |
362 task_draw->add_outData( | |
128
95e2046eb46f
texture load use hash table
gongo@charles.cr.ie.u-ryukyu.ac.jp
parents:
126
diff
changeset
|
363 &pixels[(startx-1)+this->width*(k+start_y-1)], |
109 | 364 (endx - startx + 1)*sizeof(int)); |
365 } | |
366 | |
367 task_draw->add_param(startx); | |
368 task_draw->add_param(endx); | |
369 task_draw->add_param(rangey); | |
370 task_draw->set_cpu(SPE_ANY); | |
371 task_next->wait_for(task_draw); | |
372 task_draw->spawn(); | |
373 | |
374 startx += split_screen_w; | |
375 endx += split_screen_w; | |
376 | |
377 if (endx > this->width) { | |
378 endx = this->width; | |
379 } | |
380 } | |
381 } | |
382 | |
383 task_next->spawn(); | |
384 | |
385 frames++; | |
386 } | |
387 | |
388 void | |
389 Viewer::run_finish(void) | |
390 { | |
391 if (this_time != start_time) { | |
392 printf("%f FPS\n", (((float)frames)/(this_time-start_time))*1000.0); | |
393 } | |
394 | |
137
91c74dbc32e4
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
395 scene_graph->delete_data(); |
140 | 396 scene_graph->controller->close(); |
137
91c74dbc32e4
SceneGraphPack の代わりに、今は SceneGraph をそのまま使う様に設定。
gongo@gendarme.cr.ie.u-ryukyu.ac.jp
parents:
133
diff
changeset
|
397 delete scene_graph; |
109 | 398 |
399 free(__texture); | |
400 quit(); | |
401 } |