7 CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
10 TITLE="SDL Library Documentation"
11 HREF="index.html"><LINK
14 HREF="guide.html"><LINK
16 TITLE="Using OpenGL With SDL"
17 HREF="guidevideoopengl.html"><LINK
19 TITLE="Handling the Keyboard"
20 HREF="guideinputkeyboard.html"></HEAD
31 SUMMARY="Header navigation table"
40 >SDL Library Documentation</TH
48 HREF="guidevideoopengl.html"
62 HREF="guideinputkeyboard.html"
77 >Chapter 3. Input handling</H1
87 HREF="guideinput.html#GUIDEINPUTJOYSTICK"
88 >Handling Joysticks</A
92 HREF="guideinputkeyboard.html"
93 >Handling the Keyboard</A
102 NAME="GUIDEINPUTJOYSTICK"
104 >Handling Joysticks</H1
114 >The first step in using a joystick in a SDL program is to initialize the Joystick subsystems of SDL. This done by passing the <TT
116 >SDL_INIT_JOYSTICK</TT
123 >. The joystick flag will usually be used in conjunction with other flags (like the video flag) because the joystick is usually used to control something.</P
131 >Example 3-1. Initializing SDL with Joystick Support</B
134 CLASS="PROGRAMLISTING"
135 > if (SDL_Init( SDL_INIT_VIDEO | SDL_INIT_JOYSTICK ) < 0)
137 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
142 >This will attempt to start SDL with both the video and the joystick subsystems activated.</P
153 >If we have reached this point then we can safely assume that the SDL library has been initialized and that the Joystick subsystem is active. We can now call some video and/or sound functions to get things going before we need the joystick. Eventually we have to make sure that there is actually a joystick to work with. It's wise to always check even if you know a joystick will be present on the system because it can also help detect when the joystick is unplugged. The function used to check for joysticks is <A
154 HREF="sdlnumjoysticks.html"
157 >SDL_NumJoysticks</TT
161 >This function simply returns the number of joysticks available on the system. If it is at least one then we are in good shape. The next step is to determine which joystick the user wants to use. If the number of joysticks available is only one then it is safe to assume that one joystick is the one the user wants to use. SDL has a function to get the name of the joysticks as assigned by the operations system and that function is <A
162 HREF="sdljoystickname.html"
165 >SDL_JoystickName</TT
167 >. The joystick is specified by an index where 0 is the first joystick and the last joystick is the number returned by <TT
169 >SDL_NumJoysticks</TT
170 > - 1. In the demonstration a list of all available joysticks is printed to stdout.</P
178 >Example 3-2. Querying the Number of Available Joysticks</B
181 CLASS="PROGRAMLISTING"
182 > printf("%i joysticks were found.\n\n", SDL_NumJoysticks() );
183 printf("The names of the joysticks are:\n");
185 for( i=0; i < SDL_NumJoysticks(); i++ )
187 printf(" %s\n", SDL_JoystickName(i));
198 >Opening a Joystick and Receiving Joystick Events</H2
200 >SDL's event driven architecture makes working with joysticks a snap. Joysticks can trigger 4 different types of events:
209 HREF="sdljoyaxisevent.html"
212 >SDL_JoyAxisEvent</SPAN
216 >Occurs when an axis changes</TD
221 HREF="sdljoyballevent.html"
224 >SDL_JoyBallEvent</SPAN
228 >Occurs when a joystick trackball's position changes</TD
233 HREF="sdljoyhatevent.html"
236 >SDL_JoyHatEvent</SPAN
240 >Occurs when a hat's position changes</TD
245 HREF="sdljoybuttonevent.html"
248 >SDL_JoyButtonEvent</SPAN
252 >Occurs when a button is pressed or released</TD
260 >Events are received from all joysticks opened. The first thing that needs to be done in order to receive joystick events is to call <A
261 HREF="sdljoystickeventstate.html"
264 >SDL_JoystickEventState</TT
269 > flag. Next you must open the joysticks that you want to receive envents from. This is done with the <A
270 HREF="sdljoystickopen.html"
273 >SDL_JoystickOpen</TT
275 > function. For the example we are only interested in events from the first joystick on the system, regardless of what it may be. To receive events from it we would do this:</P
283 >Example 3-3. Opening a Joystick</B
286 CLASS="PROGRAMLISTING"
287 > SDL_Joystick *joystick;
289 SDL_JoystickEventState(SDL_ENABLE);
290 joystick = SDL_JoystickOpen(0);</PRE
293 >If we wanted to receive events for other joysticks we would open them with calls to <TT
295 >SDL_JoystickOpen</TT
296 > just like we opened joystick 0, except we would store the <SPAN
299 > structure they return in a different pointer. We only need the joystick pointer when we are querying the joysticks or when we are closing the joystick.</P
301 >Up to this point all the code we have is used just to initialize the joysticks in order to read values at run time. All we need now is an event loop, which is something that all SDL programs should have anyway to receive the systems quit events. We must now add code to check the event loop for at least some of the above mentioned events. Let's assume our event loop looks like this:
303 CLASS="PROGRAMLISTING"
305 /* Other initializtion code goes here */
307 /* Start main game loop here */
309 while(SDL_PollEvent(&event))
314 /* handle keyboard stuff here */
318 /* Set whatever flags are necessary to */
319 /* end the main game loop here */
324 /* End loop here */</PRE
326 To handle Joystick events we merely add cases for them, first we'll add axis handling code. Axis checks can get kinda of tricky because alot of the joystick events received are junk. Joystick axis have a tendency to vary just a little between polling due to the way they are designed. To compensate for this you have to set a threshold for changes and ignore the events that have'nt exceeded the threshold. 10% is usually a good threshold value. This sounds a lot more complicated than it is. Here is the Axis event handler:</P
334 >Example 3-4. Joystick Axis Events</B
337 CLASS="PROGRAMLISTING"
338 > case SDL_JOYAXISMOTION: /* Handle Joystick Motion */
339 if ( ( event.jaxis.value < -3200 ) || (event.jaxis.value > 3200 ) )
346 >Another trick with axis events is that up-down and left-right movement are two different sets of axes. The most important axis is axis 0 (left-right) and axis 1 (up-down). To handle them seperatly in the code we do the following:</P
354 >Example 3-5. More Joystick Axis Events</B
357 CLASS="PROGRAMLISTING"
358 > case SDL_JOYAXISMOTION: /* Handle Joystick Motion */
359 if ( ( event.jaxis.value < -3200 ) || (event.jaxis.value > 3200 ) )
361 if( event.jaxis.axis == 0)
363 /* Left-right movement code goes here */
366 if( event.jaxis.axis == 1)
368 /* Up-Down movement code goes here */
374 >Ideally the code here should use <TT
377 >event.jaxis.value</I
379 > to scale something. For example lets assume you are using the joystick to control the movement of a spaceship. If the user is using an analog joystick and they push the stick a little bit they expect to move less than if they pushed it a lot. Designing your code for this situation is preferred because it makes the experience for users of analog controls better and remains the same for users of digital controls.</P
381 >If your joystick has any additional axis then they may be used for other sticks or throttle controls and those axis return values too just with different <TT
388 >Button handling is simple compared to the axis checking.</P
396 >Example 3-6. Joystick Button Events</B
399 CLASS="PROGRAMLISTING"
400 > case SDL_JOYBUTTONDOWN: /* Handle Joystick Button Presses */
401 if ( event.jbutton.button == 0 )
408 >Button checks are simpler than axis checks because a button can only be pressed or not pressed. The <TT
410 >SDL_JOYBUTTONDOWN</TT
411 > event is triggered when a button is pressed and the <TT
414 > event is fired when a button is released. We do have to know what button was pressed though, that is done by reading the <TT
417 >event.jbutton.button</I
421 >Lastly when we are through using our joysticks we should close them with a call to <A
422 HREF="sdljoystickclose.html"
425 >SDL_JoystickClose</TT
427 >. To close our opened joystick 0 we would do this at the end of our program:
429 CLASS="PROGRAMLISTING"
430 > SDL_JoystickClose(joystick);</PRE
440 >Advanced Joystick Functions</H2
442 >That takes care of the controls that you can count on being on every joystick under the sun, but there are a few extra things that SDL can support. Joyballs are next on our list, they are alot like axis with a few minor differences. Joyballs store relative changes unlike the the absolute postion stored in a axis event. Also one trackball event contains both the change in x and they change in y. Our case for it is as follows:</P
450 >Example 3-7. Joystick Ball Events</B
453 CLASS="PROGRAMLISTING"
454 > case SDL_JOYBALLMOTION: /* Handle Joyball Motion */
455 if( event.jball.ball == 0 )
462 >The above checks the first joyball on the joystick. The change in position will be stored in <TT
474 >Finally we have the hat event. Hats report only the direction they are pushed in. We check hat's position with the bitmasks:
485 >SDL_HAT_CENTERED</TT
522 Also there are some predefined combinations of the above:
539 >SDL_HAT_RIGHTDOWN</TT
553 >SDL_HAT_LEFTDOWN</TT
562 Our case for the hat may resemble the following:</P
570 >Example 3-8. Joystick Hat Events</B
573 CLASS="PROGRAMLISTING"
574 > case SDL_JOYHATMOTION: /* Handle Hat Motion */
575 if ( event.jhat.value & SDL_HAT_UP )
577 /* Do up stuff here */
580 if ( event.jhat.value & SDL_HAT_LEFT )
582 /* Do left stuff here */
585 if ( event.jhat.value & SDL_HAT_RIGHTDOWN )
587 /* Do right and down together stuff here */
592 >In addition to the queries for number of joysticks on the system and their names there are additional functions to query the capabilities of attached joysticks:
601 HREF="sdljoysticknumaxes.html"
604 >SDL_JoystickNumAxes</TT
608 >Returns the number of joysitck axes</TD
613 HREF="sdljoysticknumbuttons.html"
616 >SDL_JoystickNumButtons</TT
620 >Returns the number of joysitck buttons</TD
625 HREF="sdljoysticknumballs.html"
628 >SDL_JoystickNumBalls</TT
632 >Returns the number of joysitck balls</TD
637 HREF="sdljoysticknumhats.html"
640 >SDL_JoystickNumHats</TT
644 >Returns the number of joysitck hats</TD
652 To use these functions we just have to pass in the joystick structure we got when we opened the joystick. For Example:</P
660 >Example 3-9. Querying Joystick Characteristics</B
663 CLASS="PROGRAMLISTING"
664 > int number_of_buttons;
665 SDL_Joystick *joystick;
667 joystick = SDL_JoystickOpen(0);
668 number_of_buttons = SDL_JoystickNumButtons(joystick);</PRE
671 >This block of code would get the number of buttons on the first joystick in the system. </P
680 SUMMARY="Footer navigation table"
691 HREF="guidevideoopengl.html"
709 HREF="guideinputkeyboard.html"
719 >Using OpenGL With SDL</TD
733 >Handling the Keyboard</TD