CreditsState.cxx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "CreditsState.hxx"
  2. #include "GameStateManager.hxx"
  3. #include <algorithm>
  4. #include <numeric>
  5. #include <type_traits>
  6. CreditsState::CreditsState()
  7. :font_{"kenney_pixel.ttf"}, jetbrains_ai_logo_{"jetbrains-ai-logo.png"}, kenney_logo_{"kenney-logo.png"},
  8. sdl_logo_{"SDL_logo.png"}, boost_logo_{"Boost-logo.png"}
  9. {
  10. }
  11. void CreditsState::on_enter(GameStateManager& gsm)
  12. {
  13. TTF_Font* const font = font_;
  14. scroll_y_ = 0.0;
  15. done_ = false;
  16. scroll_items_ = {
  17. "Copyright © 2024, Felix Bytow <drako@drako.guru>",
  18. External{jetbrains_ai_logo_, "with some help from JetBrains AI Assistant"},
  19. External{kenney_logo_, "Font & UI Pack from kenney.nl"},
  20. sdl_logo_,
  21. boost_logo_,
  22. };
  23. int summed_size = -ITEM_PADDING;
  24. for (auto const& item: scroll_items_) {
  25. summed_size += std::visit([font]<typename T>(T const& it) {
  26. int w, h = 0;
  27. if constexpr (std::is_same_v<T, char const*>) {
  28. TTF_SizeUTF8(font, it, &w, &h);
  29. }
  30. else if constexpr (std::is_same_v<T, External>) {
  31. int logo_h, text_h;
  32. TTF_SizeUTF8(font, it.text_, &w, &text_h);
  33. SDL_QueryTexture(it.texture_, nullptr, nullptr, &w, &logo_h);
  34. h = logo_h+INNER_ITEM_PADDING+text_h;
  35. }
  36. else if constexpr (std::is_same_v<T, SDL_Texture*>) {
  37. SDL_QueryTexture(it, nullptr, nullptr, &w, &h);
  38. }
  39. return h;
  40. }, item);
  41. summed_size += ITEM_PADDING;
  42. }
  43. scroll_size_ = static_cast<double>(summed_size);
  44. }
  45. void CreditsState::on_event(GameStateManager& gsm, SDL_Event const& event)
  46. {
  47. if (event.type==SDL_KEYUP) {
  48. switch (event.key.keysym.scancode) {
  49. default:
  50. break;
  51. case SDL_SCANCODE_ESCAPE:
  52. [[fallthrough]];
  53. case SDL_SCANCODE_RETURN:
  54. [[fallthrough]];
  55. case SDL_SCANCODE_SPACE:
  56. gsm.pop_state();
  57. break;
  58. }
  59. }
  60. }
  61. void CreditsState::update(GameStateManager& gsm, std::chrono::milliseconds const delta_time)
  62. {
  63. if (done_) {
  64. gsm.pop_state();
  65. }
  66. scroll_y_ += 0.05*static_cast<double>(delta_time.count());
  67. }
  68. void CreditsState::render(SDLRenderer& renderer)
  69. {
  70. TTF_Font* const font = font_;
  71. SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
  72. SDL_RenderClear(renderer);
  73. int window_width = 0, window_height = 0;
  74. SDL_GetRendererOutputSize(renderer, &window_width, &window_height);
  75. int y = window_height-static_cast<int>(scroll_y_);
  76. if (y<=-static_cast<int>(scroll_size_)) {
  77. // everything is now outside the screen at the top
  78. done_ = true;
  79. }
  80. for (auto const& item: scroll_items_) {
  81. std::visit([&renderer, &y, window_width, font]<typename T>(T const& it) {
  82. int h = 0;
  83. if constexpr (std::is_same_v<T, char const*>) {
  84. auto const surface = TTF_RenderUTF8_Solid(font, it, {255, 255, 255, SDL_ALPHA_OPAQUE});
  85. auto const texture = SDL_CreateTextureFromSurface(renderer, surface);
  86. SDL_FreeSurface(surface);
  87. int w;
  88. SDL_QueryTexture(texture, nullptr, nullptr, &w, &h);
  89. SDL_Rect const rect{.x = (window_width-w)/2, .y = y, .w = w, .h = h};
  90. SDL_RenderCopy(renderer, texture, nullptr, &rect);
  91. SDL_DestroyTexture(texture);
  92. }
  93. else if constexpr (std::is_same_v<T, External>) {
  94. int w, logo_h, text_h;
  95. SDL_QueryTexture(it.texture_, nullptr, nullptr, &w, &logo_h);
  96. SDL_Rect const logo_rect{.x = (window_width-w)/2, .y = y, .w = w, .h = logo_h};
  97. SDL_RenderCopy(renderer, it.texture_, nullptr, &logo_rect);
  98. auto const surface = TTF_RenderUTF8_Solid(font, it.text_, {255, 255, 255, SDL_ALPHA_OPAQUE});
  99. auto const texture = SDL_CreateTextureFromSurface(renderer, surface);
  100. SDL_FreeSurface(surface);
  101. SDL_QueryTexture(texture, nullptr, nullptr, &w, &text_h);
  102. SDL_Rect const text_rect{.x = (window_width-w)/2, .y = y+logo_h+INNER_ITEM_PADDING, .w = w, .h = text_h};
  103. SDL_RenderCopy(renderer, texture, nullptr, &text_rect);
  104. SDL_DestroyTexture(texture);
  105. h = logo_h+INNER_ITEM_PADDING+text_h;
  106. }
  107. else if constexpr (std::is_same_v<T, SDL_Texture*>) {
  108. int w;
  109. SDL_QueryTexture(it, nullptr, nullptr, &w, &h);
  110. SDL_Rect const rect{.x = (window_width-w)/2, .y = y, .w = w, .h = h};
  111. SDL_RenderCopy(renderer, it, nullptr, &rect);
  112. }
  113. y += h+ITEM_PADDING;
  114. }, item);
  115. }
  116. SDL_RenderPresent(renderer);
  117. }