diff --git a/src/main.c b/src/main.c index 8b0c9f9..ebf238d 100644 --- a/src/main.c +++ b/src/main.c @@ -26,6 +26,8 @@ static void handleCommandLine(int argc, char *argv[]); int main(int argc, char *argv[]) { long then, nextSecond, frames; + + memset(&app, 0, sizeof(App)); atexit(cleanup); diff --git a/src/structs.h b/src/structs.h index c046875..cf81545 100644 --- a/src/structs.h +++ b/src/structs.h @@ -320,8 +320,9 @@ typedef struct { int soundVolume; int hideMouse; Mouse mouse; + SDL_GameController *joypad; int keyboard[MAX_KEYBOARD_KEYS]; - int joypad[SDL_CONTROLLER_BUTTON_MAX]; + int joypadButton[SDL_CONTROLLER_BUTTON_MAX]; int keyControls[CONTROL_MAX]; int joypadControls[CONTROL_MAX]; SDL_Texture *backBuffer; @@ -479,7 +480,8 @@ struct Widget { char name[MAX_NAME_LENGTH]; char group[MAX_NAME_LENGTH]; char label[MAX_NAME_LENGTH]; - char options[MAX_WIDGET_OPTIONS][MAX_NAME_LENGTH]; + int numOptions; + char **options; int value; int clicked; Widget *next; diff --git a/src/system/controls.c b/src/system/controls.c index cbbc76d..579bb29 100644 --- a/src/system/controls.c +++ b/src/system/controls.c @@ -25,7 +25,7 @@ int isControl(int type) int key = app.keyControls[type]; int btn = app.joypadControls[type]; - return ((key != 0 && app.keyboard[key]) || (btn != 0 && app.joypad[btn])); + return ((key != 0 && app.keyboard[key]) || (btn != 0 && app.joypadButton[btn])); } int isAcceptControl(void) @@ -45,7 +45,7 @@ void clearControl(int type) if (btn != 0) { - app.joypad[btn] = 0; + app.joypadButton[btn] = 0; } } diff --git a/src/system/init.c b/src/system/init.c index 4ebac9d..ca3971b 100644 --- a/src/system/init.c +++ b/src/system/init.c @@ -20,6 +20,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "init.h" +static void initJoypad(void); + void init18N(int argc, char *argv[]) { int i; @@ -87,6 +89,27 @@ void initSDL(void) printf("Couldn't initialize SDL TTF: %s\n", SDL_GetError()); exit(1); } + + initJoypad(); +} + +static void initJoypad(void) +{ + int i, n; + + n = SDL_NumJoysticks(); + + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "%d joypads available", n); + + for (i = 0 ; i < n ; i++) + { + if (SDL_IsGameController(i)) + { + app.joypad = SDL_GameControllerOpen(i); + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Using joypad '%s'", SDL_GameControllerName(app.joypad)); + return; + } + } } void initGameSystem(void) @@ -125,6 +148,10 @@ void cleanup(void) expireTexts(1); destroyGame(); + + if (app.joypad != NULL) { + SDL_GameControllerClose(app.joypad); + } SDL_DestroyRenderer(app.renderer); diff --git a/src/system/input.c b/src/system/input.c index 4345782..b5d6f30 100644 --- a/src/system/input.c +++ b/src/system/input.c @@ -108,6 +108,7 @@ void clearInput(void) memset(app.keyboard, 0, sizeof(int) * MAX_KEYBOARD_KEYS); memset(&app.mouse, 0, sizeof(Mouse)); + memset(&app.joypadButton, 0, sizeof(int) * SDL_CONTROLLER_BUTTON_MAX); while (SDL_PollEvent(&event)) { @@ -116,6 +117,7 @@ void clearInput(void) void handleInput(void) { + int i; SDL_Event event; app.mouse.dx = 0; @@ -156,4 +158,9 @@ void handleInput(void) } SDL_GetMouseState(&app.mouse.x, &app.mouse.y); + + for (i = 0 ; i < SDL_CONTROLLER_BUTTON_MAX ; i++) + { + app.joypadButton[i] = SDL_GameControllerGetButton(app.joypad, i); + } } diff --git a/src/world/player.c b/src/world/player.c index c71ce54..9870811 100644 --- a/src/world/player.c +++ b/src/world/player.c @@ -24,18 +24,19 @@ static void doCheatControls(void); void doPlayer(void) { - game.config.control[CONTROL_LEFT] = app.keyboard[SDL_SCANCODE_A]; - game.config.control[CONTROL_RIGHT] = app.keyboard[SDL_SCANCODE_D]; - game.config.control[CONTROL_UP] = app.keyboard[SDL_SCANCODE_W]; - game.config.control[CONTROL_DOWN] = app.keyboard[SDL_SCANCODE_S]; - game.config.control[CONTROL_JUMP] = app.keyboard[SDL_SCANCODE_I]; - game.config.control[CONTROL_FIRE] = app.keyboard[SDL_SCANCODE_J]; + game.config.control[CONTROL_LEFT] = app.keyboard[SDL_SCANCODE_A] || app.joypadButton[0]; + game.config.control[CONTROL_RIGHT] = app.keyboard[SDL_SCANCODE_D] || app.joypadButton[1]; + game.config.control[CONTROL_UP] = app.keyboard[SDL_SCANCODE_W] || app.joypadButton[2]; + game.config.control[CONTROL_DOWN] = app.keyboard[SDL_SCANCODE_S] || app.joypadButton[3]; + game.config.control[CONTROL_JUMP] = app.keyboard[SDL_SCANCODE_I] || app.joypadButton[4]; + game.config.control[CONTROL_FIRE] = app.keyboard[SDL_SCANCODE_J] || app.joypadButton[5]; - if (app.keyboard[SDL_SCANCODE_SPACE]) + if (app.keyboard[SDL_SCANCODE_SPACE] || app.joypadButton[6]) { world.bob->activate(1); app.keyboard[SDL_SCANCODE_SPACE] = 0; + app.joypadButton[6] = 0; } if (dev.debug)