📄 guideinput.html
字号:
break;</PRE></DIV><P>Ideally the code here should use <TTCLASS="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 <TTCLASS="STRUCTFIELD"><I>event.jaxis.axis</I></TT> values.</P><P>Button handling is simple compared to the axis checking.</P><DIVCLASS="EXAMPLE"><ANAME="AEN203"></A><P><B>Example 3-6. Joystick Button Events</B></P><PRECLASS="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 <TTCLASS="LITERAL">SDL_JOYBUTTONDOWN</TT> event is triggered when a button is pressed and the <TTCLASS="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 <TTCLASS="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 <AHREF="sdljoystickclose.html"><TTCLASS="FUNCTION">SDL_JoystickClose</TT></A>. To close our opened joystick 0 we would do this at the end of our program:<PRECLASS="PROGRAMLISTING"> SDL_JoystickClose(joystick);</PRE></P></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="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><DIVCLASS="EXAMPLE"><ANAME="AEN217"></A><P><B>Example 3-7. Joystick Ball Events</B></P><PRECLASS="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 <TTCLASS="STRUCTFIELD"><I>event.jball.xrel</I></TT> and <TTCLASS="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><TABLEBORDER="0"><TBODY><TR><TD><TTCLASS="LITERAL">SDL_HAT_CENTERED</TT></TD></TR><TR><TD><TTCLASS="LITERAL">SDL_HAT_UP</TT></TD></TR><TR><TD><TTCLASS="LITERAL">SDL_HAT_RIGHT</TT></TD></TR><TR><TD><TTCLASS="LITERAL">SDL_HAT_DOWN</TT></TD></TR><TR><TD><TTCLASS="LITERAL">SDL_HAT_LEFT</TT></TD></TR></TBODY></TABLE><P></P>Also there are some predefined combinations of the above:<P></P><TABLEBORDER="0"><TBODY><TR><TD><TTCLASS="LITERAL">SDL_HAT_RIGHTUP</TT></TD></TR><TR><TD><TTCLASS="LITERAL">SDL_HAT_RIGHTDOWN</TT></TD></TR><TR><TD><TTCLASS="LITERAL">SDL_HAT_LEFTUP</TT></TD></TR><TR><TD><TTCLASS="LITERAL">SDL_HAT_LEFTDOWN</TT></TD></TR></TBODY></TABLE><P></P>Our case for the hat may resemble the following:</P><DIVCLASS="EXAMPLE"><ANAME="AEN244"></A><P><B>Example 3-8. Joystick Hat Events</B></P><PRECLASS="PROGRAMLISTING"> case SDL_JOYHATMOTION: /* Handle Hat Motion */ if ( event.jhat.value & SDL_HAT_UP ) { /* Do up stuff here */ } if ( event.jhat.value & SDL_HAT_LEFT ) { /* Do left stuff here */ } if ( event.jhat.value & 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><TABLEBORDER="0"><TBODY><TR><TD><AHREF="sdljoysticknumaxes.html"><TTCLASS="FUNCTION">SDL_JoystickNumAxes</TT></A></TD><TD>Returns the number of joysitck axes</TD></TR><TR><TD><AHREF="sdljoysticknumbuttons.html"><TTCLASS="FUNCTION">SDL_JoystickNumButtons</TT></A></TD><TD>Returns the number of joysitck buttons</TD></TR><TR><TD><AHREF="sdljoysticknumballs.html"><TTCLASS="FUNCTION">SDL_JoystickNumBalls</TT></A></TD><TD>Returns the number of joysitck balls</TD></TR><TR><TD><AHREF="sdljoysticknumhats.html"><TTCLASS="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><DIVCLASS="EXAMPLE"><ANAME="AEN265"></A><P><B>Example 3-9. Querying Joystick Characteristics</B></P><PRECLASS="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><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="guidevideoopengl.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="guideinputkeyboard.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Using OpenGL With SDL</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="guide.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Handling the Keyboard</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -