PageRenderTime 28ms CodeModel.GetById 10ms app.highlight 14ms RepoModel.GetById 2ms app.codeStats 0ms

/t_backcompat/OpenGL/tutorial/lesson02.pl

http://github.com/PerlGameDev/SDL
Perl | 120 lines | 76 code | 31 blank | 13 comment | 8 complexity | 0a453eb9acf0bcd82c2bbc7a3d0d7331 MD5 | raw file
  1#!/usr/bin/perl -w
  2# This code was created by Jeff Molofee '99
  3# (ported to SDL by Sam Lantinga '2000)
  4# (ported to Perl/SDL by Wayne Keenan '2000)
  5#
  6# If you've found this code useful, please let me know.
  7#
  8# Visit me at www.demonews.com/hosted/nehe
  9
 10use strict;
 11use Getopt::Long;
 12
 13use SDL;
 14use SDLx::App;
 15use SDL::OpenGL;
 16use SDL::Event;
 17
 18my $arg_screen_width  = 640;
 19my $arg_screen_height = 512;
 20my $arg_fullscreen    = 0;
 21
 22GetOptions(
 23	"width:i"     => \$arg_screen_width,
 24	"height:i"    => \$arg_screen_height,
 25	"fullscreen!" => \$arg_fullscreen,
 26
 27) or die $!;
 28
 29main();
 30exit;
 31
 32sub main {
 33	my $done = 0;
 34
 35	my $app = SDLx::App->new(
 36		-title  => "Jeff Molofee's GL Code Tutorial ... NeHe '99",
 37		-icon   => "icon.png",
 38		-width  => $arg_screen_width,
 39		-height => $arg_screen_height,
 40		-d      => 16,
 41		-gl     => 1,
 42
 43	);
 44	$app->fullscreen() if ($arg_fullscreen);
 45
 46	SDL::ShowCursor(0);
 47
 48	my $event = SDL::Event->new;
 49	$event->set( SDL_SYSWMEVENT, SDL_IGNORE ); #
 50
 51	InitGL( $arg_screen_width, $arg_screen_height );
 52
 53	while ( !$done ) {
 54
 55		DrawGLScene();
 56
 57		$app->sync();
 58
 59		$event->pump;
 60		$event->poll;
 61
 62		if ( $event->type == SDL_QUIT ) {
 63			$done = 1;
 64		}
 65
 66		if ( $event->type == SDL_KEYDOWN ) {
 67			if ( $event->key_sym == SDLK_ESCAPE ) {
 68				$done = 1;
 69			}
 70		}
 71	}
 72}
 73
 74#########################################################################
 75#Pretty much in original form, but 'Perlised'
 76
 77sub InitGL {
 78	my ( $Width, $Height ) = @_;
 79
 80	glViewport( 0, 0, $Width, $Height );
 81	glClearColor( 0.0, 0.0, 0.0, 0.0 ); # This Will Clear The Background Color To Black
 82	glClearDepth(1.0);                  # Enables Clearing Of The Depth Buffer
 83	glDepthFunc( GL_LESS() );           # The Type Of Depth Test To Do
 84	glEnable( GL_DEPTH_TEST() );        # Enables Depth Testing
 85	glShadeModel( GL_SMOOTH() );        # Enables Smooth Color Shading
 86
 87	glMatrixMode( GL_PROJECTION() );
 88	glLoadIdentity();                   # Reset The Projection Matrix
 89
 90	gluPerspective( 45.0, $Width / $Height, 0.1, 100.0 ); # Calculate The Aspect Ratio Of The Window
 91
 92	glMatrixMode( GL_MODELVIEW() );
 93}
 94
 95# The main drawing function.
 96sub DrawGLScene {
 97	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); # Clear The Screen And The Depth Buffer
 98	glLoadIdentity();                                     # Reset The View
 99
100	glTranslate( -1.5, 0.0, -6.0 );                       # Move Left 1.5 Units And Into The Screen 6.0
101
102	# draw a triangle
103	glBegin(GL_POLYGON);                                  # start drawing a polygon
104	glVertex( 0.0,  1.0,  0.0 );                          # Top
105	glVertex( 1.0,  -1.0, 0.0 );                          # Bottom Right
106	glVertex( -1.0, -1.0, 0.0 );                          # Bottom Left
107	glEnd();                                              # we're done with the polygon
108
109	glTranslate( 3.0, 0.0, 0.0 );                         # Move Right 3 Units
110
111	# draw a square (quadrilateral)
112	glBegin(GL_QUADS);                                    # start drawing a polygon (4 sided)
113	glVertex( -1.0, 1.0,  0.0 );                          # Top Left
114	glVertex( 1.0,  1.0,  0.0 );                          # Top Right
115	glVertex( 1.0,  -1.0, 0.0 );                          # Bottom Right
116	glVertex( -1.0, -1.0, 0.0 );                          # Bottom Left
117	glEnd();                                              # done with the polygon
118
119}
120