- [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

49
lua.cpp
View File

@@ -459,6 +459,22 @@ extern "C" {
return 1; 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) { static int cpp_mousex(lua_State *L) {
lua_pushinteger(L, mousex()); lua_pushinteger(L, mousex());
return 1; return 1;
@@ -811,6 +827,8 @@ void push_lua_funcs() {
lua_pushcfunction(L,cpp_btn); lua_setglobal(L, "btn"); lua_pushcfunction(L,cpp_btn); lua_setglobal(L, "btn");
lua_pushcfunction(L,cpp_btnp); lua_setglobal(L, "btnp"); lua_pushcfunction(L,cpp_btnp); lua_setglobal(L, "btnp");
lua_pushcfunction(L,cpp_anykey); lua_setglobal(L, "anykey"); 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_mousex); lua_setglobal(L, "mousex");
lua_pushcfunction(L,cpp_mousey); lua_setglobal(L, "mousey"); lua_pushcfunction(L,cpp_mousey); lua_setglobal(L, "mousey");
lua_pushcfunction(L,cpp_mwheel); lua_setglobal(L, "mwheel"); 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_strlen); lua_setglobal(L, "strlen");
lua_pushcfunction(L,cpp_fopen); lua_setglobal(L, "fopen"); 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_fclose); lua_setglobal(L, "fclose");
lua_pushcfunction(L,cpp_feof); lua_setglobal(L, "feof"); lua_pushcfunction(L,cpp_feof); lua_setglobal(L, "feof");
lua_pushcfunction(L,cpp_fwritei); lua_setglobal(L, "fwritei"); 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_fullscreen); lua_setglobal(L, "fullscreen");
lua_pushcfunction(L,cpp_cursor); lua_setglobal(L, "cursor"); lua_pushcfunction(L,cpp_cursor); lua_setglobal(L, "cursor");
lua_pushcfunction(L,cpp_getconf); lua_setglobal(L, "getconf"); lua_pushcfunction(L,cpp_getconf); lua_setglobal(L, "getconf");
lua_pushcfunction(L,cpp_setconf); lua_setglobal(L, "setconf"); 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, 0); lua_setglobal(L, "KEY_UNKNOWN");
lua_pushinteger(L, 4); lua_setglobal(L, "KEY_A"); 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, 230); lua_setglobal(L, "KEY_RALT");
lua_pushinteger(L, 231); lua_setglobal(L, "KEY_RGUI"); 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, 0); lua_setglobal(L, "FILE_READ");
lua_pushinteger(L, 1); lua_setglobal(L, "FILE_WRITE"); lua_pushinteger(L, 1); lua_setglobal(L, "FILE_WRITE");
} }

View File

@@ -80,6 +80,9 @@ Uint8 key_just_pressed = 0;
int mouse_x, mouse_y, mouse_wheel; int mouse_x, mouse_y, mouse_wheel;
Uint32 mouse_buttons; Uint32 mouse_buttons;
SDL_GameController *gamepad = NULL;
int8_t pad_just_pressed = SDL_CONTROLLER_BUTTON_INVALID;
#define MAX_SOUNDS 50 #define MAX_SOUNDS 50
Mix_Music *music = NULL; Mix_Music *music = NULL;
Mix_Chunk *sounds[MAX_SOUNDS]; Mix_Chunk *sounds[MAX_SOUNDS];
@@ -206,6 +209,21 @@ void destroyDisplay() {
SDL_DestroyWindow(mini_win); 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[]){ int main(int argc,char*argv[]){
while (!should_quit) { while (!should_quit) {
@@ -239,6 +257,8 @@ int main(int argc,char*argv[]){
SDL_Init(SDL_INIT_EVERYTHING); SDL_Init(SDL_INIT_EVERYTHING);
createDisplay(); createDisplay();
initGamePad();
Mix_Init(MIX_INIT_OGG); Mix_Init(MIX_INIT_OGG);
SDL_Event mini_eve; SDL_Event mini_eve;
@@ -253,6 +273,7 @@ int main(int argc,char*argv[]){
Uint32 dt=SDL_GetTicks(); Uint32 dt=SDL_GetTicks();
key_just_pressed = 0; key_just_pressed = 0;
pad_just_pressed = SDL_CONTROLLER_BUTTON_INVALID;
while(!should_exit) { while(!should_exit) {
mouse_wheel = 0; mouse_wheel = 0;
while(SDL_PollEvent(&mini_eve)) { while(SDL_PollEvent(&mini_eve)) {
@@ -293,6 +314,9 @@ int main(int argc,char*argv[]){
if (mini_eve.type == SDL_MOUSEWHEEL) { if (mini_eve.type == SDL_MOUSEWHEEL) {
mouse_wheel = mini_eve.wheel.y; mouse_wheel = mini_eve.wheel.y;
} }
if (mini_eve.type == SDL_CONTROLLERBUTTONDOWN) {
pad_just_pressed = mini_eve.cbutton.button;
}
} }
keys = SDL_GetKeyboardState(NULL); keys = SDL_GetKeyboardState(NULL);
mouse_buttons = SDL_GetMouseState(&mouse_x, &mouse_y); mouse_buttons = SDL_GetMouseState(&mouse_x, &mouse_y);
@@ -306,6 +330,7 @@ int main(int argc,char*argv[]){
loop(); loop();
} }
key_just_pressed = 0; key_just_pressed = 0;
pad_just_pressed = SDL_CONTROLLER_BUTTON_INVALID;
} }
SDL_LockTexture(mini_bak, NULL, (void**)&pixels, &pitch); 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]]; 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; 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() { int mousex() {
return mouse_x; return mouse_x;
} }

4
mini.h
View File

@@ -204,6 +204,10 @@ int wbtnp();
bool btnp(uint8_t i); bool btnp(uint8_t i);
bool anykey(); bool anykey();
bool pad(int8_t i);
bool padp(int8_t i);
int wpad();
int mousex(); int mousex();
int mousey(); int mousey();
int mwheel(); int mwheel();