view docs/html/guideinput.html @ 1856:21f13c787b46

Date: Mon, 22 May 2006 01:16:47 -0400 From: Mike Frysinger Subject: [SDL] [patch] fix sed expression in sdl-config just noticed that sdl-config uses '[a-z]' with sed in sdl-config without forcing the locale to C ... this could cause problems with locales where the letter 'z' is not the last letter in the alphabet
author Sam Lantinga <slouken@libsdl.org>
date Tue, 23 May 2006 15:07:56 +0000
parents 355632dca928
children
line wrap: on
line source

<HTML
><HEAD
><TITLE
>Input handling</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
"><LINK
REL="HOME"
TITLE="SDL Library Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="SDL Guide"
HREF="guide.html"><LINK
REL="PREVIOUS"
TITLE="Using OpenGL With SDL"
HREF="guidevideoopengl.html"><LINK
REL="NEXT"
TITLE="Handling the Keyboard"
HREF="guideinputkeyboard.html"></HEAD
><BODY
CLASS="CHAPTER"
BGCOLOR="#FFF8DC"
TEXT="#000000"
LINK="#0000ee"
VLINK="#551a8b"
ALINK="#ff0000"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>SDL Library Documentation</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="guidevideoopengl.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="guideinputkeyboard.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="CHAPTER"
><H1
><A
NAME="GUIDEINPUT"
></A
>Chapter 3. Input handling</H1
><DIV
CLASS="TOC"
><DL
><DT
><B
>Table of Contents</B
></DT
><DT
><A
HREF="guideinput.html#GUIDEINPUTJOYSTICK"
>Handling Joysticks</A
></DT
><DT
><A
HREF="guideinputkeyboard.html"
>Handling the Keyboard</A
></DT
></DL
></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="GUIDEINPUTJOYSTICK"
></A
>Handling Joysticks</H1
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN135"
></A
>Initialization</H2
><P
>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
CLASS="LITERAL"
>SDL_INIT_JOYSTICK</TT
> flag to <A
HREF="sdlinit.html"
><TT
CLASS="FUNCTION"
>SDL_Init</TT
></A
>.  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
><DIV
CLASS="EXAMPLE"
><A
NAME="AEN141"
></A
><P
><B
>Example 3-1. Initializing SDL with Joystick Support</B
></P
><PRE
CLASS="PROGRAMLISTING"
>    if (SDL_Init( SDL_INIT_VIDEO | SDL_INIT_JOYSTICK ) &#60; 0)
    {
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
        exit(1);
    }</PRE
></DIV
><P
>This will attempt to start SDL with both the video and the joystick subsystems activated.</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN145"
></A
>Querying</H2
><P
>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
HREF="sdlnumjoysticks.html"
><TT
CLASS="FUNCTION"
>SDL_NumJoysticks</TT
></A
>.</P
><P
>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
HREF="sdljoystickname.html"
><TT
CLASS="FUNCTION"
>SDL_JoystickName</TT
></A
>.  The joystick is specified by an index where 0 is the first joystick and the last joystick is the number returned by <TT
CLASS="FUNCTION"
>SDL_NumJoysticks</TT
> - 1.  In the demonstration a list of all available joysticks is printed to stdout.</P
><DIV
CLASS="EXAMPLE"
><A
NAME="AEN154"
></A
><P
><B
>Example 3-2. Querying the Number of Available Joysticks</B
></P
><PRE
CLASS="PROGRAMLISTING"
>    printf("%i joysticks were found.\n\n", SDL_NumJoysticks() );
    printf("The names of the joysticks are:\n");
		
    for( i=0; i &#60; SDL_NumJoysticks(); i++ ) 
    {
        printf("    %s\n", SDL_JoystickName(i));
    }</PRE
></DIV
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN157"
></A
>Opening a Joystick and Receiving Joystick Events</H2
><P
>SDL's event driven architecture makes working with joysticks a snap.  Joysticks can trigger 4 different types of events:
<P
></P
><TABLE
BORDER="0"
><TBODY
><TR
><TD
><A
HREF="sdljoyaxisevent.html"
><SPAN
CLASS="STRUCTNAME"
>SDL_JoyAxisEvent</SPAN
></A
></TD
><TD
>Occurs when an axis changes</TD
></TR
><TR
><TD
><A
HREF="sdljoyballevent.html"
><SPAN
CLASS="STRUCTNAME"
>SDL_JoyBallEvent</SPAN
></A
></TD
><TD
>Occurs when a joystick trackball's position changes</TD
></TR
><TR
><TD
><A
HREF="sdljoyhatevent.html"
><SPAN
CLASS="STRUCTNAME"
>SDL_JoyHatEvent</SPAN
></A
></TD
><TD
>Occurs when a hat's position changes</TD
></TR
><TR
><TD
><A
HREF="sdljoybuttonevent.html"
><SPAN
CLASS="STRUCTNAME"
>SDL_JoyButtonEvent</SPAN
></A
></TD
><TD
>Occurs when a button is pressed or released</TD
></TR
></TBODY
></TABLE
><P
></P
></P
><P
>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
HREF="sdljoystickeventstate.html"
><TT
CLASS="FUNCTION"
>SDL_JoystickEventState</TT
></A
> with the <TT
CLASS="LITERAL"
>SDL_ENABLE</TT
> flag. Next you must open the joysticks that you want to receive envents from. This is done with the <A
HREF="sdljoystickopen.html"
><TT
CLASS="FUNCTION"
>SDL_JoystickOpen</TT
></A
> 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
><DIV
CLASS="EXAMPLE"
><A
NAME="AEN183"
></A
><P
><B
>Example 3-3. Opening a Joystick</B
></P
><PRE
CLASS="PROGRAMLISTING"
>    SDL_Joystick *joystick;

    SDL_JoystickEventState(SDL_ENABLE);
    joystick = SDL_JoystickOpen(0);</PRE
></DIV
><P
>If we wanted to receive events for other joysticks we would open them with calls to <TT
CLASS="FUNCTION"
>SDL_JoystickOpen</TT
> just like we opened joystick 0, except we would store the <SPAN
CLASS="STRUCTNAME"
>SDL_Joystick</SPAN
> 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
><P
>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:
<PRE
CLASS="PROGRAMLISTING"
>    SDL_Event event;
    /* Other initializtion code goes here */   

    /* Start main game loop here */

    while(SDL_PollEvent(&#38;event))
    {  
        switch(event.type)
        {  
            case SDL_KEYDOWN:
            /* handle keyboard stuff here */				
            break;

            case SDL_QUIT:
            /* Set whatever flags are necessary to */
            /* end the main game loop here */
            break;
        }
    }

    /* End loop here */</PRE
>
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
><DIV
CLASS="EXAMPLE"
><A
NAME="AEN191"
></A
><P
><B
>Example 3-4. Joystick Axis Events</B
></P
><PRE
CLASS="PROGRAMLISTING"
>    case SDL_JOYAXISMOTION:  /* Handle Joystick Motion */
    if ( ( event.jaxis.value &#60; -3200 ) || (event.jaxis.value &#62; 3200 ) ) 
    {
      /* code goes here */
    }
    break;</PRE
></DIV
><P
>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
><DIV
CLASS="EXAMPLE"
><A
NAME="AEN195"
></A
><P
><B
>Example 3-5. More Joystick Axis Events</B
></P
><PRE
CLASS="PROGRAMLISTING"
>    case SDL_JOYAXISMOTION:  /* Handle Joystick Motion */
    if ( ( event.jaxis.value &#60; -3200 ) || (event.jaxis.value &#62; 3200 ) ) 
    {
        if( event.jaxis.axis == 0) 
        {
            /* Left-right movement code goes here */
        }

        if( event.jaxis.axis == 1) 
        {
            /* Up-Down movement code goes here */
        }
    }
    break;</PRE
></DIV
><P
>Ideally the code here should use <TT
CLASS="STRUCTFIELD"
><I
>event.jaxis.value</I
></TT
> 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
><P
>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
CLASS="STRUCTFIELD"
><I
>event.jaxis.axis</I
></TT
> values.</P
><P
>Button handling is simple compared to the axis checking.</P
><DIV
CLASS="EXAMPLE"
><A
NAME="AEN203"
></A
><P
><B
>Example 3-6. Joystick Button Events</B
></P
><PRE
CLASS="PROGRAMLISTING"
>    case SDL_JOYBUTTONDOWN:  /* Handle Joystick Button Presses */
    if ( event.jbutton.button == 0 ) 
    {
        /* code goes here */
    }
    break;</PRE
></DIV
><P
>Button checks are simpler than axis checks because a button can only be pressed or not pressed.  The <TT
CLASS="LITERAL"
>SDL_JOYBUTTONDOWN</TT
> event is triggered when a button is pressed and the <TT
CLASS="LITERAL"
>SDL_JOYBUTTONUP</TT
> 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
CLASS="STRUCTFIELD"
><I
>event.jbutton.button</I
></TT
> field.</P
><P
>Lastly when we are through using our joysticks we should close them with a call to <A
HREF="sdljoystickclose.html"
><TT
CLASS="FUNCTION"
>SDL_JoystickClose</TT
></A
>. To close our opened joystick 0 we would do this at the end of our program:
<PRE
CLASS="PROGRAMLISTING"
>    SDL_JoystickClose(joystick);</PRE
></P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN214"
></A
>Advanced Joystick Functions</H2
><P
>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
><DIV
CLASS="EXAMPLE"
><A
NAME="AEN217"
></A
><P
><B
>Example 3-7. Joystick Ball Events</B
></P
><PRE
CLASS="PROGRAMLISTING"
>    case SDL_JOYBALLMOTION:  /* Handle Joyball Motion */
    if( event.jball.ball == 0 )
    {
      /* ball handling */
    }
    break;</PRE
></DIV
><P
>The above checks the first joyball on the joystick. The change in position will be stored in <TT
CLASS="STRUCTFIELD"
><I
>event.jball.xrel</I
></TT
> and <TT
CLASS="STRUCTFIELD"
><I
>event.jball.yrel</I
></TT
>.</P
><P
>Finally we have the hat event. Hats report only the direction they are pushed in. We check hat's position with the bitmasks:

<P
></P
><TABLE
BORDER="0"
><TBODY
><TR
><TD
><TT
CLASS="LITERAL"
>SDL_HAT_CENTERED</TT
></TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>SDL_HAT_UP</TT
></TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>SDL_HAT_RIGHT</TT
></TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>SDL_HAT_DOWN</TT
></TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>SDL_HAT_LEFT</TT
></TD
></TR
></TBODY
></TABLE
><P
></P
>

Also there are some predefined combinations of the above:
<P
></P
><TABLE
BORDER="0"
><TBODY
><TR
><TD
><TT
CLASS="LITERAL"
>SDL_HAT_RIGHTUP</TT
></TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>SDL_HAT_RIGHTDOWN</TT
></TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>SDL_HAT_LEFTUP</TT
></TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>SDL_HAT_LEFTDOWN</TT
></TD
></TR
></TBODY
></TABLE
><P
></P
>

Our case for the hat may resemble the following:</P
><DIV
CLASS="EXAMPLE"
><A
NAME="AEN244"
></A
><P
><B
>Example 3-8. Joystick Hat Events</B
></P
><PRE
CLASS="PROGRAMLISTING"
>    case SDL_JOYHATMOTION:  /* Handle Hat Motion */
    if ( event.jhat.value &#38; SDL_HAT_UP )
    {
        /* Do up stuff here */
    }

    if ( event.jhat.value &#38; SDL_HAT_LEFT )
    {
        /* Do left stuff here */
    }

    if ( event.jhat.value &#38; SDL_HAT_RIGHTDOWN )
    {
        /* Do right and down together stuff here */
    }
    break;</PRE
></DIV
><P
>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:
<P
></P
><TABLE
BORDER="0"
><TBODY
><TR
><TD
><A
HREF="sdljoysticknumaxes.html"
><TT
CLASS="FUNCTION"
>SDL_JoystickNumAxes</TT
></A
></TD
><TD
>Returns the number of joysitck axes</TD
></TR
><TR
><TD
><A
HREF="sdljoysticknumbuttons.html"
><TT
CLASS="FUNCTION"
>SDL_JoystickNumButtons</TT
></A
></TD
><TD
>Returns the number of joysitck buttons</TD
></TR
><TR
><TD
><A
HREF="sdljoysticknumballs.html"
><TT
CLASS="FUNCTION"
>SDL_JoystickNumBalls</TT
></A
></TD
><TD
>Returns the number of joysitck balls</TD
></TR
><TR
><TD
><A
HREF="sdljoysticknumhats.html"
><TT
CLASS="FUNCTION"
>SDL_JoystickNumHats</TT
></A
></TD
><TD
>Returns the number of joysitck hats</TD
></TR
></TBODY
></TABLE
><P
></P
>

To use these functions we just have to pass in the joystick structure we got when we opened the joystick. For Example:</P
><DIV
CLASS="EXAMPLE"
><A
NAME="AEN265"
></A
><P
><B
>Example 3-9. Querying Joystick Characteristics</B
></P
><PRE
CLASS="PROGRAMLISTING"
>    int number_of_buttons;
    SDL_Joystick *joystick;

    joystick = SDL_JoystickOpen(0);
    number_of_buttons = SDL_JoystickNumButtons(joystick);</PRE
></DIV
><P
>This block of code would get the number of buttons on the first joystick in the system.	</P
></DIV
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="guidevideoopengl.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="guideinputkeyboard.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Using OpenGL With SDL</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="guide.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Handling the Keyboard</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>