Skip to content
Browse files

SDLx::Controller stop_handler and its docs.

Just about finished SDLx::Controller now, too. Needs polish, tests, and... someone to tell me the docs make sense?
  • Loading branch information...
1 parent 740e7f8 commit b84b9b30ae504968658a460679744249572e2b85 @Blaizer Blaizer committed Feb 3, 2012
Showing with 34 additions and 46 deletions.
  1. +13 −26 lib/SDLx/Controller.pm
  2. +21 −20 lib/pods/SDLx/Controller.pod
View
39 lib/SDLx/Controller.pm
@@ -22,14 +22,13 @@ my %_event_handlers;
my %_move_handlers;
my %_show_handlers;
my %_delay;
-my %_eoq;
my %_paused;
my %_time;
-my %_eoq_handler;
+my %_stop_handler;
sub new {
my ($self, %args) = @_;
-
+
# if $self is blessed then it has to isa controller, so let's not even bless it to this class
unless(ref $self) {
my $a;
@@ -47,10 +46,9 @@ sub new {
$_move_handlers{ $ref } = $args{move_handlers} || [];
$_show_handlers{ $ref } = $args{show_handlers} || [];
$_delay{ $ref } = (defined $args{delay} && $args{delay} >= 1 ? $args{delay} / 1000 : $args{delay}) || 0; #phasing out ticks, but still accepting them. Remove whenever we break compat
- $_eoq{$ref} = $args{exit_on_quit} || $args{eoq};
# $_paused{ $ref } = $args{paused}; #no point
$_time{ $ref } = $args{time} || 0;
- $_eoq_handler{ $ref } = $args{exit_on_quit_handler} || $args{eoq_handler} || \&_default_exit_on_quit_handler;
+ $_stop_handler{ $ref } = $args{stop_handler} || \&_default_stop_handler;
return $self;
}
@@ -68,10 +66,9 @@ sub DESTROY {
delete $_move_handlers{ $ref};
delete $_show_handlers{ $ref};
delete $_delay { $ref};
- delete $_eoq{ $ref};
delete $_paused{ $ref};
delete $_time{ $ref};
- delete $_eoq_handler{ $ref};
+ delete $_stop_handler{ $ref};
}
sub run {
@@ -109,7 +106,7 @@ sub run {
$dt = $_dt{ $ref}; #these can change
$min_t = $_min_t{ $ref}; #during the cycle
-
+
Time::HiRes::sleep( $_delay{ $ref } ) if $_delay{ $ref };
}
@@ -123,7 +120,7 @@ sub pause {
while(1) {
SDL::Events::wait_event($_event{ $ref}) or Carp::confess("pause failed waiting for an event");
if(
- $_eoq{ $ref} && do { $_eoq_handler{ $ref}->( $_event{ $ref}, $self ); $_stop{ $ref} }
+ $_stop_handler{ $ref} && do { $_stop_handler{ $ref}->( $_event{ $ref}, $self ); $_stop{ $ref} }
or !$callback or $callback->($_event{ $ref}, $self)
) {
$_current_time{ $ref} = Time::HiRes::time; #so run doesn't catch up with the time paused
@@ -141,7 +138,7 @@ sub _event {
my ($self, $ref) = @_;
SDL::Events::pump_events();
while ( SDL::Events::poll_event( $_event{ $ref} ) ) {
- $_eoq_handler{ $ref}->( $_event{ $ref}, $self ) if $_eoq{ $ref};
+ $_stop_handler{ $ref}->( $_event{ $ref}, $self ) if $_stop_handler{ $ref};
foreach my $event_handler ( @{ $_event_handlers{ $ref} } ) {
next unless $event_handler;
$event_handler->( $_event{ $ref}, $self );
@@ -273,28 +270,18 @@ sub delay {
$_delay{ $ref};
}
-sub exit_on_quit {
- my ($self, $arg) = @_;
- my $ref = refaddr $self;
- $_eoq{ $ref} = $arg if defined $arg;
-
- $_eoq{ $ref};
-}
-*eoq = \&exit_on_quit; # alias
-
-sub exit_on_quit_handler {
+sub stop_handler {
my ($self, $arg) = @_;
my $ref = refaddr $self;
- $_eoq_handler{ $ref} = $arg if defined $arg;
+ $_stop_handler{ $ref} = $arg if @_ > 1;
- $_eoq_handler{ $ref};
+ $_stop_handler{ $ref};
}
-*eoq_handler = \&exit_on_quit_handler; #alias
-sub _default_exit_on_quit_handler {
- my ($self, $event) = @_;
+sub _default_stop_handler {
+ my ($event, $self) = @_;
- $self->stop() if $event->type == SDL_QUIT;
+ $self->stop() if $event->type == SDL_QUIT;
}
sub event {
View
41 lib/pods/SDLx/Controller.pod
@@ -58,11 +58,10 @@ ease.
dt => 0.05,
min_t => 0,
delay => 1 / 200,
- eoq => 1,
- eoq_handler => \&eoq_handler,
event_handlers => [ @event_callbacks ],
move_handlers => [ @move_callbacks ],
show_handlers => [ @show_callbacks ],
+ stop_handler => \&stop_handler,
event => $event,
time => 99,
);
@@ -92,16 +91,6 @@ Setting it to 0, as seen above, will not delay the loop at all.
The time, in seconds, to delay after every full app loop. Defaults to 0.
B<NOTE:> Picking a good delay based on the needs can hugely reduce CPU load and pressure.
-=item exit_on_quit (shortcut: eoq)
-
-If true, the app loop will stop when an C<SDL_QUIT> event is triggered. Defaults to false.
-You can also specify your own exit on quit handler, but the default will do exactly what was just mentioned.
-
-=item exit_on_quit_handler (shortcut: eoq_handler)
-
-An alternative callback to handle quitting. Defaults to a callback that stops the event loop on an C<SDL_QUIT> event.
-Things that should be specified here are additional ways to end the app, like on pressing Esc or Alt-F4.
-
=item event_handlers
=item move_handlers
@@ -113,6 +102,24 @@ This is basically a shortcut way of adding handlers.
They would otherwise be added with their corresponding C<add_..._handler> method.
See below for a full explanation of the L</run> loop and handlers.
+=item stop_handler
+
+An extra, but separate, event callback to handle all L<stopping|/stop> of the app.
+It is the same in almost every way to an event handler (see L</run>): same recieved arguments, called in the same place.
+One difference is that it is called in L</pause> so that the app can be stopped while paused.
+Another difference is that it should always apply to the app; while you add and remove and clear event handlers,
+this wont be touched. This is good, because you'd (probably) always want your app to able to be stopped.
+Because of this, it's a good idea to use the stop handler regardless of whether you will be using L</pause>.
+
+Defaults to a callback that L<stops|/stop> the event loop on an C<SDL_QUIT> event.
+Specify a code ref to use a different callback to handle quitting, or a false value to not use a stop handler.
+If you want to provide your own stop handler you should give it the code of the default stop handler:
+
+ my ($event, $self) = @_;
+ $self->stop() if $event->type == SDL_QUIT;
+
+followed by any other code to handle events also triggering the app to stop, such as pressing Esc.
+
=item event
The C<SDL::Event> object that events going to the event callbacks are polled in to. Defaults to C<< SDL::Event->new() >>.
@@ -357,20 +364,14 @@ This is only useful when used within code that will be run by L</pause>:
$app->add_event_handler(\&toggle_pause);
All the examples here are of recursive pause callbacks, but, of course, yours don't have to be.
-
-=head2 dt
-=head2 exit_on_quit
-
-(shortcut: eoq)
+=head2 dt
=head2 min_t
=head2 delay
-=head2 exit_on_quit_handler
-
-(shortcut: eoq_handler)
+=head2 stop_handler
=head2 event

0 comments on commit b84b9b3

Please sign in to comment.
Something went wrong with that request. Please try again.