3 <h3 id="TOP">Index</h3>
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="#open">open</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>
30 <h1 id="NAME">NAME</h1><p><a href="#TOP" class="toplink">Top</a></p>
31 <div id="NAME_CONTENT">
32 <p>SDL::Joystick -- SDL Bindings for the Joystick device</p>
35 <h1 id="CATEGORY">CATEGORY</h1><p><a href="#TOP" class="toplink">Top</a></p>
36 <div id="CATEGORY_CONTENT">
40 <h1 id="SYNOPSIS">SYNOPSIS</h1><p><a href="#TOP" class="toplink">Top</a></p>
41 <div id="SYNOPSIS_CONTENT">
45 SDL::init_sub_system(SDL_INIT_JOYSTICK);
47 die('no joystick found') unless(SDL::Joystick::num_joysticks());
49 my $joystick = SDL::Joystick->new(0);
54 <h1 id="METHODS">METHODS</h1><p><a href="#TOP" class="toplink">Top</a></p>
55 <div id="METHODS_CONTENT">
58 <h2 id="num_joysticks">num_joysticks</h2>
59 <div id="num_joysticks_CONTENT">
60 <pre> int SDL::Joystick::num_joysticks( void );
63 <p>Counts and returns available joysticks.</p>
66 <h2 id="name">name</h2>
67 <div id="name_CONTENT">
68 <pre> string SDL::Joystick::name( index );
71 <p>Get the implementation dependent name of joystick. The <code>index</code> parameter refers to the N'th joystick on the system. </p>
72 <pre> my $num_joysticks = SDL::Joystick::num_joysticks();
74 printf("%d joysticks found\n", $num_joysticks);
76 for($i = 0; i < $num_joysticks; $i++)
78 printf("%s\n", SDL::Joystick::name($i));
84 <h2 id="open">open</h2>
85 <div id="open_CONTENT">
86 <pre> object SDL::Joystick::open( index );
89 <p>Opens a joystick for use within SDL. The <code>index</code> refers to the N'th joystick in the system.
90 A joystick must be opened before it can be used.</p>
91 <pre> # Initialize the joystick subsystem
92 SDL::init_sub_system(SDL_INIT_JOYSTICK);
95 if(SDL::Joystick::num_joysticks() > 0)
98 my $joystick = SDL::Joystick::open(0);
102 printf("Opened Joystick 0\n");
103 printf("Name: %s\n", SDL::Joystick::name(0));
104 printf("Number of Axes: %d\n", SDL::Joystick::num_axes($joystick));
105 printf("Number of Buttons: %d\n", SDL::Joystick::num_buttons($joystick));
106 printf("Number of Balls: %d\n", SDL::Joystick::num_balls($joystick));
110 printf("Couldn't open Joystick 0\n");
114 SDL::Joystick::close($joystick) if SDL::Joystick::opened(0);
120 <h2 id="opened">opened</h2>
121 <div id="opened_CONTENT">
122 <pre> int SDL::Joystick::opened( index );
125 <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>
126 <p>Returns 1 if the joystick has been opened, or 0 if it has not.</p>
129 <h2 id="index">index</h2>
130 <div id="index_CONTENT">
131 <pre> int SDL::Joystick::index( object );
134 <p>Returns the <code>index</code> of a given <code>SDL_Joystick</code> structure. See <a href="#open">SDL::Joystick::open</a></p>
137 <h2 id="num_axes">num_axes</h2>
138 <div id="num_axes_CONTENT">
139 <pre> int SDL::Joystick::num_axes( object );
142 <p>Return the number of axes available from a previously opened joystick. See <a href="#open">SDL::Joystick::open</a></p>
145 <h2 id="num_balls">num_balls</h2>
146 <div id="num_balls_CONTENT">
147 <pre> int SDL::Joystick::num_balls( object );
150 <p>Return the number of trackballs available from a previously opened joystick. See <a href="#open">SDL::Joystick::open</a></p>
153 <h2 id="num_hats">num_hats</h2>
154 <div id="num_hats_CONTENT">
155 <pre> int SDL::Joystick::num_hats( object );
158 <p>Gets the number of joystick hats from a previously opened joystick. See <a href="#open">SDL::Joystick::open</a></p>
161 <h2 id="num_buttons">num_buttons</h2>
162 <div id="num_buttons_CONTENT">
163 <pre> int SDL::Joystick::num_buttons( object );
166 <p>Gets the number of joystick buttons from a previously opened joystick. See <a href="/SDL-Joystick::open.html">SDL::Joystick::open</a></p>
169 <h2 id="update">update</h2>
170 <div id="update_CONTENT">
171 <pre> void SDL::Joystick::update();
174 <p>Updates the state(position, buttons, etc.) of all open joysticks. If joystick events have been enabled
175 with <code>SDL::Joystick::event_state</code> then this is called automatically in the event loop. </p>
178 <h2 id="get_axis">get_axis</h2>
179 <div id="get_axis_CONTENT">
180 <p><code>get_axis</code> returns the current state of the given axis on the given joystick.</p>
181 <p>On most modern joysticks the X axis is usually represented by axis 0 and the Y axis by axis 1.
182 The value returned by <code>get_axis</code> is a signed integer (-32768 to 32767) representing the current position of the axis,
183 it may be necessary to impose certain tolerances on these values to account for jitter.</p>
184 <p><strong>Note</strong>: Some joysticks use axes 2 and 3 for extra buttons. </p>
185 <p>Returns a 16-bit signed integer representing the current position of the axis.</p>
186 <pre> my $joystick = SDL::Joystick::open(0);
188 my $x_move = SDL::Joystick::get_axis($joystick, 0);
189 my $y_move = SDL::Joystick::get_axis($joystick, 1);
194 <h2 id="get_hat">get_hat</h2>
195 <div id="get_hat_CONTENT">
196 <pre> int SDL::Joystick::get_hat( object, int );
199 <p><code>get_hat</code> returns the current state of the given <code>hat</code> on the given <code>joystick</code>. </p>
200 <p>The current state is returned which is an OR'd combination of one or more of the following:</p>
202 <li><code>SDL_HAT_CENTERED</code> </li>
203 <li><code>SDL_HAT_UP</code> </li>
204 <li><code>SDL_HAT_RIGHT</code> </li>
205 <li><code>SDL_HAT_DOWN</code> </li>
206 <li><code>SDL_HAT_LEFT</code> </li>
207 <li><code>SDL_HAT_RIGHTUP</code> </li>
208 <li><code>SDL_HAT_RIGHTDOWN</code> </li>
209 <li><code>SDL_HAT_LEFTUP</code> </li>
210 <li><code>SDL_HAT_LEFTDOWN</code></li>
213 <pre> my $joystick = SDL::Joystick::open(0);
215 my $position = SDL::Joystick::get_hat($joystick, 0);
217 print("hat is in position UP\n") if $position & SDL_HAT_UP;
222 <h2 id="get_button">get_button</h2>
223 <div id="get_button_CONTENT">
224 <pre> int SDL::Joystick::get_button( object, int );
227 <p><code>get_button</code> returns the current state of the given button on the given joystick.</p>
228 <p>Returns 1 if the button is pressed. Otherwise, 0. </p>
229 <pre> my $joystick = SDL::Joystick::open(0);
231 my $num_buttons = SDL::Joystick::num_buttons($joystick);
233 for(my $i = 0; $i < $num_buttons; $i++)
235 printf("button %d is %s\n", $i, SDL::Joystick::get_button($joystick, $i) ? 'pressed' : 'not pressed');
238 SDL::Joystick::close($joystick) if SDL::Joystick::opened(0);
243 <h2 id="get_ball">get_ball</h2>
244 <div id="get_ball_CONTENT">
245 <pre> int SDL::Joystick::get_ball(SDL_Joystick $joystick, int $ball, int $dx, int $dy);
248 <p>Get the ball axis change.</p>
249 <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>
250 <p>Returns 0 on success or -1 on failure</p>
251 <pre> my $delta_x = 0;
253 my $joystick = SDL::Joystick::open(0);
255 SDL::Joystick::update();
257 printf("TrackBall Read Error!\n") if(SDL::JoystickGetBall($joystick, 0, $delta_x, $delta_y) == -1);
258 printf("Trackball Delta- X:%d, Y:%d\n", delta_x, delta_y);
263 <h2 id="close">close</h2>
264 <div id="close_CONTENT">
265 <pre> void SDL::Joystick::close( object );
268 <p>Closes a previously opened joystick. See <a href="#open">SDL::Joystick::open</a></p>
269 <pre> SDL::Joystick::close($joystick) if SDL::Joystick::opened(0);