SDL-1.2.14
[sdl_omap.git] / docs / html / guideinput.html
CommitLineData
e14743d1 1<HTML
2><HEAD
3><TITLE
4>Input handling</TITLE
5><META
6NAME="GENERATOR"
7CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
8"><LINK
9REL="HOME"
10TITLE="SDL Library Documentation"
11HREF="index.html"><LINK
12REL="UP"
13TITLE="SDL Guide"
14HREF="guide.html"><LINK
15REL="PREVIOUS"
16TITLE="Using OpenGL With SDL"
17HREF="guidevideoopengl.html"><LINK
18REL="NEXT"
19TITLE="Handling the Keyboard"
20HREF="guideinputkeyboard.html"></HEAD
21><BODY
22CLASS="CHAPTER"
23BGCOLOR="#FFF8DC"
24TEXT="#000000"
25LINK="#0000ee"
26VLINK="#551a8b"
27ALINK="#ff0000"
28><DIV
29CLASS="NAVHEADER"
30><TABLE
31SUMMARY="Header navigation table"
32WIDTH="100%"
33BORDER="0"
34CELLPADDING="0"
35CELLSPACING="0"
36><TR
37><TH
38COLSPAN="3"
39ALIGN="center"
40>SDL Library Documentation</TH
41></TR
42><TR
43><TD
44WIDTH="10%"
45ALIGN="left"
46VALIGN="bottom"
47><A
48HREF="guidevideoopengl.html"
49ACCESSKEY="P"
50>Prev</A
51></TD
52><TD
53WIDTH="80%"
54ALIGN="center"
55VALIGN="bottom"
56></TD
57><TD
58WIDTH="10%"
59ALIGN="right"
60VALIGN="bottom"
61><A
62HREF="guideinputkeyboard.html"
63ACCESSKEY="N"
64>Next</A
65></TD
66></TR
67></TABLE
68><HR
69ALIGN="LEFT"
70WIDTH="100%"></DIV
71><DIV
72CLASS="CHAPTER"
73><H1
74><A
75NAME="GUIDEINPUT"
76></A
77>Chapter 3. Input handling</H1
78><DIV
79CLASS="TOC"
80><DL
81><DT
82><B
83>Table of Contents</B
84></DT
85><DT
86><A
87HREF="guideinput.html#GUIDEINPUTJOYSTICK"
88>Handling Joysticks</A
89></DT
90><DT
91><A
92HREF="guideinputkeyboard.html"
93>Handling the Keyboard</A
94></DT
95></DL
96></DIV
97><DIV
98CLASS="SECT1"
99><H1
100CLASS="SECT1"
101><A
102NAME="GUIDEINPUTJOYSTICK"
103></A
104>Handling Joysticks</H1
105><DIV
106CLASS="SECT2"
107><H2
108CLASS="SECT2"
109><A
110NAME="AEN135"
111></A
112>Initialization</H2
113><P
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
115CLASS="LITERAL"
116>SDL_INIT_JOYSTICK</TT
117> flag to <A
118HREF="sdlinit.html"
119><TT
120CLASS="FUNCTION"
121>SDL_Init</TT
122></A
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
124><DIV
125CLASS="EXAMPLE"
126><A
127NAME="AEN141"
128></A
129><P
130><B
131>Example 3-1. Initializing SDL with Joystick Support</B
132></P
133><PRE
134CLASS="PROGRAMLISTING"
135> if (SDL_Init( SDL_INIT_VIDEO | SDL_INIT_JOYSTICK ) &#60; 0)
136 {
137 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
138 exit(1);
139 }</PRE
140></DIV
141><P
142>This will attempt to start SDL with both the video and the joystick subsystems activated.</P
143></DIV
144><DIV
145CLASS="SECT2"
146><H2
147CLASS="SECT2"
148><A
149NAME="AEN145"
150></A
151>Querying</H2
152><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
154HREF="sdlnumjoysticks.html"
155><TT
156CLASS="FUNCTION"
157>SDL_NumJoysticks</TT
158></A
159>.</P
160><P
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
162HREF="sdljoystickname.html"
163><TT
164CLASS="FUNCTION"
165>SDL_JoystickName</TT
166></A
167>. The joystick is specified by an index where 0 is the first joystick and the last joystick is the number returned by <TT
168CLASS="FUNCTION"
169>SDL_NumJoysticks</TT
170> - 1. In the demonstration a list of all available joysticks is printed to stdout.</P
171><DIV
172CLASS="EXAMPLE"
173><A
174NAME="AEN154"
175></A
176><P
177><B
178>Example 3-2. Querying the Number of Available Joysticks</B
179></P
180><PRE
181CLASS="PROGRAMLISTING"
182> printf("%i joysticks were found.\n\n", SDL_NumJoysticks() );
183 printf("The names of the joysticks are:\n");
184
185 for( i=0; i &#60; SDL_NumJoysticks(); i++ )
186 {
187 printf(" %s\n", SDL_JoystickName(i));
188 }</PRE
189></DIV
190></DIV
191><DIV
192CLASS="SECT2"
193><H2
194CLASS="SECT2"
195><A
196NAME="AEN157"
197></A
198>Opening a Joystick and Receiving Joystick Events</H2
199><P
200>SDL's event driven architecture makes working with joysticks a snap. Joysticks can trigger 4 different types of events:
201<P
202></P
203><TABLE
204BORDER="0"
205><TBODY
206><TR
207><TD
208><A
209HREF="sdljoyaxisevent.html"
210><SPAN
211CLASS="STRUCTNAME"
212>SDL_JoyAxisEvent</SPAN
213></A
214></TD
215><TD
216>Occurs when an axis changes</TD
217></TR
218><TR
219><TD
220><A
221HREF="sdljoyballevent.html"
222><SPAN
223CLASS="STRUCTNAME"
224>SDL_JoyBallEvent</SPAN
225></A
226></TD
227><TD
228>Occurs when a joystick trackball's position changes</TD
229></TR
230><TR
231><TD
232><A
233HREF="sdljoyhatevent.html"
234><SPAN
235CLASS="STRUCTNAME"
236>SDL_JoyHatEvent</SPAN
237></A
238></TD
239><TD
240>Occurs when a hat's position changes</TD
241></TR
242><TR
243><TD
244><A
245HREF="sdljoybuttonevent.html"
246><SPAN
247CLASS="STRUCTNAME"
248>SDL_JoyButtonEvent</SPAN
249></A
250></TD
251><TD
252>Occurs when a button is pressed or released</TD
253></TR
254></TBODY
255></TABLE
256><P
257></P
258></P
259><P
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
261HREF="sdljoystickeventstate.html"
262><TT
263CLASS="FUNCTION"
264>SDL_JoystickEventState</TT
265></A
266> with the <TT
267CLASS="LITERAL"
268>SDL_ENABLE</TT
269> flag. Next you must open the joysticks that you want to receive envents from. This is done with the <A
270HREF="sdljoystickopen.html"
271><TT
272CLASS="FUNCTION"
273>SDL_JoystickOpen</TT
274></A
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
276><DIV
277CLASS="EXAMPLE"
278><A
279NAME="AEN183"
280></A
281><P
282><B
283>Example 3-3. Opening a Joystick</B
284></P
285><PRE
286CLASS="PROGRAMLISTING"
287> SDL_Joystick *joystick;
288
289 SDL_JoystickEventState(SDL_ENABLE);
290 joystick = SDL_JoystickOpen(0);</PRE
291></DIV
292><P
293>If we wanted to receive events for other joysticks we would open them with calls to <TT
294CLASS="FUNCTION"
295>SDL_JoystickOpen</TT
296> just like we opened joystick 0, except we would store the <SPAN
297CLASS="STRUCTNAME"
298>SDL_Joystick</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
300><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:
302<PRE
303CLASS="PROGRAMLISTING"
304> SDL_Event event;
305 /* Other initializtion code goes here */
306
307 /* Start main game loop here */
308
309 while(SDL_PollEvent(&#38;event))
310 {
311 switch(event.type)
312 {
313 case SDL_KEYDOWN:
314 /* handle keyboard stuff here */
315 break;
316
317 case SDL_QUIT:
318 /* Set whatever flags are necessary to */
319 /* end the main game loop here */
320 break;
321 }
322 }
323
324 /* End loop here */</PRE
325>
326To 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
327><DIV
328CLASS="EXAMPLE"
329><A
330NAME="AEN191"
331></A
332><P
333><B
334>Example 3-4. Joystick Axis Events</B
335></P
336><PRE
337CLASS="PROGRAMLISTING"
338> case SDL_JOYAXISMOTION: /* Handle Joystick Motion */
339 if ( ( event.jaxis.value &#60; -3200 ) || (event.jaxis.value &#62; 3200 ) )
340 {
341 /* code goes here */
342 }
343 break;</PRE
344></DIV
345><P
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
347><DIV
348CLASS="EXAMPLE"
349><A
350NAME="AEN195"
351></A
352><P
353><B
354>Example 3-5. More Joystick Axis Events</B
355></P
356><PRE
357CLASS="PROGRAMLISTING"
358> case SDL_JOYAXISMOTION: /* Handle Joystick Motion */
359 if ( ( event.jaxis.value &#60; -3200 ) || (event.jaxis.value &#62; 3200 ) )
360 {
361 if( event.jaxis.axis == 0)
362 {
363 /* Left-right movement code goes here */
364 }
365
366 if( event.jaxis.axis == 1)
367 {
368 /* Up-Down movement code goes here */
369 }
370 }
371 break;</PRE
372></DIV
373><P
374>Ideally the code here should use <TT
375CLASS="STRUCTFIELD"
376><I
377>event.jaxis.value</I
378></TT
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
380><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
382CLASS="STRUCTFIELD"
383><I
384>event.jaxis.axis</I
385></TT
386> values.</P
387><P
388>Button handling is simple compared to the axis checking.</P
389><DIV
390CLASS="EXAMPLE"
391><A
392NAME="AEN203"
393></A
394><P
395><B
396>Example 3-6. Joystick Button Events</B
397></P
398><PRE
399CLASS="PROGRAMLISTING"
400> case SDL_JOYBUTTONDOWN: /* Handle Joystick Button Presses */
401 if ( event.jbutton.button == 0 )
402 {
403 /* code goes here */
404 }
405 break;</PRE
406></DIV
407><P
408>Button checks are simpler than axis checks because a button can only be pressed or not pressed. The <TT
409CLASS="LITERAL"
410>SDL_JOYBUTTONDOWN</TT
411> event is triggered when a button is pressed and the <TT
412CLASS="LITERAL"
413>SDL_JOYBUTTONUP</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
415CLASS="STRUCTFIELD"
416><I
417>event.jbutton.button</I
418></TT
419> field.</P
420><P
421>Lastly when we are through using our joysticks we should close them with a call to <A
422HREF="sdljoystickclose.html"
423><TT
424CLASS="FUNCTION"
425>SDL_JoystickClose</TT
426></A
427>. To close our opened joystick 0 we would do this at the end of our program:
428<PRE
429CLASS="PROGRAMLISTING"
430> SDL_JoystickClose(joystick);</PRE
431></P
432></DIV
433><DIV
434CLASS="SECT2"
435><H2
436CLASS="SECT2"
437><A
438NAME="AEN214"
439></A
440>Advanced Joystick Functions</H2
441><P
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
443><DIV
444CLASS="EXAMPLE"
445><A
446NAME="AEN217"
447></A
448><P
449><B
450>Example 3-7. Joystick Ball Events</B
451></P
452><PRE
453CLASS="PROGRAMLISTING"
454> case SDL_JOYBALLMOTION: /* Handle Joyball Motion */
455 if( event.jball.ball == 0 )
456 {
457 /* ball handling */
458 }
459 break;</PRE
460></DIV
461><P
462>The above checks the first joyball on the joystick. The change in position will be stored in <TT
463CLASS="STRUCTFIELD"
464><I
465>event.jball.xrel</I
466></TT
467> and <TT
468CLASS="STRUCTFIELD"
469><I
470>event.jball.yrel</I
471></TT
472>.</P
473><P
474>Finally we have the hat event. Hats report only the direction they are pushed in. We check hat's position with the bitmasks:
475
476<P
477></P
478><TABLE
479BORDER="0"
480><TBODY
481><TR
482><TD
483><TT
484CLASS="LITERAL"
485>SDL_HAT_CENTERED</TT
486></TD
487></TR
488><TR
489><TD
490><TT
491CLASS="LITERAL"
492>SDL_HAT_UP</TT
493></TD
494></TR
495><TR
496><TD
497><TT
498CLASS="LITERAL"
499>SDL_HAT_RIGHT</TT
500></TD
501></TR
502><TR
503><TD
504><TT
505CLASS="LITERAL"
506>SDL_HAT_DOWN</TT
507></TD
508></TR
509><TR
510><TD
511><TT
512CLASS="LITERAL"
513>SDL_HAT_LEFT</TT
514></TD
515></TR
516></TBODY
517></TABLE
518><P
519></P
520>
521
522Also there are some predefined combinations of the above:
523<P
524></P
525><TABLE
526BORDER="0"
527><TBODY
528><TR
529><TD
530><TT
531CLASS="LITERAL"
532>SDL_HAT_RIGHTUP</TT
533></TD
534></TR
535><TR
536><TD
537><TT
538CLASS="LITERAL"
539>SDL_HAT_RIGHTDOWN</TT
540></TD
541></TR
542><TR
543><TD
544><TT
545CLASS="LITERAL"
546>SDL_HAT_LEFTUP</TT
547></TD
548></TR
549><TR
550><TD
551><TT
552CLASS="LITERAL"
553>SDL_HAT_LEFTDOWN</TT
554></TD
555></TR
556></TBODY
557></TABLE
558><P
559></P
560>
561
562Our case for the hat may resemble the following:</P
563><DIV
564CLASS="EXAMPLE"
565><A
566NAME="AEN244"
567></A
568><P
569><B
570>Example 3-8. Joystick Hat Events</B
571></P
572><PRE
573CLASS="PROGRAMLISTING"
574> case SDL_JOYHATMOTION: /* Handle Hat Motion */
575 if ( event.jhat.value &#38; SDL_HAT_UP )
576 {
577 /* Do up stuff here */
578 }
579
580 if ( event.jhat.value &#38; SDL_HAT_LEFT )
581 {
582 /* Do left stuff here */
583 }
584
585 if ( event.jhat.value &#38; SDL_HAT_RIGHTDOWN )
586 {
587 /* Do right and down together stuff here */
588 }
589 break;</PRE
590></DIV
591><P
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:
593<P
594></P
595><TABLE
596BORDER="0"
597><TBODY
598><TR
599><TD
600><A
601HREF="sdljoysticknumaxes.html"
602><TT
603CLASS="FUNCTION"
604>SDL_JoystickNumAxes</TT
605></A
606></TD
607><TD
608>Returns the number of joysitck axes</TD
609></TR
610><TR
611><TD
612><A
613HREF="sdljoysticknumbuttons.html"
614><TT
615CLASS="FUNCTION"
616>SDL_JoystickNumButtons</TT
617></A
618></TD
619><TD
620>Returns the number of joysitck buttons</TD
621></TR
622><TR
623><TD
624><A
625HREF="sdljoysticknumballs.html"
626><TT
627CLASS="FUNCTION"
628>SDL_JoystickNumBalls</TT
629></A
630></TD
631><TD
632>Returns the number of joysitck balls</TD
633></TR
634><TR
635><TD
636><A
637HREF="sdljoysticknumhats.html"
638><TT
639CLASS="FUNCTION"
640>SDL_JoystickNumHats</TT
641></A
642></TD
643><TD
644>Returns the number of joysitck hats</TD
645></TR
646></TBODY
647></TABLE
648><P
649></P
650>
651
652To use these functions we just have to pass in the joystick structure we got when we opened the joystick. For Example:</P
653><DIV
654CLASS="EXAMPLE"
655><A
656NAME="AEN265"
657></A
658><P
659><B
660>Example 3-9. Querying Joystick Characteristics</B
661></P
662><PRE
663CLASS="PROGRAMLISTING"
664> int number_of_buttons;
665 SDL_Joystick *joystick;
666
667 joystick = SDL_JoystickOpen(0);
668 number_of_buttons = SDL_JoystickNumButtons(joystick);</PRE
669></DIV
670><P
671>This block of code would get the number of buttons on the first joystick in the system. </P
672></DIV
673></DIV
674></DIV
675><DIV
676CLASS="NAVFOOTER"
677><HR
678ALIGN="LEFT"
679WIDTH="100%"><TABLE
680SUMMARY="Footer navigation table"
681WIDTH="100%"
682BORDER="0"
683CELLPADDING="0"
684CELLSPACING="0"
685><TR
686><TD
687WIDTH="33%"
688ALIGN="left"
689VALIGN="top"
690><A
691HREF="guidevideoopengl.html"
692ACCESSKEY="P"
693>Prev</A
694></TD
695><TD
696WIDTH="34%"
697ALIGN="center"
698VALIGN="top"
699><A
700HREF="index.html"
701ACCESSKEY="H"
702>Home</A
703></TD
704><TD
705WIDTH="33%"
706ALIGN="right"
707VALIGN="top"
708><A
709HREF="guideinputkeyboard.html"
710ACCESSKEY="N"
711>Next</A
712></TD
713></TR
714><TR
715><TD
716WIDTH="33%"
717ALIGN="left"
718VALIGN="top"
719>Using OpenGL With SDL</TD
720><TD
721WIDTH="34%"
722ALIGN="center"
723VALIGN="top"
724><A
725HREF="guide.html"
726ACCESSKEY="U"
727>Up</A
728></TD
729><TD
730WIDTH="33%"
731ALIGN="right"
732VALIGN="top"
733>Handling the Keyboard</TD
734></TR
735></TABLE
736></DIV
737></BODY
738></HTML
739>