CreditsState.cxx 4.2 KB

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