Skip to content

Commit 7078160

Browse files
ccawley2011mupfdev
authored andcommitted
Use the gamepad API instead of the joystick API
1 parent fb2a67b commit 7078160

File tree

2 files changed

+26
-25
lines changed

2 files changed

+26
-25
lines changed

src/app.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,18 @@ bool init_app(SDL_Renderer** renderer, SDL_Window* window)
2323
#ifdef __SYMBIAN32__
2424
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO))
2525
#else
26-
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK))
26+
if (!SDL_Init(SDL_INIT_VIDEO))
2727
#endif
2828
{
2929
SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
3030
return false;
3131
}
3232

33+
if (!SDL_InitSubSystem(SDL_INIT_GAMEPAD))
34+
{
35+
SDL_Log("Couldn't initialize gamepad subsystem: %s", SDL_GetError());
36+
}
37+
3338
window = SDL_CreateWindow("open8", WINDOW_W * SCALE, WINDOW_H * SCALE, WINDOW_FLAGS);
3439
if (!window)
3540
{

src/core.c

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -516,32 +516,30 @@ bool handle_events(SDL_Renderer* renderer, SDL_Event* event)
516516
{
517517
return false;
518518
}
519-
#ifndef __SYMBIAN32__
520-
case SDL_EVENT_JOYSTICK_ADDED:
519+
case SDL_EVENT_GAMEPAD_ADDED:
521520
{
522-
const SDL_JoystickID which = event->jdevice.which;
523-
SDL_Joystick *joystick = SDL_OpenJoystick(which);
524-
if (!joystick)
521+
const SDL_JoystickID which = event->gdevice.which;
522+
SDL_Gamepad *gamepad = SDL_OpenGamepad(which);
523+
if (!gamepad)
525524
{
526-
SDL_Log("Joystick #%u could not be opened: %s", (unsigned int)which, SDL_GetError());
525+
SDL_Log("Joystick #%" SDL_PRIu32 " could not be opened: %s", which, SDL_GetError());
527526
}
528527
else
529528
{
530-
SDL_Log("Joystick #%u connected: %s", (unsigned int)which, SDL_GetJoystickName(joystick));
529+
SDL_Log("Joystick #%" SDL_PRIu32 " connected: %s", which, SDL_GetGamepadName(gamepad));
531530
}
532531
return true;
533532
}
534-
case SDL_EVENT_JOYSTICK_REMOVED:
533+
case SDL_EVENT_GAMEPAD_REMOVED:
535534
{
536-
const SDL_JoystickID which = event->jdevice.which;
537-
SDL_Joystick* joystick = SDL_GetJoystickFromID(which);
538-
if (joystick)
535+
const SDL_JoystickID which = event->gdevice.which;
536+
SDL_Gamepad *gamepad = SDL_GetGamepadFromID(which);
537+
if (gamepad)
539538
{
540-
SDL_CloseJoystick(joystick); /* the joystick was unplugged. */
539+
SDL_CloseGamepad(gamepad); /* the joystick was unplugged. */
541540
}
542541
return true;
543542
}
544-
#endif
545543
case SDL_EVENT_KEY_DOWN:
546544
{
547545
if (state == STATE_MENU)
@@ -593,34 +591,33 @@ bool handle_events(SDL_Renderer* renderer, SDL_Event* event)
593591
}
594592
break;
595593
}
596-
#ifndef __SYMBIAN32__
597-
case SDL_EVENT_JOYSTICK_BUTTON_DOWN:
594+
case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
598595
{
599-
const SDL_JoystickID which = event->jbutton.which;
600-
SDL_Log("Joystick #%u button %d -> %s", (unsigned int)which, (int)event->jbutton.button, event->jbutton.down ? "PRESSED" : "RELEASED");
596+
const SDL_JoystickID which = event->gbutton.which;
597+
SDL_Log("Gamepad #%" SDL_PRIu32 " button %s -> %s", which, SDL_GetGamepadStringForButton(event->gbutton.button), event->gbutton.down ? "PRESSED" : "RELEASED");
601598

602599
if (state == STATE_MENU)
603600
{
604-
switch (event->jbutton.button)
601+
switch (event->gbutton.button)
605602
{
606-
case 4:
603+
case SDL_GAMEPAD_BUTTON_DPAD_LEFT:
607604
select_prev_cartridge(renderer);
608605
render_cartridge(renderer);
609606
return true;
610-
case 5:
607+
case SDL_GAMEPAD_BUTTON_DPAD_RIGHT:
611608
select_next_cartridge(renderer);
612609
render_cartridge(renderer);
613610
return true;
614-
case 0:
611+
case SDL_GAMEPAD_BUTTON_EAST: // TODO: A / Circle
615612
run_cartridge(renderer);
616613
return true;
617614
}
618615
}
619616
else if (state == STATE_EMULATOR)
620617
{
621-
switch (event->jbutton.button)
618+
switch (event->gbutton.button)
622619
{
623-
case 1:
620+
case SDL_GAMEPAD_BUTTON_SOUTH: // TODO: B / Cross
624621
destroy_vm();
625622
init_vm(renderer);
626623
reset_memory();
@@ -630,7 +627,6 @@ bool handle_events(SDL_Renderer* renderer, SDL_Event* event)
630627
}
631628
break;
632629
}
633-
#endif
634630
}
635631
return true;
636632
}

0 commit comments

Comments
 (0)