SDL-Tutorial-3DWorld

 view release on metacpan or  search on metacpan

lib/SDL/Tutorial/3DWorld/Console.pm  view on Meta::CPAN

package SDL::Tutorial::3DWorld::Console;

=pod

=head1 NAME

SDL::Tutorial::3DWorld::Console - A text-mode overlay for the world

=head1 SYNOPSIS

  $world->{console} = SDL::Tutorial::3DWorld::Console->new;

=head1 DESCRIPTION

A C<Console> is a text-mode diagnostic overlay for the world.

It is most commonly used for the display of values like frames per second
count, status, and other diagnostic information. It is not particularly
useful for proper "Heads Up Display" style overlays as the drawing is done
entirely with the glutBitmapCharacter() function.

However it does provide quite a simple and easy way to push general
information to the screen in private or scientific applications, or to
display debugging information in games and such.

This demonstration implementation generates one line of text containing
the frames-per-second cound and displays it at the bottom of the screen.

The main L<SDL::Tutorial::3DWorld> render loop considers the console to
be optional, allowing you to display and hide the console on the fly.

=cut

use 5.008;
use strict;
use warnings;
use Time::HiRes                    ();
use SDL::Tutorial::3DWorld         ();
use SDL::Tutorial::3DWorld::OpenGL ();

our $VERSION = '0.33';

# Turn OpenGL fake "constants" into real compile-time optimised constants
use constant {
	GLUT_BITMAP_9_BY_15 => OpenGL::GLUT_BITMAP_9_BY_15,
};

sub new {
	my $class = shift;
	my $self  = bless { @_ }, $class;
	return $self;
}

# This uses GLUT, which we don't have to init ourselves.
sub init {
	return 1;
}

sub display {
	my $self = shift;

	# Special case for the first execution
	unless ( defined $self->{time} ) {
		$self->{fps}   = '...';
		$self->{time}  = Time::HiRes::time();
		$self->{tint}  = int $self->{time};
		$self->{count} = 0;
		return 1;
	}

	# Only update the console text once per second
	$self->{count}++;
	my $t = Time::HiRes::time();
	my $i = int $t;
	if ( $i != $self->{tint} ) {
		# Recalculate the FPS
		my $rate = ($t - $self->{time}) / $self->{count};
		my $fps  = $rate ? (1 / $rate) : 0;
		$self->{fps} = sprintf( '%.1f', $fps );



( run in 1.262 second using v1.01-cache-2.11-cpan-39bf76dae61 )