comparison TaskManager/Test/simple_render/viewer.cpp @ 48:70a0ac46133e

*** empty log message ***
author gongo
date Fri, 15 Feb 2008 16:09:42 +0900
parents aa11038dbdc1
children 64ef030deb22
comparison
equal deleted inserted replaced
47:8266445bdac2 48:70a0ac46133e
1 #include <iostream> 1 #include <iostream>
2 #include <SDL.h> 2 #include <SDL.h>
3 #include <SDL_opengl.h> 3 #include <SDL_opengl.h>
4 #include <math.h> 4 #include <math.h>
5 #include <unistd.h>
5 #include "polygon.h" 6 #include "polygon.h"
6 //#include "demonstration.h" 7 //#include "demonstration.h"
7 //#include "scene.h" 8 //#include "scene.h"
8 #include "viewer.h" 9 #include "viewer.h"
9 #include "sys.h" 10 #include "sys.h"
12 #define redMask 0x00ff0000 13 #define redMask 0x00ff0000
13 #define greenMask 0x0000ff00 14 #define greenMask 0x0000ff00
14 #define blueMask 0x000000ff 15 #define blueMask 0x000000ff
15 #define alphaMask 0 16 #define alphaMask 0
16 17
17 extern int create_sgp(SceneGraphPack *sgp, Polygon *sg); 18 extern int create_sgp(Polygon *sg, SceneGraphPack *sgp);
18 extern int update_sgp(SceneGraphPack *sgp, SceneGraphPack *_sgp); 19 extern int update_sgp(SceneGraphPack *sgp, SceneGraphPack *_sgp);
19 extern int create_pp(PolygonPack *pp, SceneGraphPack *sgp); 20 extern int create_pp(SceneGraphPack *sgp, PolygonPack *pp);
20 21
21 Viewer::Viewer(int b, int w, int h) 22 Viewer::Viewer(int b, int w, int h)
22 { 23 {
23 bpp = b; 24 bpp = b;
24 width = w; 25 width = w;
32 { 33 {
33 fprintf(stderr,"Couldn't initialize SDL: %s\n",SDL_GetError()); 34 fprintf(stderr,"Couldn't initialize SDL: %s\n",SDL_GetError());
34 exit( 1 ); 35 exit( 1 );
35 } 36 }
36 screen = SDL_SetVideoMode( width, height, bpp, SDL_HWSURFACE ); 37 screen = SDL_SetVideoMode( width, height, bpp, SDL_HWSURFACE );
37 38 }
38 //manager = new TaskManager(1); 39
39 //manager->init();
40 }
41
42
43 void Viewer::init()
44 {
45 if(SDL_Init( SDL_INIT_VIDEO ) < 0)
46 {
47 cout << "Couldn't initialize SDL:" << SDL_GetError() << endl;
48 exit(1);
49 }
50
51 /* See if we should detect the display depth */
52 if(bpp == 0)
53 {
54 if (SDL_GetVideoInfo()->vfmt->BitsPerPixel <= 8)
55 {
56 bpp = 8;
57 }
58 else
59 {
60 bpp = 16; /* More doesn't seem to work */
61 }
62 }
63
64 //video_flags = SDL_OPENGL;
65 video_flags = SDL_HWSURFACE;
66
67 /* Initialize the display */
68 switch (bpp)
69 {
70 case 8:
71 rgb_size[0] = 3;
72 rgb_size[1] = 3;
73 rgb_size[2] = 2;
74 break;
75 case 15:
76 case 16:
77 rgb_size[0] = 5;
78 rgb_size[1] = 5;
79 rgb_size[2] = 5;
80 break;
81 default:
82 rgb_size[0] = 8;
83 rgb_size[1] = 8;
84 rgb_size[2] = 8;
85 break;
86 }
87
88 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, rgb_size[0]);
89 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, rgb_size[1]);
90 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, rgb_size[2]);
91 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
92 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
93
94 SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 0);
95
96 screen = SDL_SetVideoMode(width, height, bpp, video_flags);
97
98 if(screen == NULL)
99 {
100 cout << "Couldn't set GL mode: " << SDL_GetError() << endl;
101 SDL_Quit();
102 exit(1);
103 }
104
105 SDL_WM_SetCaption("SDL GL test", "testgl");
106
107 glViewport(0, 0, width, height);
108
109 glMatrixMode(GL_PROJECTION);
110 glLoadIdentity();
111
112 glOrtho(-width/10, width/10, -height/10, height/10, -40.0, 400.0);
113
114 glMatrixMode(GL_MODELVIEW);
115 glLoadIdentity();
116
117 glEnable(GL_DEPTH_TEST);
118
119 glDepthFunc(GL_LESS);
120
121 glShadeModel(GL_SMOOTH);
122 }
123 40
124 int Viewer::get_ticks() 41 int Viewer::get_ticks()
125 { 42 {
126 int time; 43 int time;
127 time = SDL_GetTicks(); 44 time = SDL_GetTicks();
326 Polygon *p; 243 Polygon *p;
327 SceneGraphPack *sgp; 244 SceneGraphPack *sgp;
328 PolygonPack *pp; 245 PolygonPack *pp;
329 }; 246 };
330 247
331 #if 0 248 struct run_arg_t *arg;
249
332 void 250 void
333 Viewer::run_init() 251 Viewer::run_init()
334 { 252 {
335 struct run_arg_t *arg = new run_arg_t; 253 arg = new run_arg_t;
336 HTaskPtr task; 254 HTaskPtr task;
337 int fd; 255 int fd;
256
257 fprintf(stderr, " ** %s **\n", __FUNCTION__);
338 258
339 arg->start_time = get_ticks(); 259 arg->start_time = get_ticks();
340 arg->pf = screen->format; 260 arg->pf = screen->format;
341 arg->background = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00); 261 arg->background = SDL_MapRGB(screen->format, 0x00, 0x00, 0x00);
342 arg->p = new Polygon; 262 arg->p = new Polygon;
343 arg->p->set_data("cube.xml"); 263 arg->p->set_data("cube.xml");
344 arg->p->viewer = this; 264 arg->p->viewer = this;
345 arg->sgp = new SceneGraphPack; 265 arg->sgp = new SceneGraphPack;
346 create_sgp(arg->sgp, arg->p); 266 create_sgp(arg->p, arg->sgp);
347 arg->pp = new PolygonPack; 267 arg->pp = new PolygonPack;
348 268
349 pixels = new Uint32[width*height]; 269 pixels = new Uint32[width*height];
350 270
351 graph_line(); 271 graph_line();
353 arg->bitmap = SDL_CreateRGBSurfaceFrom((void *)pixels, width, height, 32, 273 arg->bitmap = SDL_CreateRGBSurfaceFrom((void *)pixels, width, height, 32,
354 width*4, redMask, greenMask, 274 width*4, redMask, greenMask,
355 blueMask, alphaMask); 275 blueMask, alphaMask);
356 276
357 fd = manager->open("ViewerRunLoop"); 277 fd = manager->open("ViewerRunLoop");
358 task = manager->create_task(fd, sizeof(struct run_arg_t), 278 task = manager->create_task(fd, 0, 0, 0, NULL);
359 (unsigned int)arg, 0, NULL); 279 manager->spawn_task(task);
360 manager->spawn_task(task_create_pp);
361 //manager->run();
362 } 280 }
363 281
364 void 282 void
365 Viewer::run_loop(struct run_arg_t *arg) 283 Viewer::run_loop(void)
366 { 284 {
367 HTaskPtr task_update_sgp = NULL; 285 HTaskPtr task_update_sgp = NULL;
368 HTaskPtr task_create_pp = NULL; 286 HTaskPtr task_create_pp = NULL;
369 HTaskPtr task_finish = NULL; 287 HTaskPtr task_finish = NULL;
370 int fd_update_sgp; 288 int fd_update_sgp;
371 int fd_create_pp; 289 int fd_create_pp;
372 int fd_finish; 290 int fd_finish;
373 291
292 HTaskPtr task;
293 int fd;
294
295 fprintf(stderr, " ** %s **\n", __FUNCTION__);
296
297
374 if (quit_check()) { 298 if (quit_check()) {
375 arg->this_time = get_ticks(); 299 arg->this_time = get_ticks();
376 fd_finish = manager->open("ViewerRunFinish"); 300 fd_finish = manager->open("ViewerRunFinish");
377 task_finish = manager->create(fd_finish, sizeof(struct run_arg_t), 301 task_finish = manager->create_task(fd_finish, 0, 0, 0, NULL);
378 (unsigned int)arg, 0, NULL);
379 manager->spawn_task(task_finish); 302 manager->spawn_task(task_finish);
380 return; 303 return;
381 } 304 }
382 305
383 clean_pixels(); 306 clean_pixels();
384 307
385 this->zRow_init(); 308 zRow_init();
386 graph_line(); 309 graph_line();
387 310
388 fd_update_sgp = manager->open("UpdateSGP"); 311 fd_update_sgp = manager->open("UpdateSGP");
389 fd_create_pp = manager->open("CreatePP"); 312 fd_create_pp = manager->open("CreatePP");
390 task_update_sgp = 313 fd = manager->open("ViewerRunDraw");
391 manager->create_task(fd_update_sgp, 314 task_update_sgp = manager->create_task(fd_update_sgp,
392 sizeof(SceneGraphPack), 315 sizeof(SceneGraphPack),
393 (unsigned int)arg->sgp, 316 (unsigned int)arg->sgp,
394 (unsigned int)arg->sgp, 317 (unsigned int)arg->sgp,
395 NULL); 318 NULL);
396 task_create_pp = 319 task_create_pp = manager->create_task(fd_create_pp,
397 manager->create_task(fd_create_pp, 320 sizeof(SceneGraphPack),
398 sizeof(SceneGraphPack), 321 (unsigned int)arg->sgp,
399 (unsigned int)arg->sgp, 322 (unsigned int)arg->pp,
400 (unsigned int)arg->pp, 323 NULL);
401 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);
402 328
403 manager->spawn_task(task_update_sgp); 329 manager->spawn_task(task_update_sgp);
404 manager->spawn_task(task_create_pp); 330 manager->spawn_task(task_create_pp);
405 manager->run(); 331 manager->spawn_task(task);
406 332 }
407 arg->p->draw(pp); // test draw of PolygonPack 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
408 344
409 SDL_BlitSurface(arg->bitmap, NULL, screen, NULL); 345 SDL_BlitSurface(arg->bitmap, NULL, screen, NULL);
410 SDL_UpdateRect(screen, 0, 0, 0, 0); 346 SDL_UpdateRect(screen, 0, 0, 0, 0);
411 347
412 //swap_buffers(); 348 //swap_buffers();
413 arg->frames++; 349 arg->frames++;
350
351 fd = manager->open("ViewerRunLoop");
352 task = manager->create_task(fd, 0, 0, 0, NULL);
353 manager->spawn_task(task);
414 } 354 }
415 355
416 void 356 void
417 Viewer::run_finish(struct run_arg_t *arg) 357 Viewer::run_finish(void)
418 { 358 {
359 fprintf(stderr, " ** %s **\n", __FUNCTION__);
360
419 if (arg->this_time != arg->start_time) { 361 if (arg->this_time != arg->start_time) {
420 cout<< ((float)arg->frames/(this_time-start_time))*1000.0 << " FPS\n"; 362 cout<< ((float)arg->frames/(arg->this_time-arg->start_time))*1000.0 << " FPS\n";
421 } 363 }
422 364
423 SDL_FreeSurface(arg->bitmap); 365 SDL_FreeSurface(arg->bitmap);
424 delete [] pixels; 366 delete [] pixels;
425 arg->p->delete_data(); 367 arg->p->delete_data();
428 delete arg->pp; 370 delete arg->pp;
429 quit(); 371 quit();
430 372
431 delete arg; 373 delete arg;
432 } 374 }
433 #endif 375
434 376 #if 0
435 void Viewer::run() 377 void Viewer::run()
436 { 378 {
437 int frames = 0; 379 int frames = 0;
438 int start_time, this_time; 380 int start_time, this_time;
439 381
461 403
462 p->viewer = this; 404 p->viewer = this;
463 //p->screen = screen; 405 //p->screen = screen;
464 406
465 SceneGraphPack *sgp = new SceneGraphPack; 407 SceneGraphPack *sgp = new SceneGraphPack;
466 create_sgp(sgp,p); 408 create_sgp(p, sgp);
467 409
468 PolygonPack *pp = new PolygonPack; 410 PolygonPack *pp = new PolygonPack;
469 411
470 graph_line(); 412 graph_line();
471 413
565 507
566 //swap_buffers(); 508 //swap_buffers();
567 ++frames; 509 ++frames;
568 } 510 }
569 } 511 }
512 #else
513 void Viewer::run() {}
514 #endif
570 515
571 void 516 void
572 Viewer::zRow_init() 517 Viewer::zRow_init()
573 { 518 {
574 for (int i = 0; i < width; i++) { 519 for (int i = 0; i < width; i++) {