/lib/SDLx/FPS.pm

http://github.com/PerlGameDev/SDL · Perl · 108 lines · 69 code · 38 blank · 1 comment · 3 complexity · 03775163ecae26194b2d78b355ccb7ed MD5 · raw file

  1. package SDLx::FPS;
  2. use strict;
  3. use warnings;
  4. use SDL::GFX::Framerate;
  5. use SDL::GFX::FPSManager;
  6. use Carp;
  7. our @ISA = qw(SDL::GFX::FPSManager);
  8. our $VERSION = 2.548;
  9. sub new {
  10. my ( $class, %args ) = @_;
  11. for ( grep { $_ ne 'fps' } keys %args ) {
  12. Carp::cluck("Unrecognized constructor hash key: $_");
  13. }
  14. my $fps = $class->SDL::GFX::FPSManager::new( 0, 0, 0, 0 );
  15. SDL::GFX::Framerate::init( $fps );
  16. $fps->set( $args{fps} ) if defined $args{fps};
  17. $fps;
  18. }
  19. sub set {
  20. SDL::GFX::Framerate::set( @_[ 0, 1 ] );
  21. }
  22. sub get {
  23. SDL::GFX::Framerate::get( $_[0] );
  24. }
  25. sub delay {
  26. SDL::GFX::Framerate::delay( $_[0] );
  27. }
  28. 1;
  29. __END__
  30. =head1 NAME
  31. SDLx::FPS - a more convenient way to set a framerate
  32. =head1 SYNOPSIS
  33. use SDLx::FPS;
  34. my $fps = SDLx::FPS->new(fps => 60);
  35. while(1) { # Main game loop
  36. # Do game related stuff
  37. $fps->delay;
  38. }
  39. =head1 DESCRIPTION
  40. SDLx::FPS simplifies the task of giving your game a framerate.
  41. Basically, it combines the methods of C<SDL::GFX::Framerate> and C<SDL::GFX::FPSManager> into a single module.
  42. Use it to delay the main loop to keep it at a specified framerate.
  43. =head1 METHODS
  44. =head2 new
  45. my $fps = SDLx::FPS->new( fps => 30 );
  46. No arguments are required, if no C<fps> is specified, the default FPS is 30.
  47. =head2 set
  48. $fps->set($new_framerate);
  49. Same as C<SDL::GFX::Framerate::set>.
  50. Set the new desired framerate.
  51. =head2 get
  52. Same as C<SDL::GFX::Framerate::get>.
  53. Get the currently set framerate.
  54. =head2 delay
  55. Same as C<SDL::GFX::Framerate::delay>.
  56. Generate a delay to accommodate currently set framerate.
  57. Call once in the graphics/rendering loop.
  58. If the computer cannot keep up with the rate (i.e. drawing too slow), the delay is 0 and the delay interpolation is reset.
  59. =head2 framecount
  60. Return the C<framecount>.
  61. =head2 rateticks
  62. Return the C<rateticks>.
  63. =head2 lastticks
  64. Return the C<lastticks>.
  65. =head2 rate
  66. Return the C<rate>.
  67. =head1 AUTHORS
  68. See L<SDL/AUTHORS>.
  69. =head1 SEE ALSO
  70. L<< SDL::GFX::Framerate >>, L<< SDL::GFX::FPSManager >>