📄 guideinput.html
字号:
<HTML><HEAD><TITLE>Input handling</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="SDL Library Documentation"HREF="index.html"><LINKREL="UP"TITLE="SDL Guide"HREF="guide.html"><LINKREL="PREVIOUS"TITLE="Using OpenGL With SDL"HREF="guidevideoopengl.html"><LINKREL="NEXT"TITLE="Handling the Keyboard"HREF="guideinputkeyboard.html"></HEAD><BODYCLASS="CHAPTER"BGCOLOR="#FFF8DC"TEXT="#000000"LINK="#0000ee"VLINK="#551a8b"ALINK="#ff0000"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">SDL Library Documentation</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="guidevideoopengl.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="guideinputkeyboard.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="GUIDEINPUT"></A>Chapter 3. Input handling</H1><DIVCLASS="TOC"><DL><DT><B>Table of Contents</B></DT><DT><AHREF="guideinput.html#GUIDEINPUTJOYSTICK">Handling Joysticks</A></DT><DT><AHREF="guideinputkeyboard.html">Handling the Keyboard</A></DT></DL></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="GUIDEINPUTJOYSTICK"></A>Handling Joysticks</H1><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="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 <TTCLASS="LITERAL">SDL_INIT_JOYSTICK</TT> flag to <AHREF="sdlinit.html"><TTCLASS="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><DIVCLASS="EXAMPLE"><ANAME="AEN141"></A><P><B>Example 3-1. Initializing SDL with Joystick Support</B></P><PRECLASS="PROGRAMLISTING"> if (SDL_Init( SDL_INIT_VIDEO | SDL_INIT_JOYSTICK ) < 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><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="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 <AHREF="sdlnumjoysticks.html"><TTCLASS="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 <AHREF="sdljoystickname.html"><TTCLASS="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 <TTCLASS="FUNCTION">SDL_NumJoysticks</TT> - 1. In the demonstration a list of all available joysticks is printed to stdout.</P><DIVCLASS="EXAMPLE"><ANAME="AEN154"></A><P><B>Example 3-2. Querying the Number of Available Joysticks</B></P><PRECLASS="PROGRAMLISTING"> printf("%i joysticks were found.\n\n", SDL_NumJoysticks() ); printf("The names of the joysticks are:\n"); for( i=0; i < SDL_NumJoysticks(); i++ ) { printf(" %s\n", SDL_JoystickName(i)); }</PRE></DIV></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="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><TABLEBORDER="0"><TBODY><TR><TD><AHREF="sdljoyaxisevent.html"><SPANCLASS="STRUCTNAME">SDL_JoyAxisEvent</SPAN></A></TD><TD>Occurs when an axis changes</TD></TR><TR><TD><AHREF="sdljoyballevent.html"><SPANCLASS="STRUCTNAME">SDL_JoyBallEvent</SPAN></A></TD><TD>Occurs when a joystick trackball's position changes</TD></TR><TR><TD><AHREF="sdljoyhatevent.html"><SPANCLASS="STRUCTNAME">SDL_JoyHatEvent</SPAN></A></TD><TD>Occurs when a hat's position changes</TD></TR><TR><TD><AHREF="sdljoybuttonevent.html"><SPANCLASS="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 <AHREF="sdljoystickeventstate.html"><TTCLASS="FUNCTION">SDL_JoystickEventState</TT></A> with the <TTCLASS="LITERAL">SDL_ENABLE</TT> flag. Next you must open the joysticks that you want to receive envents from. This is done with the <AHREF="sdljoystickopen.html"><TTCLASS="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><DIVCLASS="EXAMPLE"><ANAME="AEN183"></A><P><B>Example 3-3. Opening a Joystick</B></P><PRECLASS="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 <TTCLASS="FUNCTION">SDL_JoystickOpen</TT> just like we opened joystick 0, except we would store the <SPANCLASS="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:<PRECLASS="PROGRAMLISTING"> SDL_Event event; /* Other initializtion code goes here */ /* Start main game loop here */ while(SDL_PollEvent(&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><DIVCLASS="EXAMPLE"><ANAME="AEN191"></A><P><B>Example 3-4. Joystick Axis Events</B></P><PRECLASS="PROGRAMLISTING"> case SDL_JOYAXISMOTION: /* Handle Joystick Motion */ if ( ( event.jaxis.value < -3200 ) || (event.jaxis.value > 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><DIVCLASS="EXAMPLE"><ANAME="AEN195"></A><P><B>Example 3-5. More Joystick Axis Events</B></P><PRECLASS="PROGRAMLISTING"> case SDL_JOYAXISMOTION: /* Handle Joystick Motion */ if ( ( event.jaxis.value < -3200 ) || (event.jaxis.value > 3200 ) ) { if( event.jaxis.axis == 0) { /* Left-right movement code goes here */ } if( event.jaxis.axis == 1) { /* Up-Down movement code goes here */ } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -