diff options
-rw-r--r-- | src/main.cpp | 69 |
1 files 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; | |||
51 | const int VIEW_WIDTH = GAME_WIDTH / TILE_WIDTH; | 51 | const int VIEW_WIDTH = GAME_WIDTH / TILE_WIDTH; |
52 | const int VIEW_HEIGHT = GAME_HEIGHT / TILE_HEIGHT; | 52 | const int VIEW_HEIGHT = GAME_HEIGHT / TILE_HEIGHT; |
53 | 53 | ||
54 | struct Input { | ||
55 | bool left = false; | ||
56 | bool right = false; | ||
57 | bool up = false; | ||
58 | bool down = false; | ||
59 | }; | ||
60 | |||
54 | class Map { | 61 | class Map { |
55 | public: | 62 | public: |
56 | 63 | ||
@@ -258,28 +265,34 @@ void recalculateLighting(Map& map, fov_settings_type* fov) | |||
258 | map.lighting[player_x+VIEW_WIDTH*player_y] = true; | 265 | map.lighting[player_x+VIEW_WIDTH*player_y] = true; |
259 | } | 266 | } |
260 | 267 | ||
261 | void processKeys(Map& map) | 268 | void processKeys(Map& map, const Input& keystate) |
262 | { | 269 | { |
263 | const Uint8* state = SDL_GetKeyboardState(NULL); | 270 | int px = player_x; |
271 | int py = player_y; | ||
272 | |||
273 | if (keystate.up) | ||
274 | { | ||
275 | py--; | ||
276 | } | ||
264 | 277 | ||
265 | if (state[SDL_SCANCODE_UP]) | 278 | if (keystate.down) |
266 | { | 279 | { |
267 | movePlayer(player_x, player_y-1, map); | 280 | py++; |
268 | } | 281 | } |
269 | 282 | ||
270 | if (state[SDL_SCANCODE_DOWN]) | 283 | if (keystate.left) |
271 | { | 284 | { |
272 | movePlayer(player_x, player_y+1, map); | 285 | px--; |
273 | } | 286 | } |
274 | 287 | ||
275 | if (state[SDL_SCANCODE_LEFT]) | 288 | if (keystate.right) |
276 | { | 289 | { |
277 | movePlayer(player_x-1, player_y, map); | 290 | px++; |
278 | } | 291 | } |
279 | 292 | ||
280 | if (state[SDL_SCANCODE_RIGHT]) | 293 | if (!(player_x == px && player_y == px)) |
281 | { | 294 | { |
282 | movePlayer(player_x+1, player_y, map); | 295 | movePlayer(px, py, map); |
283 | } | 296 | } |
284 | } | 297 | } |
285 | 298 | ||
@@ -335,11 +348,13 @@ int main(int, char**) | |||
335 | tick(map); | 348 | tick(map); |
336 | 349 | ||
337 | bool quit = false; | 350 | bool quit = false; |
351 | Input keystate; | ||
338 | SDL_Event e; | 352 | SDL_Event e; |
339 | while (!quit) | 353 | while (!quit) |
340 | { | 354 | { |
341 | bool input = false; | 355 | //bool input = false; |
342 | int presses = 0; | 356 | //int presses = 0; |
357 | bool pressedSpace = false; | ||
343 | while (SDL_PollEvent(&e)) | 358 | while (SDL_PollEvent(&e)) |
344 | { | 359 | { |
345 | if (e.type == SDL_QUIT) | 360 | if (e.type == SDL_QUIT) |
@@ -347,7 +362,7 @@ int main(int, char**) | |||
347 | quit = true; | 362 | quit = true; |
348 | } else if (e.type == SDL_KEYDOWN) | 363 | } else if (e.type == SDL_KEYDOWN) |
349 | { | 364 | { |
350 | presses++; | 365 | //presses++; |
351 | 366 | ||
352 | switch (e.key.keysym.sym) | 367 | switch (e.key.keysym.sym) |
353 | { | 368 | { |
@@ -359,7 +374,8 @@ int main(int, char**) | |||
359 | 374 | ||
360 | case SDLK_SPACE: | 375 | case SDLK_SPACE: |
361 | { | 376 | { |
362 | input = true; | 377 | pressedSpace = true; |
378 | //input = true; | ||
363 | 379 | ||
364 | std::deque<std::tuple<int, int>> lamps; | 380 | std::deque<std::tuple<int, int>> lamps; |
365 | lamps.emplace_back(player_x, player_y); | 381 | lamps.emplace_back(player_x, player_y); |
@@ -368,7 +384,7 @@ int main(int, char**) | |||
368 | 384 | ||
369 | for (int i = 0; i < 5; i++) | 385 | for (int i = 0; i < 5; i++) |
370 | { | 386 | { |
371 | processKeys(map); | 387 | processKeys(map, keystate); |
372 | 388 | ||
373 | tick( | 389 | tick( |
374 | map, | 390 | map, |
@@ -431,13 +447,18 @@ int main(int, char**) | |||
431 | break; | 447 | break; |
432 | } | 448 | } |
433 | } | 449 | } |
434 | } else if (e.type == SDL_KEYUP) | ||
435 | { | ||
436 | presses++; | ||
437 | } | 450 | } |
438 | } | 451 | } |
439 | 452 | ||
440 | if (presses > 0) | 453 | const Uint8* state = SDL_GetKeyboardState(NULL); |
454 | keystate.left = state[SDL_SCANCODE_LEFT]; | ||
455 | keystate.right = state[SDL_SCANCODE_RIGHT]; | ||
456 | keystate.up = state[SDL_SCANCODE_UP]; | ||
457 | keystate.down = state[SDL_SCANCODE_DOWN]; | ||
458 | |||
459 | bool input = keystate.left || keystate.right || keystate.up || keystate.down || pressedSpace; | ||
460 | |||
461 | if (input) | ||
441 | { | 462 | { |
442 | for (int y = 0; y < VIEW_HEIGHT; y++) | 463 | for (int y = 0; y < VIEW_HEIGHT; y++) |
443 | { | 464 | { |
@@ -451,14 +472,10 @@ int main(int, char**) | |||
451 | } | 472 | } |
452 | } | 473 | } |
453 | 474 | ||
454 | for (int i = 0; i < presses; i++) | 475 | processKeys(map, keystate); |
455 | { | ||
456 | processKeys(map); | ||
457 | } | ||
458 | |||
459 | recalculateLighting(map, fov.get()); | 476 | recalculateLighting(map, fov.get()); |
460 | 477 | ||
461 | if (presses > 0) | 478 | if (input) |
462 | { | 479 | { |
463 | for (int y = 0; y < VIEW_HEIGHT; y++) | 480 | for (int y = 0; y < VIEW_HEIGHT; y++) |
464 | { | 481 | { |
@@ -482,7 +499,7 @@ int main(int, char**) | |||
482 | } | 499 | } |
483 | 500 | ||
484 | render(ren.get(), map, true); | 501 | render(ren.get(), map, true); |
485 | SDL_Delay(10); | 502 | SDL_Delay(50); |
486 | } | 503 | } |
487 | } catch (const sdl_error& ex) | 504 | } catch (const sdl_error& ex) |
488 | { | 505 | { |