Skip to content

Commit

Permalink
Added code for max_t and couldn't resist a bit of cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Blaizer authored and wchristian committed Mar 14, 2015
1 parent f62c0e2 commit a6e30b0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
16 changes: 10 additions & 6 deletions lib/SDLx/Controller.pm
Expand Up @@ -14,6 +14,7 @@ use Scalar::Util 'refaddr';
# inside out, so this can work as the superclass of another class
my %_dt;
my %_min_t;
my %_max_t;
my %_stop;
my %_event;
my %_event_handlers;
Expand All @@ -37,6 +38,7 @@ sub new {

$_dt{ $ref } = defined $args{dt} ? $args{dt} : 0.1;
$_min_t{ $ref } = defined $args{min_t} ? $args{min_t} : 1 / 60;
$_max_t{ $ref } = defined $args{max_t} ? $args{max_t} : 0.5;
$_stop{ $ref } = 1;
$_event{ $ref } = $args{event} || SDL::Event->new();
$_event_handlers{ $ref } = $args{event_handlers} || [];
Expand All @@ -56,6 +58,7 @@ sub DESTROY {

delete $_dt{ $ref};
delete $_min_t{ $ref};
delete $_max_t{ $ref };
delete $_stop{ $ref};
delete $_event{ $ref};
delete $_event_handlers{ $ref};
Expand All @@ -71,7 +74,6 @@ sub run {
my ($self) = @_;
my $ref = refaddr $self;
my $dt = $_dt{ $ref };
my $min_t = $_min_t{ $ref };
my $delay = $_delay{ $ref };

# these should never change
Expand All @@ -84,16 +86,17 @@ sub run {
delete $_paused{ $ref };

my $current_time = Time::HiRes::time;
Time::HiRes::sleep( $_delay{ $ref } ) if $_delay{ $ref };
Time::HiRes::sleep( $delay ) if $delay;

while ( !$_stop{ $ref } ) {
my $new_time = Time::HiRes::time;
my $delta_time = $new_time - $current_time;
if($delta_time < $min_t) {
if( $delta_time < $_min_t{ $ref } ) {
Time::HiRes::sleep(0.001); # sleep at least a millisecond
redo;
}
$current_time = $new_time;
$delta_time = $_max_t{ $ref } if $delta_time > $_max_t{ $ref };
my $delta_copy = $delta_time;
my $time_ref = \$_time{ $ref};

Expand All @@ -110,12 +113,11 @@ sub run {

$self->_show( $show_handlers, $delta_time );

Time::HiRes::sleep( $delay ) if $delay;

# these can change during the cycle
$dt = $_dt{ $ref};
$min_t = $_min_t{ $ref};
$delay = $_delay{ $ref };

Time::HiRes::sleep( $delay ) if $delay;
}

# pause works by stopping the app and running it again
Expand All @@ -124,6 +126,8 @@ sub run {

$self->_pause($ref);

delete $_paused{ $ref };

# exit out of this sub before going back in so we don't recurse deeper and deeper
goto $self->can('run')
unless $_stop{ $ref};
Expand Down
2 changes: 1 addition & 1 deletion lib/pods/SDLx/Controller.pod
Expand Up @@ -199,7 +199,7 @@ and back to false when it is lifted.
$event->type == SDL_MOUSEBUTTONUP ? 0 : undef
;
return unless defined $state; # not a mouse click

if($event->button_button == SDL_BUTTON_LEFT) {
$click = $state;
}
Expand Down

0 comments on commit a6e30b0

Please sign in to comment.