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