sdlterm/src/sdlterm.cc
changeset 61 15088f62c4ac
parent 60 fccca2a60492
child 62 2f61931520c9
--- a/sdlterm/src/sdlterm.cc	Wed Jan 09 22:32:15 2013 +0100
+++ b/sdlterm/src/sdlterm.cc	Thu Jan 10 00:03:34 2013 +0100
@@ -372,6 +372,7 @@
 bool Terminal::wait_event(Event &event, Uint32 timeout)
 {
     static SDL_Event sdl_event;
+    bool event_ready = false;
 
     // use timer to simulate SDL_WaitEventTimeout, which is not available in SDL 1.2
     SDL_TimerID timer_id = NULL;
@@ -380,36 +381,34 @@
         timer_id = SDL_AddTimer(timeout, _wait_event_callback, NULL);
     }
 
-    while (SDL_WaitEvent(&sdl_event))
+    while (SDL_WaitEvent(&sdl_event) && !event_ready)
     {
-        if (timer_id)
-        {
-            if (sdl_event.type == SDL_USEREVENT && sdl_event.user.code == 1)
-            {
-                // timeout
-                return false;
-            }
-            // some event came before timeout...
-            SDL_RemoveTimer(timer_id);
-            timer_id = NULL;
-        }
         switch (sdl_event.type)
         {
             case SDL_USEREVENT:
+                // timeout
+                if (sdl_event.user.code == 1)
+                {
+                    SDL_RemoveTimer(timer_id);
+                    return false;
+                }
                 // toggle blink
                 if (sdl_event.user.code == 2)
                 {
                     _screen.toggle_blink();
                 }
                 break; // continue loop
+
             case SDL_QUIT:
                 event.type = Event::QUIT;
-                return true;
+                event_ready = true;
+                break;
 
             case SDL_VIDEORESIZE:
                 event.type = Event::RESIZE;
                 _screen.resize(sdl_event.resize.w, sdl_event.resize.h);
-                return true;
+                event_ready = true;
+                break;
 
             case SDL_VIDEOEXPOSE:
                 _screen.redraw();
@@ -433,7 +432,8 @@
                     if (!event.key.unicode)
                         break; // continue loop (unknown key)
                 }
-                return true;
+                event_ready = true;
+                break;
             }
 
             case SDL_MOUSEBUTTONDOWN:
@@ -449,7 +449,8 @@
                     event.type = Event::MOUSEWHEEL;
                 }
                 _mousemove_last_x = -1;
-                return true;
+                event_ready = true;
+                break;
 
             case SDL_MOUSEMOTION:
                 if (sdl_event.motion.state == 0)
@@ -462,7 +463,8 @@
                 {
                     _mousemove_last_x = event.mouse.x;
                     _mousemove_last_y = event.mouse.y;
-                    return true;
+                    event_ready = true;
+                    break;
                 }
                 break; // continue loop when mouse position did not change
 
@@ -470,7 +472,22 @@
                 break; // continue loop
         }
     }
-    throw SDLTermError(std::string("SDL_WaitEvent: ") + SDL_GetError());
+
+    // remove timer when other event came before timeout
+    if (timeout)
+    {
+        SDL_RemoveTimer(timer_id);
+    }
+
+    // ok or error?
+    if (event_ready)
+    {
+        return true;
+    }
+    else
+    {
+        throw SDLTermError(std::string("SDL_WaitEvent: ") + SDL_GetError());
+    }
 }