Skip to content

Commit

Permalink
Quick hack to implement controller support.
Browse files Browse the repository at this point in the history
  • Loading branch information
dulsi committed Oct 25, 2023
1 parent 59ae45f commit 0e9cef9
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/battle/player.c
Original file line number Diff line number Diff line change
Expand Up @@ -400,20 +400,23 @@ static void handleMouse(void)
preFireMissile();

app.mouse.button[SDL_BUTTON_MIDDLE] = 0;
app.controllerButton[CONTROL_MISSILE] = 0;
}

if (isControl(CONTROL_GUNS))
{
switchGuns();

app.mouse.button[SDL_BUTTON_X1] = 0;
app.controllerButton[CONTROL_GUNS] = 0;
}

if (isControl(CONTROL_RADAR))
{
cycleRadarZoom();

app.mouse.button[SDL_BUTTON_X2] = 0;
app.controllerButton[CONTROL_RADAR] = 0;
}
}
}
Expand All @@ -425,7 +428,14 @@ static void faceMouse(void)

x = player->x - battle.camera.x;
y = player->y - battle.camera.y;
wantedAngle = getAngle(x, y, app.mouse.x, app.mouse.y);
if (app.controller)
{
wantedAngle = getAngle(0, 0, app.controllerAxis[0], app.controllerAxis[1]);
}
else
{
wantedAngle = getAngle(x, y, app.mouse.x, app.mouse.y);
}

wantedAngle %= 360;

Expand Down
1 change: 1 addition & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ int main(int argc, char *argv[])

memset(&app, 0, sizeof(App));
memset(&dev, 0, sizeof(Dev));
app.controllerIndex = -1;

handleLoggingArgs(argc, argv);

Expand Down
5 changes: 5 additions & 0 deletions src/structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,8 @@ typedef struct
Mouse mouse;
PointF uiMouse;
int keyboard[MAX_KEYBOARD_KEYS];
int controllerButton[CONTROL_MAX];
int controllerAxis[2];
SDL_Texture *backBuffer;
SDL_Texture *uiBuffer;
SDL_Renderer *renderer;
Expand All @@ -552,6 +554,9 @@ typedef struct
int lastButtonPressed;
int keyControls[CONTROL_MAX];
int mouseControls[CONTROL_MAX];
SDL_GameController *controller;
int controllerIndex;
int controllerControls[CONTROL_MAX][2];
int textWidth;
} App;

Expand Down
3 changes: 2 additions & 1 deletion src/system/controls.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ int isControl(int type)
int key = app.keyControls[type];
int btn = app.mouseControls[type];

return ((key != 0 && app.keyboard[key]) || (btn != 0 && app.mouse.button[btn]));
return ((key != 0 && app.keyboard[key]) || (btn != 0 && app.mouse.button[btn]) || (app.controller != NULL && app.controllerButton[type]));
}

int isAcceptControl(void)
Expand All @@ -123,6 +123,7 @@ void clearControl(int type)
{
app.mouse.button[btn] = 0;
}
app.controllerButton[type] = 0;
}

void resetAcceptControls(void)
Expand Down
36 changes: 35 additions & 1 deletion src/system/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ void init18N(int argc, char *argv[])
void initSDL(int argc, char *argv[])
{
int rendererFlags, windowFlags;
int i;

/* do this here, so we don't destroy the save dir stored in app */
memset(&app, 0, sizeof(App));
Expand All @@ -117,7 +118,7 @@ void initSDL(int argc, char *argv[])
windowFlags |= SDL_WINDOW_FULLSCREEN;
}

if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_GAMECONTROLLER) < 0)
{
printf("Couldn't initialize SDL: %s\n", SDL_GetError());
exit(1);
Expand Down Expand Up @@ -149,6 +150,15 @@ void initSDL(int argc, char *argv[])
printf("Couldn't initialize SDL TTF: %s\n", SDL_GetError());
exit(1);
}
for (i = 0; i < SDL_NumJoysticks(); i++)
{
if (SDL_IsGameController(i))
{
app.controllerIndex = i;
app.controller = SDL_GameControllerOpen(i);
break;
}
}

app.backBuffer = SDL_CreateTexture(app.renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, app.winWidth, app.winHeight);

Expand Down Expand Up @@ -276,6 +286,30 @@ static void loadConfigFile(char *filename)
node = node->next;
}
}
app.controllerControls[CONTROL_FIRE][0] = 1;
app.controllerControls[CONTROL_FIRE][1] = SDL_CONTROLLER_AXIS_TRIGGERRIGHT;
app.controllerControls[CONTROL_ACCELERATE][0] = 0;
app.controllerControls[CONTROL_ACCELERATE][1] = SDL_CONTROLLER_BUTTON_A;
app.controllerControls[CONTROL_BOOST][0] = 0;
app.controllerControls[CONTROL_BOOST][1] = SDL_CONTROLLER_BUTTON_B;
app.controllerControls[CONTROL_ECM][0] = 0;
app.controllerControls[CONTROL_ECM][1] = SDL_CONTROLLER_BUTTON_X;
app.controllerControls[CONTROL_BRAKE][0] = 0;
app.controllerControls[CONTROL_BRAKE][1] = SDL_CONTROLLER_BUTTON_Y;
app.controllerControls[CONTROL_TARGET][0] = 1;
app.controllerControls[CONTROL_TARGET][1] = SDL_CONTROLLER_AXIS_TRIGGERLEFT;
app.controllerControls[CONTROL_MISSILE][0] = 0;
app.controllerControls[CONTROL_MISSILE][1] = SDL_CONTROLLER_BUTTON_LEFTSHOULDER;
app.controllerControls[CONTROL_GUNS][0] = 0;
app.controllerControls[CONTROL_GUNS][1] = SDL_CONTROLLER_BUTTON_RIGHTSHOULDER;
app.controllerControls[CONTROL_RADAR][0] = 0;
app.controllerControls[CONTROL_RADAR][1] = SDL_CONTROLLER_BUTTON_DPAD_UP;
app.controllerControls[CONTROL_NEXT_FIGHTER][0] = 0;
app.controllerControls[CONTROL_NEXT_FIGHTER][1] = SDL_CONTROLLER_BUTTON_DPAD_RIGHT;
app.controllerControls[CONTROL_PREV_FIGHTER][0] = 0;
app.controllerControls[CONTROL_PREV_FIGHTER][1] = SDL_CONTROLLER_BUTTON_DPAD_LEFT;
app.controllerControls[CONTROL_SCREENSHOT][0] = 0;
app.controllerControls[CONTROL_SCREENSHOT][1] = SDL_CONTROLLER_BUTTON_BACK;

gameplayJSON = cJSON_GetObjectItem(root, "gameplay");
if (gameplayJSON)
Expand Down
77 changes: 77 additions & 0 deletions src/system/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,71 @@ void drawMouse(void)
blit(mousePointer, app.mouse.x, app.mouse.y, 1);
}

void doControllerAxis(SDL_ControllerAxisEvent *event)
{
int i;
if (event->which == app.controllerIndex)
{
if (event->axis == SDL_CONTROLLER_AXIS_LEFTX)
{
app.controllerAxis[0] = event->value / 512;
}
else if (event->axis == SDL_CONTROLLER_AXIS_LEFTY)
{
app.controllerAxis[1] = event->value / 512;
}
else
{
for (i = 0; i < CONTROL_MAX; i++)
{
if (app.controllerControls[i][0] == 1 && app.controllerControls[i][1] == event->axis)
{
if (event->value > 3200)
{
app.controllerButton[i] = 1;
}
else
{
app.controllerButton[i] = 0;
}
}
}
}
}
}

void doControllerButtonDown(SDL_ControllerButtonEvent *event)
{
int i;
if (event->which == app.controllerIndex)
{
for (i = 0; i < CONTROL_MAX; i++)
{
if (app.controllerControls[i][0] == 0 && app.controllerControls[i][1] == event->button)
{
app.controllerButton[i] = 1;
break;
}
}
}
}

void doControllerButtonUp(SDL_ControllerButtonEvent *event)
{
int i;
if (event->which == app.controllerIndex)
{
for (i = 0; i < CONTROL_MAX; i++)
{
if (app.controllerControls[i][0] == 0 && app.controllerControls[i][1] == event->button)
{
app.controllerButton[i] = 0;
break;
}
}
}
}

void doInput(void)
{
SDL_Event event;
Expand Down Expand Up @@ -153,6 +218,18 @@ void doInput(void)
doKeyUp(&event.key);
break;

case SDL_CONTROLLERAXISMOTION:
doControllerAxis(&event.caxis);
break;

case SDL_CONTROLLERBUTTONDOWN:
doControllerButtonDown(&event.cbutton);
break;

case SDL_CONTROLLERBUTTONUP:
doControllerButtonUp(&event.cbutton);
break;

case SDL_QUIT:
exit(0);
break;
Expand Down
3 changes: 3 additions & 0 deletions src/system/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ void doMouseUp(SDL_MouseButtonEvent *event);
void doMouseDown(SDL_MouseButtonEvent *event);
void doKeyUp(SDL_KeyboardEvent *event);
void doKeyDown(SDL_KeyboardEvent *event);
void doControllerAxis(SDL_ControllerAxisEvent *event);
void doControllerButtonUp(SDL_ControllerButtonEvent *event);
void doControllerButtonDown(SDL_ControllerButtonEvent *event);
void initInput(void);

0 comments on commit 0e9cef9

Please sign in to comment.