-
Notifications
You must be signed in to change notification settings - Fork 28
/
FPS.pm
137 lines (98 loc) · 2.81 KB
/
FPS.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package SDLx::FPS;
use strict;
use warnings;
use SDL::GFX::Framerate;
use SDL::GFX::FPSManager;
use Carp;
our @ISA = qw(SDL::GFX::FPSManager);
sub new {
my ( $class, @args ) = @_;
if ( ref $args[0] ) {
my %options = %{ $args[0] };
if ( @args > 1 ) {
Carp::cluck("Extra arguments are not taken when hash is specified");
}
for (
grep {
my $key = $_;
!grep $_ eq $key, qw/fps framecount rateticks lastticks rate/;
} keys %options
)
{
Carp::cluck("Unrecognized constructor hash key: $_");
}
@args = ( @options{qw/fps framecount rateticks lastticks rate/} );
} elsif ( @args > 4 ) {
Carp::cluck("Too many arguments given");
}
my $fps = $class->SDL::GFX::FPSManager::new(
map defined() ? $_ : 0,
@args[ 1 .. 4 ]
);
$fps->init;
$fps->set( $args[0] ) if defined $args[0];
$fps;
}
sub init {
SDL::GFX::Framerate::init( $_[0] );
}
sub set {
SDL::GFX::Framerate::set( @_[ 0, 1 ] );
}
sub get {
SDL::GFX::Framerate::get( $_[0] );
}
sub delay {
SDL::GFX::Framerate::delay( $_[0] );
}
1;
__END__
=head1 NAME
SDLx::FPS - a more convenient way to set a framerate
=head1 SYNOPSIS
use SDLx::FPS;
my $fps = SDLx::FPS->new(fps => 60);
while(1) { # Main game loop
# Do game related stuff
$fps->delay;
}
=head1 DESCRIPTION
SDLx::FPS simplifies the task of giving your game a framerate.
Basically, it combines the methods of C<SDL::GFX::Framerate> and C<SDL::GFX::FPSManager> into a single module.
Use it to delay the main loop to keep it at a specified framerate.
=head1 METHODS
=head2 new
my $fps = SDLx::FPS->new(
fps => 30, framecount => 0, rateticks => 0, lastticks => 0, rate => 0
);
The constructor takes a hash with 5 possible arguments as shown.
No arguments are required, if no C<fps> is specified, the default FPS is 30.
C<framecount>, C<rateticks>, C<lastticks> and C<rate> correspond to the 4 arguments given to C<SDL::GFX::FPSManager->new>.
=head2 init
Same as C<SDL::GFX::Framerate::init>.
Initialize the framerate manager, set default framerate of 30Hz and reset delay interpolation.
You don't need to call this; C<new> does it for you.
=head2 set
$fps->set($new_framerate);
Same as C<SDL::GFX::Framerate::set>.
Set the new desired framerate.
=head2 get
Same as C<SDL::GFX::Framerate::get>.
Get the currently set framerate.
=head2 delay
Same as C<SDL::GFX::Framerate::delay>.
Generate a delay to accommodate currently set framerate.
Call once in the graphics/rendering loop.
If the computer cannot keep up with the rate (i.e. drawing too slow), the delay is 0 and the delay interpolation is reset.
=head2 framecount
Return the C<framecount>.
=head2 rateticks
Return the C<rateticks>.
=head2 lastticks
Return the C<lastticks>.
=head2 rate
Return the C<rate>.
=head1 AUTHORS
See L<SDL/AUTHORS>.
=head1 SEE ALSO
L<< SDL::GFX::Framerate >>, L<< SDL::GFX::FPSManager >>