PageRenderTime 49ms CodeModel.GetById 38ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

/lib/SDLx/FPS.pm

http://github.com/PerlGameDev/SDL
Perl | 108 lines | 69 code | 38 blank | 1 comment | 3 complexity | 03775163ecae26194b2d78b355ccb7ed MD5 | raw file
  1package SDLx::FPS;
  2use strict;
  3use warnings;
  4use SDL::GFX::Framerate;
  5use SDL::GFX::FPSManager;
  6use Carp;
  7our @ISA = qw(SDL::GFX::FPSManager);
  8
  9our $VERSION = 2.548;
 10
 11sub new {
 12	my ( $class, %args ) = @_;
 13
 14	for ( grep { $_ ne 'fps' } keys %args ) {
 15		Carp::cluck("Unrecognized constructor hash key: $_");
 16	}
 17	my $fps = $class->SDL::GFX::FPSManager::new( 0, 0, 0, 0 );
 18	SDL::GFX::Framerate::init( $fps );
 19	$fps->set( $args{fps} ) if defined $args{fps};
 20	$fps;
 21}
 22
 23sub set {
 24	SDL::GFX::Framerate::set( @_[ 0, 1 ] );
 25}
 26
 27sub get {
 28	SDL::GFX::Framerate::get( $_[0] );
 29}
 30
 31sub delay {
 32	SDL::GFX::Framerate::delay( $_[0] );
 33}
 34
 351;
 36
 37__END__
 38
 39=head1 NAME
 40
 41SDLx::FPS - a more convenient way to set a framerate
 42
 43=head1 SYNOPSIS
 44
 45 use SDLx::FPS;
 46 my $fps = SDLx::FPS->new(fps => 60);
 47 while(1) { # Main game loop
 48     # Do game related stuff
 49
 50     $fps->delay;
 51 }
 52
 53=head1 DESCRIPTION
 54
 55SDLx::FPS simplifies the task of giving your game a framerate.
 56Basically, it combines the methods of C<SDL::GFX::Framerate> and C<SDL::GFX::FPSManager> into a single module.
 57Use it to delay the main loop to keep it at a specified framerate.
 58
 59=head1 METHODS
 60
 61=head2 new
 62
 63 my $fps = SDLx::FPS->new( fps => 30 );
 64
 65No arguments are required, if no C<fps> is specified, the default FPS is 30.
 66
 67=head2 set
 68
 69 $fps->set($new_framerate);
 70
 71Same as C<SDL::GFX::Framerate::set>.
 72Set the new desired framerate.
 73
 74=head2 get
 75
 76Same as C<SDL::GFX::Framerate::get>.
 77Get the currently set framerate.
 78
 79=head2 delay
 80
 81Same as C<SDL::GFX::Framerate::delay>.
 82Generate a delay to accommodate currently set framerate.
 83Call once in the graphics/rendering loop.
 84If the computer cannot keep up with the rate (i.e. drawing too slow), the delay is 0 and the delay interpolation is reset.
 85
 86=head2 framecount
 87
 88Return the C<framecount>.
 89
 90=head2 rateticks
 91
 92Return the C<rateticks>.
 93
 94=head2 lastticks
 95
 96Return the C<lastticks>.
 97
 98=head2 rate
 99
100Return the C<rate>.
101
102=head1 AUTHORS
103
104See L<SDL/AUTHORS>.
105
106=head1 SEE ALSO
107
108L<< SDL::GFX::Framerate >>, L<< SDL::GFX::FPSManager >>