Importing SDLPerl 2.2
[sdlgit/SDL_perl.git] / test / OpenGL / test1.sdlpl
1 #!/usr/bin/env perl
2 #
3 # test1.pl
4 #
5 # Copyright (C) 2005 David J. Goehrig <dgoehrig@cpan.org>
6 #
7 # ------------------------------------------------------------------------------
8 #
9 # This library is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public
11 # License as published by the Free Software Foundation; either
12 # version 2.1 of the License, or (at your option) any later version.
13
14 # This library is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # Lesser General Public License for more details.
18
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with this library; if not, write to the Free Software
21 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 #
23 # ------------------------------------------------------------------------------
24 #
25 # Please feel free to send questions, suggestions or improvements to:
26 #
27 #       David J. Goehrig
28 #       dgoehrig@cpan.org
29 #
30
31 use strict;
32 use SDL;
33 use SDL::App;
34 use SDL::Surface;
35 use SDL::Event;
36 use SDL::OpenGL;
37 use SDL::OpenGL::Constants;
38
39 #for ( keys %main:: ) {
40 #       print "$_\n";
41 #}
42
43 print "Starting $0\n";
44
45 my $app = new SDL::App  -w => 800, -h => 600, -d => 16, -gl => 1;
46
47 print "Initializing OpenGL settings\n";
48 printf "%-24s%s\n", "GL_RED_SIZE ", $app->attribute( SDL_GL_RED_SIZE() );
49 printf "%-24s%s\n", "GL_GREEN_SIZE ", $app->attribute( SDL_GL_GREEN_SIZE());
50 printf "%-24s%s\n", "GL_BLUE_SIZE ", $app->attribute( SDL_GL_BLUE_SIZE() );
51 printf "%-24s%s\n", "GL_DEPTH_SIZE ", $app->attribute( SDL_GL_DEPTH_SIZE() );
52 printf "%-24s%s\n", "GL_DOUBLEBUFFER ", $app->attribute( SDL_GL_DOUBLEBUFFER() );
53
54 sub DrawScene {
55
56         glClear( GL_DEPTH_BUFFER_BIT() 
57                 | GL_COLOR_BUFFER_BIT());
58
59         glLoadIdentity();
60
61         glTranslate(-1.5,0,-6);
62         
63         glColor(1,1,1);
64
65         glBegin(GL_TRIANGLES());
66                 glColor(1,0,0) if (@_);
67                 glVertex(0,1,0);
68                 glColor(0,1,0) if (@_);
69                 glVertex(-1,-1,0);
70                 glColor(0,0,1) if (@_);
71                 glVertex(1,-1,0);
72         glEnd();
73
74         glTranslate(3,0,0);
75
76         glBegin(GL_QUADS());
77                 glColor(1,0,0) if (@_);
78                 glVertex(-1,1,0);
79                 glColor(0,1,0) if (@_);
80                 glVertex(1,1,0);
81                 glColor(0,0,1) if (@_);
82                 glVertex(1,-1,0);
83                 glColor(1,1,0) if (@_);
84                 glVertex(-1,-1,0);
85         glEnd();
86 }
87
88 sub DrawColorScene {
89         DrawScene 'with color';
90 }
91
92 sub InitView {
93         glViewport(0,0,800,600);
94
95         glMatrixMode(GL_PROJECTION());
96         glLoadIdentity();
97
98         if ( @_ ) {
99                 gluPerspective(45.0,4/3,0.1,100.0);
100         } else {
101                 glFrustum(-0.1,0.1,-0.075,0.075,0.175,100.0);
102         }
103
104         glMatrixMode(GL_MODELVIEW());
105         glLoadIdentity();
106 }
107
108 InitView();
109
110 DrawScene();
111
112 $app->sync();
113
114 my $toggle = 1;
115
116 $app->loop( { 
117         SDL_QUIT() => sub { exit(0); },
118         SDL_KEYDOWN() => sub {  $toggle = ($toggle) ? 0 : 1; 
119                                 ($toggle) ? DrawScene() : DrawColorScene(); 
120                                 $app->sync();
121                                 },
122         } );