Sophie

Sophie

distrib > Arklinux > devel > x86_64 > media > main > by-pkgid > c13bc007afe382f898b3b1cfcaf62e82 > files > 780

allegro-devel-4.4.1.1-2ark.x86_64.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><head><title>
Allegro Manual: Joystick routines
</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<link rel="stylesheet" title="Default" type="text/css" href="allegro.css"></head><body bgcolor=white text=black link="#0000ee" alink="#ff0000" vlink="#551a8b">
<h1><a name="Joystick routines">Joystick routines</a></h1>

<ul>
<li><a href="#calibrate_joystick">calibrate_joystick</a> &mdash; Calibrates the specified joystick.
<li><a href="#calibrate_joystick_name">calibrate_joystick_name</a> &mdash; Returns the next calibration text string.
<li><a href="#initialise_joystick">initialise_joystick</a> &mdash; Deprecated version of install_joystick().
<li><a href="#install_joystick">install_joystick</a> &mdash; Initialises the joystick.
<li><a href="#joy">joy</a> &mdash; Global array of joystick state information.
<li><a href="#load_joystick_data">load_joystick_data</a> &mdash; Loads joystick calibration data.
<li><a href="#num_joysticks">num_joysticks</a> &mdash; Global variable saying how many joysticks there are.
<li><a href="#poll_joystick">poll_joystick</a> &mdash; Polls the joystick.
<li><a href="#remove_joystick">remove_joystick</a> &mdash; Removes the joystick handler.
<li><a href="#save_joystick_data">save_joystick_data</a> &mdash; Saves joystick calibration data.
</ul>

<p>
Unlike keyboard or mouse input, which are usually read through hardware
interrupts by Allegro, joystick input functions have to be polled because
there are no hardware interrupts for them on most platforms. This doesn't
mean that you have to poll the joysticks on each line of code you want to
read their values, but you should make sure to poll them at least once per
frame in your game loop. Otherwise you face the possibility of reading stale
incorrect data.

<p><br>
<div class="al-api"><b>int <a name="install_joystick">install_joystick</a>(int type);</b></div><br>
   Installs Allegro's joystick handler, and calibrates the centre position
   values. The type parameter should usually be JOY_TYPE_AUTODETECT, or see
   the platform specific documentation for a list of the available drivers.
   You must call this routine before using any other joystick functions, and
   you should make sure that all joysticks are in the middle position at the
   time. Example:
<blockquote class="code"><pre>
      <a href="alleg018.html#textout_centre_ex" class="autotype" title="Writes a centered string on a bitmap.">textout_centre_ex</a>(<a href="alleg009.html#screen" class="autotype" title="Global pointer to the screen hardware video memory.">screen</a>, <a href="alleg018.html#font" class="autotype" title="A simple 8x8 fixed size font.">font</a>,
                        "Center the joystick and press a <a href="alleg006.html#key" class="autotype" title="Array of flags indicating key state.">key</a>",
                        <a href="alleg009.html#SCREEN_W" class="autotype" title="Global define to obtain the size of the screen.">SCREEN_W</a>/2, <a href="alleg009.html#SCREEN_H" class="autotype" title="Global define to obtain the size of the screen.">SCREEN_H</a>/2, red_color, -1);
      <a href="alleg006.html#readkey" class="autotype" title="Returns the next character from the keyboard buffer.">readkey</a>();
      if (<a href="#install_joystick" class="autotype" title="Initialises the joystick.">install_joystick</a>(JOY_TYPE_AUTODETECT) != 0)
         abort_on_error("Error initialising joystick!");</pre></blockquote>
<p><b>Return value:</b>
   Returns zero on success. As soon as you have installed the joystick 
   module, you will be able to read the button state and digital (on/off 
   toggle) direction information, which may be enough for some games. If you 
   want to get full analogue input, though, you need to use the 
   calibrate_joystick() functions to measure the exact range of the inputs: 
   see below.


<blockquote class="xref"><em><b>See also:</b></em>
<a class="xref" href="#remove_joystick" title="Removes the joystick handler.">remove_joystick</a>,
<a class="xref" href="#num_joysticks" title="Global variable saying how many joysticks there are.">num_joysticks</a>,
<a class="xref" href="#load_joystick_data" title="Loads joystick calibration data.">load_joystick_data</a>,
<a class="xref" href="#calibrate_joystick" title="Calibrates the specified joystick.">calibrate_joystick</a>,
<a class="xref" href="#calibrate_joystick_name" title="Returns the next calibration text string.">calibrate_joystick_name</a>,
<a class="xref" href="#poll_joystick" title="Polls the joystick.">poll_joystick</a>,
<a class="xref" href="alleg003.html#Standard config variables" title="">Standard config variables</a>,
<a class="xref" href="alleg036.html#JOY_TYPE_*/DOS" title="Supported DOS joystick drivers.">JOY_TYPE_*/DOS</a>,
<a class="xref" href="alleg037.html#JOY_TYPE_*/Windows" title="Supported Windows joystick drivers.">JOY_TYPE_*/Windows</a>,
<a class="xref" href="alleg038.html#JOY_TYPE_*/Linux" title="Supported Linux joystick drivers.">JOY_TYPE_*/Linux</a>.</blockquote>

<blockquote class="eref"><em><b>Examples using this:</b></em>
<a class="eref" href="alleg045.html#exjoy" title="Detecting, calibrating and using joystick input.">exjoy</a>.</blockquote>
<div class="al-api"><b>void <a name="remove_joystick">remove_joystick</a>();</b></div><br>
   Removes the joystick handler. You don't normally need to bother calling 
   this, because allegro_exit() will do it for you.


<blockquote class="xref"><em><b>See also:</b></em>
<a class="xref" href="#install_joystick" title="Initialises the joystick.">install_joystick</a>,
<a class="xref" href="alleg000.html#allegro_exit" title="Closes down the Allegro system.">allegro_exit</a>.</blockquote>
<div class="al-api"><b>int <a name="poll_joystick">poll_joystick</a>();</b></div><br>
   The joystick handler is not interrupt driven, so you need to call this
   function every now and again to update the global position values. Example:
<blockquote class="code"><pre>
   do {
      /* Get joystick input */
      <a href="#poll_joystick" class="autotype" title="Polls the joystick.">poll_joystick</a>();
      
      /* Process input for the first joystick */
      if (<a href="#joy" class="autotype" title="Global array of joystick state information.">joy</a>[0].button[0].b)
         first_button_pressed();

      if (<a href="#joy" class="autotype" title="Global array of joystick state information.">joy</a>[0].button[1].b)
         second_button_pressed();
      ...
   } while(!done);</pre></blockquote>
<p><b>Return value:</b>
   Returns zero on success or a negative number on failure (usually because
   no joystick driver was installed).


<blockquote class="xref"><em><b>See also:</b></em>
<a class="xref" href="#install_joystick" title="Initialises the joystick.">install_joystick</a>,
<a class="xref" href="#joy" title="Global array of joystick state information.">joy</a>,
<a class="xref" href="#num_joysticks" title="Global variable saying how many joysticks there are.">num_joysticks</a>.</blockquote>

<blockquote class="eref"><em><b>Examples using this:</b></em>
<a class="eref" href="alleg045.html#exjoy" title="Detecting, calibrating and using joystick input.">exjoy</a>.</blockquote>
<div class="al-api"><b>extern int <a name="num_joysticks">num_joysticks</a>;</b></div><br>
   Global variable containing the number of active joystick devices. The 
   current drivers support a maximum of eight controllers.


<blockquote class="xref"><em><b>See also:</b></em>
<a class="xref" href="#install_joystick" title="Initialises the joystick.">install_joystick</a>,
<a class="xref" href="#joy" title="Global array of joystick state information.">joy</a>.</blockquote>

<blockquote class="eref"><em><b>Examples using this:</b></em>
<a class="eref" href="alleg045.html#exjoy" title="Detecting, calibrating and using joystick input.">exjoy</a>.</blockquote>
<div class="al-api"><b>extern <a class="autotype" href="alleg001.html#JOYSTICK_INFO" title="Stores information about joysticks.">JOYSTICK_INFO</a> <a name="joy">joy</a>[n];</b></div><br>
   Global array of joystick state information, which is updated by the 
   poll_joystick() function. Only the first num_joysticks elements will 
   contain meaningful information. The JOYSTICK_INFO structure is defined as:
<blockquote class="code"><pre>
      typedef struct <a href="alleg001.html#JOYSTICK_INFO" class="autotype" title="Stores information about joysticks.">JOYSTICK_INFO</a>
      {
         int flags;                       - status flags for this
                                            joystick
         int num_sticks;                  - how many stick inputs?
         int num_buttons;                 - how many buttons?
         <a href="alleg001.html#JOYSTICK_STICK_INFO" class="autotype" title="Stores joystick stick information.">JOYSTICK_STICK_INFO</a> stick[n];    - stick state information
         <a href="alleg001.html#JOYSTICK_BUTTON_INFO" class="autotype" title="Stores joystick button information.">JOYSTICK_BUTTON_INFO</a> button[n];  - button state information
      } <a href="alleg001.html#JOYSTICK_INFO" class="autotype" title="Stores information about joysticks.">JOYSTICK_INFO</a>;
</pre></blockquote>
   The button status is stored in the structure:
<blockquote class="code"><pre>
      typedef struct <a href="alleg001.html#JOYSTICK_BUTTON_INFO" class="autotype" title="Stores joystick button information.">JOYSTICK_BUTTON_INFO</a>
      {
         int b;                           - boolean on/off flag
         char *name;                      - description of this
                                            button
      } <a href="alleg001.html#JOYSTICK_BUTTON_INFO" class="autotype" title="Stores joystick button information.">JOYSTICK_BUTTON_INFO</a>;
</pre></blockquote>
   You may wish to display the button names as part of an input 
   configuration screen to let the user choose what game function will be 
   performed by each button, but in simpler situations you can safely assume 
   that the first two elements in the button array will always be the main 
   trigger controls.

<p>
   Each joystick will provide one or more stick inputs, of varying types. 
   These can be digital controls which snap to specific positions (eg. a 
   gamepad controller, the coolie hat on a Flightstick Pro or Wingman 
   Extreme, or a normal joystick which hasn't yet been calibrated), or they 
   can be full analogue inputs with a smooth range of motion. Sticks may 
   also have different numbers of axes, for example a normal directional 
   control has two, but the Flightstick Pro throttle is only a single axis, 
   and it is possible that the system could be extended in the future to 
   support full 3d controllers. A stick input is described by the structure:
<blockquote class="code"><pre>
      typedef struct <a href="alleg001.html#JOYSTICK_STICK_INFO" class="autotype" title="Stores joystick stick information.">JOYSTICK_STICK_INFO</a>
      {
         int flags;                       - status flags for this
                                            input
         int num_axis;                    - how many axes do we
                                            have? (note the misspelling)
         <a href="alleg001.html#JOYSTICK_AXIS_INFO" class="autotype" title="Stores joystick axis information.">JOYSTICK_AXIS_INFO</a> axis[n];      - axis state information
         char *name;                      - description of this
                                            input
      } <a href="alleg001.html#JOYSTICK_STICK_INFO" class="autotype" title="Stores joystick stick information.">JOYSTICK_STICK_INFO</a>;
</pre></blockquote>
   A single joystick may provide several different stick inputs, but you can 
   safely assume that the first element in the stick array will always be 
   the main directional controller.

<p>
   Information about each of the stick axis is stored in the substructure:
<blockquote class="code"><pre>
      typedef struct <a href="alleg001.html#JOYSTICK_AXIS_INFO" class="autotype" title="Stores joystick axis information.">JOYSTICK_AXIS_INFO</a>
      {
         int pos;                         - analogue axis position
         int d1, d2;                      - digital axis position
         char *name;                      - description of this axis
      } <a href="alleg001.html#JOYSTICK_AXIS_INFO" class="autotype" title="Stores joystick axis information.">JOYSTICK_AXIS_INFO</a>;
</pre></blockquote>
   This provides both analogue input in the pos field (ranging from -128 to 
   128 or from 0 to 255, depending on the type of the control), and digital 
   values in the d1 and d2 fields. For example, when describing the X-axis 
   position, the pos field will hold the horizontal position of the 
   joystick, d1 will be set if it is moved left, and d2 will be set if it is 
   moved right. Allegro will fill in all these values regardless of whether 
   it is using a digital or analogue joystick, emulating the pos field for 
   digital inputs by snapping it to the min, middle, and maximum positions, 
   and emulating the d1 and d2 values for an analogue stick by comparing the 
   current position with the centre point.

<p>
   The joystick flags field may contain any combination of the bit flags:

<p>
   <b>JOYFLAG_DIGITAL</b><br>
      This control is currently providing digital input.

<p>
   <b>JOYFLAG_ANALOGUE</b><br>
      This control is currently providing analogue input.

<p>
   <b>JOYFLAG_CALIB_DIGITAL</b><br>
      This control will be capable of providing digital input once it has 
      been calibrated, but is not doing this at the moment.

<p>
   <b>JOYFLAG_CALIB_ANALOGUE</b><br>
      This control will be capable of providing analogue input once it has 
      been calibrated, but is not doing this at the moment.

<p>
   <b>JOYFLAG_CALIBRATE</b><br>
      Indicates that this control needs to be calibrated. Many devices 
      require multiple calibration steps, so you should call the 
      calibrate_joystick() function from a loop until this flag is cleared.

<p>
   <b>JOYFLAG_SIGNED</b><br>
      Indicates that the analogue axis position is in signed format, ranging 
      from -128 to 128. This is the case for all 2d directional controls.

<p>
   <b>JOYFLAG_UNSIGNED</b><br>
      Indicates that the analogue axis position is in unsigned format, 
      ranging from 0 to 255. This is the case for all 1d throttle controls.

<p>
   Note for people who spell funny: in case you don't like having to type 
   "analogue", there are some #define aliases in allegro/joystick.h that
   will allow you to write "analog" instead.


<blockquote class="xref"><em><b>See also:</b></em>
<a class="xref" href="#install_joystick" title="Initialises the joystick.">install_joystick</a>,
<a class="xref" href="#poll_joystick" title="Polls the joystick.">poll_joystick</a>,
<a class="xref" href="#num_joysticks" title="Global variable saying how many joysticks there are.">num_joysticks</a>,
<a class="xref" href="#calibrate_joystick" title="Calibrates the specified joystick.">calibrate_joystick</a>,
<a class="xref" href="#calibrate_joystick_name" title="Returns the next calibration text string.">calibrate_joystick_name</a>.</blockquote>

<blockquote class="eref"><em><b>Examples using this:</b></em>
<a class="eref" href="alleg045.html#exjoy" title="Detecting, calibrating and using joystick input.">exjoy</a>.</blockquote>
<div class="al-api"><b>const char *<a name="calibrate_joystick_name">calibrate_joystick_name</a>(int n);</b></div><br>
   Pass the number of the joystick you want to calibrate as the parameter.
<p><b>Return value:</b>
   Returns a text description for the next type of calibration that will be
   done on the specified joystick, or NULL if no more calibration is 
   required.


<blockquote class="xref"><em><b>See also:</b></em>
<a class="xref" href="#install_joystick" title="Initialises the joystick.">install_joystick</a>,
<a class="xref" href="#calibrate_joystick" title="Calibrates the specified joystick.">calibrate_joystick</a>,
<a class="xref" href="#joy" title="Global array of joystick state information.">joy</a>,
<a class="xref" href="#num_joysticks" title="Global variable saying how many joysticks there are.">num_joysticks</a>.</blockquote>

<blockquote class="eref"><em><b>Examples using this:</b></em>
<a class="eref" href="alleg045.html#exjoy" title="Detecting, calibrating and using joystick input.">exjoy</a>.</blockquote>
<div class="al-api"><b>int <a name="calibrate_joystick">calibrate_joystick</a>(int n);</b></div><br>
   Most joysticks need to be calibrated before they can provide full 
   analogue input. This function performs the next operation in the 
   calibration series for the specified stick, assuming that the joystick 
   has been positioned in the manner described by a previous call to 
   calibrate_joystick_name(), returning zero on success. For example, a 
   simple routine to fully calibrate all the joysticks might look like:
<blockquote class="code"><pre>
      int i;

      for (i=0; i&lt;;<a href="#num_joysticks" class="autotype" title="Global variable saying how many joysticks there are.">num_joysticks</a>; i++) {
         while (<a href="#joy" class="autotype" title="Global array of joystick state information.">joy</a>[i].flags &amp; JOYFLAG_CALIBRATE) {
            char *msg = <a href="#calibrate_joystick_name" class="autotype" title="Returns the next calibration text string.">calibrate_joystick_name</a>(i);
            <a href="alleg018.html#textprintf_ex" class="autotype" title="Formatted output of a string.">textprintf_ex</a>(..., "%s, and press a <a href="alleg006.html#key" class="autotype" title="Array of flags indicating key state.">key</a>\n", msg);
            <a href="alleg006.html#readkey" class="autotype" title="Returns the next character from the keyboard buffer.">readkey</a>();
            if (<a href="#calibrate_joystick" class="autotype" title="Calibrates the specified joystick.">calibrate_joystick</a>(i) != 0) {
               <a href="alleg018.html#textprintf_ex" class="autotype" title="Formatted output of a string.">textprintf_ex</a>(..., "oops!\n");
               <a href="alleg006.html#readkey" class="autotype" title="Returns the next character from the keyboard buffer.">readkey</a>();
               exit(1);
            }
         }
      }</pre></blockquote>
<p><b>Return value:</b>
   Returns zero on success, non-zero if the calibration could not be
   performed successfully.
   

<blockquote class="xref"><em><b>See also:</b></em>
<a class="xref" href="#install_joystick" title="Initialises the joystick.">install_joystick</a>,
<a class="xref" href="#calibrate_joystick_name" title="Returns the next calibration text string.">calibrate_joystick_name</a>,
<a class="xref" href="#joy" title="Global array of joystick state information.">joy</a>,
<a class="xref" href="#num_joysticks" title="Global variable saying how many joysticks there are.">num_joysticks</a>.</blockquote>

<blockquote class="eref"><em><b>Examples using this:</b></em>
<a class="eref" href="alleg045.html#exjoy" title="Detecting, calibrating and using joystick input.">exjoy</a>.</blockquote>
<div class="al-api"><b>int <a name="save_joystick_data">save_joystick_data</a>(const char *filename);</b></div><br>
   After all the headache of calibrating the joystick, you may not want to 
   make your poor users repeat the process every time they run your program. 
   Call this function to save the joystick calibration data into the 
   specified configuration file, from which it can later be read by 
   load_joystick_data(). Pass a NULL filename to write the data to the 
   currently selected configuration file.
<p><b>Return value:</b>
   Returns zero on success, non-zero if the data could not be saved.


<blockquote class="xref"><em><b>See also:</b></em>
<a class="xref" href="#load_joystick_data" title="Loads joystick calibration data.">load_joystick_data</a>,
<a class="xref" href="alleg003.html#set_config_file" title="Sets the configuration file.">set_config_file</a>.</blockquote>
<div class="al-api"><b>int <a name="load_joystick_data">load_joystick_data</a>(const char *filename);</b></div><br>
   Restores calibration data previously stored by save_joystick_data() or 
   the setup utility. This sets up all aspects of the joystick code: you 
   don't even need to call install_joystick() if you are using this 
   function. Pass a NULL filename to read the data from the currently 
   selected configuration file.
<p><b>Return value:</b>
   Returns zero on success: if it fails the joystick state is undefined and
   you must reinitialise it from scratch.


<blockquote class="xref"><em><b>See also:</b></em>
<a class="xref" href="#install_joystick" title="Initialises the joystick.">install_joystick</a>,
<a class="xref" href="#save_joystick_data" title="Saves joystick calibration data.">save_joystick_data</a>,
<a class="xref" href="alleg003.html#set_config_file" title="Sets the configuration file.">set_config_file</a>.</blockquote>
<div class="al-api"><b>int <a name="initialise_joystick">initialise_joystick</a>();</b></div><br>
   Deprecated. Use install_joystick() instead.




<blockquote class="xref"><em><b>See also:</b></em>
<a class="xref" href="#install_joystick" title="Initialises the joystick.">install_joystick</a>.</blockquote>
<hr><div class="al-back-to-contents"><a href="allegro.html">Back to contents</a></div>

</body>
</html>