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:
authorBenoit Bolsee <benoit.bolsee@online.be>2008-08-31 22:42:58 +0400
committerBenoit Bolsee <benoit.bolsee@online.be>2008-08-31 22:42:58 +0400
commit74339c639a7dee831cb54bd056c111a4c37a40e4 (patch)
tree1e7499706e445902810aaf4bd7e5076b2e9447bc /source/gameengine/GameLogic/Joystick
parenta4e74c92f776a362be542098db7844faa9498c74 (diff)
BGE patch approved: BGE Multiple Joysticks
Diffstat (limited to 'source/gameengine/GameLogic/Joystick')
-rw-r--r--source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp95
-rw-r--r--source/gameengine/GameLogic/Joystick/SCA_Joystick.h23
-rw-r--r--source/gameengine/GameLogic/Joystick/SCA_JoystickDefines.h2
3 files changed, 53 insertions, 67 deletions
diff --git a/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp b/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp
index 18d7b6ffcd0..b244bddcacd 100644
--- a/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp
+++ b/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp
@@ -29,9 +29,9 @@
#include "SCA_Joystick.h"
#include "SCA_JoystickPrivate.h"
-
-SCA_Joystick::SCA_Joystick()
+SCA_Joystick::SCA_Joystick(short int index)
:
+ m_joyindex(index),
m_axis10(0),
m_axis11(0),
m_axis20(0),
@@ -52,50 +52,53 @@ SCA_Joystick::~SCA_Joystick()
delete m_private;
}
-SCA_Joystick *SCA_Joystick::m_instance = NULL;
+SCA_Joystick *SCA_Joystick::m_instance[JOYINDEX_MAX];
int SCA_Joystick::m_refCount = 0;
-SCA_Joystick *SCA_Joystick::GetInstance()
+SCA_Joystick *SCA_Joystick::GetInstance( short int joyindex )
{
- if (m_instance == 0)
+ if (joyindex < 0 || joyindex >= JOYINDEX_MAX) {
+ echo("Error-invalid joystick index: " << joyindex);
+ return NULL;
+ }
+
+ if (m_refCount == 0)
{
- m_instance = new SCA_Joystick();
- m_instance->CreateJoystickDevice();
+ int i;
+ // do this once only
+ if(SDL_InitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO ) == -1 ){
+ echo("Error-Initializing-SDL: " << SDL_GetError());
+ return NULL;
+ }
+ for (i=0; i<JOYINDEX_MAX; i++) {
+ m_instance[i] = new SCA_Joystick(i);
+ m_instance[i]->CreateJoystickDevice();
+ }
m_refCount = 1;
}
else
{
m_refCount++;
}
- return m_instance;
+ return m_instance[joyindex];
}
void SCA_Joystick::ReleaseInstance()
{
if (--m_refCount == 0)
{
- DestroyJoystickDevice();
- delete m_instance;
- m_instance = NULL;
+ int i;
+ for (i=0; i<JOYINDEX_MAX; i++) {
+ if (m_instance[i]) {
+ m_instance[i]->DestroyJoystickDevice();
+ delete m_instance[i];
+ }
+ m_instance[i]= NULL;
+ }
+ SDL_QuitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO );
}
}
-
-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)
@@ -334,40 +337,34 @@ int SCA_Joystick::GetNumberOfHats()
return -1;
}
-bool SCA_Joystick::pCreateJoystickDevice()
+bool SCA_Joystick::CreateJoystickDevice(void)
{
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)");
+ if (m_joyindex>=SDL_NumJoysticks()) {
+ // don't print a message, because this is done anyway
+ //echo("Joystick-Error: " << SDL_NumJoysticks() << " avaiable joystick(s)");
return false;
}
+
+ m_private->m_joystick = SDL_JoystickOpen(m_joyindex);
+ SDL_JoystickEventState(SDL_ENABLE);
+
+ echo("Joystick " << m_joyindex << " initialized");
+ m_isinit = true;
}
- return false;
+ return true;
}
-void SCA_Joystick::pDestroyJoystickDevice()
+void SCA_Joystick::DestroyJoystickDevice(void)
{
- echo("Closing-");
- for(int i=0; i<SDL_NumJoysticks(); i++){
- if(SDL_JoystickOpened(i)){
+ if (m_isinit){
+ if(SDL_JoystickOpened(m_joyindex)){
+ echo("Closing-joystick " << m_joyindex);
SDL_JoystickClose(m_private->m_joystick);
}
+ m_isinit = false;
}
- SDL_QuitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO );
}
diff --git a/source/gameengine/GameLogic/Joystick/SCA_Joystick.h b/source/gameengine/GameLogic/Joystick/SCA_Joystick.h
index 1e853070b09..689efc72975 100644
--- a/source/gameengine/GameLogic/Joystick/SCA_Joystick.h
+++ b/source/gameengine/GameLogic/Joystick/SCA_Joystick.h
@@ -76,7 +76,7 @@
class SCA_Joystick
{
- static SCA_Joystick *m_instance;
+ static SCA_Joystick *m_instance[JOYINDEX_MAX];
static int m_refCount;
class PrivateData;
@@ -85,14 +85,6 @@ class SCA_Joystick
int m_joyindex;
- /*!
-
- * the number of avail joysticks
-
- */
-
- int m_numjoys;
-
/*
*support for 2 axes
@@ -185,7 +177,7 @@ class SCA_Joystick
*/
- bool pCreateJoystickDevice(void);
+ bool CreateJoystickDevice(void);
/*
@@ -193,7 +185,7 @@ class SCA_Joystick
*/
- void pDestroyJoystickDevice(void);
+ void DestroyJoystickDevice(void);
@@ -259,18 +251,13 @@ class SCA_Joystick
int pGetHat(int direction);
- SCA_Joystick();
+ SCA_Joystick(short int index);
~SCA_Joystick();
- bool CreateJoystickDevice(void);
-
- void DestroyJoystickDevice(void);
-
-
public:
- static SCA_Joystick *GetInstance();
+ static SCA_Joystick *GetInstance( short int joyindex );
void ReleaseInstance();
diff --git a/source/gameengine/GameLogic/Joystick/SCA_JoystickDefines.h b/source/gameengine/GameLogic/Joystick/SCA_JoystickDefines.h
index 15a421188b9..8d8f88ecaf2 100644
--- a/source/gameengine/GameLogic/Joystick/SCA_JoystickDefines.h
+++ b/source/gameengine/GameLogic/Joystick/SCA_JoystickDefines.h
@@ -38,6 +38,8 @@
#define echo(x) std::cout << x << std::endl;
#endif
+#define JOYINDEX_MAX 8
+
/* function callbacks */
#define HANDLE_AXISMOTION(fn) ((fn)(), 0L)
#define HANDLE_HATMOTION(fn) ((fn)(), 0L)