Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKester Maddock <Christopher.Maddock.1@uni.massey.ac.nz>2005-01-23 04:36:29 +0300
committerKester Maddock <Christopher.Maddock.1@uni.massey.ac.nz>2005-01-23 04:36:29 +0300
commita1f0f165e42982fb5ef45dea54f3ebc48cbca892 (patch)
tree09d843f18ce514a76779d90767145dba7c8c8ad6 /source/gameengine/GameLogic/Joystick
parenta572d196e655e60c68c622adcb28cf45138c4d15 (diff)
Added Joystick sensor (from snailrose)
Diffstat (limited to 'source/gameengine/GameLogic/Joystick')
-rw-r--r--source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp355
-rw-r--r--source/gameengine/GameLogic/Joystick/SCA_Joystick.h195
-rw-r--r--source/gameengine/GameLogic/Joystick/SCA_JoystickDefines.h52
-rw-r--r--source/gameengine/GameLogic/Joystick/SCA_JoystickEvents.cpp73
-rw-r--r--source/gameengine/GameLogic/Joystick/SCA_JoystickPrivate.h52
5 files changed, 727 insertions, 0 deletions
diff --git a/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp b/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp
new file mode 100644
index 00000000000..7d1d4b28730
--- /dev/null
+++ b/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp
@@ -0,0 +1,355 @@
+/**
+ * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version. The Blender
+ * Foundation also sells licenses for use in proprietary software under
+ * the Blender License. See http://www.blender.org/BL/ for information
+ * about this.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): snailrose.
+ *
+ * ***** END GPL/BL DUAL LICENSE BLOCK *****
+ */
+#include <SDL.h>
+
+#include "SCA_Joystick.h"
+#include "SCA_JoystickPrivate.h"
+
+SCA_Joystick::SCA_Joystick()
+ :
+ m_axis10(0),
+ m_axis11(0),
+ m_axis20(0),
+ m_axis21(0),
+ m_prec(3200),
+ m_buttonnum(-2),
+ m_hatdir(-2),
+ m_isinit(0),
+ m_istrig(0)
+{
+ m_private = new PrivateData();
+}
+
+SCA_Joystick::~SCA_Joystick()
+{
+ delete m_private;
+}
+
+bool SCA_Joystick::CreateJoystickDevice()
+{
+ bool init = false;
+ init = pCreateJoystickDevice();
+ return init;
+}
+
+
+void SCA_Joystick::DestroyJoystickDevice()
+{
+ if(m_isinit)
+ pDestroyJoystickDevice();
+}
+
+void SCA_Joystick::HandleEvents()
+{
+ if(m_isinit)
+ {
+ if(SDL_PollEvent(&m_private->m_event))
+ {
+ switch(m_private->m_event.type)
+ {
+ case SDL_JOYAXISMOTION: {HANDLE_AXISMOTION(OnAxisMotion);break;}
+ case SDL_JOYHATMOTION: {HANDLE_HATMOTION(OnHatMotion); break;}
+ case SDL_JOYBUTTONUP: {HANDLE_BUTTONUP(OnButtonUp); break;}
+ case SDL_JOYBUTTONDOWN: {HANDLE_BUTTONDOWN(OnButtonDown);break;}
+ case SDL_JOYBALLMOTION: {HANDLE_BALLMOTION(OnBallMotion);break;}
+ default: {HANDLE_NOEVENT(OnNothing); break;}
+ }
+ }
+ }
+}
+
+
+void SCA_Joystick::cSetPrecision(int val)
+{
+ m_prec = val;
+}
+
+bool SCA_Joystick::aRightAxisIsPositive(int axis)
+{
+ bool result;
+ int res = pGetAxis(axis,1);
+ res > m_prec? result = true: result = false;
+ m_istrig = result;
+ return result;
+}
+
+
+bool SCA_Joystick::aUpAxisIsPositive(int axis)
+{
+ bool result;
+ int res = pGetAxis(axis,0);
+ res < -m_prec? result = true : result = false;
+ m_istrig = result;
+ return result;
+}
+
+
+bool SCA_Joystick::aLeftAxisIsPositive(int axis)
+{
+ bool result;
+ int res = pGetAxis(axis,1);
+ res < -m_prec ? result = true : result = false;
+ m_istrig = result;
+ return result;
+}
+
+
+bool SCA_Joystick::aDownAxisIsPositive(int axis)
+{
+ bool result;
+ int res = pGetAxis(axis,0);
+ res > m_prec ? result = true:result = false;
+ m_istrig = result;
+ return result;
+}
+
+
+bool SCA_Joystick::aButtonPressIsPositive(int button)
+{
+ bool result;
+ SDL_JoystickGetButton(m_private->m_joystick, button)? result = true:result = false;
+ m_istrig = result;
+ return result;
+}
+
+
+bool SCA_Joystick::aButtonReleaseIsPositive(int button)
+{
+ bool result;
+ SDL_JoystickGetButton(m_private->m_joystick, button)? result = false : result = true;
+ m_istrig = result;
+ return result;
+}
+
+
+bool SCA_Joystick::aHatIsPositive(int dir)
+{
+ bool result;
+ int res = pGetHat(dir);
+ res == dir? result = true : result = false;
+ m_istrig = result;
+ return result;
+}
+
+
+int SCA_Joystick::pGetButtonPress(int button)
+{
+ if(button == m_buttonnum)
+ return m_buttonnum;
+ return -2;
+
+}
+
+
+int SCA_Joystick::pGetButtonRelease(int button)
+{
+ if(button == m_buttonnum)
+ return m_buttonnum;
+ return -2;
+}
+
+
+int SCA_Joystick::pGetHat(int direction)
+{
+ if(direction == m_hatdir){
+ return m_hatdir;
+ }
+ return 0;
+}
+
+
+bool SCA_Joystick::GetJoyAxisMotion()
+{
+ bool result = false;
+ if(m_isinit){
+ if(SDL_PollEvent(&m_private->m_event)){
+ switch(m_private->m_event.type)
+ {
+ case SDL_JOYAXISMOTION:
+ result = true;
+ break;
+ }
+ }
+ }
+ return result;
+}
+
+
+bool SCA_Joystick::GetJoyButtonPress()
+{
+ bool result = false;
+ if(m_isinit){
+ if(SDL_PollEvent(&m_private->m_event)){
+ switch(m_private->m_event.type)
+ {
+ case SDL_JOYBUTTONDOWN:
+ result = true;
+ break;
+ }
+ }
+ }
+ return result;
+}
+
+
+bool SCA_Joystick::GetJoyButtonRelease()
+{
+ bool result = false;
+ if(m_isinit)
+ {
+ if(SDL_PollEvent(&m_private->m_event)){
+ switch(m_private->m_event.type)
+ {
+ case SDL_JOYBUTTONUP:
+ result = true;
+ break;
+ }
+ }
+ }
+ return result;
+}
+
+
+bool SCA_Joystick::GetJoyHatMotion()
+{
+ bool result = false;
+ if(m_isinit){
+ if(SDL_PollEvent(&m_private->m_event)){
+ switch(m_private->m_event.type)
+ {
+ case SDL_JOYHATMOTION:
+ result = true;
+ break;
+ }
+ }
+ }
+ return 0;
+}
+
+
+int SCA_Joystick::GetNumberOfAxes()
+{
+ int number;
+ if(m_isinit){
+ if(m_private->m_joystick){
+ number = SDL_JoystickNumAxes(m_private->m_joystick);
+ return number;
+ }
+ }
+ return -1;
+}
+
+
+int SCA_Joystick::GetNumberOfButtons()
+{
+ int number;
+ if(m_isinit){
+ if(m_private->m_joystick){
+ number = SDL_JoystickNumButtons(m_private->m_joystick);
+ return number;
+ }
+ }
+ return -1;
+}
+
+int SCA_Joystick::GetNumberOfHats()
+{
+ int number;
+ if(m_isinit){
+ if(m_private->m_joystick){
+ number = SDL_JoystickNumHats(m_private->m_joystick);
+ return number;
+ }
+ }
+ return -1;
+}
+
+
+bool SCA_Joystick::pCreateJoystickDevice()
+{
+ if(m_isinit == false){
+ if(SDL_InitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO ) == -1 ){
+ echo("Error-Initializing-SDL: " << SDL_GetError());
+ return false;
+ }
+ if(SDL_NumJoysticks() > 0){
+ for(int i=0; i<SDL_NumJoysticks();i++){
+ m_private->m_joystick = SDL_JoystickOpen(i);
+ SDL_JoystickEventState(SDL_ENABLE);
+ m_numjoys = i;
+ }
+ echo("Joystick-initialized");
+ m_isinit = true;
+ return true;
+ }else{
+ echo("Joystick-Error: " << SDL_NumJoysticks() << " avaiable joystick(s)");
+ return false;
+ }
+ }
+ return false;
+}
+
+
+void SCA_Joystick::pDestroyJoystickDevice()
+{
+ echo("Closing-");
+ for(int i=0; i<SDL_NumJoysticks(); i++){
+ if(SDL_JoystickOpened(i)){
+ SDL_JoystickClose(m_private->m_joystick);
+ }
+ }
+ SDL_QuitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO );
+}
+
+void SCA_Joystick::pFillAxes()
+{
+ if(GetNumberOfAxes() == 1){
+ m_axis10 = SDL_JoystickGetAxis(m_private->m_joystick, 0);
+ m_axis11 = SDL_JoystickGetAxis(m_private->m_joystick, 1);
+ }else if(GetNumberOfAxes() > 1){
+ m_axis10 = SDL_JoystickGetAxis(m_private->m_joystick, 0);
+ m_axis11 = SDL_JoystickGetAxis(m_private->m_joystick, 1);
+ m_axis20 = SDL_JoystickGetAxis(m_private->m_joystick, 2);
+ m_axis21 = SDL_JoystickGetAxis(m_private->m_joystick, 3);
+ }else{
+ m_axis10 = 0;m_axis11 = 0;
+ m_axis20 = 0;m_axis21 = 0;
+ }
+}
+
+int SCA_Joystick::pGetAxis(int axisnum, int udlr)
+{
+ if(axisnum == 1 && udlr == 1)return m_axis10; //u/d
+ if(axisnum == 1 && udlr == 0)return m_axis11; //l/r
+ if(axisnum == 2 && udlr == 0)return m_axis20; //...
+ if(axisnum == 2 && udlr == 1)return m_axis21;
+ return 0;
+}
+
diff --git a/source/gameengine/GameLogic/Joystick/SCA_Joystick.h b/source/gameengine/GameLogic/Joystick/SCA_Joystick.h
new file mode 100644
index 00000000000..413dbfa4771
--- /dev/null
+++ b/source/gameengine/GameLogic/Joystick/SCA_Joystick.h
@@ -0,0 +1,195 @@
+/**
+ * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version. The Blender
+ * Foundation also sells licenses for use in proprietary software under
+ * the Blender License. See http://www.blender.org/BL/ for information
+ * about this.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): snailrose.
+ *
+ * ***** END GPL/BL DUAL LICENSE BLOCK *****
+ */
+#ifndef _SCA_JOYSTICK_H_
+#define _SCA_JOYSTICK_H_
+
+#include "SCA_JoystickDefines.h"
+
+using namespace std;
+
+/*
+ * Basic Joystick class
+ */
+class SCA_Joystick
+{
+ class PrivateData;
+ PrivateData *m_private;
+ int m_joyindex;
+ /*!
+ * the number of avail joysticks
+ */
+ int m_numjoys;
+ /*
+ *support for 2 axes
+ */
+ int m_axis10,m_axis11;
+ int m_axis20,m_axis21;
+ /*
+ * Precision or range of the axes
+ */
+ int m_prec;
+ /*
+ * multiple axis values stored here
+ */
+ int m_axisnum;
+ int m_axisvalue;
+ /*
+ * max # of axes avail
+ */
+ /*disabled
+ int m_axismax;
+ */
+ /*
+ * button values stored here
+ */
+ int m_buttonnum;
+ /*
+ * max # of buttons avail
+ */
+ int m_buttonmax;
+ /*
+ * hat values stored here
+ */
+ int m_hatnum;
+ int m_hatdir;
+ /*
+ * max # of hats avail
+ disabled
+ int m_hatmax;
+ */
+ /* is the joystick initialized ?*/
+ bool m_isinit;
+
+ /* is triggered */
+ bool m_istrig;
+ /*
+ * Open the joystick
+ */
+ bool pCreateJoystickDevice(void);
+ /*
+ * Close the joystick
+ */
+ void pDestroyJoystickDevice(void);
+
+ /*
+ * event callbacks
+ */
+ void OnAxisMotion(void);
+ void OnHatMotion(void);
+ void OnButtonUp(void);
+ void OnButtonDown(void);
+ void OnNothing(void);
+ void OnBallMotion(void){}
+ /*
+ * fills the axis mnember values
+ */
+ void pFillAxes(void);
+
+ void pFillButtons(void);
+ /*
+ * returns m_axis10,m_axis11...
+ */
+ int pGetAxis(int axisnum, int udlr);
+ /*
+ * gets the current button
+ */
+ int pGetButtonPress(int button);
+ /*
+ * returns if no button is pressed
+ */
+ int pGetButtonRelease(int button);
+ /*
+ * gets the current hat direction
+ */
+ int pGetHat(int direction);
+
+public:
+ SCA_Joystick();
+ ~SCA_Joystick();
+
+ bool CreateJoystickDevice(void);
+ void DestroyJoystickDevice(void);
+ void HandleEvents();
+ /*
+ */
+ bool aUpAxisIsPositive(int axis);
+ bool aDownAxisIsPositive(int axis);
+ bool aLeftAxisIsPositive(int axis);
+ bool aRightAxisIsPositive(int axis);
+ bool aButtonPressIsPositive(int button);
+ bool aButtonReleaseIsPositive(int button);
+ bool aHatIsPositive(int dir);
+ /*
+ * precision is default '3200' which is overridden by input
+ */
+ void cSetPrecision(int val);
+
+ int GetAxis10(void){
+ return m_axis10;
+ }
+ int GetAxis11(void){
+ return m_axis11;
+ }
+ int GetAxis20(void){
+ return m_axis20;
+ }
+ int GetAxis21(void){
+ return m_axis21;
+ }
+ int GetButton(void){
+ return m_buttonnum;
+ }
+ int GetHat(void){
+ return m_hatdir;
+ }
+ int GetThreshold(void){
+ return m_prec;
+ }
+ bool IsTrig(void){
+ return m_istrig;
+ }
+
+ /*
+ * returns true if an event is being processed
+ */
+ bool GetJoyAxisMotion(void);
+ bool GetJoyButtonPress(void);
+ bool GetJoyButtonRelease(void);
+ bool GetJoyHatMotion(void);
+ /*
+ * returns the # of...
+ */
+ int GetNumberOfAxes(void);
+ int GetNumberOfButtons(void);
+ int GetNumberOfHats(void);
+
+};
+
+#endif
diff --git a/source/gameengine/GameLogic/Joystick/SCA_JoystickDefines.h b/source/gameengine/GameLogic/Joystick/SCA_JoystickDefines.h
new file mode 100644
index 00000000000..c4ddd5b9c40
--- /dev/null
+++ b/source/gameengine/GameLogic/Joystick/SCA_JoystickDefines.h
@@ -0,0 +1,52 @@
+/**
+ * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version. The Blender
+ * Foundation also sells licenses for use in proprietary software under
+ * the Blender License. See http://www.blender.org/BL/ for information
+ * about this.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): snailrose.
+ *
+ * ***** END GPL/BL DUAL LICENSE BLOCK *****
+ */
+#ifndef _SCA_JOYSTICKDEFINES_H_
+#define _SCA_JOYSTICKDEFINES_H_
+
+#ifdef main
+#undef main
+#endif
+
+#ifndef _DEBUG
+#define echo(x)
+#else
+#include <iostream>
+#define echo(x) std::cout << x << std::endl;
+#endif
+
+/* function callbacks */
+#define HANDLE_AXISMOTION(fn) ((fn)(), 0L)
+#define HANDLE_HATMOTION(fn) ((fn)(), 0L)
+#define HANDLE_BUTTONUP(fn) ((fn)(), 0L)
+#define HANDLE_BUTTONDOWN(fn) ((fn)(), 0L)
+#define HANDLE_BALLMOTION(fn) ((fn)(), 0L)
+#define HANDLE_NOEVENT(fn) ((fn)(), 0L)
+
+#endif
diff --git a/source/gameengine/GameLogic/Joystick/SCA_JoystickEvents.cpp b/source/gameengine/GameLogic/Joystick/SCA_JoystickEvents.cpp
new file mode 100644
index 00000000000..90bb728936b
--- /dev/null
+++ b/source/gameengine/GameLogic/Joystick/SCA_JoystickEvents.cpp
@@ -0,0 +1,73 @@
+/**
+ * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version. The Blender
+ * Foundation also sells licenses for use in proprietary software under
+ * the Blender License. See http://www.blender.org/BL/ for information
+ * about this.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): snailrose.
+ *
+ * ***** END GPL/BL DUAL LICENSE BLOCK *****
+ */
+#include <SDL.h>
+
+#include "SCA_Joystick.h"
+#include "SCA_JoystickPrivate.h"
+
+void SCA_Joystick::OnAxisMotion(void)
+{
+ pFillAxes();
+ m_axisnum = m_private->m_event.jaxis.axis;
+ m_axisvalue = m_private->m_event.jaxis.value;
+ m_istrig = 1;
+}
+
+
+void SCA_Joystick::OnHatMotion(void)
+{
+ m_hatdir = m_private->m_event.jhat.value;
+ m_hatnum = m_private->m_event.jhat.hat;
+ m_istrig = 1;
+}
+
+
+void SCA_Joystick::OnButtonUp(void)
+{
+ m_buttonnum = -2;
+}
+
+
+void SCA_Joystick::OnButtonDown(void)
+{
+ m_buttonmax = GetNumberOfButtons();
+ if(m_private->m_event.jbutton.button >= 1 || m_private->m_event.jbutton.button <= m_buttonmax)
+ {
+ m_istrig = 1;
+ m_buttonnum = m_private->m_event.jbutton.button;
+ }
+}
+
+
+void SCA_Joystick::OnNothing(void)
+{
+ m_istrig = 0;
+}
+
diff --git a/source/gameengine/GameLogic/Joystick/SCA_JoystickPrivate.h b/source/gameengine/GameLogic/Joystick/SCA_JoystickPrivate.h
new file mode 100644
index 00000000000..08435749cfe
--- /dev/null
+++ b/source/gameengine/GameLogic/Joystick/SCA_JoystickPrivate.h
@@ -0,0 +1,52 @@
+/**
+ * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version. The Blender
+ * Foundation also sells licenses for use in proprietary software under
+ * the Blender License. See http://www.blender.org/BL/ for information
+ * about this.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): snailrose.
+ *
+ * ***** END GPL/BL DUAL LICENSE BLOCK *****
+ */
+
+#ifndef __SCA_JOYSTICKPRIVATE_H__
+#define __SCA_JOYSTICKPRIVATE_H__
+#include "SCA_Joystick.h"
+
+class SCA_Joystick::PrivateData
+{
+public:
+ /*
+ * SDL events structure
+ */
+ SDL_Event m_event;
+ /*
+ * The Joystick
+ */
+ SDL_Joystick* m_joystick;
+
+ PrivateData()
+ : m_joystick(NULL)
+ {
+ }
+};
+#endif