Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Manually merged Balizer's Patch as his branch is seriously behind

  • Loading branch information...
commit fee969e6c666e2fac4417a65e5d6c0185546af01 1 parent 94697f7
@kthakore kthakore authored
Showing with 39 additions and 0 deletions.
  1. +2 −0  CHANGELOG
  2. +10 −0 lib/SDLx/Controller.pm
  3. +27 −0 lib/pods/SDLx/Controller.pod
View
2  CHANGELOG
@@ -5,6 +5,8 @@ Revision history for Perl extension SDL_perl.
- SDLx::Controller::Interface fixed angular velocity [jtpalmer]
- SDLx::Text using SDL::TTF::Font correctly [FROGGS]
- SDLx::Surface draws polygons [FROGGS]
+ - SDLx::Controller now handles eoq option as a non handler [KTHAKORE]
+ - SDLx::Controller keeps track of paused state [BLAIZER]
* 2.531 Feb 27 2011
- protecting dstrect from modification during SDL_BlitSurface (FROGGS)
View
10 lib/SDLx/Controller.pm
@@ -23,6 +23,7 @@ my %_move_handlers;
my %_show_handlers;
my %_sleep_cycle;
my %_eoq;
+my %_paused;
sub new {
my ($self, %args) = @_;
@@ -64,6 +65,7 @@ sub DESTROY {
delete $_show_handlers{ $ref};
delete $_sleep_cycle { $ref };
delete $_eoq{$ref};
+ delete $_paused{$ref};
}
sub run {
@@ -109,6 +111,7 @@ sub pause {
my $ref = refaddr $self;
$callback ||= sub {1};
my $event = SDL::Event->new();
+ $_paused{ $ref } = 1;
while(1) {
SDL::Events::wait_event($event) or Carp::confess("pause failed waiting for an event");
if($callback->($event, $self)) {
@@ -116,6 +119,8 @@ sub pause {
last;
}
}
+
+ delete $_paused{$ref};
}
sub _event {
@@ -251,12 +256,17 @@ sub current_time {
$_current_time{ $ref};
}
+sub paused {
+ $_paused{ refaddr $_[0]};
+}
+
sub _exit_on_quit {
my ($self, $event) = @_;
$self->stop() if $event->type == SDL_QUIT;
}
+
1; #not 42 man!
__END__
View
27 lib/pods/SDLx/Controller.pod
@@ -142,6 +142,33 @@ Otherwise, time will accumulate while the application is paused, and many moveme
Note 2: a pause will be potentially dangerous to the C<run> cycle (even if you implement your own) unless called by an C<event> callback.
+=head2 paused
+
+Returns 1 if the app is paused, undef otherwise.
+This is only useful when used within code that will be run by C<pause>:
+
+ sub pause {
+ # press P to toggle pause
+
+ my ($e, $app) = @_;
+ if($e->type == SDL_QUIT) {
+ $app->stop;
+ # quit handling is here so that the app
+ # can be stopped while paused
+ }
+ elsif($e->type == SDL_KEYDOWN) {
+ if($e->key_sym == SDLK_P) {
+ # We're paused, so end pause
+ return 1 if $app->paused;
+
+ # We're not paused, so pause
+ $app->pause(\&pause);
+ }
+ }
+ return 0;
+ }
+
+
=head2 add_event_handler
Register a callback to handle events. You can add as many subs as you need.
Please sign in to comment.
Something went wrong with that request. Please try again.