From: Holger Schemel Date: Sat, 19 Mar 2016 15:19:07 +0000 (+0100) Subject: code cleanup of main event loop X-Git-Tag: 4.0.0.0-rc1~46 X-Git-Url: https://git.artsoft.org/?a=commitdiff_plain;h=b2c07ef9208b06f07e8d45fdddbffd8db721a02d;p=rocksndiamonds.git code cleanup of main event loop --- diff --git a/src/events.c b/src/events.c index 4c71dca0..d9104aa1 100644 --- a/src/events.c +++ b/src/events.c @@ -152,124 +152,63 @@ boolean NextValidEvent(Event *event) return FALSE; } -void EventLoop(void) +void HandleEvents() { - static unsigned int sync_frame_delay = 0; - unsigned int sync_frame_delay_value = GAME_FRAME_DELAY; + Event event; + unsigned int event_frame_delay = 0; + unsigned int event_frame_delay_value = GAME_FRAME_DELAY; - while (1) + ResetDelayCounter(&event_frame_delay); + + while (NextValidEvent(&event)) { - if (PendingEvent()) /* got event */ + switch (event.type) { - // use separate frame delay counter to not reset main delay counter - unsigned int sync_frame_delay2 = 0; - unsigned int sync_frame_delay_value2 = sync_frame_delay_value; - Event event; - - ResetDelayCounter(&sync_frame_delay2); + case EVENT_BUTTONPRESS: + case EVENT_BUTTONRELEASE: + HandleButtonEvent((ButtonEvent *) &event); + break; - while (NextValidEvent(&event)) - { - switch (event.type) - { - case EVENT_BUTTONPRESS: - case EVENT_BUTTONRELEASE: - HandleButtonEvent((ButtonEvent *) &event); - break; - - case EVENT_MOTIONNOTIFY: - HandleMotionEvent((MotionEvent *) &event); - break; + case EVENT_MOTIONNOTIFY: + HandleMotionEvent((MotionEvent *) &event); + break; #if defined(TARGET_SDL2) - case SDL_WINDOWEVENT: - HandleWindowEvent((WindowEvent *) &event); - break; - - case EVENT_FINGERPRESS: - case EVENT_FINGERRELEASE: - case EVENT_FINGERMOTION: - HandleFingerEvent((FingerEvent *) &event); - break; - - case EVENT_TEXTINPUT: - HandleTextEvent((TextEvent *) &event); - break; - - case SDL_APP_WILLENTERBACKGROUND: - case SDL_APP_DIDENTERBACKGROUND: - case SDL_APP_WILLENTERFOREGROUND: - case SDL_APP_DIDENTERFOREGROUND: - HandlePauseResumeEvent((PauseResumeEvent *) &event); - break; -#endif - - case EVENT_KEYPRESS: - case EVENT_KEYRELEASE: - HandleKeyEvent((KeyEvent *) &event); - break; - - default: - HandleOtherEvents(&event); - break; - } + case SDL_WINDOWEVENT: + HandleWindowEvent((WindowEvent *) &event); + break; - // do not handle events for longer than standard frame delay period - if (DelayReached(&sync_frame_delay2, sync_frame_delay_value2)) - break; - } - } + case EVENT_FINGERPRESS: + case EVENT_FINGERRELEASE: + case EVENT_FINGERMOTION: + HandleFingerEvent((FingerEvent *) &event); + break; - // always handle non-event game actions for every game frame interval - { - if (game_status == GAME_MODE_TITLE) - { - /* when showing title screens, hide mouse pointer (if not moved) */ + case EVENT_TEXTINPUT: + HandleTextEvent((TextEvent *) &event); + break; - if (gfx.cursor_mode != CURSOR_NONE && - DelayReached(&special_cursor_delay, special_cursor_delay_value)) - { - SetMouseCursor(CURSOR_NONE); - } - } - else if (game_status == GAME_MODE_PLAYING && (!tape.pausing || - tape.single_step)) - { - /* when playing, display a special mouse pointer inside the playfield */ + case SDL_APP_WILLENTERBACKGROUND: + case SDL_APP_DIDENTERBACKGROUND: + case SDL_APP_WILLENTERFOREGROUND: + case SDL_APP_DIDENTERFOREGROUND: + HandlePauseResumeEvent((PauseResumeEvent *) &event); + break; +#endif - if (gfx.cursor_mode != CURSOR_PLAYFIELD && - cursor_inside_playfield && - DelayReached(&special_cursor_delay, special_cursor_delay_value)) - { - SetMouseCursor(CURSOR_PLAYFIELD); - } - } - else if (gfx.cursor_mode != CURSOR_DEFAULT) - { - SetMouseCursor(CURSOR_DEFAULT); - } + case EVENT_KEYPRESS: + case EVENT_KEYRELEASE: + HandleKeyEvent((KeyEvent *) &event); + break; - /* this is set after all pending events have been processed */ - cursor_mode_last = gfx.cursor_mode; + default: + HandleOtherEvents(&event); + break; } - /* also execute after pending events have been processed before */ - HandleNoEvent(); - - /* don't use all CPU time when idle; the main loop while playing - has its own synchronization and is CPU friendly, too */ - - if (game_status == GAME_MODE_PLAYING) - HandleGameActions(); - - /* refresh window contents from drawing buffer, if needed */ - BackToFront(); - - if (game_status != GAME_MODE_PLAYING) - WaitUntilDelayReached(&sync_frame_delay, sync_frame_delay_value); - - if (game_status == GAME_MODE_QUIT) - return; + // do not handle events for longer than standard frame delay period + if (DelayReached(&event_frame_delay, event_frame_delay_value)) + break; } } @@ -315,6 +254,71 @@ void HandleOtherEvents(Event *event) } } +void HandleMouseCursor() +{ + if (game_status == GAME_MODE_TITLE) + { + /* when showing title screens, hide mouse pointer (if not moved) */ + + if (gfx.cursor_mode != CURSOR_NONE && + DelayReached(&special_cursor_delay, special_cursor_delay_value)) + { + SetMouseCursor(CURSOR_NONE); + } + } + else if (game_status == GAME_MODE_PLAYING && (!tape.pausing || + tape.single_step)) + { + /* when playing, display a special mouse pointer inside the playfield */ + + if (gfx.cursor_mode != CURSOR_PLAYFIELD && + cursor_inside_playfield && + DelayReached(&special_cursor_delay, special_cursor_delay_value)) + { + SetMouseCursor(CURSOR_PLAYFIELD); + } + } + else if (gfx.cursor_mode != CURSOR_DEFAULT) + { + SetMouseCursor(CURSOR_DEFAULT); + } + + /* this is set after all pending events have been processed */ + cursor_mode_last = gfx.cursor_mode; +} + +void EventLoop(void) +{ + unsigned int sync_frame_delay = 0; + unsigned int sync_frame_delay_value = GAME_FRAME_DELAY; + + while (1) + { + if (PendingEvent()) + HandleEvents(); + else + HandleMouseCursor(); + + /* also execute after pending events have been processed before */ + HandleNoEvent(); + + /* don't use all CPU time when idle; the main loop while playing + has its own synchronization and is CPU friendly, too */ + + if (game_status == GAME_MODE_PLAYING) + HandleGameActions(); + + /* refresh window contents from drawing buffer, if needed */ + BackToFront(); + + if (game_status != GAME_MODE_PLAYING) + WaitUntilDelayReached(&sync_frame_delay, sync_frame_delay_value); + + if (game_status == GAME_MODE_QUIT) + return; + } +} + void ClearEventQueue() { while (PendingEvent())