blobwarsAttrition/src/system/controls.c

103 lines
2.5 KiB
C
Raw Normal View History

2018-02-05 23:08:35 +01:00
/*
Copyright (C) 2015-2017 Parallel Realities
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "controls.h"
2018-02-07 19:27:42 +01:00
void initControls(void)
{
int i;
game.config.keyControls[CONTROL_LEFT] = SDL_SCANCODE_A;
game.config.keyControls[CONTROL_RIGHT] = SDL_SCANCODE_D;
game.config.keyControls[CONTROL_UP] = SDL_SCANCODE_W;
game.config.keyControls[CONTROL_DOWN] = SDL_SCANCODE_S;
game.config.keyControls[CONTROL_JUMP] = SDL_SCANCODE_I;
game.config.keyControls[CONTROL_FIRE] = SDL_SCANCODE_J;
game.config.keyControls[CONTROL_JETPACK] = SDL_SCANCODE_L;
/* can't use memset here, as it doesn't work */
for (i = 0 ; i < CONTROL_MAX ; i++)
{
game.config.joypadControls[i] = -1;
}
2018-02-07 20:20:36 +01:00
game.config.joypadControls[CONTROL_JUMP] = 1;
game.config.joypadControls[CONTROL_FIRE] = 3;
game.config.joypadControls[CONTROL_JETPACK] = 2;
2018-02-07 19:27:42 +01:00
}
2018-02-05 23:08:35 +01:00
int isControl(int type)
{
2018-02-07 20:20:36 +01:00
int key, btn;
key = game.config.keyControls[type];
btn = game.config.joypadControls[type];
if (type == CONTROL_LEFT && app.joypadAxis[JOYPAD_AXIS_X] <= -16384)
{
return 1;
}
if (type == CONTROL_RIGHT && app.joypadAxis[JOYPAD_AXIS_X] >= 16384)
{
return 1;
}
if (type == CONTROL_UP && app.joypadAxis[JOYPAD_AXIS_Y] <= -16384)
{
return 1;
}
if (type == CONTROL_DOWN && app.joypadAxis[JOYPAD_AXIS_Y] >= 16384)
{
return 1;
}
2018-02-05 23:08:35 +01:00
2018-02-07 19:27:42 +01:00
return ((key != 0 && app.keyboard[key]) || (btn != -1 && app.joypadButton[btn]));
2018-02-05 23:08:35 +01:00
}
int isAcceptControl(void)
{
return (app.keyboard[SDL_SCANCODE_SPACE] ||app.keyboard[SDL_SCANCODE_RETURN] || isControl(CONTROL_FIRE));
}
void clearControl(int type)
{
2018-02-07 19:27:42 +01:00
int key = game.config.keyControls[type];
int btn = game.config.joypadControls[type];
2018-02-05 23:08:35 +01:00
if (key != 0)
{
app.keyboard[key] = 0;
}
if (btn != 0)
{
2018-02-06 23:26:05 +01:00
app.joypadButton[btn] = 0;
2018-02-05 23:08:35 +01:00
}
}
void resetAcceptControls(void)
{
app.keyboard[SDL_SCANCODE_SPACE] = app.keyboard[SDL_SCANCODE_RETURN] = 0;
clearControl(CONTROL_FIRE);
}