aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStapleButter <thetotalworm@gmail.com>2017-03-25 16:18:35 +0100
committerStapleButter <thetotalworm@gmail.com>2017-03-25 16:18:35 +0100
commit762b91d640503a85f90c87a7c849742bfa347d77 (patch)
tree40483e79ed3d98f7715e204aac8ddb87ba9eba0d
parent12daf16282414310756c81cf265d126ba3f906ff (diff)
actual joystick support
-rw-r--r--src/wx/main.cpp86
-rw-r--r--src/wx/main.h4
2 files changed, 89 insertions, 1 deletions
diff --git a/src/wx/main.cpp b/src/wx/main.cpp
index eb23343..a5287ea 100644
--- a/src/wx/main.cpp
+++ b/src/wx/main.cpp
@@ -129,6 +129,10 @@ MainFrame::MainFrame()
SDL_GetWindowPosition(sdlwin, &WindowX, &WindowY);
SDL_GetWindowSize(sdlwin, &WindowW, &WindowH);
+
+ joy = NULL;
+ joyid = -1;
+ axismask = 0;
}
void MainFrame::OnClose(wxCloseEvent& event)
@@ -155,6 +159,13 @@ void MainFrame::OnClose(wxCloseEvent& event)
NDS::DeInit();
+ if (joy)
+ {
+ SDL_JoystickClose(joy);
+ joy = NULL;
+ joyid = -1;
+ }
+
SDL_UnlockTexture(sdltex);
delete texmutex;
@@ -192,12 +203,34 @@ void MainFrame::OnOpenROM(wxCommandEvent& event)
emustatuschangemutex->Lock();
emustatuschange->Signal();
emustatuschangemutex->Unlock();
+
+ if (!joy)
+ {
+ if (SDL_NumJoysticks() > 0)
+ {
+ joy = SDL_JoystickOpen(0);
+ joyid = SDL_JoystickInstanceID(joy);
+ }
+ }
}
void MainFrame::OnInputConfig(wxCommandEvent& event)
{
+ if (joy)
+ {
+ SDL_JoystickClose(joy);
+ joy = NULL;
+ joyid = -1;
+ }
+
InputConfigDialog dlg(this);
dlg.ShowModal();
+
+ if (SDL_NumJoysticks() > 0)
+ {
+ joy = SDL_JoystickOpen(0);
+ joyid = SDL_JoystickInstanceID(joy);
+ }
}
void MainFrame::ProcessSDLEvents()
@@ -243,7 +276,58 @@ void MainFrame::ProcessSDLEvents()
break;
case SDL_JOYBUTTONDOWN:
- printf("button %d %d\n", evt.jbutton.which, evt.jbutton.button);
+ if (!running) return;
+ if (evt.jbutton.which != joyid) return;
+ for (int i = 0; i < 10; i++)
+ if (evt.jbutton.button == Config::JoyMapping[i]) NDS::PressKey(i);
+ if (evt.jbutton.button == Config::JoyMapping[10]) NDS::PressKey(16);
+ if (evt.jbutton.button == Config::JoyMapping[11]) NDS::PressKey(17);
+ break;
+
+ case SDL_JOYBUTTONUP:
+ if (!running) return;
+ if (evt.jbutton.which != joyid) return;
+ for (int i = 0; i < 10; i++)
+ if (evt.jbutton.button == Config::JoyMapping[i]) NDS::ReleaseKey(i);
+ if (evt.jbutton.button == Config::JoyMapping[10]) NDS::ReleaseKey(16);
+ if (evt.jbutton.button == Config::JoyMapping[11]) NDS::ReleaseKey(17);
+ break;
+
+ case SDL_JOYHATMOTION:
+ if (!running) return;
+ if (evt.jhat.which != joyid) return;
+ if (evt.jhat.hat != 0) return;
+ for (int i = 0; i < 12; i++)
+ {
+ int j = (i >= 10) ? (i+6) : i;
+ if (Config::JoyMapping[i] == 0x101)
+ (evt.jhat.value & SDL_HAT_UP) ? NDS::PressKey(j) : NDS::ReleaseKey(j);
+ else if (Config::JoyMapping[i] == 0x102)
+ (evt.jhat.value & SDL_HAT_RIGHT) ? NDS::PressKey(j) : NDS::ReleaseKey(j);
+ else if (Config::JoyMapping[i] == 0x104)
+ (evt.jhat.value & SDL_HAT_DOWN) ? NDS::PressKey(j) : NDS::ReleaseKey(j);
+ else if (Config::JoyMapping[i] == 0x108)
+ (evt.jhat.value & SDL_HAT_LEFT) ? NDS::PressKey(j) : NDS::ReleaseKey(j);
+ }
+ break;
+
+ case SDL_JOYAXISMOTION:
+ if (!running) return;
+ if (evt.jaxis.which != joyid) return;
+ if (evt.jaxis.axis == 0)
+ {
+ if (evt.jaxis.value >= 16384) { NDS::PressKey(4); axismask |= 0x1; }
+ else if (axismask & 0x1) NDS::ReleaseKey(4);
+ if (evt.jaxis.value <= -16384) { NDS::PressKey(5); axismask |= 0x2; }
+ else if (axismask & 0x2) NDS::ReleaseKey(5);
+ }
+ else if (evt.jaxis.axis == 1)
+ {
+ if (evt.jaxis.value >= 16384) { NDS::PressKey(7); axismask |= 0x4; }
+ else if (axismask & 0x4) NDS::ReleaseKey(7);
+ if (evt.jaxis.value <= -16384) { NDS::PressKey(6); axismask |= 0x8; }
+ else if (axismask & 0x8) NDS::ReleaseKey(6);
+ }
break;
}
}
diff --git a/src/wx/main.h b/src/wx/main.h
index c05a8a0..eb54a21 100644
--- a/src/wx/main.h
+++ b/src/wx/main.h
@@ -55,6 +55,10 @@ public:
SDL_Renderer* sdlrend;
SDL_Texture* sdltex;
+ SDL_Joystick* joy;
+ SDL_JoystickID joyid;
+ u8 axismask;
+
wxMutex* texmutex;
void* texpixels;
int texstride;