actual joystick input

also sync keyboard input nicely
This commit is contained in:
StapleButter 2017-09-30 20:05:56 +02:00
parent 9f486de76a
commit ab9800fbac
4 changed files with 61 additions and 9 deletions

View File

@ -508,6 +508,16 @@ void ReleaseScreen()
}
void SetKeyMask(u32 mask)
{
u32 key_lo = mask & 0x3FF;
u32 key_hi = (mask >> 10) & 0x3;
KeyInput &= 0xFFFCFC00;
KeyInput |= key_lo | (key_hi << 16);
}
void Halt()
{
printf("Halt()\n");

View File

@ -117,6 +117,8 @@ void ReleaseKey(u32 key);
void TouchScreen(u16 x, u16 y);
void ReleaseScreen();
void SetKeyMask(u32 mask);
void ScheduleEvent(u32 id, bool periodic, s32 delay, void (*func)(u32), u32 param);
void CancelEvent(u32 id);

View File

@ -106,7 +106,13 @@ int OnAreaKeyEvent(uiAreaHandler* handler, uiArea* area, uiAreaKeyEvent* evt)
if (pollid < 0x100) return 0;
int id = pollid & 0xFF;
if (id > 12) return 0;
if (evt->Scancode != 0x1) return 0; // ESC
if (evt->Scancode != 0x1) // ESC
{
if (evt->Scancode == 0xE) // backspace
joymap[id] = -1;
else
return 1;
}
char keyname[16];
JoyMappingName(joymap[id], keyname);

View File

@ -57,6 +57,7 @@ u32 ScreenBuffer[256*384];
bool Touching = false;
u32 KeyInputMask;
SDL_Joystick* Joystick;
@ -95,6 +96,8 @@ int EmuThreadFunc(void* burp)
SDL_PauseAudioDevice(audio, 0);
}
KeyInputMask = 0xFFF;
// TODO: support more joysticks
if (SDL_NumJoysticks() > 0)
Joystick = SDL_JoystickOpen(0);
@ -114,6 +117,39 @@ int EmuThreadFunc(void* burp)
{
EmuStatus = 1;
// poll input
u32 keymask = KeyInputMask;
u32 joymask = 0xFFF;
if (Joystick)
{
SDL_JoystickUpdate();
Uint32 hat = SDL_JoystickGetHat(Joystick, 0);
Sint16 axisX = SDL_JoystickGetAxis(Joystick, 0);
Sint16 axisY = SDL_JoystickGetAxis(Joystick, 1);
for (int i = 0; i < 12; i++)
{
int btnid = Config::JoyMapping[i];
if (btnid < 0) continue;
bool pressed;
if (btnid == 0x101) // up
pressed = (hat & SDL_HAT_UP) || (axisY <= -16384);
else if (btnid == 0x104) // down
pressed = (hat & SDL_HAT_DOWN) || (axisY >= 16384);
else if (btnid == 0x102) // right
pressed = (hat & SDL_HAT_RIGHT) || (axisX >= 16384);
else if (btnid == 0x108) // left
pressed = (hat & SDL_HAT_LEFT) || (axisX <= -16384);
else
pressed = SDL_JoystickGetButton(Joystick, btnid);
if (pressed) joymask &= ~(1<<i);
}
}
NDS::SetKeyMask(keymask & joymask);
// emulate
u32 nlines = NDS::RunFrame();
@ -258,17 +294,15 @@ int OnAreaKeyEvent(uiAreaHandler* handler, uiArea* area, uiAreaKeyEvent* evt)
if (evt->Up)
{
for (int i = 0; i < 10; i++)
if (evt->Scancode == Config::KeyMapping[i]) NDS::ReleaseKey(i);
if (evt->Scancode == Config::KeyMapping[10]) NDS::ReleaseKey(16);
if (evt->Scancode == Config::KeyMapping[11]) NDS::ReleaseKey(17);
for (int i = 0; i < 12; i++)
if (evt->Scancode == Config::KeyMapping[i])
KeyInputMask |= (1<<i);
}
else if (!evt->Repeat)
{
for (int i = 0; i < 10; i++)
if (evt->Scancode == Config::KeyMapping[i]) NDS::PressKey(i);
if (evt->Scancode == Config::KeyMapping[10]) NDS::PressKey(16);
if (evt->Scancode == Config::KeyMapping[11]) NDS::PressKey(17);
for (int i = 0; i < 12; i++)
if (evt->Scancode == Config::KeyMapping[i])
KeyInputMask &= ~(1<<i);
}
return 1;