[Add] More error checking paranoia.

This commit is contained in:
Allanis 2013-03-22 19:01:45 +00:00
parent 5e68d64085
commit 8c7a38686c
2 changed files with 17 additions and 13 deletions

View File

@ -192,7 +192,7 @@ int main(int argc, char** argv) {
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
fps_control(); // Who doesn't love FPS control? fps_control(); // Who doesn't love FPS control?
toolkit_update(); // Simulate key repetition. if(toolkit) toolkit_update(); // Simulate key repetition.
if(!menu_isOpen(MENU_MAIN)) { if(!menu_isOpen(MENU_MAIN)) {
if(!paused && !toolkit) update_space(); // Update the game. if(!paused && !toolkit) update_space(); // Update the game.

View File

@ -300,12 +300,12 @@ void window_addInput(const unsigned int wid,
wgt->name = strdup(name); wgt->name = strdup(name);
// Specific. // Specific.
wgt->dat.inp.max = max; wgt->dat.inp.max = max+1;
wgt->dat.inp.oneline = oneline; wgt->dat.inp.oneline = oneline;
wgt->dat.inp.pos = 0; wgt->dat.inp.pos = 0;
wgt->dat.inp.view = 0; wgt->dat.inp.view = 0;
wgt->dat.inp.input = malloc(sizeof(char)*max); wgt->dat.inp.input = malloc(sizeof(char)*wgt->dat.inp.max);
memset(wgt->dat.inp.input, 0, max*sizeof(char)); memset(wgt->dat.inp.input, 0, wgt->dat.inp.max*sizeof(char));
// Position/Size. // Position/Size.
wgt->w = (double)w; wgt->w = (double)w;
@ -942,7 +942,7 @@ static int toolkit_inputInput(Uint8 type, Widget* inp, SDLKey key) {
// Backspace -> delete text. // Backspace -> delete text.
if((type == SDL_KEYDOWN) && (key == '\b') && (inp->dat.inp.pos > 0)) if((type == SDL_KEYDOWN) && (key == '\b') && (inp->dat.inp.pos > 0))
inp->dat.inp.input[--inp->dat.inp.pos] = '\0'; inp->dat.inp.input[--inp->dat.inp.pos] = '\0';
else if((type == SDL_KEYDOWN) && (inp->dat.inp.pos < inp->dat.inp.max)) { else if((type == SDL_KEYDOWN) && (inp->dat.inp.pos < inp->dat.inp.max-1)) {
if((key == SDLK_RETURN) && !inp->dat.inp.oneline) if((key == SDLK_RETURN) && !inp->dat.inp.oneline)
inp->dat.inp.input[inp->dat.inp.pos++] = '\n'; inp->dat.inp.input[inp->dat.inp.pos++] = '\n';
@ -956,8 +956,9 @@ static int toolkit_inputInput(Uint8 type, Widget* inp, SDLKey key) {
// Didn't get a useful key. // Didn't get a useful key.
else return 0; else return 0;
return 1;
} }
return 1;
} }
return 0; return 0;
} }
@ -1100,8 +1101,10 @@ static int toolkit_keyEvent(SDL_Event* event) {
Widget* wgt; Widget* wgt;
SDLKey key; SDLKey key;
if(nwindows <= 0) return 0;
wdw = &windows[nwindows-1]; wdw = &windows[nwindows-1];
wgt = (wdw->focus >= 0) ? &wdw->widgets[wdw->focus] : NULL; wgt = (wdw->focus != -1) ? &wdw->widgets[wdw->focus] : NULL;
key = event->key.keysym.sym; key = event->key.keysym.sym;
// Hack to simulate key repetition. // Hack to simulate key repetition.
@ -1150,9 +1153,6 @@ void toolkit_update(void) {
Window* wdw; Window* wdw;
Widget* wgt; Widget* wgt;
wdw = &windows[nwindows-1];
wgt = (wdw->focus >= 0) ? &wdw->widgets[wdw->focus] : NULL;
t = SDL_GetTicks(); t = SDL_GetTicks();
if(input_key == 0) return; if(input_key == 0) return;
@ -1162,9 +1162,13 @@ void toolkit_update(void) {
input_keyCounter++; input_keyCounter++;
if(wgt && (wgt->type == WIDGET_INPUT) && if(nwindows > 0) {
(input_key == SDLK_BACKSPACE || isalnum(input_key))) wdw = &windows[nwindows-1];
toolkit_inputInput(SDL_KEYDOWN, wgt, input_key); wgt = (wdw->focus >= 0) ? &wdw->widgets[wdw->focus] : NULL;
if(wgt && (wgt->type == WIDGET_INPUT) &&
(input_key == SDLK_BACKSPACE || isalnum(input_key)))
toolkit_inputInput(SDL_KEYDOWN, wgt, input_key);
}
switch(input_key) { switch(input_key) {
case SDLK_UP: case SDLK_UP: