Joystick.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (C) 2002-2004 The Pentagram team
00003 
00004 This program is free software; you can redistribute it and/or
00005 modify it under the terms of the GNU General Public License
00006 as published by the Free Software Foundation; either version 2
00007 of the License, or (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; if not, write to the Free Software
00016 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 */
00018 
00019 #include "pent_include.h"
00020 #include "Joystick.h"
00021 
00022 #include "SDL_timer.h"
00023 #include "SDL_events.h"
00024 #include "GUIApp.h"
00025 
00026 static SDL_Joystick * joy[JOY_LAST] = {0};
00027 
00028 void InitJoystick()
00029 {
00030         int i, buttons, axes, balls, hats;
00031         int joys = SDL_NumJoysticks();
00032 
00033         for (i = 0; i < joys; ++i)
00034         {
00035                 if (i >= JOY_LAST)
00036                 {
00037                         perr << "Additional joysticks detected. Cannot initialize more than "
00038                                 << JOY_LAST << "." << std::endl;
00039                         break;
00040                 }
00041 
00042                 joy[i] = 0;
00043 
00044                 if(! SDL_JoystickOpened(i))
00045                 {
00046                         joy[i] = SDL_JoystickOpen(i);
00047                         if (joy[i])
00048                         {
00049                                 buttons = SDL_JoystickNumButtons(joy[i]);
00050                                 axes = SDL_JoystickNumAxes(joy[i]);
00051                                 balls = SDL_JoystickNumBalls(joy[i]);
00052                                 hats = SDL_JoystickNumHats(joy[i]);
00053 
00054                                 pout << "Initialized joystick " << i + 1 << "." << std::endl;
00055                                 pout << "\tButtons: " << buttons << std::endl;
00056                                 pout << "\tAxes: " << axes << std::endl;
00057                                 pout << "\tBalls: " << balls << std::endl;
00058                                 pout << "\tHats: " << hats << std::endl;
00059                         }
00060                         else
00061                         {
00062                                 perr << "Error while initializing joystick " << i + 1 << "."
00063                                         << std::endl;
00064                         }
00065                 }
00066         }
00067 }
00068 
00069 void ShutdownJoystick()
00070 {
00071         int i;
00072         for (i = 0; i < JOY_LAST; ++i)
00073         {
00074                 if(joy[i] && SDL_JoystickOpened(i))
00075                 {
00076                         SDL_JoystickClose(joy[i]);
00077                 }
00078                 joy[i] = 0;
00079         }
00080 }
00081 
00082 DEFINE_RUNTIME_CLASSTYPE_CODE(JoystickCursorProcess,Process);
00083 
00084 JoystickCursorProcess::JoystickCursorProcess()
00085         : Process(), js(JOY1), x_axis(0), y_axis(1), ticks(0), accel(0)
00086 {
00087 }
00088 
00089 JoystickCursorProcess::JoystickCursorProcess(Joystick js_, int x_axis_, int y_axis_)
00090         : Process(), js(js_), x_axis(x_axis_), y_axis(y_axis_), ticks(0), accel(0)
00091 {
00092         flags |= PROC_RUNPAUSED;
00093         type = 1;
00094 
00095         if(joy[js] && js < JOY_LAST)
00096         {
00097                 int axes = SDL_JoystickNumAxes(joy[js]);
00098                 if (x_axis >= axes && y_axis >= axes)
00099                 {
00100                         perr << "Failed to start JoystickCursorProcess: illegal axis for x (" << x_axis << ") or y (" << y_axis << ")" << std::endl;
00101                         terminate();
00102                 }
00103         }
00104         else
00105         {
00106                 terminate();
00107         }
00108 
00109 }
00110 
00111 JoystickCursorProcess::~JoystickCursorProcess()
00112 {
00113 }
00114 
00115 #define AXIS_TOLERANCE 1000
00116 
00118 bool JoystickCursorProcess::run(const uint32 /*framenum*/)
00119 {
00120         int dx = 0, dy = 0;
00121         int now = SDL_GetTicks();
00122 
00123         if(joy[js] && ticks)
00124         {
00125                 int tx = now - ticks;
00126                 int r = 350 - accel * 30;
00127                 sint16 jx = SDL_JoystickGetAxis(joy[js], x_axis);
00128                 sint16 jy = SDL_JoystickGetAxis(joy[js], y_axis);
00129                 if (jx > AXIS_TOLERANCE || jx < -AXIS_TOLERANCE)
00130                         dx = ((jx / 1000) * tx) / r;
00131                 if (jy > AXIS_TOLERANCE || jy < -AXIS_TOLERANCE)
00132                         dy = ((jy / 1000) * tx) / r;
00133         }
00134 
00135         ticks = now;
00136 
00137         if (dx || dy)
00138         {
00139                 int mx, my;
00140                 GUIApp * app = GUIApp::get_instance();
00141                 app->getMouseCoords(mx, my);
00142                 mx += dx;
00143                 my += dy;
00144                 app->setMouseCoords(mx, my);
00145                 ++accel;
00146                 if (accel > 10)
00147                         accel = 10;
00148         }
00149         else
00150         {
00151                 accel = 0;
00152         }
00153 
00154         return false;
00155 }
00156 
00157 bool JoystickCursorProcess::loadData(IDataSource* ids, uint32 version)
00158 {
00159         if (!Process::loadData(ids, version)) return false;
00160 
00161         terminateDeferred(); // Don't allow this process to continue
00162         return true;
00163 }
00164 
00165 void JoystickCursorProcess::saveData(ODataSource* ods)
00166 {
00167         Process::saveData(ods);
00168         // saves no status
00169 }

Generated on Fri Jul 27 22:27:22 2007 for pentagram by  doxygen 1.4.7