History

allow a call to Joysticks.reset to reset SDL while interface is running

Stephen Birarda committed 12 years ago

Commit 17b9f4a1 parent c56923d4 files · +78 −24Browse files

Showing the changes to interface/src/scripting/JoystickScriptingInterface.cpp (1 of 4 files). Show all

Unified · Split

interface/src/scripting/JoystickScriptingInterface.cpp+65−24
@@ -27,17 +27,10 @@ JoystickScriptingInterface& JoystickScriptingInterface::getInstance() { · 26 unmodified lines
2727
2828 JoystickScriptingInterface::JoystickScriptingInterface() :
2929 _openJoysticks(),
30− _availableDeviceNames()
30+ _availableDeviceNames(),
31+ _isInitialized(false)
3132 {
32−#ifdef HAVE_SDL
33− SDL_Init(SDL_INIT_JOYSTICK);
34−
35− int joystickCount = SDL_NumJoysticks();
36−
37− for (int i = 0; i < joystickCount; i++) {
38− _availableDeviceNames << SDL_JoystickName(i);
39− }
40−#endif
33+ reset();
4134 }
4235
4336 JoystickScriptingInterface::~JoystickScriptingInterface() {
@@ -45,18 +38,53 @@ JoystickScriptingInterface::~JoystickScriptingInterface() { · 1 unmodified line
4538
4639 #ifdef HAVE_SDL
4740 SDL_Quit();
41+ _isInitialized = false;
4842 #endif
4943 }
5044
51−void JoystickScriptingInterface::update() {
45+void JoystickScriptingInterface::reset() {
5246 #ifdef HAVE_SDL
53− PerformanceTimer perfTimer("JoystickScriptingInterface::update");
54− SDL_JoystickUpdate();
5547
56− foreach(Joystick* joystick, _openJoysticks) {
57− joystick->update();
48+ if (_isInitialized) {
49+ _isInitialized = false;
50+
51+ // close all the open joysticks before we quit
52+ foreach(Joystick* openJoystick, _openJoysticks) {
53+ openJoystick->closeJoystick();
54+ }
55+
56+ SDL_Quit();
5857 }
5958
59+ bool initSuccess = (SDL_Init(SDL_INIT_JOYSTICK) == 0);
60+
61+ if (initSuccess) {
62+
63+ int joystickCount = SDL_NumJoysticks();
64+
65+ for (int i = 0; i < joystickCount; i++) {
66+ _availableDeviceNames << SDL_JoystickName(i);
67+ }
68+
69+ foreach(const QString& joystickName, _openJoysticks.keys()) {
70+ _openJoysticks[joystickName]->setSDLJoystick(openSDLJoystickWithName(joystickName));
71+ }
72+
73+ _isInitialized = true;
74+ }
75+#endif
76+}
77+
78+void JoystickScriptingInterface::update() {
79+#ifdef HAVE_SDL
80+ if (_isInitialized) {
81+ PerformanceTimer perfTimer("JoystickScriptingInterface::update");
82+ SDL_JoystickUpdate();
83+
84+ foreach(Joystick* joystick, _openJoysticks) {
85+ joystick->update();
86+ }
87+ }
6088 #endif
6189 }
6290
@@ -64,17 +92,13 @@ Joystick* JoystickScriptingInterface::joystickWithName(const QString& name) { · 1 unmodified line
6492 Joystick* matchingJoystick = _openJoysticks.value(name);
6593 #ifdef HAVE_SDL
6694 if (!matchingJoystick) {
67− // we haven't opened a joystick with this name yet - enumerate our SDL devices and see if it exists
68− int joystickCount = SDL_NumJoysticks();
95+ SDL_Joystick* openSDLJoystick = openSDLJoystickWithName(name);
6996
70− for (int i = 0; i < joystickCount; i++) {
71− if (SDL_JoystickName(i) == name) {
72− matchingJoystick = _openJoysticks.insert(name, new Joystick(name, SDL_JoystickOpen(i))).value();
73− break;
74− }
97+ if (openSDLJoystick) {
98+ matchingJoystick = _openJoysticks.insert(name, new Joystick(name, openSDLJoystick)).value();
99+ } else {
100+ qDebug() << "No matching joystick found with name" << name << "- returning NULL pointer.";
75101 }
76−
77− qDebug() << "No matching joystick found with name" << name << "- returning NULL pointer.";
78102 }
79103 #endif
80104
@@ -82,3 +106,20 @@ Joystick* JoystickScriptingInterface::joystickWithName(const QString& name) { · 1 unmodified line
82106 }
83107
84108
109+#ifdef HAVE_SDL
110+
111+SDL_Joystick* JoystickScriptingInterface::openSDLJoystickWithName(const QString &name) {
112+ // we haven't opened a joystick with this name yet - enumerate our SDL devices and see if it exists
113+ int joystickCount = SDL_NumJoysticks();
114+
115+ for (int i = 0; i < joystickCount; i++) {
116+ if (SDL_JoystickName(i) == name) {
117+ return SDL_JoystickOpen(i);
118+ break;
119+ }
120+ }
121+
122+ return NULL;
123+}
124+
125+#endif

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t