From b96e80ab1d9433d07bb86d7a8e3aed585c7e885d Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Wed, 18 Jan 2023 18:43:27 +0100 Subject: [PATCH] - [NEW] Gamepad support (not tested) - [NEW] pad(), padp(), gamepad constants --- lua.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++---- mini.cpp | 38 ++++++++++++++++++++++++++++++++++++++ mini.h | 4 ++++ 3 files changed, 87 insertions(+), 4 deletions(-) diff --git a/lua.cpp b/lua.cpp index 14a4563..67aecb1 100644 --- a/lua.cpp +++ b/lua.cpp @@ -459,6 +459,22 @@ extern "C" { return 1; } + static int cpp_pad(lua_State *L) { + int8_t i = luaL_checkinteger(L, 1); + lua_pushboolean(L, pad(i)); + return 1; + } + + static int cpp_padp(lua_State *L) { + if (lua_gettop(L) >=1 ) { + int8_t i = luaL_checkinteger(L, 1); + lua_pushboolean(L, padp(i)); + } else { + lua_pushinteger(L, wpad()); + } + return 1; + } + static int cpp_mousex(lua_State *L) { lua_pushinteger(L, mousex()); return 1; @@ -811,6 +827,8 @@ void push_lua_funcs() { lua_pushcfunction(L,cpp_btn); lua_setglobal(L, "btn"); lua_pushcfunction(L,cpp_btnp); lua_setglobal(L, "btnp"); lua_pushcfunction(L,cpp_anykey); lua_setglobal(L, "anykey"); + lua_pushcfunction(L,cpp_pad); lua_setglobal(L, "pad"); + lua_pushcfunction(L,cpp_padp); lua_setglobal(L, "padp"); lua_pushcfunction(L,cpp_mousex); lua_setglobal(L, "mousex"); lua_pushcfunction(L,cpp_mousey); lua_setglobal(L, "mousey"); lua_pushcfunction(L,cpp_mwheel); lua_setglobal(L, "mwheel"); @@ -835,7 +853,7 @@ void push_lua_funcs() { lua_pushcfunction(L,cpp_strlen); lua_setglobal(L, "strlen"); lua_pushcfunction(L,cpp_fopen); lua_setglobal(L, "fopen"); - lua_pushcfunction(L,cpp_fopenres); lua_setglobal(L, "fopenres"); + lua_pushcfunction(L,cpp_fopenres); lua_setglobal(L, "fopenres"); lua_pushcfunction(L,cpp_fclose); lua_setglobal(L, "fclose"); lua_pushcfunction(L,cpp_feof); lua_setglobal(L, "feof"); lua_pushcfunction(L,cpp_fwritei); lua_setglobal(L, "fwritei"); @@ -863,10 +881,10 @@ void push_lua_funcs() { lua_pushcfunction(L,cpp_fullscreen); lua_setglobal(L, "fullscreen"); lua_pushcfunction(L,cpp_cursor); lua_setglobal(L, "cursor"); - lua_pushcfunction(L,cpp_getconf); lua_setglobal(L, "getconf"); - lua_pushcfunction(L,cpp_setconf); lua_setglobal(L, "setconf"); + lua_pushcfunction(L,cpp_getconf); lua_setglobal(L, "getconf"); + lua_pushcfunction(L,cpp_setconf); lua_setglobal(L, "setconf"); - lua_pushcfunction(L,cpp_exit); lua_setglobal(L, "quit"); + lua_pushcfunction(L,cpp_exit); lua_setglobal(L, "quit"); lua_pushinteger(L, 0); lua_setglobal(L, "KEY_UNKNOWN"); lua_pushinteger(L, 4); lua_setglobal(L, "KEY_A"); @@ -976,6 +994,29 @@ void push_lua_funcs() { lua_pushinteger(L, 230); lua_setglobal(L, "KEY_RALT"); lua_pushinteger(L, 231); lua_setglobal(L, "KEY_RGUI"); + lua_pushinteger(L, SDL_CONTROLLER_BUTTON_INVALID); lua_setglobal(L, "BTN_INVALID"); + lua_pushinteger(L, SDL_CONTROLLER_BUTTON_A); lua_setglobal(L, "BTN_A"); + lua_pushinteger(L, SDL_CONTROLLER_BUTTON_B); lua_setglobal(L, "BTN_B"); + lua_pushinteger(L, SDL_CONTROLLER_BUTTON_X); lua_setglobal(L, "BTN_X"); + lua_pushinteger(L, SDL_CONTROLLER_BUTTON_Y); lua_setglobal(L, "BTN_Y"); + lua_pushinteger(L, SDL_CONTROLLER_BUTTON_BACK); lua_setglobal(L, "BTN_BACK"); + lua_pushinteger(L, SDL_CONTROLLER_BUTTON_GUIDE); lua_setglobal(L, "BTN_GUIDE"); + lua_pushinteger(L, SDL_CONTROLLER_BUTTON_START); lua_setglobal(L, "BTN_START"); + lua_pushinteger(L, SDL_CONTROLLER_BUTTON_LEFTSTICK); lua_setglobal(L, "BTN_LEFTSTICK"); + lua_pushinteger(L, SDL_CONTROLLER_BUTTON_RIGHTSTICK); lua_setglobal(L, "BTN_RIGHTSTICK"); + lua_pushinteger(L, SDL_CONTROLLER_BUTTON_LEFTSHOULDER); lua_setglobal(L, "BTN_LEFTSHOULDER"); + lua_pushinteger(L, SDL_CONTROLLER_BUTTON_RIGHTSHOULDER); lua_setglobal(L, "BTN_RIGHTSHOULDER"); + lua_pushinteger(L, SDL_CONTROLLER_BUTTON_DPAD_UP); lua_setglobal(L, "BTN_UP"); + lua_pushinteger(L, SDL_CONTROLLER_BUTTON_DPAD_DOWN); lua_setglobal(L, "BTN_DOWN"); + lua_pushinteger(L, SDL_CONTROLLER_BUTTON_DPAD_LEFT); lua_setglobal(L, "BTN_LEFT"); + lua_pushinteger(L, SDL_CONTROLLER_BUTTON_DPAD_RIGHT); lua_setglobal(L, "BTN_RIGHT"); + lua_pushinteger(L, SDL_CONTROLLER_BUTTON_MISC1); lua_setglobal(L, "BTN_MISC1"); + lua_pushinteger(L, SDL_CONTROLLER_BUTTON_PADDLE1); lua_setglobal(L, "BTN_PADDLE1"); + lua_pushinteger(L, SDL_CONTROLLER_BUTTON_PADDLE2); lua_setglobal(L, "BTN_PADDLE2"); + lua_pushinteger(L, SDL_CONTROLLER_BUTTON_PADDLE3); lua_setglobal(L, "BTN_PADDLE3"); + lua_pushinteger(L, SDL_CONTROLLER_BUTTON_PADDLE4); lua_setglobal(L, "BTN_PADDLE4"); + lua_pushinteger(L, SDL_CONTROLLER_BUTTON_TOUCHPAD); lua_setglobal(L, "BTN_TOUCHPAD"); + lua_pushinteger(L, 0); lua_setglobal(L, "FILE_READ"); lua_pushinteger(L, 1); lua_setglobal(L, "FILE_WRITE"); } diff --git a/mini.cpp b/mini.cpp index a64b06e..6bc2b72 100644 --- a/mini.cpp +++ b/mini.cpp @@ -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; isize;++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; } diff --git a/mini.h b/mini.h index ef8089a..5c0e26b 100644 --- a/mini.h +++ b/mini.h @@ -204,6 +204,10 @@ int wbtnp(); bool btnp(uint8_t i); bool anykey(); +bool pad(int8_t i); +bool padp(int8_t i); +int wpad(); + int mousex(); int mousey(); int mwheel();