Start of joypad handling.

This commit is contained in:
Steve 2018-02-06 22:26:05 +00:00
parent 4e981ecf78
commit b92c9dea9c
6 changed files with 50 additions and 11 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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);

View File

@ -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);
}
}

View File

@ -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)