Merge branch 'master' of git.shadowcat.co.uk:SDL-Site
[sdlgit/SDL-Site.git] / pages / SDLx-Controller-Interface.html-inc
CommitLineData
3afd3fb0 1<div class="pod">
2<!-- INDEX START -->
3<h3 id="TOP">Index</h3>
4
5<ul><li><a href="#NAME">NAME</a></li>
6<li><a href="#CATEGORY">CATEGORY</a></li>
7<li><a href="#SYNOPSIS">SYNOPSIS</a></li>
8<li><a href="#DESCRIPTION">DESCRIPTION</a></li>
9<li><a href="#METHODS">METHODS</a>
10<ul><li><a href="#set_acceleration">set_acceleration</a></li>
11<li><a href="#attach">attach</a></li>
12<li><a href="#current">current</a></li>
13<li><a href="#previous">previous</a></li>
14<li><a href="#detach">detach</a></li>
15</ul>
16</li>
17<li><a href="#OTHER_METHODS">OTHER METHODS</a>
18<ul><li><a href="#acceleration">acceleration</a></li>
19<li><a href="#interpolate">interpolate</a></li>
20<li><a href="#evaluate">evaluate</a></li>
21<li><a href="#update">update</a></li>
22</ul>
23</li>
24<li><a href="#AUTHORS">AUTHORS</a>
25</li>
26</ul><hr />
27<!-- INDEX END -->
28
29<h1 id="NAME">NAME</h1><p><a href="#TOP" class="toplink">Top</a></p>
30<div id="NAME_CONTENT">
31<p>SDLx::Controller::Interface - Interface Physics and Rendering with the Controller with callbacks</p>
32
33</div>
34<h1 id="CATEGORY">CATEGORY</h1><p><a href="#TOP" class="toplink">Top</a></p>
35<div id="CATEGORY_CONTENT">
39c8ca1e 36<p>Extension, Controller</p>
3afd3fb0 37
38</div>
39<h1 id="SYNOPSIS">SYNOPSIS</h1><p><a href="#TOP" class="toplink">Top</a></p>
40<div id="SYNOPSIS_CONTENT">
41<pre> use SDL;
42 use SDLx::App;
43 use SDLx::Controller::Interface;
44
45 #SDLx::App is a controller
46 my $app = SDLx::App-&gt;new(width =&gt; 200, height =&gt; 200 );
47
48 my $ball = SDLx::Controller::Interface-&gt;new( x=&gt; 10, y =&gt; 50, v_x =&gt; 10, v_y=&gt; 20 );
49 #Set the initial state of the ball's physics, this is optional
50
51 $ball-&gt;set_acceleration(
52 sub {
53 my ($time, $current_state) = @_;
54 return( 0, -10, 0 ); # Return accelerations (x,y,rotation)
55 }
56 );
57
58 my $ball_render = sub {
59 my $state = shift;
60
61 $app-&gt;draw_rect( undef, 0 );
62 $app-&gt;draw_rect( [$state-&gt;x, $state-&gt;y, 10,10], [255,0,0,255] );
63 $app-&gt;update();
64 };
65
66
67
68
69 $ball-&gt;attach( $app, $ball_render, @params );
70
71 $app-&gt;run();
72
73 $ball-&gt;detach(); #can be called at anytime (for example when ball 'dies')
74
75</pre>
76
77</div>
78<h1 id="DESCRIPTION">DESCRIPTION</h1><p><a href="#TOP" class="toplink">Top</a></p>
79<div id="DESCRIPTION_CONTENT">
80
81</div>
82<h1 id="METHODS">METHODS</h1><p><a href="#TOP" class="toplink">Top</a></p>
83<div id="METHODS_CONTENT">
84
85</div>
86<h2 id="set_acceleration">set_acceleration</h2>
87<div id="set_acceleration_CONTENT">
88<p>Allows you to set the acceleration callback for defining the inferface's
89behaviour in terms of x,y and rotation.</p>
90<pre> $interface-&gt;set_acceleration (
91 sub {
92 my ($time, $current_state) = @_;
93
94 return ( $accel_x, $accel_y, $torque );
95 }
96 );
97
98</pre>
99<p>These accelerations are arbitary and can be set to any frame of reference.
100Your render callback will handle how to interpret it.</p>
101<p>The callback will receive the time and the current state as a
102<code>SDLx::Controller::State</code> element.</p>
103
104</div>
105<h2 id="attach">attach</h2>
106<div id="attach_CONTENT">
107<p>Attaches the interface to a controller with a render callback</p>
108<pre> $interface-&gt;attach( $controller, $render, @params );
109
110</pre>
111<p>Where $render is a callback that receives the interpolated
112<code>SDLx::Controller::State</code>.</p>
113<pre> my $render = sub {
114 my ($state, @params) = @_;
115 # draw the current $state.
116 };
117
118</pre>
119<p>The @params are any extra parameters you would like to pass to the $render
120callback.</p>
121
122</div>
123<h2 id="current">current</h2>
124<div id="current_CONTENT">
125<pre> my $current_state = $interface-&gt;current();
126
127</pre>
128<p>Returns the current state of the interface as a <code>SDLx::Controller::State</code>.</p>
129
130</div>
131<h2 id="previous">previous</h2>
132<div id="previous_CONTENT">
133<pre> my $previous_state = $interface-&gt;previous();
134
135</pre>
136<p>Returns the previous state of the interface as a <code>SDLx::Controller::State</code>.</p>
137
138</div>
139<h2 id="detach">detach</h2>
140<div id="detach_CONTENT">
141<pre> $interface-&gt;detach();
142
143</pre>
144<p>If $interface has been <code>attach()</code>'ed to any controller it will be detached now.</p>
145
146</div>
147<h1 id="OTHER_METHODS">OTHER METHODS</h1><p><a href="#TOP" class="toplink">Top</a></p>
148<div id="OTHER_METHODS_CONTENT">
149<p>Don't use these unless you really really want to.</p>
150
151</div>
152<h2 id="acceleration">acceleration</h2>
153<div id="acceleration_CONTENT">
154<p>Call the acceleration callback once.</p>
155
156</div>
157<h2 id="interpolate">interpolate</h2>
158<div id="interpolate_CONTENT">
159<p>Interpolate the current state</p>
160
161</div>
162<h2 id="evaluate">evaluate</h2>
163<div id="evaluate_CONTENT">
164<p>Evaluate the new currrent and previous state.</p>
165
166</div>
167<h2 id="update">update</h2>
168<div id="update_CONTENT">
169<p>Update the states by integrating with time.</p>
170
171</div>
172<h1 id="AUTHORS">AUTHORS</h1><p><a href="#TOP" class="toplink">Top</a></p>
173<div id="AUTHORS_CONTENT">
1dbe1697 174<p>See <a href="/SDL.html#AUTHORS">/SDL.html#AUTHORS</a>.</p>
3afd3fb0 175
176</div>
177</div>