GameOverState.cxx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "GameOverState.hxx"
  2. #include "GameStateManager.hxx"
  3. #include "TranslationManager.hxx"
  4. #include "HighScoreManager.hxx"
  5. #include "../SDLRenderer.hxx"
  6. #include <regex>
  7. namespace {
  8. std::regex const PLACEHOLDER_REGEX{R"(\{\})"};
  9. }
  10. GameOverState::GameOverState()
  11. :font_{"kenney_pixel.ttf"}
  12. {
  13. }
  14. void GameOverState::on_enter(GameStateManager& gsm)
  15. {
  16. SDL_ShowCursor(SDL_ENABLE);
  17. ok_button_.set_title(TranslationManager::instance().get_translation("OK"));
  18. if (HighScoreManager::instance().has_new_score()) {
  19. name_input_.set_focus(true);
  20. name_input_.set_value("Anon");
  21. name_input_.set_visible(true);
  22. }
  23. else {
  24. name_input_.set_focus(false);
  25. name_input_.set_value("");
  26. name_input_.set_visible(false);
  27. }
  28. ok_button_.set_on_click([&gsm, this] {
  29. HighScoreManager::instance().provide_name_for_new_score(name_input_.value());
  30. gsm.replace_state(GameStates::MainMenu);
  31. });
  32. }
  33. void GameOverState::on_leave()
  34. {
  35. SDL_StopTextInput();
  36. SDL_ShowCursor(SDL_DISABLE);
  37. }
  38. void GameOverState::on_event(GameStateManager& gsm, SDL_Event const& evt)
  39. {
  40. name_input_.on_event(evt);
  41. if (evt.type==SDL_KEYUP) {
  42. switch (evt.key.keysym.scancode) {
  43. default:
  44. break;
  45. case SDL_SCANCODE_RETURN:
  46. HighScoreManager::instance().provide_name_for_new_score(name_input_.value());
  47. [[fallthrough]];
  48. case SDL_SCANCODE_ESCAPE:
  49. gsm.replace_state(GameStates::MainMenu);
  50. break;
  51. }
  52. } else if (evt.type==SDL_CONTROLLERBUTTONUP) {
  53. switch (evt.cbutton.which) {
  54. default:
  55. break;
  56. // TODO: for full controller support, the input must provide an onscreen keyboard
  57. // in that case the controller buttons etc. used to interact with this state will need adjustment
  58. case SDL_CONTROLLER_BUTTON_A:
  59. HighScoreManager::instance().provide_name_for_new_score(name_input_.value());
  60. [[fallthrough]];
  61. case SDL_CONTROLLER_BUTTON_B:
  62. [[fallthrough]];
  63. case SDL_CONTROLLER_BUTTON_START:
  64. gsm.replace_state(GameStates::MainMenu);
  65. break;
  66. }
  67. }
  68. }
  69. void GameOverState::update(GameStateManager& gsm, std::chrono::milliseconds const delta_time)
  70. {
  71. ok_button_.update();
  72. name_input_.update(delta_time);
  73. }
  74. void GameOverState::render(SDLRenderer& renderer)
  75. {
  76. auto const& tm = TranslationManager::instance();
  77. int width, height;
  78. SDL_GetRendererOutputSize(renderer, &width, &height);
  79. int base_y;
  80. auto const& hsm = HighScoreManager::instance();
  81. if (hsm.has_new_score()) {
  82. auto score_text = (tm.get_translation("Congratulations, you made it to the top 10!")
  83. +"\n"+tm.get_translation("You reached {} points!")+"\n"+tm.get_translation("Please enter your name:"));
  84. score_text = std::regex_replace(score_text, PLACEHOLDER_REGEX, std::to_string(hsm.get_new_score()));
  85. SDL_Surface* text_surface = TTF_RenderUTF8_Solid_Wrapped(font_, score_text.c_str(),
  86. {255, 255, 255, SDL_ALPHA_OPAQUE}, 0);
  87. SDL_Texture* text = SDL_CreateTextureFromSurface(renderer, text_surface);
  88. SDL_FreeSurface(text_surface);
  89. int text_width, text_height;
  90. SDL_QueryTexture(text, nullptr, nullptr, &text_width, &text_height);
  91. // some up-scaling
  92. text_width *= 2;
  93. text_height *= 2;
  94. base_y = (height-(text_height+name_input_.get_bounding_box().h+ok_button_.get_bounding_box().h+30))/2;
  95. SDL_Rect render_quad = {(width-text_width)/2, base_y, text_width, text_height};
  96. SDL_RenderCopy(renderer, text, nullptr, &render_quad);
  97. SDL_DestroyTexture(text);
  98. name_input_.move((width-name_input_.get_bounding_box().w)/2, base_y += (text_height+20));
  99. name_input_.render(renderer);
  100. base_y += name_input_.get_bounding_box().h+10;
  101. }
  102. else {
  103. SDL_Surface* text_surface = TTF_RenderUTF8_Solid(font_, tm.get_translation("Game over!").c_str(),
  104. {255, 255, 255, SDL_ALPHA_OPAQUE});
  105. SDL_Texture* text = SDL_CreateTextureFromSurface(renderer, text_surface);
  106. SDL_FreeSurface(text_surface);
  107. int text_width, text_height;
  108. SDL_QueryTexture(text, nullptr, nullptr, &text_width, &text_height);
  109. // some up-scaling
  110. text_width *= 2;
  111. text_height *= 2;
  112. base_y = (height-(text_height+ok_button_.get_bounding_box().h+20))/2;
  113. SDL_Rect render_quad = {(width-text_width)/2, base_y, text_width, text_height};
  114. SDL_RenderCopy(renderer, text, nullptr, &render_quad);
  115. SDL_DestroyTexture(text);
  116. base_y += text_height+20;
  117. }
  118. ok_button_.move((width-ok_button_.get_bounding_box().w)/2, base_y);
  119. ok_button_.render(renderer);
  120. }