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