PageRenderTime 21ms CodeModel.GetById 14ms app.highlight 3ms 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
  1
  2=pod
  3
  4=head1 NAME
  5
  6SDLx::Controller::Interface - Interface Physics and Rendering with the Controller with callbacks
  7
  8=head1 CATEGORY
  9
 10Extension, Controller
 11
 12=head1 SYNOPSIS
 13
 14 use SDL;
 15 use SDLx::App;
 16 use SDLx::Controller::Interface;
 17
 18 #SDLx::App is a controller
 19 my $app = SDLx::App->new(width => 200, height => 200 ); 
 20
 21 my $ball = SDLx::Controller::Interface->new( x=> 10, y => 50, v_x => 10, v_y=> 20 );
 22 #Set the initial state of the ball's physics, this is optional
 23
 24 $ball->set_acceleration( 
 25    sub { 
 26              my ($time, $current_state) = @_;
 27              return( 0, -10, 0 ); # Return accelerations (x,y,rotation)
 28        }
 29 );
 30
 31 my $ball_render = sub {
 32       my $state = shift;
 33 
 34       $app->draw_rect( undef, 0 );
 35       $app->draw_rect( [$state->x, $state->y, 10,10], [255,0,0,255] );
 36       $app->update();
 37 };
 38
 39
 40 $ball->attach( $app, $ball_render, @params ); 
 41
 42 $app->run();
 43
 44 $ball->detach(); #can be called at anytime (for example when ball 'dies')
 45
 46=head1 DESCRIPTION
 47
 48=head1 METHODS
 49
 50=head2 set_acceleration
 51
 52Allows you to set the acceleration callback for defining the interface's
 53behaviour in terms of x,y and rotation.
 54
 55  $interface->set_acceleration ( 
 56      sub {
 57         my ($time, $current_state) = @_; 
 58       
 59         return ( $accel_x, $accel_y, $torque );
 60      }
 61  );
 62
 63These accelerations are arbitrary and can be set to any frame of reference.
 64Your render callback will handle how to interpret it.
 65
 66The callback will receive the time and the current state as a
 67C<SDLx::Controller::State> element.
 68
 69=head2 attach
 70
 71Attaches the interface to a controller with a render callback
 72
 73  $interface->attach( $controller, $render, @params );
 74
 75Where $render is a callback that receives the interpolated
 76C<SDLx::Controller::State>.
 77
 78  my $render = sub {
 79        my ($state, @params) = @_; 
 80        # draw the current $state.
 81  };
 82
 83The @params are any extra parameters you would like to pass to the $render
 84callback.
 85
 86=head2 current
 87
 88  my $current_state = $interface->current();
 89
 90Returns the current state of the interface as a C<SDLx::Controller::State>.
 91
 92=head2 previous
 93
 94  my $previous_state = $interface->previous();
 95
 96Returns the previous state of the interface as a C<SDLx::Controller::State>.
 97
 98=head2 detach
 99
100  $interface->detach();
101
102If $interface has been C<attach()>'ed to any controller it will be detached now.
103
104=head1 OTHER METHODS
105
106Don't use these unless you really really want to.
107
108=head2 acceleration
109
110Call the acceleration callback once.
111
112=head2 interpolate
113
114Interpolate the current state
115
116=head2 evaluate
117
118Evaluate the new current and previous state.
119
120=head2 update
121
122Update the states by integrating with time.
123
124=head1 AUTHORS
125
126See L<SDL/AUTHORS>.
127
128=cut