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