[Change] Still cleaning. Adding all the input crap to it's rightful place.
This commit is contained in:
parent
95ac1a61d7
commit
e6057d04af
@ -10,6 +10,7 @@
|
|||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "player.h"
|
#include "player.h"
|
||||||
#include "opengl.h"
|
#include "opengl.h"
|
||||||
|
#include "input.h"
|
||||||
#include "conf.h"
|
#include "conf.h"
|
||||||
|
|
||||||
#define conf_loadInt(n,i) \
|
#define conf_loadInt(n,i) \
|
||||||
|
277
src/input.c
Normal file
277
src/input.c
Normal file
@ -0,0 +1,277 @@
|
|||||||
|
#include "main.h"
|
||||||
|
#include "log.h"
|
||||||
|
#include "player.h"
|
||||||
|
#include "input.h"
|
||||||
|
|
||||||
|
#define KEY_PRESS ( 1.);
|
||||||
|
#define KEY_RELEASE (-1.);
|
||||||
|
|
||||||
|
// Keybind structure.
|
||||||
|
typedef struct {
|
||||||
|
char* name; // Keybinding name, taken from keybindNames[]
|
||||||
|
KeybindType type; // type, defined in player.h.
|
||||||
|
unsigned int key; // Key/axis/button event number.
|
||||||
|
double reverse; // 1. if normal, -1 if reversed, only useful for joystick axis.
|
||||||
|
} Keybind;
|
||||||
|
static Keybind** player_input; // Contains the players keybindings.
|
||||||
|
// Name of each keybinding.
|
||||||
|
const char* keybindNames[] = { "accel", "left", "right", // Movement.
|
||||||
|
"primary", "target", "target_nearest", "face", "board", // Combat.
|
||||||
|
"secondary", "secondary_next", // Secondary weapons.
|
||||||
|
"target_planet", "land", // Navigation.
|
||||||
|
"mapzoomin", "mapzoomout", "screenshot", "end" }; // Misc.
|
||||||
|
// From player.c
|
||||||
|
extern double player_turn;
|
||||||
|
extern double player_acc;
|
||||||
|
extern unsigned int player_target;
|
||||||
|
extern int planet_target;
|
||||||
|
|
||||||
|
|
||||||
|
// Set the default input keys.
|
||||||
|
void input_setDefault(void) {
|
||||||
|
// Movement.
|
||||||
|
input_setKeybind("accel", KEYBIND_KEYBOARD, SDLK_w, 0);
|
||||||
|
input_setKeybind("left", KEYBIND_KEYBOARD, SDLK_a, 0);
|
||||||
|
input_setKeybind("right", KEYBIND_KEYBOARD, SDLK_d, 0);
|
||||||
|
// Combat.
|
||||||
|
input_setKeybind("primary", KEYBIND_KEYBOARD, SDLK_SPACE, 0);
|
||||||
|
input_setKeybind("target", KEYBIND_KEYBOARD, SDLK_TAB, 0);
|
||||||
|
input_setKeybind("target_nearest", KEYBIND_KEYBOARD, SDLK_r, 0);
|
||||||
|
input_setKeybind("face", KEYBIND_KEYBOARD, SDLK_f, 0);
|
||||||
|
input_setKeybind("board", KEYBIND_KEYBOARD, SDLK_b, 0);
|
||||||
|
// Secondary weapon.
|
||||||
|
input_setKeybind("secondary", KEYBIND_KEYBOARD, SDLK_LSHIFT, 0);
|
||||||
|
input_setKeybind("secondary_next", KEYBIND_KEYBOARD, SDLK_q, 0);
|
||||||
|
// Space
|
||||||
|
input_setKeybind("target_planet", KEYBIND_KEYBOARD, SDLK_p, 0);
|
||||||
|
input_setKeybind("land", KEYBIND_KEYBOARD, SDLK_l, 0);
|
||||||
|
// Misc.
|
||||||
|
input_setKeybind("mapzoomin", KEYBIND_KEYBOARD, SDLK_UP, 0);
|
||||||
|
input_setKeybind("mapzoomout", KEYBIND_KEYBOARD, SDLK_DOWN, 0);
|
||||||
|
input_setKeybind("screenshot", KEYBIND_KEYBOARD, SDLK_F12, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initialization/exit functions (does not assign keys).
|
||||||
|
void input_init(void) {
|
||||||
|
Keybind* tmp;
|
||||||
|
int i;
|
||||||
|
for(i = 0; strcmp(keybindNames[i], "end"); i++); // Get number of bindings.
|
||||||
|
player_input = malloc(i*sizeof(Keybind*));
|
||||||
|
|
||||||
|
// Create a null keybinding for each.
|
||||||
|
for(i = 0; strcmp(keybindNames[i], "end"); i++) {
|
||||||
|
tmp = MALLOC_L(Keybind);
|
||||||
|
tmp->name = (char*)keybindNames[i];
|
||||||
|
tmp->type = KEYBIND_NULL;
|
||||||
|
tmp->key = 0;
|
||||||
|
tmp->reverse = 1.;
|
||||||
|
player_input[i] = tmp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void input_exit(void) {
|
||||||
|
int i;
|
||||||
|
for(i = 0; strcmp(keybindNames[i], "end"); i++)
|
||||||
|
free(player_input[i]);
|
||||||
|
free(player_input);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Binds key of type [type] to action keybind.
|
||||||
|
void input_setKeybind(char* keybind, KeybindType type, int key, int reverse) {
|
||||||
|
int i;
|
||||||
|
for(i = 0; strcmp(keybindNames[i], "end"); i++)
|
||||||
|
if(strcmp(keybind, player_input[i]->name)==0) {
|
||||||
|
player_input[i]->type = type;
|
||||||
|
player_input[i]->key = key;
|
||||||
|
player_input[i]->reverse = reverse ? -1. : 1.;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
WARN("Unable to set keybind '%s', That command does not exist.", keybind);
|
||||||
|
}
|
||||||
|
|
||||||
|
// == Run input method. ================================================
|
||||||
|
// keynum : Index of the player_input keybind.
|
||||||
|
// value : Value of keypress (defined above).
|
||||||
|
// abs : Whether or not it's an abs value (For those pesky joysticks.
|
||||||
|
// =====================================================================
|
||||||
|
static void input_key(int keynum, double value, int abs) {
|
||||||
|
// Accelerating.
|
||||||
|
if(strcmp(player_input[keynum]->name, "accel")==0) {
|
||||||
|
if(abs)player_acc = value;
|
||||||
|
else player_acc += value;
|
||||||
|
player_acc = ABS(player_acc); // Make sure value is sane.
|
||||||
|
}
|
||||||
|
// Turning left.
|
||||||
|
else if(strcmp(player_input[keynum]->name, "left")==0) {
|
||||||
|
// Set flags for facing correction.
|
||||||
|
if(value == KEY_PRESS) player_setFlag(PLAYER_TURN_LEFT);
|
||||||
|
else if(value == KEY_RELEASE) player_rmFlag(PLAYER_TURN_LEFT);
|
||||||
|
|
||||||
|
if(abs)player_turn = -value;
|
||||||
|
else player_turn -= value;
|
||||||
|
if(player_turn < -1.) player_turn = -1.; // Make sure value is sane.
|
||||||
|
}
|
||||||
|
// Turning right.
|
||||||
|
else if(strcmp(player_input[keynum]->name, "right")==0) {
|
||||||
|
// Set flags for facing correction.
|
||||||
|
if(value == KEY_PRESS) player_setFlag(PLAYER_TURN_RIGHT);
|
||||||
|
else if(value == KEY_RELEASE) player_rmFlag(PLAYER_TURN_RIGHT);
|
||||||
|
|
||||||
|
if(abs) player_turn = value;
|
||||||
|
else player_turn += value;
|
||||||
|
|
||||||
|
if(player_turn < -1.) player_turn = -1.; // Make sure value is sane.
|
||||||
|
}
|
||||||
|
// Shoot primary weapon. BOOM BOOM.
|
||||||
|
else if(strcmp(player_input[keynum]->name, "primary")==0) {
|
||||||
|
if(value == KEY_PRESS) player_setFlag(PLAYER_PRIMARY);
|
||||||
|
else if(value == KEY_RELEASE) player_rmFlag(PLAYER_PRIMARY);
|
||||||
|
}
|
||||||
|
// Targetting.
|
||||||
|
else if(strcmp(player_input[keynum]->name, "target")==0) {
|
||||||
|
if(value == KEY_PRESS) player_target = pilot_getNext(player_target);
|
||||||
|
}
|
||||||
|
else if(strcmp(player_input[keynum]->name, "target_nearest")==0) {
|
||||||
|
if(value == KEY_PRESS) player_target = pilot_getHostile();
|
||||||
|
}
|
||||||
|
// Face the target.
|
||||||
|
else if(strcmp(player_input[keynum]->name, "face")==0) {
|
||||||
|
if(value == KEY_PRESS) player_setFlag(PLAYER_FACE);
|
||||||
|
else if(value == KEY_RELEASE) {
|
||||||
|
player_rmFlag(PLAYER_FACE);
|
||||||
|
|
||||||
|
// Turning corrections.
|
||||||
|
player_turn = 0;
|
||||||
|
if(player_isFlag(PLAYER_TURN_LEFT)) player_turn -= 1;
|
||||||
|
if(player_isFlag(PLAYER_TURN_RIGHT)) player_turn += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Board those ships.
|
||||||
|
else if(strcmp(player_input[keynum]->name, "board")==0) {
|
||||||
|
if(value == KEY_PRESS) player_board();
|
||||||
|
}
|
||||||
|
// Shooting secondary weapon.
|
||||||
|
else if(strcmp(player_input[keynum]->name, "secondary")==0) {
|
||||||
|
if(value == KEY_PRESS) player_setFlag(PLAYER_SECONDARY);
|
||||||
|
else if(value == KEY_RELEASE) player_rmFlag(PLAYER_SECONDARY);
|
||||||
|
}
|
||||||
|
// Selecting secondary weapon.
|
||||||
|
else if(strcmp(player_input[keynum]->name, "secondary_next")==0) {
|
||||||
|
if(value == KEY_PRESS) player_secondaryNext();
|
||||||
|
}
|
||||||
|
// Target planet (cycles just like target).
|
||||||
|
else if(strcmp(player_input[keynum]->name, "target_planet")==0) {
|
||||||
|
if(value == KEY_PRESS) player_targetPlanet();
|
||||||
|
}
|
||||||
|
// Target nearest planet or attempt to land.
|
||||||
|
else if(strcmp(player_input[keynum]->name, "land")==0) {
|
||||||
|
if(value == KEY_PRESS) player_land();
|
||||||
|
}
|
||||||
|
// Zoom in.
|
||||||
|
else if(strcmp(player_input[keynum]->name, "mapzoomin")==0) {
|
||||||
|
if((value == KEY_PRESS) && (gui.radar.res < RADAR_RES_MAX))
|
||||||
|
gui.radar.res += RADAR_RES_INTERVAL;
|
||||||
|
}
|
||||||
|
// Zoom out.
|
||||||
|
else if(strcmp(player_input[keynum]->name, "mapzoomout")==0) {
|
||||||
|
if((value == KEY_PRESS) && (gui.radar.res > RADAR_RES_MIN))
|
||||||
|
gui.radar.res -= RADAR_RES_INTERVAL;
|
||||||
|
}
|
||||||
|
// Take a screenshot.
|
||||||
|
else if(strcmp(player_input[keynum]->name, "screenshot")==0) {
|
||||||
|
if(value == KEY_PRESS) player_screenshot();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --Events--
|
||||||
|
|
||||||
|
static void input_joyaxis(const unsigned int axis, const int value);
|
||||||
|
static void input_joydown(const unsigned int button);
|
||||||
|
static void input_joyup(const unsigned int button);
|
||||||
|
static void input_keydown(SDLKey key);
|
||||||
|
static void input_keyup(SDLKey key);
|
||||||
|
|
||||||
|
// Joystick.
|
||||||
|
|
||||||
|
// Axis.
|
||||||
|
static void input_joyaxis(const unsigned int axis, const int value) {
|
||||||
|
int i;
|
||||||
|
for(i = 0; strcmp(keybindNames[i], "end"); i++)
|
||||||
|
if(player_input[i]->type == KEYBIND_JAXIS && player_input[i]->key == axis) {
|
||||||
|
input_key(i, -(player_input[i]->reverse) * (double)value / 32767., 1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Joystick button down.
|
||||||
|
static void input_joydown(const unsigned int button) {
|
||||||
|
int i;
|
||||||
|
for(i = 0; strcmp(keybindNames[i], "end");i++)
|
||||||
|
if(player_input[i]->type == KEYBIND_JBUTTON && player_input[i]->key == button) {
|
||||||
|
input_key(i, KEY_RELEASE, 0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Joystick button up.
|
||||||
|
static void input_joyup(const unsigned int button) {
|
||||||
|
int i;
|
||||||
|
for(i = 0; strcmp(keybindNames[i], "end"); i++)
|
||||||
|
if(player_input[i]->type == KEYBIND_JBUTTON && player_input[i]->key == button) {
|
||||||
|
input_key(i, KEY_RELEASE, 0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keyboard.
|
||||||
|
|
||||||
|
// Key down.
|
||||||
|
static void input_keydown(SDLKey key) {
|
||||||
|
int i;
|
||||||
|
for(i = 0; strcmp(keybindNames[i], "end"); i++)
|
||||||
|
if(player_input[i]->type == KEYBIND_KEYBOARD && player_input[i]->key == key) {
|
||||||
|
input_key(i, KEY_PRESS, 0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fire Escape.
|
||||||
|
SDL_Event quit;
|
||||||
|
if(key == SDLK_ESCAPE) {
|
||||||
|
quit.type = SDL_QUIT;
|
||||||
|
SDL_PushEvent(&quit);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Key up.
|
||||||
|
static void input_keyup(SDLKey key) {
|
||||||
|
int i;
|
||||||
|
for(i = 0; strcmp(keybindNames[i], "end"); i++)
|
||||||
|
if(player_input[i]->type == KEYBIND_KEYBOARD && player_input[i]->key == key) {
|
||||||
|
input_key(i, KEY_RELEASE, 0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Global input.
|
||||||
|
|
||||||
|
// Just seperates the event types.
|
||||||
|
void input_handle(SDL_Event* event) {
|
||||||
|
switch(event->type) {
|
||||||
|
case SDL_JOYAXISMOTION:
|
||||||
|
input_joyaxis(event->jaxis.axis, event->jaxis.value);
|
||||||
|
break;
|
||||||
|
case SDL_JOYBUTTONDOWN:
|
||||||
|
input_joydown(event->jbutton.button);
|
||||||
|
break;
|
||||||
|
case SDL_JOYBUTTONUP:
|
||||||
|
input_joyup(event->jbutton.button);
|
||||||
|
break;
|
||||||
|
case SDL_KEYDOWN:
|
||||||
|
input_keydown(event->key.keysym.sym);
|
||||||
|
break;
|
||||||
|
case SDL_KEYUP:
|
||||||
|
input_keyup(event->key.keysym.sym);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
17
src/input.h
Normal file
17
src/input.h
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "SDL.h"
|
||||||
|
|
||||||
|
// Input types.
|
||||||
|
typedef enum { KEYBIND_NULL, KEYBIND_KEYBOARD, KEYBIND_JAXIS, KEYBIND_JBUTTON } KeybindType;
|
||||||
|
|
||||||
|
// Set input.
|
||||||
|
void input_setDefault(void);
|
||||||
|
void input_setKeybind(char* keybind, KeybindType type, int key, int reverse);
|
||||||
|
|
||||||
|
// Handle the events.
|
||||||
|
void input_handle(SDL_Event* event);
|
||||||
|
|
||||||
|
// Init/Exit.
|
||||||
|
void input_init(void);
|
||||||
|
void input_exit(void);
|
||||||
|
|
28
src/player.h
28
src/player.h
@ -1,13 +1,24 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <SDL.h>
|
|
||||||
#include "pilot.h"
|
#include "pilot.h"
|
||||||
|
|
||||||
// The pilot.
|
// Flag definitions.
|
||||||
|
#define PLAYER_TURN_LEFT (1<<0) // Player is turning left.
|
||||||
|
#define PLAYER_TURN_RIGHT (1<<1) // Player is turning right.
|
||||||
|
#define PLAYER_FACE (1<<2) // Player is facing target.
|
||||||
|
#define PLAYER_PRIMARY (1<<3) // Player is shooting primary weapon.
|
||||||
|
#define PLAYER_SECONDARY (1<<4) // Player is shooting secondary weapon.
|
||||||
|
|
||||||
|
// Flag functions.
|
||||||
|
#define player_isFlag(f) (player_flags & f)
|
||||||
|
#define player_setFlag(f) (player_flags |= f)
|
||||||
|
#define player_rmFlag(f) (player_flags ^= f)
|
||||||
|
|
||||||
|
// The player.
|
||||||
extern Pilot* pilot;
|
extern Pilot* pilot;
|
||||||
|
extern unsigned int player_flags;
|
||||||
extern unsigned int credits;
|
extern unsigned int credits;
|
||||||
|
|
||||||
typedef enum { RADAR_RECT, RADAR_CIRCLE } RadarShape; // For render functions.
|
typedef enum { RADAR_RECT, RADAR_CIRCLE } RadarShape; // For render functions.
|
||||||
typedef enum { KEYBIND_NULL, KEYBIND_KEYBOARD, KEYBIND_JAXIS, KEYBIND_JBUTTON } KeybindType;
|
|
||||||
|
|
||||||
// Creation.
|
// Creation.
|
||||||
void player_new(void);
|
void player_new(void);
|
||||||
@ -17,17 +28,6 @@ int gui_init(void);
|
|||||||
void gui_free(void);
|
void gui_free(void);
|
||||||
void player_render(void);
|
void player_render(void);
|
||||||
|
|
||||||
int player_isFlag(unsigned int flag);
|
|
||||||
void player_setFlag(unsigned int flag);
|
|
||||||
void player_rmFlag(unsigned int flag);
|
|
||||||
|
|
||||||
// Input.
|
|
||||||
void input_init(void);
|
|
||||||
void input_exit(void);
|
|
||||||
void input_setDefault(void);
|
|
||||||
void input_setKeybind(char* keybind, KeybindType type, int key, int reverse);
|
|
||||||
void input_handle(SDL_Event* event);
|
|
||||||
|
|
||||||
// Misc.
|
// Misc.
|
||||||
void player_message(const char* fmt, ...);
|
void player_message(const char* fmt, ...);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user