updated docs
[sdlgit/SDL-Site.git] / pages / SDL-Joystick.html-inc
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="#METHODS">METHODS</a>
9 <ul><li><a href="#num_joysticks">num_joysticks</a></li>
10 <li><a href="#name">name</a></li>
11 <li><a href="#new">new</a></li>
12 <li><a href="#opened">opened</a></li>
13 <li><a href="#index">index</a></li>
14 <li><a href="#num_axes">num_axes</a></li>
15 <li><a href="#num_balls">num_balls</a></li>
16 <li><a href="#num_hats">num_hats</a></li>
17 <li><a href="#num_buttons">num_buttons</a></li>
18 <li><a href="#update">update</a></li>
19 <li><a href="#get_axis">get_axis</a></li>
20 <li><a href="#get_hat">get_hat</a></li>
21 <li><a href="#get_button">get_button</a></li>
22 <li><a href="#get_ball">get_ball</a></li>
23 <li><a href="#close">close</a></li>
24 </ul>
25 </li>
26 <li><a href="#AUTHORS">AUTHORS</a>
27 </li>
28 </ul><hr />
29 <!-- INDEX END -->
30
31 <h1 id="NAME">NAME</h1><p><a href="#TOP" class="toplink">Top</a></p>
32 <div id="NAME_CONTENT">
33 <p>SDL::Joystick -- SDL Bindings for the Joystick device</p>
34
35 </div>
36 <h1 id="CATEGORY">CATEGORY</h1><p><a href="#TOP" class="toplink">Top</a></p>
37 <div id="CATEGORY_CONTENT">
38 <p>Core, Joystick</p>
39
40 </div>
41 <h1 id="SYNOPSIS">SYNOPSIS</h1><p><a href="#TOP" class="toplink">Top</a></p>
42 <div id="SYNOPSIS_CONTENT">
43 <pre> use SDL;
44  use SDL::Joystick;
45
46  SDL::init_sub_system(SDL_INIT_JOYSTICK);
47
48  die('no joystick found') unless(SDL::Joystick::num_joysticks());
49
50  my $joystick = SDL::Joystick-&gt;new(0);
51
52 </pre>
53
54 </div>
55 <h1 id="METHODS">METHODS</h1><p><a href="#TOP" class="toplink">Top</a></p>
56 <div id="METHODS_CONTENT">
57
58 </div>
59 <h2 id="num_joysticks">num_joysticks</h2>
60 <div id="num_joysticks_CONTENT">
61 <pre> int SDL::Joystick::num_joysticks( void );
62
63 </pre>
64 <p>Counts and returns available joysticks.</p>
65
66 </div>
67 <h2 id="name">name</h2>
68 <div id="name_CONTENT">
69 <pre> string SDL::Joystick::name( index );
70
71 </pre>
72 <p>Get the implementation dependent name of joystick. The <code>index</code> parameter refers to the N'th joystick on the system. </p>
73 <pre> my $num_joysticks = SDL::Joystick::num_joysticks();
74
75  printf(&quot;%d joysticks found\n&quot;, $num_joysticks);
76
77  for($i = 0; $i &lt; $num_joysticks; $i++)
78  {
79      printf(&quot;%s\n&quot;, SDL::Joystick::name($i));
80  }
81
82 </pre>
83
84 </div>
85 <h2 id="new">new</h2>
86 <div id="new_CONTENT">
87 <pre> object SDL::Joystick-&gt;new( index );
88
89 </pre>
90 <p>Opens a joystick for use within SDL. The <code>index</code> refers to the N'th joystick in the system. 
91 A joystick must be opened before it can be used.</p>
92 <pre> # Initialize the joystick subsystem
93  SDL::init_sub_system(SDL_INIT_JOYSTICK);
94
95  # Check for joystick
96  if(SDL::Joystick::num_joysticks() &gt; 0)
97  {
98      # Open joystick
99      my $joystick = SDL::Joystick-&gt;new(0);
100
101      if($joystick)
102      {
103          printf(&quot;Opened Joystick 0\n&quot;);
104          printf(&quot;Name: %s\n&quot;,              SDL::Joystick::name(0));
105          printf(&quot;Number of Axes: %d\n&quot;,    SDL::Joystick::num_axes($joystick));
106          printf(&quot;Number of Buttons: %d\n&quot;, SDL::Joystick::num_buttons($joystick));
107          printf(&quot;Number of Balls: %d\n&quot;,   SDL::Joystick::num_balls($joystick));
108      }
109      else
110      {
111          printf(&quot;Couldn't open Joystick 0\n&quot;);
112      }
113
114      # Close if opened
115      SDL::Joystick::close($joystick) if SDL::Joystick::opened(0);
116  }
117
118 </pre>
119
120 </div>
121 <h2 id="opened">opened</h2>
122 <div id="opened_CONTENT">
123 <pre> int SDL::Joystick::opened( index );
124
125 </pre>
126 <p>Determines whether a joystick has already been opened within the application. <code>index</code> refers to the N'th joystick on the system.</p>
127 <p>Returns 1 if the joystick has been opened, or 0 if it has not.</p>
128
129 </div>
130 <h2 id="index">index</h2>
131 <div id="index_CONTENT">
132 <pre> int SDL::Joystick::index( object );
133
134 </pre>
135 <p>Returns the <code>index</code> of a given <code>SDL_Joystick</code> structure. See <a href="#new">SDL::Joystick::new</a></p>
136
137 </div>
138 <h2 id="num_axes">num_axes</h2>
139 <div id="num_axes_CONTENT">
140 <pre> int SDL::Joystick::num_axes( object );
141
142 </pre>
143 <p>Return the number of axes available from a previously opened joystick. See <a href="#new">SDL::Joystick::new</a></p>
144
145 </div>
146 <h2 id="num_balls">num_balls</h2>
147 <div id="num_balls_CONTENT">
148 <pre> int SDL::Joystick::num_balls( object );
149
150 </pre>
151 <p>Return the number of trackballs available from a previously opened joystick. See <a href="#new">SDL::Joystick::new</a></p>
152
153 </div>
154 <h2 id="num_hats">num_hats</h2>
155 <div id="num_hats_CONTENT">
156 <pre> int SDL::Joystick::num_hats( object );
157
158 </pre>
159 <p>Gets the number of joystick hats from a previously opened joystick. See <a href="#new">SDL::Joystick::new</a></p>
160
161 </div>
162 <h2 id="num_buttons">num_buttons</h2>
163 <div id="num_buttons_CONTENT">
164 <pre> int SDL::Joystick::num_buttons( object );
165
166 </pre>
167 <p>Gets the number of joystick buttons from a previously opened joystick. See <a href="#new">SDL::Joystick::new</a></p>
168
169 </div>
170 <h2 id="update">update</h2>
171 <div id="update_CONTENT">
172 <pre> void SDL::Joystick::update();
173
174 </pre>
175 <p>Updates the state(position, buttons, etc.) of all open joysticks. If joystick events have been enabled 
176 with <code>SDL::Joystick::event_state</code> then this is called automatically in the event loop. </p>
177
178 </div>
179 <h2 id="get_axis">get_axis</h2>
180 <div id="get_axis_CONTENT">
181 <p><code>get_axis</code> returns the current state of the given axis on the given joystick.</p>
182 <p>On most modern joysticks the X axis is usually represented by axis 0 and the Y axis by axis 1. 
183 The value returned by <code>get_axis</code> is a signed integer (-32768 to 32767) representing the current position of the axis, 
184 it may be necessary to impose certain tolerances on these values to account for jitter.</p>
185 <p><strong>Note</strong>: Some joysticks use axes 2 and 3 for extra buttons. </p>
186 <p>Returns a 16-bit signed integer representing the current position of the axis.</p>
187 <pre> my $joystick = SDL::Joystick-&gt;new(0);
188
189  my $x_move   = SDL::Joystick::get_axis($joystick, 0);
190  my $y_move   = SDL::Joystick::get_axis($joystick, 1);
191
192 </pre>
193
194 </div>
195 <h2 id="get_hat">get_hat</h2>
196 <div id="get_hat_CONTENT">
197 <pre> int SDL::Joystick::get_hat( object, int );
198
199 </pre>
200 <p><code>get_hat</code> returns the current state of the given <code>hat</code> on the given <code>joystick</code>. </p>
201 <p>The current state is returned which is an OR'd combination of one or more of the following:</p>
202 <ul>
203                 <li><code>SDL_HAT_CENTERED</code>       </li>
204                 <li><code>SDL_HAT_UP</code>     </li>
205                 <li><code>SDL_HAT_RIGHT</code>  </li>
206                 <li><code>SDL_HAT_DOWN</code>   </li>
207                 <li><code>SDL_HAT_LEFT</code>   </li>
208                 <li><code>SDL_HAT_RIGHTUP</code>        </li>
209                 <li><code>SDL_HAT_RIGHTDOWN</code>      </li>
210                 <li><code>SDL_HAT_LEFTUP</code> </li>
211                 <li><code>SDL_HAT_LEFTDOWN</code></li>
212 </ul>
213
214 <pre> my $joystick = SDL::Joystick-&gt;new(0);
215
216  my $position = SDL::Joystick::get_hat($joystick, 0);
217
218  print(&quot;hat is in position UP\n&quot;) if $position &amp; SDL_HAT_UP;
219
220 </pre>
221
222 </div>
223 <h2 id="get_button">get_button</h2>
224 <div id="get_button_CONTENT">
225 <pre> int SDL::Joystick::get_button( object, int );
226
227 </pre>
228 <p><code>get_button</code> returns the current state of the given button on the given joystick.</p>
229 <p>Returns 1 if the button is pressed. Otherwise, 0. </p>
230 <pre> my $joystick    = SDL::Joystick-&gt;new(0);
231
232  my $num_buttons = SDL::Joystick::num_buttons($joystick);
233
234  for(my $i = 0; $i &lt; $num_buttons; $i++)
235  {
236      printf(&quot;button %d is %s\n&quot;, $i, SDL::Joystick::get_button($joystick, $i) ? 'pressed' : 'not pressed');
237  }
238
239  SDL::Joystick::close($joystick) if SDL::Joystick::opened(0);
240
241 </pre>
242
243 </div>
244 <h2 id="get_ball">get_ball</h2>
245 <div id="get_ball_CONTENT">
246 <pre> int SDL::Joystick::get_ball(SDL_Joystick $joystick, int $ball, int $dx, int $dy);
247
248 </pre>
249 <p>Get the ball axis change.</p>
250 <p>Trackballs can only return relative motion since the last call to SDL::Joystick::get_ball, these motion deltas are placed into <code>dx</code> and <code>dy</code>.</p>
251 <p>Returns 0 on success or -1 on failure</p>
252 <pre> my $delta_x  = 0;
253  my $delta_y  = 0;
254  my $joystick = SDL::Joystick-&gt;new(0);
255
256  SDL::Joystick::update();
257
258  printf(&quot;TrackBall Read Error!\n&quot;) if(SDL::JoystickGetBall($joystick, 0, $delta_x, $delta_y) == -1);
259  printf(&quot;Trackball Delta- X:%d, Y:%d\n&quot;, delta_x, delta_y);
260
261 </pre>
262
263 </div>
264 <h2 id="close">close</h2>
265 <div id="close_CONTENT">
266 <pre> void SDL::Joystick::close( object );
267
268 </pre>
269 <p>Closes a previously opened joystick. See <a href="#new">SDL::Joystick::new</a></p>
270 <pre> SDL::Joystick::close($joystick) if SDL::Joystick::opened(0);
271
272 </pre>
273
274 </div>
275 <h1 id="AUTHORS">AUTHORS</h1><p><a href="#TOP" class="toplink">Top</a></p>
276 <div id="AUTHORS_CONTENT">
277 <p>See <b>AUTHORS</b> in <cite>SDL</cite>.</p>
278
279 </div>
280 </div>