[Add] More input error checking and safety crap.

-- This also fixes a segmentation fault on application close.
This commit is contained in:
Allanis 2013-02-10 21:45:06 +00:00
parent 3db7241663
commit 34b01c7545
4 changed files with 39 additions and 25 deletions

View File

@ -80,14 +80,7 @@ int main(int argc, char** argv) {
// input.
input_init();
input_setKeybind("accel", KEYBIND_KEYBOARD, SDLK_w, 0);
input_setKeybind("left", KEYBIND_KEYBOARD, SDLK_a, 0);
input_setKeybind("right", KEYBIND_KEYBOARD, SDLK_d, 0);
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("mapzoomin", KEYBIND_KEYBOARD, SDLK_UP, 0);
input_setKeybind("mapzoomout", KEYBIND_KEYBOARD, SDLK_DOWN, 0);
input_setDefault(); // Call input crap from player.c
// Use Lua to parse configuration file.
lua_State* L = luaL_newstate();
@ -208,7 +201,7 @@ int main(int argc, char** argv) {
if(gl_init()) {
// Initializes video output.
WARN("Error initializing video output, exiting...");
ERR("Initializing video output failed, exiting...");
SDL_Quit();
exit(EXIT_FAILURE);
}
@ -231,12 +224,18 @@ int main(int argc, char** argv) {
WARN("Error initializing joystick input");
if(namjoystick != NULL) {
// Use a joystick name to find joystick.
joystick_use(joystick_get(namjoystick));
if(joystick_use(joystick_get(namjoystick))) {
WARN("Failure to open any joystick, falling back to default keybinds");
input_setDefault();
}
free(namjoystick);
}
else if(indjoystick >= 0)
// Must be using an id instead.
joystick_use(indjoystick);
if(joystick_use(indjoystick)) {
WARN("Failure to open any joystick, falling back to default keybinds");
input_setDefault();
}
}
// Misc.

View File

@ -578,7 +578,7 @@ int gl_init(void) {
// Get available fullscreen modes.
if(gl_screen.fullscreen) {
modes = SDL_ListModes(NULL, SDL_OPENGL | SDL_FULLSCREEN);
if(modes == NULL) {
if(modes == NULL) { // Could happen, but rare.
WARN("No fullscreen modes available");
if(flags & SDL_FULLSCREEN) {
WARN("Disabling fullscreen mode");
@ -592,14 +592,14 @@ int gl_init(void) {
for(i = 0; modes[i]; ++i) {
DEBUG("\t%dx%d", modes[i]->w, modes[i]->h);
if((flags & SDL_FULLSCREEN) && (modes[i]->w == gl_screen.w) && (modes[i]->h == gl_screen.h))
supported = 1;
supported = 1; // Mode we asked for is supported.
}
}
// Make sure fullscreen mode is supported.
if(flags & SDL_FULLSCREEN && !supported) {
WARN("Fullscreen mode %dx%d is not supported by your current setup, switching to another mode",
gl_screen.w, gl_screen.h);
WARN("Fullscreen mode %dx%d is not supported by your current setup, attempting %dx%d",
gl_screen.w, gl_screen.h, modes[0]->w, modes[0]->h);
gl_screen.w = modes[0]->w;
gl_screen.h = modes[0]->h;
}
@ -618,7 +618,7 @@ int gl_init(void) {
// Actually creating the screen.
if(SDL_SetVideoMode(gl_screen.w, gl_screen.h, gl_screen.depth, flags) == NULL) {
WARN("Unable to create OpenGL window: %s", SDL_GetError());
ERR("Unable to create OpenGL window: %s", SDL_GetError());
SDL_Quit();
return -1;
}

View File

@ -386,11 +386,25 @@ void player_think(Pilot* player) {
// ================
// INPUT!
// ================
// Set the default input keys.
void input_setDefault(void) {
input_setKeybind("accel", KEYBIND_KEYBOARD, SDLK_w, 0);
input_setKeybind("left", KEYBIND_KEYBOARD, SDLK_a, 0);
input_setKeybind("right", KEYBIND_KEYBOARD, SDLK_d, 0);
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("mapzoomin", KEYBIND_KEYBOARD, SDLK_UP, 0);
input_setKeybind("mapzoomout", KEYBIND_KEYBOARD, SDLK_DOWN, 0);
}
// Initialization/exit functions (does not assign keys).
void input_init(void) {
Keybind* tmp;
int i;
for(i = 0; keybindNames[i]; i++); // Get number of bindings.
for(i = 0; strcmp(keybindNames[i], "end"); i++); // Get number of bindings.
player_input = malloc(i*sizeof(Keybind*));
// Create a null keybinding for each.
@ -406,7 +420,7 @@ void input_init(void) {
void input_exit(void) {
int i;
for(i = 0; keybindNames[i]; i++)
for(i = 0; strcmp(keybindNames[i], "end"); i++)
free(player_input[i]);
free(player_input);
}
@ -414,14 +428,14 @@ void input_exit(void) {
// Binds key of type [type] to action keybind.
void input_setKeybind(char* keybind, KeybindType type, int key, int reverse) {
int i;
for(i = 0; keybindNames[i]; 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. ================================================
@ -485,7 +499,7 @@ static void input_keyup(SDLKey key);
// Axis.
static void input_joyaxis(const unsigned int axis, const int value) {
int i;
for(i = 0; keybindNames[i]; 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;
@ -495,7 +509,7 @@ static void input_joyaxis(const unsigned int axis, const int value) {
// Joystick button down.
static void input_joydown(const unsigned int button) {
int i;
for(i = 0; keybindNames[i]; 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;
@ -505,7 +519,7 @@ static void input_joydown(const unsigned int button) {
// Joystick button up.
static void input_joyup(const unsigned int button) {
int i;
for(i = 0; keybindNames[i]; 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;
@ -517,7 +531,7 @@ static void input_joyup(const unsigned int button) {
// Key down.
static void input_keydown(SDLKey key) {
int i;
for(i = 0; keybindNames[i]; 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;
@ -534,7 +548,7 @@ static void input_keydown(SDLKey key) {
// Key up.
static void input_keyup(SDLKey key) {
int i;
for(i = 0; keybindNames[i]; 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;

View File

@ -18,6 +18,7 @@ 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);