PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/pods/SDLx/Controller/Interface.pod

http://github.com/PerlGameDev/SDL
Unknown | 128 lines | 76 code | 52 blank | 0 comment | 0 complexity | 4d325555b25be060f1d6837ec526dcf4 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0
  1. =pod
  2. =head1 NAME
  3. SDLx::Controller::Interface - Interface Physics and Rendering with the Controller with callbacks
  4. =head1 CATEGORY
  5. Extension, Controller
  6. =head1 SYNOPSIS
  7. use SDL;
  8. use SDLx::App;
  9. use SDLx::Controller::Interface;
  10. #SDLx::App is a controller
  11. my $app = SDLx::App->new(width => 200, height => 200 );
  12. my $ball = SDLx::Controller::Interface->new( x=> 10, y => 50, v_x => 10, v_y=> 20 );
  13. #Set the initial state of the ball's physics, this is optional
  14. $ball->set_acceleration(
  15. sub {
  16. my ($time, $current_state) = @_;
  17. return( 0, -10, 0 ); # Return accelerations (x,y,rotation)
  18. }
  19. );
  20. my $ball_render = sub {
  21. my $state = shift;
  22. $app->draw_rect( undef, 0 );
  23. $app->draw_rect( [$state->x, $state->y, 10,10], [255,0,0,255] );
  24. $app->update();
  25. };
  26. $ball->attach( $app, $ball_render, @params );
  27. $app->run();
  28. $ball->detach(); #can be called at anytime (for example when ball 'dies')
  29. =head1 DESCRIPTION
  30. =head1 METHODS
  31. =head2 set_acceleration
  32. Allows you to set the acceleration callback for defining the interface's
  33. behaviour in terms of x,y and rotation.
  34. $interface->set_acceleration (
  35. sub {
  36. my ($time, $current_state) = @_;
  37. return ( $accel_x, $accel_y, $torque );
  38. }
  39. );
  40. These accelerations are arbitrary and can be set to any frame of reference.
  41. Your render callback will handle how to interpret it.
  42. The callback will receive the time and the current state as a
  43. C<SDLx::Controller::State> element.
  44. =head2 attach
  45. Attaches the interface to a controller with a render callback
  46. $interface->attach( $controller, $render, @params );
  47. Where $render is a callback that receives the interpolated
  48. C<SDLx::Controller::State>.
  49. my $render = sub {
  50. my ($state, @params) = @_;
  51. # draw the current $state.
  52. };
  53. The @params are any extra parameters you would like to pass to the $render
  54. callback.
  55. =head2 current
  56. my $current_state = $interface->current();
  57. Returns the current state of the interface as a C<SDLx::Controller::State>.
  58. =head2 previous
  59. my $previous_state = $interface->previous();
  60. Returns the previous state of the interface as a C<SDLx::Controller::State>.
  61. =head2 detach
  62. $interface->detach();
  63. If $interface has been C<attach()>'ed to any controller it will be detached now.
  64. =head1 OTHER METHODS
  65. Don't use these unless you really really want to.
  66. =head2 acceleration
  67. Call the acceleration callback once.
  68. =head2 interpolate
  69. Interpolate the current state
  70. =head2 evaluate
  71. Evaluate the new current and previous state.
  72. =head2 update
  73. Update the states by integrating with time.
  74. =head1 AUTHORS
  75. See L<SDL/AUTHORS>.
  76. =cut