Mercurial > hg > Game > Cerium
comparison TaskManager/Test/simple_pack/viewer.cpp @ 53:0c8ae614d421
Initial revision
author | chiaki |
---|---|
date | Fri, 15 Feb 2008 20:58:50 +0900 |
parents | |
children | 35a6cf176c38 |
comparison
equal
deleted
inserted
replaced
52:e198c3e01cec | 53:0c8ae614d421 |
---|---|
1 #include <iostream> | |
2 #include <SDL.h> | |
3 #include <SDL_opengl.h> | |
4 #include <math.h> | |
5 #include <unistd.h> | |
6 #include "polygon.h" | |
7 //#include "demonstration.h" | |
8 //#include "scene.h" | |
9 #include "viewer.h" | |
10 #include "sys.h" | |
11 using namespace std; | |
12 | |
13 #define redMask 0x00ff0000 | |
14 #define greenMask 0x0000ff00 | |
15 #define blueMask 0x000000ff | |
16 #define alphaMask 0 | |
17 | |
18 extern int create_sgp(Polygon *sg, SceneGraphPack *sgp); | |
19 extern int update_sgp(SceneGraphPack *sgp, SceneGraphPack *_sgp); | |
20 extern int create_pp(SceneGraphPack *sgp, PolygonPack *pp); | |
21 | |
22 Viewer::Viewer(int b, int w, int h) | |
23 { | |
24 bpp = b; | |
25 width = w; | |
26 height = h; | |
27 } | |
28 | |
29 | |
30 void Viewer::sdl_init() | |
31 { | |
32 if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) | |
33 { | |
34 fprintf(stderr,"Couldn't initialize SDL: %s\n",SDL_GetError()); | |
35 exit( 1 ); | |
36 } | |
37 screen = SDL_SetVideoMode( width, height, bpp, SDL_HWSURFACE ); | |
38 } | |
39 | |
40 | |
41 int Viewer::get_ticks() | |
42 { | |
43 int time; | |
44 time = SDL_GetTicks(); | |
45 return time; | |
46 } | |
47 | |
48 bool Viewer::quit_check() | |
49 { | |
50 bool quit = false; | |
51 SDL_Event event; | |
52 while(SDL_PollEvent(&event)) | |
53 { | |
54 if(event.type==SDL_QUIT) | |
55 { | |
56 quit = true; | |
57 return quit; | |
58 } | |
59 } | |
60 return quit; | |
61 } | |
62 | |
63 void Viewer::quit() | |
64 { | |
65 SDL_Quit(); | |
66 } | |
67 | |
68 | |
69 void Viewer::swap_buffers() | |
70 { | |
71 SDL_GL_SwapBuffers(); | |
72 } | |
73 | |
74 | |
75 void Viewer::write_pixel(int x, int y,float z, Uint32 rgb) { | |
76 SDL_PixelFormat *pf; | |
77 pf = screen->format; | |
78 //printf("x:%d y:%d z:%d\n",x,y,z); | |
79 | |
80 //cout << "write_pixel" << endl; | |
81 //cout << x << " " << y << endl; | |
82 | |
83 //cout << SDL_MapRGB(pf,0,0,150) << endl; | |
84 | |
85 x += width/2; | |
86 y += height/2; | |
87 | |
88 static int diffz,diffz1; | |
89 diffz1 = diffz; | |
90 diffz = (zRow[x][y]>z); | |
91 if(diffz != diffz1) { | |
92 //printf("diffz :%d zRow[%d][%d] = %f z = %f\n",diffz,x,y,zRow[x][y],z); | |
93 } | |
94 //printf("x:%d,y:%d,z:%f,zRow:%f\n",x,y,z,zRow[x][y]); | |
95 if(z < zRow[x][y]) { | |
96 // printf("x:%d,y:%d,z:%d\n",x,y,z,zRow); | |
97 if(x < width && x > 0 && y > 0 && y < height) | |
98 { | |
99 //pixels[width*y + x] = SDL_MapRGB(pf,70,70,71); | |
100 zRow[x][y] = z; | |
101 y = height - y; | |
102 pixels[width*y + x] = rgb; | |
103 } | |
104 } | |
105 } | |
106 | |
107 void Viewer::write_line(float x1, float y1, float x2, float y2, Uint32 rgb) | |
108 { | |
109 //cout << "write_line("<< x1 << "," << y1 << "," << x2 << "," << y2 << ")"<< endl; | |
110 | |
111 //Uint32 rgb = 9830400; | |
112 | |
113 if(x1 > x2) | |
114 { | |
115 float x=0; | |
116 float y=0; | |
117 x=x1; | |
118 y=y1; | |
119 x1 = x2; | |
120 y1 = y2; | |
121 x2 = x; | |
122 y2 = y; | |
123 } | |
124 float s = y1; | |
125 | |
126 if((int)x1 == (int)x2) | |
127 { | |
128 if(y1 > y2) | |
129 { | |
130 float y=0; | |
131 y = y1; | |
132 y1 = y2; | |
133 y2 = y; | |
134 } | |
135 for(float i=y1; i<y2; i++) | |
136 { | |
137 //write_pixel((int)x1,(int)i); | |
138 write_pixel((int)x1,(int)i,0,rgb); | |
139 } | |
140 } | |
141 else | |
142 { | |
143 float t = (y2 - y1)/(x2 - x1); | |
144 if(t < -1) | |
145 { | |
146 float f = 0; | |
147 for(float i=x1; i<x2; i++) | |
148 { | |
149 for(float a=(int)t; a<0; a++) | |
150 { | |
151 //write_pixel((int)i,(int)s); | |
152 write_pixel((int)i,(int)s,0,rgb); | |
153 s--; | |
154 } | |
155 f += t-(int)t; | |
156 if(f <= -1) | |
157 { | |
158 //write_pixel((int)i,(int)s); | |
159 write_pixel((int)i,(int)s,0,rgb); | |
160 f = 0; | |
161 s--; | |
162 } | |
163 } | |
164 } | |
165 else if(t <= 1) | |
166 { | |
167 for(float i=x1; i<x2; i++) | |
168 { | |
169 //write_pixel((int)i,(int)s); | |
170 write_pixel((int)i,(int)s,0,rgb); | |
171 s += t; | |
172 } | |
173 } | |
174 else | |
175 { | |
176 float f = 0; | |
177 for(float i=x1; i<x2; i++) | |
178 { | |
179 for(float a=0; a<(int)t; a++) | |
180 { | |
181 //write_pixel((int)i,(int)s); | |
182 write_pixel((int)i,(int)s,0,rgb); | |
183 s++; | |
184 } | |
185 f += t-(int)t; | |
186 if(f >= 1) | |
187 { | |
188 //write_pixel((int)i,(int)s); | |
189 write_pixel((int)i,(int)s,0,rgb); | |
190 f = 0; | |
191 s++; | |
192 } | |
193 } | |
194 } | |
195 } | |
196 } | |
197 | |
198 void Viewer::write_triangle(float x1, float y1, float x2, float y2, float x3, float y3, Uint32 rgb) | |
199 { | |
200 write_line(x1,y1,x2,y2,rgb); | |
201 write_line(x2,y2,x3,y3,rgb); | |
202 write_line(x3,y3,x1,y1,rgb); | |
203 } | |
204 | |
205 void Viewer::clean_pixels() | |
206 { | |
207 for(int i=0; i<width*height; i++) | |
208 { | |
209 pixels[i] = 0x00; | |
210 } | |
211 } | |
212 | |
213 void Viewer::graph_line() | |
214 { | |
215 int xl = width*height/2; | |
216 int yl = width/2; | |
217 for(int i=0; i<width; i++) | |
218 { | |
219 for(int t=0; t<height; t+=20) | |
220 { | |
221 pixels[width*t+i] = 0x5a; | |
222 } | |
223 pixels[xl +i] = 0xff; | |
224 } | |
225 for(int i=0; i<height; i++) | |
226 { | |
227 for(int t=0; t<width; t+=20) | |
228 { | |
229 pixels[i*width+t] = 0x5a; | |
230 } | |
231 pixels[i*width+yl] = 0xff; | |
232 } | |
233 } | |
234 | |
235 | |
236 struct run_arg_t { | |
237 int start_time; | |
238 int this_time; | |
239 int frames; | |
240 SDL_Surface *bitmap; | |
241 SDL_PixelFormat *pf; | |
242 Uint32 background; | |
243 Polygon *p; | |
244 SceneGraphPack *sgp; | |
245 PolygonPack *pp; | |
246 }; | |
247 | |
248 struct run_arg_t *arg; | |
249 | |
250 void | |
251 Viewer::run_init() | |
252 { | |
253 arg = new run_arg_t; | |
254 HTaskPtr task; | |
255 int fd; | |
256 | |
257 fprintf(stderr, " ** %s **\n", __FUNCTION__); | |
258 | |
259 arg->start_time = get_ticks(); | |
260 arg->pf = screen->format; | |
261 arg->background = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00); | |
262 arg->p = new Polygon; | |
263 arg->p->set_data("cube.xml"); | |
264 arg->p->viewer = this; | |
265 arg->sgp = new SceneGraphPack; | |
266 create_sgp(arg->p, arg->sgp); | |
267 arg->pp = new PolygonPack; | |
268 | |
269 pixels = new Uint32[width*height]; | |
270 | |
271 graph_line(); | |
272 | |
273 arg->bitmap = SDL_CreateRGBSurfaceFrom((void *)pixels, width, height, 32, | |
274 width*4, redMask, greenMask, | |
275 blueMask, alphaMask); | |
276 | |
277 fd = manager->open("ViewerRunLoop"); | |
278 task = manager->create_task(fd, 0, 0, 0, NULL); | |
279 manager->spawn_task(task); | |
280 } | |
281 | |
282 void | |
283 Viewer::run_loop(void) | |
284 { | |
285 HTaskPtr task_update_sgp = NULL; | |
286 HTaskPtr task_create_pp = NULL; | |
287 HTaskPtr task_finish = NULL; | |
288 int fd_update_sgp; | |
289 int fd_create_pp; | |
290 int fd_finish; | |
291 | |
292 HTaskPtr task; | |
293 int fd; | |
294 | |
295 fprintf(stderr, " ** %s **\n", __FUNCTION__); | |
296 | |
297 | |
298 if (quit_check()) { | |
299 arg->this_time = get_ticks(); | |
300 fd_finish = manager->open("ViewerRunFinish"); | |
301 task_finish = manager->create_task(fd_finish, 0, 0, 0, NULL); | |
302 manager->spawn_task(task_finish); | |
303 return; | |
304 } | |
305 | |
306 clean_pixels(); | |
307 | |
308 zRow_init(); | |
309 graph_line(); | |
310 | |
311 fd_update_sgp = manager->open("UpdateSGP"); | |
312 fd_create_pp = manager->open("CreatePP"); | |
313 fd = manager->open("ViewerRunDraw"); | |
314 task_update_sgp = manager->create_task(fd_update_sgp, | |
315 sizeof(SceneGraphPack), | |
316 (unsigned int)arg->sgp, | |
317 (unsigned int)arg->sgp, | |
318 NULL); | |
319 task_create_pp = manager->create_task(fd_create_pp, | |
320 sizeof(SceneGraphPack), | |
321 (unsigned int)arg->sgp, | |
322 (unsigned int)arg->pp, | |
323 NULL); | |
324 task = manager->create_task(fd, 0, 0, 0, NULL); | |
325 | |
326 manager->set_task_depend(task_update_sgp, task); | |
327 manager->set_task_depend(task_create_pp, task); | |
328 | |
329 manager->spawn_task(task_update_sgp); | |
330 manager->spawn_task(task_create_pp); | |
331 manager->spawn_task(task); | |
332 } | |
333 | |
334 void | |
335 Viewer::run_draw(void) | |
336 { | |
337 HTaskPtr task; | |
338 int fd; | |
339 | |
340 | |
341 fprintf(stderr, " ** %s **\n", __FUNCTION__); | |
342 | |
343 arg->p->draw(arg->pp); // test draw of PolygonPack | |
344 | |
345 SDL_BlitSurface(arg->bitmap, NULL, screen, NULL); | |
346 SDL_UpdateRect(screen, 0, 0, 0, 0); | |
347 | |
348 //swap_buffers(); | |
349 arg->frames++; | |
350 | |
351 fd = manager->open("ViewerRunLoop"); | |
352 task = manager->create_task(fd, 0, 0, 0, NULL); | |
353 manager->spawn_task(task); | |
354 } | |
355 | |
356 void | |
357 Viewer::run_finish(void) | |
358 { | |
359 fprintf(stderr, " ** %s **\n", __FUNCTION__); | |
360 | |
361 if (arg->this_time != arg->start_time) { | |
362 cout<< ((float)arg->frames/(arg->this_time-arg->start_time))*1000.0 << " FPS\n"; | |
363 } | |
364 | |
365 SDL_FreeSurface(arg->bitmap); | |
366 delete [] pixels; | |
367 arg->p->delete_data(); | |
368 delete arg->p; | |
369 delete arg->sgp; | |
370 delete arg->pp; | |
371 quit(); | |
372 | |
373 delete arg; | |
374 } | |
375 | |
376 #if 0 | |
377 void Viewer::run() | |
378 { | |
379 int frames = 0; | |
380 int start_time, this_time; | |
381 | |
382 HTaskPtr task_create_sgp = NULL; | |
383 HTaskPtr task_update_sgp = NULL; | |
384 HTaskPtr task_create_pp = NULL; | |
385 int fd_create_sgp; | |
386 int fd_update_sgp; | |
387 int fd_create_pp; | |
388 | |
389 start_time = get_ticks(); | |
390 | |
391 SDL_Surface *bitmap = NULL; | |
392 SDL_PixelFormat *pf; | |
393 pf = screen->format; | |
394 | |
395 Uint32 background; | |
396 background = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00); | |
397 | |
398 pixels = new Uint32[width*height]; | |
399 | |
400 Polygon *p = new Polygon; | |
401 p->set_data("cube.xml"); | |
402 //p->set_data("cube-p.xml"); | |
403 | |
404 p->viewer = this; | |
405 //p->screen = screen; | |
406 | |
407 SceneGraphPack *sgp = new SceneGraphPack; | |
408 create_sgp(p, sgp); | |
409 | |
410 PolygonPack *pp = new PolygonPack; | |
411 | |
412 graph_line(); | |
413 | |
414 float r = 0; | |
415 float x = 0; | |
416 float y = 0; | |
417 float z = 0; | |
418 | |
419 bitmap = SDL_CreateRGBSurfaceFrom((void *)pixels, width, height, 32, | |
420 width*4, redMask, greenMask, | |
421 blueMask, alphaMask); | |
422 | |
423 // Loop until done. | |
424 while(1) | |
425 { | |
426 // Destroy our GL context, etc. | |
427 //if(quit_check() || scene->action_scene==NULL) | |
428 if(quit_check()) | |
429 { | |
430 this_time = get_ticks(); | |
431 if (this_time != start_time) | |
432 { | |
433 cout<<((float)frames/(this_time-start_time))*1000.0<<" FPS\n"; | |
434 } | |
435 SDL_FreeSurface(bitmap); | |
436 delete pixels; | |
437 p->delete_data(); | |
438 delete p; | |
439 delete sgp; | |
440 delete pp; | |
441 quit(); | |
442 break; | |
443 } | |
444 ///////////////////// | |
445 clean_pixels(); | |
446 | |
447 this->zRow_init(); | |
448 graph_line(); | |
449 | |
450 #if 0 // ここは update_sgp | |
451 if(r > 360) r = 0; | |
452 r+= 1.0; | |
453 // r= 0; | |
454 p->angle[0] = 0; | |
455 p->angle[1] = r; | |
456 p->angle[2] = 0; | |
457 //p->child->angle[1] = r*2; | |
458 //p->child->brother->angle[1] = r*3; | |
459 //p->child->brother->child->angle[1] = r*4; | |
460 x += 0.5; | |
461 y += 0.5; | |
462 z += 0.5; | |
463 p->xyz[0] = x; | |
464 p->xyz[1] = y; | |
465 p->xyz[2] = z; | |
466 //p->tree_draw(); | |
467 #endif | |
468 | |
469 #if 0 | |
470 update_sgp(sgp, sgp); | |
471 create_pp(pp, sgp); | |
472 #else | |
473 fd_update_sgp = manager->open("UpdateSGP"); | |
474 fd_create_pp = manager->open("CreatePP"); | |
475 task_update_sgp = | |
476 manager->create_task(fd_update_sgp, | |
477 sizeof(SceneGraphPack), | |
478 (unsigned int)sgp, | |
479 (unsigned int)sgp, | |
480 NULL); | |
481 task_create_pp = | |
482 manager->create_task(fd_create_pp, | |
483 sizeof(SceneGraphPack), | |
484 (unsigned int)sgp, | |
485 (unsigned int)pp, | |
486 NULL); | |
487 | |
488 manager->spawn_task(task_update_sgp); | |
489 manager->spawn_task(task_create_pp); | |
490 manager->run(); | |
491 #endif | |
492 | |
493 //p->draw(sgp); // test draw of SceneGraphPack | |
494 p->draw(pp); // test draw of PolygonPack | |
495 | |
496 // 一回のみ生成で、その後は再利用 | |
497 //bitmap = SDL_CreateRGBSurfaceFrom((void *)pixels, width, height, 32, | |
498 //width*4, redMask, greenMask, | |
499 //blueMask, alphaMask); | |
500 | |
501 //SDL_FillRect(screen, NULL, background); | |
502 SDL_BlitSurface(bitmap, NULL, screen, NULL); | |
503 SDL_UpdateRect(screen, 0, 0, 0, 0); | |
504 | |
505 | |
506 ///////////////////// | |
507 | |
508 //swap_buffers(); | |
509 ++frames; | |
510 } | |
511 } | |
512 #else | |
513 void Viewer::run() {} | |
514 #endif | |
515 | |
516 void | |
517 Viewer::zRow_init() | |
518 { | |
519 for (int i = 0; i < width; i++) { | |
520 for (int j = 0; j < height; j++) { | |
521 zRow[i][j] = 65535; | |
522 } | |
523 } | |
524 } |