- [NEW] Gamepad support (not tested)

- [NEW] pad(), padp(), gamepad constants
This commit is contained in:
2023-01-18 18:43:27 +01:00
parent e53befb700
commit b96e80ab1d
3 changed files with 87 additions and 4 deletions

View File

@@ -80,6 +80,9 @@ Uint8 key_just_pressed = 0;
int mouse_x, mouse_y, mouse_wheel;
Uint32 mouse_buttons;
SDL_GameController *gamepad = NULL;
int8_t pad_just_pressed = SDL_CONTROLLER_BUTTON_INVALID;
#define MAX_SOUNDS 50
Mix_Music *music = NULL;
Mix_Chunk *sounds[MAX_SOUNDS];
@@ -206,6 +209,21 @@ void destroyDisplay() {
SDL_DestroyWindow(mini_win);
}
void initGamePad() {
const int num_joysticks = SDL_NumJoysticks();
if (num_joysticks==0) return;
int gamepad_num=-1;
for (int i=0; i<num_joysticks; ++i) {
if (SDL_IsGameController(i)) {
gamepad = SDL_GameControllerOpen(i);
if (SDL_GameControllerGetAttached(gamepad) == SDL_TRUE) {
SDL_GameControllerEventState(SDL_ENABLE);
return;
}
}
}
}
int main(int argc,char*argv[]){
while (!should_quit) {
@@ -239,6 +257,8 @@ int main(int argc,char*argv[]){
SDL_Init(SDL_INIT_EVERYTHING);
createDisplay();
initGamePad();
Mix_Init(MIX_INIT_OGG);
SDL_Event mini_eve;
@@ -253,6 +273,7 @@ int main(int argc,char*argv[]){
Uint32 dt=SDL_GetTicks();
key_just_pressed = 0;
pad_just_pressed = SDL_CONTROLLER_BUTTON_INVALID;
while(!should_exit) {
mouse_wheel = 0;
while(SDL_PollEvent(&mini_eve)) {
@@ -293,6 +314,9 @@ int main(int argc,char*argv[]){
if (mini_eve.type == SDL_MOUSEWHEEL) {
mouse_wheel = mini_eve.wheel.y;
}
if (mini_eve.type == SDL_CONTROLLERBUTTONDOWN) {
pad_just_pressed = mini_eve.cbutton.button;
}
}
keys = SDL_GetKeyboardState(NULL);
mouse_buttons = SDL_GetMouseState(&mouse_x, &mouse_y);
@@ -306,6 +330,7 @@ int main(int argc,char*argv[]){
loop();
}
key_just_pressed = 0;
pad_just_pressed = SDL_CONTROLLER_BUTTON_INVALID;
}
SDL_LockTexture(mini_bak, NULL, (void**)&pixels, &pitch);
for (uint32_t i=0;i<screen_surface->size;++i) pixels[i] = palette[screen_surface->p[i]];
@@ -873,6 +898,19 @@ bool anykey() {
return key_just_pressed != 0;
}
bool pad(int8_t i) {
if (!gamepad) return false;
return SDL_GameControllerGetButton(gamepad, SDL_GameControllerButton(i)) == 1;
}
bool padp(int8_t i) {
return pad_just_pressed == i;
}
int wpad() {
return pad_just_pressed;
}
int mousex() {
return mouse_x;
}