| 1 | package SDL::Tutorial::3DWorld::Console; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | SDL::Tutorial::3DWorld::Console - A text-mode overlay for the world | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | $world->{console} = SDL::Tutorial::3DWorld::Console->new; | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | A C<Console> is a text-mode diagnostic overlay for the world. | ||
| 16 | |||
| 17 | It is most commonly used for the display of values like frames per second | ||
| 18 | count, status, and other diagnostic information. It is not particularly | ||
| 19 | useful for proper "Heads Up Display" style overlays as the drawing is done | ||
| 20 | entirely with the glutBitmapCharacter() function. | ||
| 21 | |||
| 22 | However it does provide quite a simple and easy way to push general | ||
| 23 | information to the screen in private or scientific applications, or to | ||
| 24 | display debugging information in games and such. | ||
| 25 | |||
| 26 | This demonstration implementation generates one line of text containing | ||
| 27 | the frames-per-second cound and displays it at the bottom of the screen. | ||
| 28 | |||
| 29 | The main L<SDL::Tutorial::3DWorld> render loop considers the console to | ||
| 30 | be optional, allowing you to display and hide the console on the fly. | ||
| 31 | |||
| 32 | =cut | ||
| 33 | |||
| 34 | use 5.008; | ||
| 35 | use strict; | ||
| 36 | use warnings; | ||
| 37 | use Time::HiRes (); | ||
| 38 | use SDL::Tutorial::3DWorld (); | ||
| 39 | use SDL::Tutorial::3DWorld::OpenGL (); | ||
| 40 | |||
| 41 | our $VERSION = '0.33'; | ||
| 42 | |||
| 43 | # Turn OpenGL fake "constants" into real compile-time optimised constants | ||
| 44 | use constant { | ||
| 45 | GLUT_BITMAP_9_BY_15 => OpenGL::GLUT_BITMAP_9_BY_15, | ||
| 46 | }; | ||
| 47 | |||
| 48 | sub new { | ||
| 49 | my $class = shift; | ||
| 50 | my $self = bless { @_ }, $class; | ||
| 51 | return $self; | ||
| 52 | } | ||
| 53 | |||
| 54 | # This uses GLUT, which we don't have to init ourselves. | ||
| 55 | sub init { | ||
| 56 | return 1; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub display { | ||
| 60 | my $self = shift; | ||
| 61 | |||
| 62 | # Special case for the first execution | ||
| 63 | unless ( defined $self->{time} ) { | ||
| 64 | $self->{fps} = '...'; | ||
| 65 | $self->{time} = Time::HiRes::time(); | ||
| 66 | $self->{tint} = int $self->{time}; | ||
| 67 | $self->{count} = 0; | ||
| 68 | return 1; | ||
| 69 | } | ||
| 70 | |||
| 71 | # Only update the console text once per second | ||
| 72 | $self->{count}++; | ||
| 73 | my $t = Time::HiRes::time(); | ||
| 74 | my $i = int $t; | ||
| 75 | if ( $i != $self->{tint} ) { | ||
| 76 | # Recalculate the FPS | ||
| 77 | my $rate = ($t - $self->{time}) / $self->{count}; | ||
| 78 | my $fps = $rate ? (1 / $rate) : 0; | ||
| 79 | $self->{fps} = sprintf( '%.1f', $fps ); | ||
| 80 | |||
| 81 | # Reset the timer | ||
| 82 | $self->{time} = $t; | ||
| 83 | $self->{tint} = $i; | ||
| 84 | $self->{count} = 0; | ||
| 85 | } | ||
| 86 | |||
| 87 | # Fetch more values and generate the final text | ||
| 88 | my $text = sprintf( | ||
| 89 | "FPS: %s", # Angle: %.1f Elevation: %.1f", | ||
| 90 | $self->{fps}, | ||
| 91 | # SDL::Tutorial::3DWorld->current->camera->{angle}, | ||
| 92 | # SDL::Tutorial::3DWorld->current->camera->{elevation}, | ||
| 93 | ); | ||
| 94 | |||
| 95 | # Backup the model-view matrix | ||
| 96 | OpenGL::glPushMatrix(); | ||
| 97 | OpenGL::glLoadIdentity(); | ||
| 98 | |||
| 99 | # Switch to the projection matrix | ||
| 100 | OpenGL::glMatrixMode( OpenGL::GL_PROJECTION ); | ||
| 101 | OpenGL::glPushMatrix(); | ||
| 102 | OpenGL::glLoadIdentity(); | ||
| 103 | |||
| 104 | # Set up for 2D writing | ||
| 105 | my $world = SDL::Tutorial::3DWorld->current; | ||
| 106 | OpenGL::gluOrtho2D( 0, $world->{width}, 0, $world->{height} ); | ||
| 107 | |||
| 108 | # Disable textures and lighting | ||
| 109 | OpenGL::glDisable( OpenGL::GL_TEXTURE_2D ); | ||
| 110 | OpenGL::glDisable( OpenGL::GL_LIGHTING ); | ||
| 111 | OpenGL::glColor4f( 1, 1, 1, 1 ); | ||
| 112 | OpenGL::glRasterPos2i( 5, 5 ); | ||
| 113 | |||
| 114 | # Disable depth testing while rendering so our text doesn't | ||
| 115 | # get blanked out by very close objects in the world. | ||
| 116 | OpenGL::glDisable( OpenGL::GL_DEPTH_TEST ); | ||
| 117 | |||
| 118 | # Draw each character | ||
| 119 | foreach ( split //, $text ) { | ||
| 120 | OpenGL::glutBitmapCharacter( | ||
| 121 | GLUT_BITMAP_9_BY_15, | ||
| 122 | ord($_), | ||
| 123 | ); | ||
| 124 | } | ||
| 125 | |||
| 126 | # Restore the projection matrix | ||
| 127 | OpenGL::glEnable( OpenGL::GL_DEPTH_TEST ); | ||
| 128 | OpenGL::glEnable( OpenGL::GL_LIGHTING ); | ||
| 129 | OpenGL::glPopMatrix(); | ||
| 130 | |||
| 131 | # Clean up | ||
| 132 | OpenGL::glMatrixMode( OpenGL::GL_MODELVIEW ); | ||
| 133 | OpenGL::glPopMatrix(); | ||
| 134 | |||
| 135 | return 1; | ||
| 136 | } | ||
| 137 | |||
| 138 | 1; | ||
| 139 | |||
| 140 | =pod | ||
| 141 | |||
| 142 | =head1 SUPPORT | ||
| 143 | |||
| 144 | Bugs should be reported via the CPAN bug tracker at | ||
| 145 | |||
| 146 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SDL-Tutorial-3DWorld> | ||
| 147 | |||
| 148 | =head1 AUTHOR | ||
| 149 | |||
| 150 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||
| 151 | |||
| 152 | =head1 SEE ALSO | ||
| 153 | |||
| 154 | L<SDL>, L<OpenGL> | ||
| 155 | |||
| 156 | =head1 COPYRIGHT | ||
| 157 | |||
| 158 | Copyright 2010 Adam Kennedy. | ||
| 159 | |||
| 160 | This program is free software; you can redistribute | ||
| 161 | it and/or modify it under the same terms as Perl itself. | ||
| 162 | |||
| 163 | The full text of the license can be found in the | ||
| 164 | LICENSE file included with this module. | ||
| 165 | |||
| 166 | =cut |