Add basic controller input using the SDL2 GameController API

This commit is contained in:
Nadia Holmquist Pedersen 2023-06-27 22:40:38 +02:00
parent bf85b405af
commit 71dddc0020
4 changed files with 102 additions and 6 deletions

View file

@ -23,6 +23,8 @@ class Emulator {
SDL_Window* window;
SDL_GLContext glContext;
SDL_GameController* gameController;
int gameControllerID;
static constexpr u32 width = 400;
static constexpr u32 height = 240 * 2; // * 2 because 2 screens
@ -40,6 +42,13 @@ public:
Helpers::panic("Failed to initialize SDL2");
}
// Make SDL use consistent positional button mapping
SDL_SetHint(SDL_HINT_GAMECONTROLLER_USE_BUTTON_LABELS, "0");
if (SDL_Init(SDL_INIT_GAMECONTROLLER) < 0) {
Helpers::warn("Failed to initialize SDL2 GameController: %s", SDL_GetError());
}
// Request OpenGL 4.1 Core (Max available on MacOS)
// MacOS gets mad if we don't explicitly demand a core profile
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
@ -61,6 +70,15 @@ public:
Helpers::panic("OpenGL init failed: %s", SDL_GetError());
}
if (SDL_WasInit(SDL_INIT_GAMECONTROLLER)) {
gameController = SDL_GameControllerOpen(0);
if (gameController != nullptr) {
SDL_Joystick* stick = SDL_GameControllerGetJoystick(gameController);
gameControllerID = SDL_JoystickInstanceID(stick);
}
}
reset();
}
@ -75,4 +93,4 @@ public:
bool loadELF(const std::filesystem::path& path);
bool loadELF(std::ifstream& file);
void initGraphicsContext() { gpu.initGraphicsContext(); }
};
};

View file

@ -87,6 +87,14 @@ public:
void pressKey(u32 mask) { newButtons |= mask; }
void releaseKey(u32 mask) { newButtons &= ~mask; }
s16 getCirclepadX() {
return circlePadX;
}
s16 getCirclepadY() {
return circlePadY;
}
void setCirclepadX(s16 x) {
circlePadX = x;
@ -129,4 +137,4 @@ public:
void releaseTouchScreen() {
touchScreenPressed = false;
}
};
};

View file

@ -90,9 +90,11 @@ class ServiceManager {
// Input function wrappers
void pressKey(u32 key) { hid.pressKey(key); }
void releaseKey(u32 key) { hid.releaseKey(key); }
void setCirclepadX(u16 x) { hid.setCirclepadX(x); }
void setCirclepadY(u16 y) { hid.setCirclepadY(y); }
s16 getCirclepadX() { return hid.getCirclepadX(); }
s16 getCirclepadY() { return hid.getCirclepadY(); }
void setCirclepadX(s16 x) { hid.setCirclepadX(x); }
void setCirclepadY(s16 y) { hid.setCirclepadY(y); }
void updateInputs(u64 currentTimestamp) { hid.updateInputs(currentTimestamp); }
void setTouchScreenPress(u16 x, u16 y) { hid.setTouchScreenPress(x, y); }
void releaseTouchScreen() { hid.releaseTouchScreen(); }
};
};