From 2348d58d70bcca7fb4a424f4536a489714ffe392 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Thu, 24 May 2018 21:08:59 -0400 Subject: no key repeat delay, implemented weirdly --- src/main.cpp | 69 +++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 43 insertions(+), 26 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 28c89cc..5e4cc1e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -51,6 +51,13 @@ const int TILE_HEIGHT = 8*2; const int VIEW_WIDTH = GAME_WIDTH / TILE_WIDTH; const int VIEW_HEIGHT = GAME_HEIGHT / TILE_HEIGHT; +struct Input { + bool left = false; + bool right = false; + bool up = false; + bool down = false; +}; + class Map { public: @@ -258,28 +265,34 @@ void recalculateLighting(Map& map, fov_settings_type* fov) map.lighting[player_x+VIEW_WIDTH*player_y] = true; } -void processKeys(Map& map) +void processKeys(Map& map, const Input& keystate) { - const Uint8* state = SDL_GetKeyboardState(NULL); + int px = player_x; + int py = player_y; + + if (keystate.up) + { + py--; + } - if (state[SDL_SCANCODE_UP]) + if (keystate.down) { - movePlayer(player_x, player_y-1, map); + py++; } - if (state[SDL_SCANCODE_DOWN]) + if (keystate.left) { - movePlayer(player_x, player_y+1, map); + px--; } - if (state[SDL_SCANCODE_LEFT]) + if (keystate.right) { - movePlayer(player_x-1, player_y, map); + px++; } - if (state[SDL_SCANCODE_RIGHT]) + if (!(player_x == px && player_y == px)) { - movePlayer(player_x+1, player_y, map); + movePlayer(px, py, map); } } @@ -335,11 +348,13 @@ int main(int, char**) tick(map); bool quit = false; + Input keystate; SDL_Event e; while (!quit) { - bool input = false; - int presses = 0; + //bool input = false; + //int presses = 0; + bool pressedSpace = false; while (SDL_PollEvent(&e)) { if (e.type == SDL_QUIT) @@ -347,7 +362,7 @@ int main(int, char**) quit = true; } else if (e.type == SDL_KEYDOWN) { - presses++; + //presses++; switch (e.key.keysym.sym) { @@ -359,7 +374,8 @@ int main(int, char**) case SDLK_SPACE: { - input = true; + pressedSpace = true; + //input = true; std::deque> lamps; lamps.emplace_back(player_x, player_y); @@ -368,7 +384,7 @@ int main(int, char**) for (int i = 0; i < 5; i++) { - processKeys(map); + processKeys(map, keystate); tick( map, @@ -431,13 +447,18 @@ int main(int, char**) break; } } - } else if (e.type == SDL_KEYUP) - { - presses++; } } - if (presses > 0) + const Uint8* state = SDL_GetKeyboardState(NULL); + keystate.left = state[SDL_SCANCODE_LEFT]; + keystate.right = state[SDL_SCANCODE_RIGHT]; + keystate.up = state[SDL_SCANCODE_UP]; + keystate.down = state[SDL_SCANCODE_DOWN]; + + bool input = keystate.left || keystate.right || keystate.up || keystate.down || pressedSpace; + + if (input) { for (int y = 0; y < VIEW_HEIGHT; y++) { @@ -451,14 +472,10 @@ int main(int, char**) } } - for (int i = 0; i < presses; i++) - { - processKeys(map); - } - + processKeys(map, keystate); recalculateLighting(map, fov.get()); - if (presses > 0) + if (input) { for (int y = 0; y < VIEW_HEIGHT; y++) { @@ -482,7 +499,7 @@ int main(int, char**) } render(ren.get(), map, true); - SDL_Delay(10); + SDL_Delay(50); } } catch (const sdl_error& ex) { -- cgit 1.4.1