[Change] Finished cleaning input. Making way for toolkit.

This commit is contained in:
Allanis 2013-02-16 12:56:29 +00:00
parent e6057d04af
commit 67480a47e3
5 changed files with 74 additions and 340 deletions

View File

@ -43,7 +43,7 @@ extern char* namjoystick;
// From player.c
extern const char* keybindNames[]; // Keybindings.
static void print_usae(char** argv);
static void print_usage(char** argv);
// Print usage.
static void print_usage(char** argv) {

View File

@ -3,8 +3,8 @@
#include "player.h"
#include "input.h"
#define KEY_PRESS ( 1.);
#define KEY_RELEASE (-1.);
#define KEY_PRESS ( 1.)
#define KEY_RELEASE (-1.)
// Keybind structure.
typedef struct {
@ -13,7 +13,10 @@ typedef struct {
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.
static Keybind** input_keybinds; // Contains the players keybindings.
// Name of each keybinding.
const char* keybindNames[] = { "accel", "left", "right", // Movement.
"primary", "target", "target_nearest", "face", "board", // Combat.
@ -56,7 +59,7 @@ 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*));
input_keybinds = malloc(i*sizeof(Keybind*));
// Create a null keybinding for each.
for(i = 0; strcmp(keybindNames[i], "end"); i++) {
@ -65,44 +68,45 @@ void input_init(void) {
tmp->type = KEYBIND_NULL;
tmp->key = 0;
tmp->reverse = 1.;
player_input[i] = tmp;
input_keybinds[i] = tmp;
}
}
void input_exit(void) {
int i;
for(i = 0; strcmp(keybindNames[i], "end"); i++)
free(player_input[i]);
free(player_input);
free(input_keybinds[i]);
free(input_keybinds);
}
// 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.;
if(strcmp(keybind, input_keybinds[i]->name)==0) {
input_keybinds[i]->type = type;
input_keybinds[i]->key = key;
input_keybinds[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.
// keynum : Index of the input_keybinds keybind.
// value : Value of keypress (defined above).
// abs : Whether or not it's an abs value (For those pesky joysticks.
// =====================================================================
#define KEY(s) strcmp(input_keybinds[keynum]->name, s)==0
static void input_key(int keynum, double value, int abs) {
// Accelerating.
if(strcmp(player_input[keynum]->name, "accel")==0) {
if(KEY("accel")) {
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) {
else if(KEY("left")) {
// Set flags for facing correction.
if(value == KEY_PRESS) player_setFlag(PLAYER_TURN_LEFT);
else if(value == KEY_RELEASE) player_rmFlag(PLAYER_TURN_LEFT);
@ -112,7 +116,7 @@ static void input_key(int keynum, double value, int abs) {
if(player_turn < -1.) player_turn = -1.; // Make sure value is sane.
}
// Turning right.
else if(strcmp(player_input[keynum]->name, "right")==0) {
else if(KEY("right")) {
// Set flags for facing correction.
if(value == KEY_PRESS) player_setFlag(PLAYER_TURN_RIGHT);
else if(value == KEY_RELEASE) player_rmFlag(PLAYER_TURN_RIGHT);
@ -123,19 +127,19 @@ static void input_key(int keynum, double value, int abs) {
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) {
else if(KEY("primary")) {
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) {
else if(KEY("target")) {
if(value == KEY_PRESS) player_target = pilot_getNext(player_target);
}
else if(strcmp(player_input[keynum]->name, "target_nearest")==0) {
else if(KEY("target_nearest")) {
if(value == KEY_PRESS) player_target = pilot_getHostile();
}
// Face the target.
else if(strcmp(player_input[keynum]->name, "face")==0) {
else if(KEY("face")) {
if(value == KEY_PRESS) player_setFlag(PLAYER_FACE);
else if(value == KEY_RELEASE) {
player_rmFlag(PLAYER_FACE);
@ -147,38 +151,36 @@ static void input_key(int keynum, double value, int abs) {
}
}
// Board those ships.
else if(strcmp(player_input[keynum]->name, "board")==0) {
else if(KEY("board")) {
if(value == KEY_PRESS) player_board();
}
// Shooting secondary weapon.
else if(strcmp(player_input[keynum]->name, "secondary")==0) {
else if(KEY("secondary")) {
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) {
else if(KEY("secondary_next")) {
if(value == KEY_PRESS) player_secondaryNext();
}
// Target planet (cycles just like target).
else if(strcmp(player_input[keynum]->name, "target_planet")==0) {
else if(KEY("target_planet")) {
if(value == KEY_PRESS) player_targetPlanet();
}
// Target nearest planet or attempt to land.
else if(strcmp(player_input[keynum]->name, "land")==0) {
else if(KEY("land")) {
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;
else if(KEY("mapzoomin")) {
if(value == KEY_PRESS) player_setRadarRel(1);
}
// 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;
else if(KEY("mapzoomout")) {
if(value == KEY_PRESS) player_setRadarRel(-1);
}
// Take a screenshot.
else if(strcmp(player_input[keynum]->name, "screenshot")==0) {
else if(KEY("screenshot")) {
if(value == KEY_PRESS) player_screenshot();
}
}
@ -197,8 +199,8 @@ static void input_keyup(SDLKey key);
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);
if(input_keybinds[i]->type == KEYBIND_JAXIS && input_keybinds[i]->key == axis) {
input_key(i, -(input_keybinds[i]->reverse) * (double)value / 32767., 1);
return;
}
}
@ -207,7 +209,7 @@ static void input_joyaxis(const unsigned int axis, const int value) {
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) {
if(input_keybinds[i]->type == KEYBIND_JBUTTON && input_keybinds[i]->key == button) {
input_key(i, KEY_RELEASE, 0);
return;
}
@ -217,7 +219,7 @@ static void input_joydown(const unsigned int button) {
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) {
if(input_keybinds[i]->type == KEYBIND_JBUTTON && input_keybinds[i]->key == button) {
input_key(i, KEY_RELEASE, 0);
return;
}
@ -229,7 +231,7 @@ static void input_joyup(const unsigned int button) {
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) {
if(input_keybinds[i]->type == KEYBIND_KEYBOARD && input_keybinds[i]->key == key) {
input_key(i, KEY_PRESS, 0);
return;
}
@ -246,7 +248,7 @@ static void input_keydown(SDLKey key) {
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) {
if(input_keybinds[i]->type == KEYBIND_KEYBOARD && input_keybinds[i]->key == key) {
input_key(i, KEY_RELEASE, 0);
return;
}

View File

@ -9,6 +9,7 @@
#include "ship.h"
#include "pilot.h"
#include "player.h"
#include "input.h"
#include "joystick.h"
#include "space.h"
#include "rng.h"
@ -29,7 +30,7 @@
#define MINIMUM_FPS 0.5
#define FONT_SIZE 12
static int space = 1; // Global value, control whether or not player is flying.
int toolkit = 0; // Toolkit has a window open.
static int quit = 0; // Primary loop.
static unsigned int time = 0; // Calculate FPS and movement.
static char version[VERSION_LEN];
@ -145,11 +146,12 @@ int main(int argc, char** argv) {
while(SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT) quit = 1; // Handle quit.
if(space) // Player is flying around happily.
input_handle(&event); // handles all the events the player keybinds.
}
if(space) {
if(toolkit) {
} else {
// Player is flying around.
update_space();
glClear(GL_COLOR_BUFFER_BIT);

View File

@ -22,44 +22,14 @@
#define pow2(x) ((x)*(x))
// 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)
#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.
// Player stuff.
Pilot* player = NULL; // extern in pilot.h
unsigned int credits = 0;
static unsigned int player_flags = 0; // Player flags.
static double player_turn = 0.; // Turn velocity from input.
static double player_acc = 0.; // Accel velocity from input.
static unsigned int player_target = PLAYER_ID; // Targetted pilot.
static int planet_target = -1; // Targetted planet.
unsigned int player_flags = 0; // Player flags.
double player_turn = 0.; // Turn velocity from input.
double player_acc = 0.; // Accel velocity from input.
unsigned int player_target = PLAYER_ID; // Targetted pilot.
int planet_target = -1; // Targetted planet.
// Pilot stuff for GUI.
extern Pilot** pilot_stack;
@ -147,13 +117,6 @@ static int gui_parse(const xmlNodePtr parent, const char* name);
static void gui_renderPilot(const Pilot* p);
static void gui_renderBar(const glColour* c, const Vec2* p, const Rect* r, const double w);
// Keybinds.
static void player_board(void);
static void player_secondaryNext(void);
static void player_targetPlanet(void);
static void player_land(void);
static void player_screenshot(void);
// Create a new player.
void player_new(void) {
Ship* ship;
@ -815,6 +778,17 @@ void player_think(Pilot* player) {
vect_pset(&player->solid->force, player->ship->thrust * player_acc, player->solid->dir);
}
// Modify the radar resolution.
void player_setRadarRel(int mod) {
if(mod > 0) {
gui.radar.res += mod * RADAR_RES_INTERVAL;
if(gui.radar.res > RADAR_RES_MAX) gui.radar.res = RADAR_RES_MAX;
} else {
gui.radar.res -= mod * RADAR_RES_INTERVAL;
if(gui.radar.res < RADAR_RES_MIN) gui.radar.res = RADAR_RES_MIN;
}
}
void player_board(void) {
Pilot* p;
@ -843,7 +817,7 @@ void player_board(void) {
}
// Get the next secondary weapon.
static void player_secondaryNext(void) {
void player_secondaryNext(void) {
int i = 0;
// Get the current secondary weapon pos.
@ -868,7 +842,7 @@ static void player_secondaryNext(void) {
}
// Cycle through planet targets.
static void player_targetPlanet(void) {
void player_targetPlanet(void) {
if((planet_target == -1) && (cur_system->nplanets > 0)) {
// No target.
planet_target = 0;
@ -883,7 +857,7 @@ static void player_targetPlanet(void) {
}
// Attempt to land or target closest planet if no land target.
static void player_land(void) {
void player_land(void) {
Planet* planet = &cur_system->planets[planet_target];
if(planet_target >= 0) {
if(vect_dist(&player->solid->vel, &planet->pos) > planet->gfx_space->sw) {
@ -914,262 +888,10 @@ static void player_land(void) {
}
// Take a screenshot.
static void player_screenshot(void) {
void player_screenshot(void) {
char filename[20];
// TODO not overwirte old screenshots.
strncpy(filename, "screenshot.png", 20);
gl_screenshot(filename);
}
// ================
// INPUT!
// ================
// 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;
}
}

View File

@ -31,3 +31,11 @@ void player_render(void);
// Misc.
void player_message(const char* fmt, ...);
// Keybind actions.
void player_setRadarRel(int mod);
void player_board(void);
void player_secondaryNext(void);
void player_targetPlanet(void);
void player_land(void);
void player_screenshot(void);