MenuState.cxx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "MenuState.hxx"
  2. #include "GameStateManager.hxx"
  3. #include "../SDLRenderer.hxx"
  4. void MenuState::on_enter(GameStateManager& gsm)
  5. {
  6. new_game_button_.set_on_click([&gsm] {
  7. if (gsm.parent()!=nullptr)
  8. gsm.pop_state();
  9. gsm.replace_state(GameStates::Game);
  10. });
  11. continue_button_.set_on_click([&gsm] {
  12. gsm.pop_state();
  13. });
  14. quit_button_.set_on_click([&gsm] {
  15. while (gsm.current()!=nullptr) {
  16. gsm.pop_state();
  17. }
  18. });
  19. }
  20. void MenuState::on_event(GameStateManager& gsm, SDL_Event const& evt)
  21. {
  22. if (evt.type==SDL_KEYUP && evt.key.keysym.scancode==SDL_SCANCODE_ESCAPE) {
  23. gsm.pop_state();
  24. }
  25. }
  26. void MenuState::update(GameStateManager& gsm, std::chrono::milliseconds delta_time)
  27. {
  28. (void) delta_time;
  29. continue_button_.set_visible(gsm.parent()!=nullptr);
  30. new_game_button_.update();
  31. continue_button_.update();
  32. quit_button_.update();
  33. }
  34. void MenuState::render(SDLRenderer& renderer)
  35. {
  36. int screen_w, screen_h;
  37. SDL_GetRendererOutputSize(renderer, &screen_w, &screen_h);
  38. int const button_count = continue_button_.is_visible() ? 3 : 2;
  39. int const x = (screen_w-BUTTON_WIDTH)/2;
  40. int const y = (screen_h-(button_count*BUTTON_HEIGHT+(button_count-1)*20))/2;
  41. new_game_button_.move(x, y);
  42. continue_button_.move(x, y+BUTTON_HEIGHT+20);
  43. quit_button_.move(x, y+(button_count-1)*(BUTTON_HEIGHT+20));
  44. SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
  45. SDL_RenderClear(renderer);
  46. new_game_button_.render(renderer);
  47. continue_button_.render(renderer);
  48. quit_button_.render(renderer);
  49. SDL_RenderPresent(renderer);
  50. }