guideinput.html 15.1 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
<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
>