1SDL::Joystick(3) User Contributed Perl Documentation SDL::Joystick(3)
2
3
4
6 SDL::Joystick -- SDL Bindings for the Joystick device
7
9 Core, Joystick
10
12 use SDL;
13 use SDL::Joystick;
14
15 SDL::init_sub_system(SDL_INIT_JOYSTICK);
16
17 die('no joystick found') unless(SDL::Joystick::num_joysticks());
18
19 my $joystick = SDL::Joystick->new(0);
20
22 num_joysticks
23 int SDL::Joystick::num_joysticks( void );
24
25 Counts and returns available joysticks.
26
27 name
28 string SDL::Joystick::name( index );
29
30 Get the implementation dependent name of joystick. The "index"
31 parameter refers to the N'th joystick on the system.
32
33 my $num_joysticks = SDL::Joystick::num_joysticks();
34
35 printf("%d joysticks found\n", $num_joysticks);
36
37 for($i = 0; $i < $num_joysticks; $i++)
38 {
39 printf("%s\n", SDL::Joystick::name($i));
40 }
41
42 new
43 object SDL::Joystick->new( index );
44
45 Opens a joystick for use within SDL. The "index" refers to the N'th
46 joystick in the system. A joystick must be opened before it can be
47 used.
48
49 # Initialize the joystick subsystem
50 SDL::init_sub_system(SDL_INIT_JOYSTICK);
51
52 # Check for joystick
53 if(SDL::Joystick::num_joysticks() > 0)
54 {
55 # Open joystick
56 my $joystick = SDL::Joystick->new(0);
57
58 if($joystick)
59 {
60 printf("Opened Joystick 0\n");
61 printf("Name: %s\n", SDL::Joystick::name(0));
62 printf("Number of Axes: %d\n", SDL::Joystick::num_axes($joystick));
63 printf("Number of Buttons: %d\n", SDL::Joystick::num_buttons($joystick));
64 printf("Number of Balls: %d\n", SDL::Joystick::num_balls($joystick));
65 }
66 else
67 {
68 printf("Couldn't open Joystick 0\n");
69 }
70
71 # Close if opened
72 SDL::Joystick::close($joystick) if SDL::Joystick::opened(0);
73 }
74
75 opened
76 int SDL::Joystick::opened( index );
77
78 Determines whether a joystick has already been opened within the
79 application. "index" refers to the N'th joystick on the system.
80
81 Returns 1 if the joystick has been opened, or 0 if it has not.
82
83 index
84 int SDL::Joystick::index( object );
85
86 Returns the "index" of a given "SDL_Joystick" structure. See
87 SDL::Joystick::new
88
89 num_axes
90 int SDL::Joystick::num_axes( object );
91
92 Return the number of axes available from a previously opened joystick.
93 See SDL::Joystick::new
94
95 num_balls
96 int SDL::Joystick::num_balls( object );
97
98 Return the number of trackballs available from a previously opened
99 joystick. See SDL::Joystick::new
100
101 num_hats
102 int SDL::Joystick::num_hats( object );
103
104 Gets the number of joystick hats from a previously opened joystick. See
105 SDL::Joystick::new
106
107 num_buttons
108 int SDL::Joystick::num_buttons( object );
109
110 Gets the number of joystick buttons from a previously opened joystick.
111 See SDL::Joystick::new
112
113 update
114 void SDL::Joystick::update();
115
116 Updates the state(position, buttons, etc.) of all open joysticks. If
117 joystick events have been enabled with "SDL::Joystick::event_state"
118 then this is called automatically in the event loop.
119
120 get_axis
121 "get_axis" returns the current state of the given axis on the given
122 joystick.
123
124 On most modern joysticks the X axis is usually represented by axis 0
125 and the Y axis by axis 1. The value returned by "get_axis" is a signed
126 integer (-32768 to 32767) representing the current position of the
127 axis, it may be necessary to impose certain tolerances on these values
128 to account for jitter.
129
130 Note: Some joysticks use axes 2 and 3 for extra buttons.
131
132 Returns a 16-bit signed integer representing the current position of
133 the axis.
134
135 my $joystick = SDL::Joystick->new(0);
136
137 my $x_move = SDL::Joystick::get_axis($joystick, 0);
138 my $y_move = SDL::Joystick::get_axis($joystick, 1);
139
140 get_hat
141 int SDL::Joystick::get_hat( object, int );
142
143 "get_hat" returns the current state of the given "hat" on the given
144 "joystick".
145
146 The current state is returned which is an OR'd combination of one or
147 more of the following:
148
149 • "SDL_HAT_CENTERED"
150
151 • "SDL_HAT_UP"
152
153 • "SDL_HAT_RIGHT"
154
155 • "SDL_HAT_DOWN"
156
157 • "SDL_HAT_LEFT"
158
159 • "SDL_HAT_RIGHTUP"
160
161 • "SDL_HAT_RIGHTDOWN"
162
163 • "SDL_HAT_LEFTUP"
164
165 • "SDL_HAT_LEFTDOWN"
166
167 my $joystick = SDL::Joystick->new(0);
168
169 my $position = SDL::Joystick::get_hat($joystick, 0);
170
171 print("hat is in position UP\n") if $position & SDL_HAT_UP;
172
173 get_button
174 int SDL::Joystick::get_button( object, int );
175
176 "get_button" returns the current state of the given button on the given
177 joystick.
178
179 Returns 1 if the button is pressed. Otherwise, 0.
180
181 my $joystick = SDL::Joystick->new(0);
182
183 my $num_buttons = SDL::Joystick::num_buttons($joystick);
184
185 for(my $i = 0; $i < $num_buttons; $i++)
186 {
187 printf("button %d is %s\n", $i, SDL::Joystick::get_button($joystick, $i) ? 'pressed' : 'not pressed');
188 }
189
190 SDL::Joystick::close($joystick) if SDL::Joystick::opened(0);
191
192 get_ball
193 int SDL::Joystick::get_ball(SDL_Joystick $joystick, int $ball, int $dx, int $dy);
194
195 Get the ball axis change.
196
197 Trackballs can only return relative motion since the last call to
198 SDL::Joystick::get_ball, these motion deltas are placed into "dx" and
199 "dy".
200
201 Returns 0 on success or -1 on failure
202
203 my $delta_x = 0;
204 my $delta_y = 0;
205 my $joystick = SDL::Joystick->new(0);
206
207 SDL::Joystick::update();
208
209 printf("TrackBall Read Error!\n") if(SDL::JoystickGetBall($joystick, 0, $delta_x, $delta_y) == -1);
210 printf("Trackball Delta- X:%d, Y:%d\n", delta_x, delta_y);
211
212 close
213 void SDL::Joystick::close( object );
214
215 Closes a previously opened joystick. See SDL::Joystick::new
216
217 SDL::Joystick::close($joystick) if SDL::Joystick::opened(0);
218
220 See "AUTHORS" in SDL.
221
222
223
224perl v5.34.0 2022-01-21 SDL::Joystick(3)