/t_backcompat/OpenGL/test1.pl

http://github.com/PerlGameDev/SDL · Perl · 94 lines · 67 code · 23 blank · 4 comment · 9 complexity · 4d1c021464f39d5efe6a13c5451e8467 MD5 · raw file

  1. #!/usr/bin/env perl
  2. use SDL;
  3. use SDLx::App;
  4. use SDL::Surface;
  5. use SDL::Event;
  6. use SDL::OpenGL;
  7. use SDL::OpenGL::Constants;
  8. #for ( keys %main:: ) {
  9. # print "$_\n";
  10. #}
  11. print "Starting $0\n";
  12. my $app = SDLx::App->new( -w => 800, -h => 600, -d => 16, -gl => 1 );
  13. print "Initializing OpenGL settings\n";
  14. printf "%-24s%s\n", "GL_RED_SIZE ", $app->attribute( SDL_GL_RED_SIZE() );
  15. printf "%-24s%s\n", "GL_GREEN_SIZE ", $app->attribute( SDL_GL_GREEN_SIZE() );
  16. printf "%-24s%s\n", "GL_BLUE_SIZE ", $app->attribute( SDL_GL_BLUE_SIZE() );
  17. printf "%-24s%s\n", "GL_DEPTH_SIZE ", $app->attribute( SDL_GL_DEPTH_SIZE() );
  18. printf "%-24s%s\n", "GL_DOUBLEBUFFER ", $app->attribute( SDL_GL_DOUBLEBUFFER() );
  19. sub DrawScene {
  20. glClear( GL_DEPTH_BUFFER_BIT() | GL_COLOR_BUFFER_BIT() );
  21. glLoadIdentity();
  22. glTranslate( -1.5, 0, -6 );
  23. glColor( 1, 1, 1 );
  24. glBegin( GL_TRIANGLES() );
  25. glColor( 1, 0, 0 ) if (@_);
  26. glVertex( 0, 1, 0 );
  27. glColor( 0, 1, 0 ) if (@_);
  28. glVertex( -1, -1, 0 );
  29. glColor( 0, 0, 1 ) if (@_);
  30. glVertex( 1, -1, 0 );
  31. glEnd();
  32. glTranslate( 3, 0, 0 );
  33. glBegin( GL_QUADS() );
  34. glColor( 1, 0, 0 ) if (@_);
  35. glVertex( -1, 1, 0 );
  36. glColor( 0, 1, 0 ) if (@_);
  37. glVertex( 1, 1, 0 );
  38. glColor( 0, 0, 1 ) if (@_);
  39. glVertex( 1, -1, 0 );
  40. glColor( 1, 1, 0 ) if (@_);
  41. glVertex( -1, -1, 0 );
  42. glEnd();
  43. }
  44. sub DrawColorScene {
  45. DrawScene 'with color';
  46. }
  47. sub InitView {
  48. glViewport( 0, 0, 800, 600 );
  49. glMatrixMode( GL_PROJECTION() );
  50. glLoadIdentity();
  51. if (@_) {
  52. gluPerspective( 45.0, 4 / 3, 0.1, 100.0 );
  53. } else {
  54. glFrustum( -0.1, 0.1, -0.075, 0.075, 0.175, 100.0 );
  55. }
  56. glMatrixMode( GL_MODELVIEW() );
  57. glLoadIdentity();
  58. }
  59. InitView();
  60. DrawScene();
  61. $app->sync();
  62. $toggle = 1;
  63. $app->loop(
  64. { SDL_QUIT() => sub { exit(0); },
  65. SDL_KEYDOWN() => sub {
  66. $toggle = ($toggle) ? 0 : 1;
  67. ($toggle) ? DrawScene() : DrawColorScene();
  68. $app->sync();
  69. },
  70. }
  71. );