From 74339c639a7dee831cb54bd056c111a4c37a40e4 Mon Sep 17 00:00:00 2001 From: Benoit Bolsee Date: Sun, 31 Aug 2008 18:42:58 +0000 Subject: BGE patch approved: BGE Multiple Joysticks --- .../gameengine/GameLogic/Joystick/SCA_Joystick.cpp | 95 +++++++++++----------- .../gameengine/GameLogic/Joystick/SCA_Joystick.h | 23 ++---- .../GameLogic/Joystick/SCA_JoystickDefines.h | 2 + 3 files changed, 53 insertions(+), 67 deletions(-) (limited to 'source/gameengine/GameLogic/Joystick') 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; iCreateJoystickDevice(); + } 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; iDestroyJoystickDevice(); + 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; im_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; im_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) -- cgit v1.2.3