HighScoreState.cxx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
  46. SDL_RenderClear(renderer);
  47. SDL_Color const color = {255, 255, 255, SDL_ALPHA_OPAQUE};
  48. render_heading(renderer, width, color);
  49. auto const scores = HighScoreManager::instance().get_scores();
  50. for (auto n = scores.size(); n--;) {
  51. auto const current_height = 180+static_cast<int>(n)*50;
  52. auto const& score = scores[n];
  53. std::string const text = std::to_string(n+1)+" - "+score.player_name_+": "+std::to_string(score.points_);
  54. SDL_Surface* surface = TTF_RenderUTF8_Solid(font, text.c_str(), color);
  55. SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
  56. SDL_Rect textRect;
  57. textRect.w = surface->w;
  58. textRect.h = surface->h;
  59. textRect.x = (width-textRect.w)/2;
  60. textRect.y = current_height;
  61. SDL_RenderCopy(renderer, texture, nullptr, &textRect);
  62. SDL_FreeSurface(surface);
  63. SDL_DestroyTexture(texture);
  64. }
  65. SDL_RenderPresent(renderer);
  66. }
  67. void HighScoreState::render_heading(SDLRenderer& renderer, int const width, SDL_Color const& color)
  68. {
  69. SDL_Surface* headingSurface = TTF_RenderUTF8_Solid(font_,
  70. TranslationManager::instance().get_translation("High Scores").c_str(), color);
  71. SDL_Texture* headingTexture = SDL_CreateTextureFromSurface(renderer, headingSurface);
  72. SDL_Rect heading_rect;
  73. heading_rect.w = headingSurface->w*2;
  74. heading_rect.h = headingSurface->h*2;
  75. heading_rect.x = (width-heading_rect.w)/2;
  76. heading_rect.y = 80;
  77. SDL_RenderCopy(renderer, headingTexture, nullptr, &heading_rect);
  78. SDL_FreeSurface(headingSurface);
  79. SDL_DestroyTexture(headingTexture);
  80. }