Control updates.

This commit is contained in:
Steve 2018-02-07 18:27:42 +00:00
parent 0618c05b52
commit c287a1333e
11 changed files with 88 additions and 55 deletions

View File

@ -424,27 +424,33 @@ static void bobWalk(void)
{
world.bob->dx = 0;
if (game.config.control[CONTROL_LEFT])
if (isControl(CONTROL_LEFT))
{
world.bob->dx = -WALK_SPEED;
world.bob->facing = FACING_LEFT;
}
if (game.config.control[CONTROL_RIGHT])
if (isControl(CONTROL_RIGHT))
{
world.bob->dx = WALK_SPEED;
world.bob->facing = FACING_RIGHT;
}
if (game.config.control[CONTROL_JUMP] && world.bob->isOnGround)
if (isControl(CONTROL_JUMP) && world.bob->isOnGround)
{
world.bob->dy = JUMP_POWER;
}
if (game.config.control[CONTROL_FIRE] && world.bob->reload == 0)
if (isControl(CONTROL_FIRE) && world.bob->reload == 0)
{
fireWeapon();
}
if (isControl(CONTROL_JETPACK))
{
activate(1);
clearControl(CONTROL_JETPACK);
}
}
static void bobSwim(void)
@ -452,62 +458,74 @@ static void bobSwim(void)
world.bob->dx = 0;
world.bob->dy = 0.5f;
if (game.config.control[CONTROL_LEFT])
if (isControl(CONTROL_LEFT))
{
world.bob->dx = -SWIM_SPEED;
world.bob->facing = FACING_LEFT;
}
if (game.config.control[CONTROL_RIGHT])
if (isControl(CONTROL_RIGHT))
{
world.bob->dx = SWIM_SPEED;
world.bob->facing = FACING_RIGHT;
}
if (game.config.control[CONTROL_JUMP] || game.config.control[CONTROL_UP])
if (isControl(CONTROL_JUMP) || isControl(CONTROL_UP))
{
world.bob->dy = -SWIM_SPEED;
}
if (game.config.control[CONTROL_DOWN])
if (isControl(CONTROL_DOWN))
{
world.bob->dy = SWIM_SPEED;
}
if (game.config.control[CONTROL_FIRE] && world.bob->reload == 0 && world.bob->weaponType == WPN_PISTOL)
if (isControl(CONTROL_FIRE) && world.bob->reload == 0 && world.bob->weaponType == WPN_PISTOL)
{
firePistol();
}
if (isControl(CONTROL_JETPACK))
{
activate(1);
clearControl(CONTROL_JETPACK);
}
}
static void bobFly(void)
{
if (game.config.control[CONTROL_LEFT])
if (isControl(CONTROL_LEFT))
{
world.bob->dx -= FLY_ACCEL;
world.bob->facing = FACING_LEFT;
}
if (game.config.control[CONTROL_RIGHT])
if (isControl(CONTROL_RIGHT))
{
world.bob->dx += FLY_ACCEL;
world.bob->facing = FACING_RIGHT;
}
if (game.config.control[CONTROL_UP])
if (isControl(CONTROL_UP))
{
world.bob->dy -= FLY_ACCEL;
}
if (game.config.control[CONTROL_DOWN])
if (isControl(CONTROL_DOWN))
{
world.bob->dy += FLY_ACCEL;
}
if (game.config.control[CONTROL_FIRE] && world.bob->reload == 0)
if (isControl(CONTROL_FIRE) && world.bob->reload == 0)
{
fireWeapon();
}
if (isControl(CONTROL_JETPACK))
{
activate(1);
clearControl(CONTROL_JETPACK);
}
world.bob->dx = MIN(FLY_SPEED, MAX(world.bob->dx, -FLY_SPEED));
world.bob->dy = MIN(FLY_SPEED, MAX(world.bob->dy, -FLY_SPEED));

View File

@ -21,9 +21,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "../../common.h"
#include "../../json/cJSON.h"
#define WALK_SPEED 5
#define SWIM_SPEED 3
#define FLY_ACCEL 0.1
#define WALK_SPEED 5
#define SWIM_SPEED 3
#define FLY_ACCEL 0.1
#define FLY_SPEED 8
extern Sprite *getSprite(char *name);
@ -44,6 +44,8 @@ extern void fireSpread(Entity *e, int n);
extern void fireLaser(Entity *e);
extern void addTeleportStars(Entity *e);
extern void initEntity(Entity *e);
extern int isControl(int type);
extern void clearControl(int type);
extern Dev dev;
extern Game game;

View File

@ -34,6 +34,8 @@ void initGame(void)
game.timePlayed = 0;
loadMetaInfo();
initControls();
}
void addRescuedMIA(char *name)

View File

@ -22,6 +22,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "../json/cJSON.h"
extern char *readFile(const char *filename);
extern void initControls(void);
extern Game game;
extern World world;

View File

@ -315,16 +315,11 @@ typedef struct {
int winHeight;
float scaleX;
float scaleY;
int fullscreen;
int musicVolume;
int soundVolume;
int hideMouse;
Mouse mouse;
SDL_GameController *joypad;
SDL_Joystick *joypad;
int keyboard[MAX_KEYBOARD_KEYS];
int joypadButton[SDL_CONTROLLER_BUTTON_MAX];
int keyControls[CONTROL_MAX];
int joypadControls[CONTROL_MAX];
SDL_Texture *backBuffer;
SDL_Renderer *renderer;
SDL_Window *window;
@ -335,9 +330,12 @@ typedef struct {
} App;
typedef struct {
int sound;
int music;
int fullscreen;
int soundVolume;
int musicVolume;
int control[CONTROL_MAX];
int keyControls[CONTROL_MAX];
int joypadControls[CONTROL_MAX];
} Config;
typedef struct {

View File

@ -20,12 +20,31 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "controls.h"
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;
}
}
int isControl(int type)
{
int key = app.keyControls[type];
int btn = app.joypadControls[type];
int key = game.config.keyControls[type];
int btn = game.config.joypadControls[type];
return ((key != 0 && app.keyboard[key]) || (btn != 0 && app.joypadButton[btn]));
return ((key != 0 && app.keyboard[key]) || (btn != -1 && app.joypadButton[btn]));
}
int isAcceptControl(void)
@ -35,8 +54,8 @@ int isAcceptControl(void)
void clearControl(int type)
{
int key = app.keyControls[type];
int btn = app.joypadControls[type];
int key = game.config.keyControls[type];
int btn = game.config.joypadControls[type];
if (key != 0)
{

View File

@ -21,3 +21,4 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "../common.h"
extern App app;
extern Game game;

View File

@ -62,7 +62,7 @@ void initSDL(void)
windowFlags = 0;
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) < 0)
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_JOYSTICK) < 0)
{
printf("Couldn't initialize SDL: %s\n", SDL_GetError());
exit(1);
@ -91,6 +91,8 @@ void initSDL(void)
}
initJoypad();
initControls();
}
static void initJoypad(void)
@ -99,16 +101,19 @@ static void initJoypad(void)
n = SDL_NumJoysticks();
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "%d joypads available", n);
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "%d joysticks 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;
}
app.joypad = SDL_JoystickOpen(i);
if (app.joypad)
{
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Using joystick '%s'", SDL_JoystickNameForIndex(i));
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "\tAxes: %d", SDL_JoystickNumAxes(app.joypad));
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "\tButtons: %d", SDL_JoystickNumButtons(app.joypad));
return;
}
}
}
@ -119,6 +124,7 @@ void initGameSystem(void)
initLookups,
initGraphics,
initFonts,
initControls,
initAtlas,
initSounds,
initSprites,
@ -150,7 +156,7 @@ void cleanup(void)
destroyGame();
if (app.joypad != NULL) {
SDL_GameControllerClose(app.joypad);
SDL_JoystickClose(app.joypad);
}
SDL_DestroyRenderer(app.renderer);

View File

@ -34,6 +34,7 @@ extern void initFonts(void);
extern void initAtlas(void);
extern void initSounds(void);
extern void initSprites(void);
extern void initControls(void);
extern void initEntityFactory(void);
extern void destroyLookups(void);
extern void destroyFonts(void);

View File

@ -161,6 +161,6 @@ void handleInput(void)
for (i = 0 ; i < SDL_CONTROLLER_BUTTON_MAX ; i++)
{
app.joypadButton[i] = SDL_GameControllerGetButton(app.joypad, i);
app.joypadButton[i] = SDL_JoystickGetButton(app.joypad, i);
}
}

View File

@ -24,21 +24,6 @@ static void doCheatControls(void);
void doPlayer(void)
{
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] || app.joypadButton[6])
{
world.bob->activate(1);
app.keyboard[SDL_SCANCODE_SPACE] = 0;
app.joypadButton[6] = 0;
}
if (dev.debug)
{
doCheatControls();