diff options
author | P. J. McDermott <pj@pehjota.net> | 2021-08-07 16:50:47 (EDT) |
---|---|---|
committer | P. J. McDermott <pj@pehjota.net> | 2021-08-07 17:18:02 (EDT) |
commit | 60388cb5de1d93636688cabfe84c18759c4d2141 (patch) | |
tree | b10fa82b849dbde054f9a498f78b7cf57c1b347b | |
parent | d3783c83f614b3beded3d6a5c528005f480524e3 (diff) | |
download | mazefight-60388cb5de1d93636688cabfe84c18759c4d2141.zip mazefight-60388cb5de1d93636688cabfe84c18759c4d2141.tar.gz mazefight-60388cb5de1d93636688cabfe84c18759c4d2141.tar.bz2 |
game: Poll for events
-rw-r--r-- | src/game.c | 58 |
1 files changed, 39 insertions, 19 deletions
@@ -55,6 +55,10 @@ mf_game(long seed, int size, int fow, int reveal, SDL_Renderer *renderer) SDL_Color form_color; SDL_Color text_color; struct mftk_window *win = NULL; + int fr; + Uint32 beg; + Uint32 end; + Uint32 delay; SDL_Event event; /* Create maze */ @@ -90,27 +94,31 @@ mf_game(long seed, int size, int fow, int reveal, SDL_Renderer *renderer) _mf_game_form(renderer, text_font, &text_color))); - while (SDL_WaitEvent(&event)) { - switch (event.type) { - case SDL_QUIT: - goto quit; - case SDL_KEYDOWN: - switch (event.key.keysym.sym) { - case SDLK_ESCAPE: - goto exit; + fr = 30; + while(1) { + beg = SDL_GetTicks(); + while (SDL_PollEvent(&event)) { + switch (event.type) { + case SDL_QUIT: + goto quit; + case SDL_KEYDOWN: + switch (event.key.keysym.sym) { + case SDLK_ESCAPE: + goto exit; + default: + break; + } default: break; - } - default: - break; - } - switch (mftk_window_event(win, &event)) { - case 0: - break; - case 1: - goto exit; - default: - goto err; + } + switch (mftk_window_event(win, &event)) { + case 0: + break; + case 1: + goto exit; + default: + goto err; + } } SDL_SetRenderDrawColor(renderer, MF_COLOR_BACK_R, MF_COLOR_BACK_G, @@ -119,6 +127,18 @@ mf_game(long seed, int size, int fow, int reveal, SDL_Renderer *renderer) mf_maze_render(maze, renderer, &maze_color, MF_WINDOW_H / size); mftk_window_render(win, renderer); SDL_RenderPresent(renderer); + end = SDL_GetTicks(); + if ((Uint32) (1000 / fr) > (end - beg)) { + delay = 1000 / fr - (end - beg); + } else { + SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION, + "Frame took longer than frame period"); + delay = 0; + } + SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, + "Frame took %u ms, delaying %u ms", + end - beg, delay); + SDL_Delay(delay); } exit: |