.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "pods::SDLx::Controller 3pm" .TH pods::SDLx::Controller 3pm 2024-03-28 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME SDLx::Controller \- Handles the loops for events, movement and rendering .SH CATEGORY .IX Header "CATEGORY" Extension, Controller .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& use SDLx::Controller; \& \& # create our controller object \& my $app = SDLx::Controller\->new; \& \& # we could also do: \& my $app = SDLx::App\->new; \& # because App is also a controller \& \& # register some callbacks \& $app\->add_event_handler( \e&on_event ); \& $app\->add_move_handler( \e&on_move ); \& $app\->add_show_handler( \e&on_show ); \& \& # run our game loop \& $app\->run; .Ve .SS DESCRIPTION .IX Subsection "DESCRIPTION" The core of an SDL application/game is the main loop, where you handle events and display your elements on the screen until something signals the end of the program. This usually goes in the form of: .PP .Vb 3 \& while (1) { \& ... \& } .Ve .PP The problem most developers face, besides the repetitive work, is to ensure the screen update is independent of the frame rate. Otherwise, your game will run at different speeds on different machines and this is never good (old MS-DOS games, anyone?). .PP One way to circumvent this is by capping the frame rate so it's the same no matter what, but this is not the right way to do it as it penalizes better hardware. .PP This module provides an industry-proven standard for frame independent movement. It calls the movement handlers based on time (hi-res seconds) rather than frame rate. You can add/remove handlers and control your main loop with ease. .SH METHODS .IX Header "METHODS" .SS new .IX Subsection "new" .Vb 5 \& SDLx::Controller\->new( \& dt => 0.5, \& min_t => 0, \& event => $event_object, \& ); .Ve .PP The \f(CW\*(C`dt\*(C'\fR parameter specifies the length, in seconds, of a full movement step, and defaults to 0.1. The \f(CW\*(C`dt\*(C'\fR can be anything and the game can still look the same. It is only when you change the \f(CW\*(C`dt\*(C'\fR without changing all the things in the movement step that are being multiplied by the first move argument that it will make a difference. If you lower the \f(CW\*(C`dt\*(C'\fR, everything will move faster than it did with it set higher, and vice-versa. This is useful to add slo-mo and fast-forward features to the game, all you would have to do is change the \f(CW\*(C`dt\*(C'\fR. .PP \&\f(CW\*(C`min_t\*(C'\fR specifies the minimum time, in seconds, that has to accumulate before any move or show handlers are called, and defaults to 1 / 60. Having the \f(CW\*(C`min_t\*(C'\fR at 1 / 60 ensures that the controller can update the screen at a maximum of 60 times per second. A "V\-Sync" such as this is necessary to prevent video "tear", which occurs when the app is updating faster than the monitor can display. Setting it to 0, as seen above, will let the app run as fast as it possibly can. .PP \&\f(CW\*(C`delay\*(C'\fR specifies a loop delay in millisecs to place on the controller loop. \fBNOTE:\fR Picking a good delay based on the needs can help reduce CPU load and pressure. .PP \&\f(CW\*(C`event\*(C'\fR is a SDL::Event object that events going to the event callbacks are polled in to. It defaults to \f(CW\*(C`SDL::Event\->new()\*(C'\fR. .PP All parameters are optional. .PP Returns the new object. .SS run .IX Subsection "run" After creating and setting up your handlers (see below), call this method to activate the main loop. The main loop will run until \f(CW\*(C`stop\*(C'\fR is called. .PP All hooked functions will be called during the main loop, in this order: .IP "1. Events" 4 .IX Item "1. Events" .PD 0 .IP "2. Movements" 4 .IX Item "2. Movements" .IP "3. Displaying" 4 .IX Item "3. Displaying" .PD .PP Please refer to each handler below for information on received arguments. Note that the second argument received by every callback is the \f(CW\*(C`SDLx::Controller\*(C'\fR object. .SS stop .IX Subsection "stop" Returns from the \f(CW\*(C`run\*(C'\fR loop. .SS pause .IX Subsection "pause" Attempts to pause the application with a call to \f(CW\*(C`SDL::Events::wait_event\*(C'\fR. See SDL::Events. .PP Takes 1 argument which is a callback. The application waits for the next event with \f(CW\*(C`wait_event\*(C'\fR. When one is received, it is passed to the callback as the first argument, along with the \f(CW\*(C`SDLx::Controller\*(C'\fR object as the second argument. If the callback then returns a true value, \f(CW\*(C`pause\*(C'\fR will return. If the callback returns a false value, \f(CW\*(C`pause\*(C'\fR will repeat the process. .PP This can be used to easily implement a pause when the app loses focus: .PP .Vb 10 \& sub window { \& 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_ACTIVEEVENT) { \& if($e\->active_state & SDL_APPINPUTFOCUS) { \& if($e\->active_gain) { \& return 1; \& } \& else { \& $app\->pause(\e&window); \& # recursive, but only once since the window \& # can\*(Aqt lose focus again without gaining it first \& } \& } \& } \& return 0; \& } .Ve .PP Note: if you implement your own pause function, remember to update \f(CW\*(C`current_time\*(C'\fR to the current time when the application unpauses. This should be done with \f(CW\*(C`Time::HiRes::time\*(C'\fR. Otherwise, time will accumulate while the application is paused, and many movement steps will be called all at once when it unpauses. .PP Note 2: a pause will be potentially dangerous to the \f(CW\*(C`run\*(C'\fR cycle (even if you implement your own) unless called by an \f(CW\*(C`event\*(C'\fR callback. .SS paused .IX Subsection "paused" Returns 1 if the app is paused, undef otherwise. This is only useful when used within code that will be run by \f(CW\*(C`pause\*(C'\fR: .PP .Vb 2 \& 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\*(Aqre paused, so end pause \& return 1 if $app\->paused; \& \& # We\*(Aqre not paused, so pause \& $app\->pause(\e&pause); \& } \& } \& return 0; \& } .Ve .SS add_event_handler .IX Subsection "add_event_handler" Register a callback to handle events. You can add as many subs as you need. Whenever a SDL::Event occurs, all registered callbacks will be triggered in order. Returns the order queue number of the added callback. .PP The first argument passed to registered callbacks is the SDL::Event object. The second is the \f(CW\*(C`SDLx::Controller\*(C'\fR object. .PP .Vb 7 \& sub stop { \& my ($event, $app) = @_; \& if($event\->type == SDL_QUIT) { \& $app\->stop; \& } \& } \& $app\->add_event_handler(\e&stop); .Ve .SS add_move_handler .IX Subsection "add_move_handler" Register a callback to update your objects. You can add as many subs as you need. Returns the order queue number of the added callback. .PP All registered callbacks will be triggered in order for as many \f(CW\*(C`dt\*(C'\fR as have happened between calls, and once more for any remaining time less than \f(CW\*(C`dt\*(C'\fR. The first argument passed to the callbacks is the portion of the step, which will be 1 for a full step, and less than 1 for a partial step. Movement values should be multiplied by this value. The full steps correspond to the amount of \f(CW\*(C`dt\*(C'\fR passed between calls, and the partial step corresponds to the call with the remaining time less than \f(CW\*(C`dt\*(C'\fR. The argument can be 0 if no time has passed since the last cycle. If you need to protect against this, set a \f(CW\*(C`min_t\*(C'\fR, or put a \f(CW\*(C`return unless $_[0]\*(C'\fR at the start of every move handler. .PP The second argument passed to the callbacks is the \f(CW\*(C`SDLx::Controller\*(C'\fR object. The third is the total amount of time passed since the call of \f(CW\*(C`run\*(C'\fR. .PP You should use these handlers to update your in-game objects, check collisions, etc. so you can check and/or update it as necessary. .PP .Vb 5 \& sub move_ball { \& my ($step, $app, $t) = @_; \& $ball\->move_x( $ball\->x_vel * $step ); \& $ball\->move_y( $ball\->y_vel * $step ); \& } .Ve .SS add_show_handler .IX Subsection "add_show_handler" Register a callback to render objects. You can add as many subs as you need. Returns the order queue number of the added callback. All registered callbacks will be triggered in order, once per run of the \f(CW\*(C`run\*(C'\fR loop. .PP The first argument passed is the time, in seconds, since the previous call. The second is the \f(CW\*(C`SDLx::Controller\*(C'\fR object. .PP .Vb 7 \& sub show_ball { \& my ($delta, $app) = @_; \& $app\->draw_rect( \& [ $ball\->x, $ball\->y, $ball\->size, $ball\->size ], \& $ball\->colour \& ); \& } .Ve .ie n .SS "remove_move_handler( $index )" .el .SS "remove_move_handler( \f(CW$index\fP )" .IX Subsection "remove_move_handler( $index )" .ie n .SS "remove_event_handler( $index )" .el .SS "remove_event_handler( \f(CW$index\fP )" .IX Subsection "remove_event_handler( $index )" .ie n .SS "remove_show_handler( $index )" .el .SS "remove_show_handler( \f(CW$index\fP )" .IX Subsection "remove_show_handler( $index )" Removes the handler with the given index from the respective calling queue. .PP You can also pass a coderef. The first coderef in the handler list that this matches will be removed. .PP Returns the removed handler. .SS remove_all_move_handlers .IX Subsection "remove_all_move_handlers" .SS remove_all_event_handlers .IX Subsection "remove_all_event_handlers" .SS remove_all_show_handlers .IX Subsection "remove_all_show_handlers" Removes all handlers from the respective calling queue. .SS remove_all_handlers .IX Subsection "remove_all_handlers" Quick access to removing all handlers at once. .SS dt .IX Subsection "dt" .SS min_t .IX Subsection "min_t" .SS current_time .IX Subsection "current_time" If an argument is passed, modifies the corresponding value to the argument. \&\f(CW\*(C`dt\*(C'\fR and \f(CW\*(C`min_t\*(C'\fR will keep their old value until the beginning of the next \f(CW\*(C`run\*(C'\fR cycle. .PP Returns the corresponding value. .SH AUTHORS .IX Header "AUTHORS" See "AUTHORS" in SDL. .SS ACKNOWLEDGEMENTS .IX Subsection "ACKNOWLEDGEMENTS" The idea and base for this module comes from Lazy Foo's Frame Independent Movement tutorial, and Glenn Fiedler's Fix Your Timestep article on timing.