HighScoreState.cxx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "HighScoreState.hxx"
  2. #include "HighScoreManager.hxx"
  3. #include "GameStateManager.hxx"
  4. #include "TranslationManager.hxx"
  5. #include "../SDLRenderer.hxx"
  6. HighScoreState::HighScoreState()
  7. :font_{"kenney_pixel.ttf"}
  8. {
  9. }
  10. void HighScoreState::on_event(GameStateManager& gsm, SDL_Event const& event)
  11. {
  12. if (event.type==SDL_KEYUP) {
  13. switch (event.key.keysym.scancode) {
  14. default:
  15. break;
  16. case SDL_SCANCODE_ESCAPE:
  17. [[fallthrough]];
  18. case SDL_SCANCODE_SPACE:
  19. [[fallthrough]];
  20. case SDL_SCANCODE_RETURN:
  21. gsm.pop_state();
  22. break;
  23. }
  24. }
  25. else if (event.type==SDL_CONTROLLERBUTTONUP) {
  26. switch (event.cbutton.which) {
  27. default:
  28. break;
  29. case SDL_CONTROLLER_BUTTON_A:
  30. [[fallthrough]];
  31. case SDL_CONTROLLER_BUTTON_B:
  32. [[fallthrough]];
  33. case SDL_CONTROLLER_BUTTON_START:
  34. gsm.pop_state();
  35. break;
  36. }
  37. }
  38. }
  39. void HighScoreState::render(SDLRenderer& renderer)
  40. {
  41. TTF_Font* const font = font_;
  42. SDL_Rect viewport;
  43. SDL_RenderGetViewport(renderer, &viewport);
  44. int width = viewport.w;
  45. SDL_Color const color = {255, 255, 255, SDL_ALPHA_OPAQUE};
  46. render_heading(renderer, width, color);
  47. auto const scores = HighScoreManager::instance().get_scores();
  48. for (auto n = scores.size(); n--;) {
  49. auto const current_height = 180+static_cast<int>(n)*50;
  50. auto const& score = scores[n];
  51. std::string const text = std::to_string(n+1)+" - "+score.player_name_+": "+std::to_string(score.points_);
  52. SDL_Surface* surface = TTF_RenderUTF8_Solid(font, text.c_str(), color);
  53. SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
  54. SDL_Rect textRect;
  55. textRect.w = surface->w;
  56. textRect.h = surface->h;
  57. textRect.x = (width-textRect.w)/2;
  58. textRect.y = current_height;
  59. SDL_RenderCopy(renderer, texture, nullptr, &textRect);
  60. SDL_FreeSurface(surface);
  61. SDL_DestroyTexture(texture);
  62. }
  63. }
  64. void HighScoreState::render_heading(SDLRenderer& renderer, int const width, SDL_Color const& color)
  65. {
  66. SDL_Surface* headingSurface = TTF_RenderUTF8_Solid(font_,
  67. TranslationManager::instance().get_translation("High Scores").c_str(), color);
  68. SDL_Texture* headingTexture = SDL_CreateTextureFromSurface(renderer, headingSurface);
  69. SDL_Rect heading_rect;
  70. heading_rect.w = headingSurface->w*2;
  71. heading_rect.h = headingSurface->h*2;
  72. heading_rect.x = (width-heading_rect.w)/2;
  73. heading_rect.y = 80;
  74. SDL_RenderCopy(renderer, headingTexture, nullptr, &heading_rect);
  75. SDL_FreeSurface(headingSurface);
  76. SDL_DestroyTexture(headingTexture);
  77. }