Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hello
i am trying to learn opengl using perl opengl module, and have downloaded some Nehe perl lessons, the first lesson in:
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=01
is just to open a windows, it's code is below, the problem is that when i want to close the window it is closing okay if i click on the X at the top right of the window, but if before that i press any key whether Escape or any other the program is hanging, may be the problem is whithin the " if ($key == ESCAPE)...", please give me hints about the correct methods to exit opengl programs.
i am using windows xp, activestate perl 5.10
the opengl module i have installed from:
http://www.bribes.org/perl/ppmdir.html
thanks

#! /usr/bin/perl -w use OpenGL qw(:all); # Use the OpenGL module use strict; # Use strict typechecking # ASCII constant for the escape key use constant ESCAPE => 27; # Global variable for our window my $window; # A general GL initialization function # Called right after our OpenGL window is created # Sets all of the initial parameters sub InitGL { # Shift the width and height off of @_, in that order my ($width, $height) = @_; # Set the background "clearing color" to black glClearColor(0.0, 0.0, 0.0, 0.0); # Enables clearing of the Depth buffer glClearDepth(1.0); # The type of depth test to do glDepthFunc(GL_LESS); # Enables depth testing with that type glEnable(GL_DEPTH_TEST); # Enables smooth color shading glShadeModel(GL_SMOOTH); # Reset the projection matrix glMatrixMode(GL_PROJECTION); glLoadIdentity; # Calculate the aspect ratio of the Window gluPerspective(45.0, $width/$height, 0.1, 100.0); # Reset the modelview matrix glMatrixMode(GL_MODELVIEW); } # The function called when our window is resized # This shouldn't happen, because we're fullscreen sub ReSizeGLScene { # Shift width and height off of @_, in that order my ($width, $height) = @_; # Prevent divide by zero error if window is too small if ($height == 0) { $height = 1; } # Reset the current viewport and perspective transformation glViewport(0, 0, $width, $height); # Re-initialize the window (same lines from InitGL) glMatrixMode(GL_PROJECTION); glLoadIdentity; gluPerspective(45.0, $width/$height, 0.1, 100.0); glMatrixMode(GL_MODELVIEW); } # The main drawing function. sub DrawGLScene { # Clear the screen and the depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); # Reset the view glLoadIdentity; # Since this is double buffered, swap the buffers. # This will display what just got drawn. glutSwapBuffers; } # The function called whenever a key is pressed. sub keyPressed { # Shift the unsigned char key, and the x,y placement off @_, in # that order. my ($key, $x, $y) = @_; # Avoid thrashing this procedure # Note Perl does not support usleep # For finer resolution sleep than seconds, try: # 'select undef, undef, undef, 0.1;' # to sleep for (at least) 0.1 seconds sleep(100); # If escape is pressed, kill everything. if ($key == ESCAPE) { # Shut down our window glutDestroyWindow($window); # Exit the program...normal termination. exit(0); } } # --- Main program --- # Initialize GLUT state glutInit; # Select type of Display mode: # Double buffer # RGB color (Also try GLUT RGBA) # Alpha components removed (try GLUT_ALPHA) # Depth buffer */ glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); # Get a 640 x 480 window glutInitWindowSize(300, 200); # The window starts at the upper left corner of the screen glutInitWindowPosition(0, 0); # Open the window $window = glutCreateWindow("Lesson01"); # Register the function to do all our OpenGL drawing. glutDisplayFunc(\&DrawGLScene); # Go fullscreen. This is as soon as possible. #glutFullScreen; # Even if there are no events, redraw our gl scene. glutIdleFunc(\&DrawGLScene); # Register the function called when our window is resized. glutReshapeFunc(\&ReSizeGLScene); # Register the function called when the keyboard is pressed. glutKeyboardFunc(\&keyPressed); # Initialize our window. InitGL(300, 200); # Start Event Processing Engine glutMainLoop; return 1;

Replies are listed 'Best First'.
Re: problem in exiting an OpenGL program
by BrowserUk (Patriarch) on Jan 05, 2009 at 11:38 UTC
    i press any key whether Escape or any other the program is hanging

    It's not hanging, it's sleeping. Because you're asking it to sleep for 100 seconds after every key:

    # Avoid thrashing this procedure # Note Perl does not support usleep # For finer resolution sleep than seconds, try: # 'select undef, undef, undef, 0.1;' # to sleep for (at least) 0.1 seconds sleep(100);

    If you change that to 1, the "hanging" will go away. Better still would be to use Time::HiRes and usleep 100; which is probably what the author intended. Eg. Add

    use OpenGL qw(:all); # Use the OpenGL module use strict; # Use strict typechecking use Time::HiRes qw[ usleep ];

    at the top and change to:

    # to sleep for (at least) 0.1 seconds usleep(100);

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.