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; world.bob->dx = 0;
if (game.config.control[CONTROL_LEFT]) if (isControl(CONTROL_LEFT))
{ {
world.bob->dx = -WALK_SPEED; world.bob->dx = -WALK_SPEED;
world.bob->facing = FACING_LEFT; world.bob->facing = FACING_LEFT;
} }
if (game.config.control[CONTROL_RIGHT]) if (isControl(CONTROL_RIGHT))
{ {
world.bob->dx = WALK_SPEED; world.bob->dx = WALK_SPEED;
world.bob->facing = FACING_RIGHT; 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; world.bob->dy = JUMP_POWER;
} }
if (game.config.control[CONTROL_FIRE] && world.bob->reload == 0) if (isControl(CONTROL_FIRE) && world.bob->reload == 0)
{ {
fireWeapon(); fireWeapon();
} }
if (isControl(CONTROL_JETPACK))
{
activate(1);
clearControl(CONTROL_JETPACK);
}
} }
static void bobSwim(void) static void bobSwim(void)
@ -452,62 +458,74 @@ static void bobSwim(void)
world.bob->dx = 0; world.bob->dx = 0;
world.bob->dy = 0.5f; world.bob->dy = 0.5f;
if (game.config.control[CONTROL_LEFT]) if (isControl(CONTROL_LEFT))
{ {
world.bob->dx = -SWIM_SPEED; world.bob->dx = -SWIM_SPEED;
world.bob->facing = FACING_LEFT; world.bob->facing = FACING_LEFT;
} }
if (game.config.control[CONTROL_RIGHT]) if (isControl(CONTROL_RIGHT))
{ {
world.bob->dx = SWIM_SPEED; world.bob->dx = SWIM_SPEED;
world.bob->facing = FACING_RIGHT; 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; world.bob->dy = -SWIM_SPEED;
} }
if (game.config.control[CONTROL_DOWN]) if (isControl(CONTROL_DOWN))
{ {
world.bob->dy = SWIM_SPEED; 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(); firePistol();
} }
if (isControl(CONTROL_JETPACK))
{
activate(1);
clearControl(CONTROL_JETPACK);
}
} }
static void bobFly(void) static void bobFly(void)
{ {
if (game.config.control[CONTROL_LEFT]) if (isControl(CONTROL_LEFT))
{ {
world.bob->dx -= FLY_ACCEL; world.bob->dx -= FLY_ACCEL;
world.bob->facing = FACING_LEFT; world.bob->facing = FACING_LEFT;
} }
if (game.config.control[CONTROL_RIGHT]) if (isControl(CONTROL_RIGHT))
{ {
world.bob->dx += FLY_ACCEL; world.bob->dx += FLY_ACCEL;
world.bob->facing = FACING_RIGHT; world.bob->facing = FACING_RIGHT;
} }
if (game.config.control[CONTROL_UP]) if (isControl(CONTROL_UP))
{ {
world.bob->dy -= FLY_ACCEL; world.bob->dy -= FLY_ACCEL;
} }
if (game.config.control[CONTROL_DOWN]) if (isControl(CONTROL_DOWN))
{ {
world.bob->dy += FLY_ACCEL; world.bob->dy += FLY_ACCEL;
} }
if (game.config.control[CONTROL_FIRE] && world.bob->reload == 0) if (isControl(CONTROL_FIRE) && world.bob->reload == 0)
{ {
fireWeapon(); fireWeapon();
} }
if (isControl(CONTROL_JETPACK))
{
activate(1);
clearControl(CONTROL_JETPACK);
}
world.bob->dx = MIN(FLY_SPEED, MAX(world.bob->dx, -FLY_SPEED)); world.bob->dx = MIN(FLY_SPEED, MAX(world.bob->dx, -FLY_SPEED));
world.bob->dy = MIN(FLY_SPEED, MAX(world.bob->dy, -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 "../../common.h"
#include "../../json/cJSON.h" #include "../../json/cJSON.h"
#define WALK_SPEED 5 #define WALK_SPEED 5
#define SWIM_SPEED 3 #define SWIM_SPEED 3
#define FLY_ACCEL 0.1 #define FLY_ACCEL 0.1
#define FLY_SPEED 8 #define FLY_SPEED 8
extern Sprite *getSprite(char *name); extern Sprite *getSprite(char *name);
@ -44,6 +44,8 @@ extern void fireSpread(Entity *e, int n);
extern void fireLaser(Entity *e); extern void fireLaser(Entity *e);
extern void addTeleportStars(Entity *e); extern void addTeleportStars(Entity *e);
extern void initEntity(Entity *e); extern void initEntity(Entity *e);
extern int isControl(int type);
extern void clearControl(int type);
extern Dev dev; extern Dev dev;
extern Game game; extern Game game;

View File

@ -34,6 +34,8 @@ void initGame(void)
game.timePlayed = 0; game.timePlayed = 0;
loadMetaInfo(); loadMetaInfo();
initControls();
} }
void addRescuedMIA(char *name) 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" #include "../json/cJSON.h"
extern char *readFile(const char *filename); extern char *readFile(const char *filename);
extern void initControls(void);
extern Game game; extern Game game;
extern World world; extern World world;

View File

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

View File

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

View File

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

View File

@ -62,7 +62,7 @@ void initSDL(void)
windowFlags = 0; 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()); printf("Couldn't initialize SDL: %s\n", SDL_GetError());
exit(1); exit(1);
@ -91,6 +91,8 @@ void initSDL(void)
} }
initJoypad(); initJoypad();
initControls();
} }
static void initJoypad(void) static void initJoypad(void)
@ -99,16 +101,19 @@ static void initJoypad(void)
n = SDL_NumJoysticks(); 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++) for (i = 0 ; i < n ; i++)
{ {
if (SDL_IsGameController(i)) app.joypad = SDL_JoystickOpen(i);
{
app.joypad = SDL_GameControllerOpen(i); if (app.joypad)
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Using joypad '%s'", SDL_GameControllerName(app.joypad)); {
return; 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, initLookups,
initGraphics, initGraphics,
initFonts, initFonts,
initControls,
initAtlas, initAtlas,
initSounds, initSounds,
initSprites, initSprites,
@ -150,7 +156,7 @@ void cleanup(void)
destroyGame(); destroyGame();
if (app.joypad != NULL) { if (app.joypad != NULL) {
SDL_GameControllerClose(app.joypad); SDL_JoystickClose(app.joypad);
} }
SDL_DestroyRenderer(app.renderer); SDL_DestroyRenderer(app.renderer);

View File

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

View File

@ -161,6 +161,6 @@ void handleInput(void)
for (i = 0 ; i < SDL_CONTROLLER_BUTTON_MAX ; i++) 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) 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) if (dev.debug)
{ {
doCheatControls(); doCheatControls();