CreditsState.cxx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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"}
  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. };
  20. int summed_size = -ITEM_PADDING;
  21. for (auto const& item: scroll_items_) {
  22. summed_size += std::visit([font]<typename T>(T const& it) {
  23. int w, h = 0;
  24. if constexpr (std::is_same_v<T, char const*>) {
  25. TTF_SizeUTF8(font, it, &w, &h);
  26. }
  27. else if constexpr (std::is_same_v<T, External>) {
  28. int logo_h, text_h;
  29. TTF_SizeUTF8(font, it.text_, &w, &text_h);
  30. SDL_QueryTexture(it.texture_, nullptr, nullptr, &w, &logo_h);
  31. h = logo_h+INNER_ITEM_PADDING+text_h;
  32. }
  33. else if constexpr (std::is_same_v<T, SDL_Texture*>) {
  34. SDL_QueryTexture(it, nullptr, nullptr, &w, &h);
  35. }
  36. return h;
  37. }, item);
  38. summed_size += ITEM_PADDING;
  39. }
  40. scroll_size_ = static_cast<double>(summed_size);
  41. }
  42. void CreditsState::on_event(GameStateManager& gsm, SDL_Event const& event)
  43. {
  44. if (event.type==SDL_KEYUP) {
  45. switch (event.key.keysym.scancode) {
  46. default:
  47. break;
  48. case SDL_SCANCODE_ESCAPE:
  49. [[fallthrough]];
  50. case SDL_SCANCODE_RETURN:
  51. [[fallthrough]];
  52. case SDL_SCANCODE_SPACE:
  53. gsm.pop_state();
  54. break;
  55. }
  56. }
  57. else if (event.type==SDL_CONTROLLERBUTTONUP) {
  58. switch (event.cbutton.which) {
  59. default:
  60. break;
  61. case SDL_CONTROLLER_BUTTON_A:
  62. [[fallthrough]];
  63. case SDL_CONTROLLER_BUTTON_B:
  64. [[fallthrough]];
  65. case SDL_CONTROLLER_BUTTON_START:
  66. gsm.pop_state();
  67. break;
  68. }
  69. }
  70. }
  71. void CreditsState::update(GameStateManager& gsm, std::chrono::milliseconds const delta_time)
  72. {
  73. if (done_) {
  74. gsm.pop_state();
  75. }
  76. scroll_y_ += 0.05*static_cast<double>(delta_time.count());
  77. }
  78. void CreditsState::render(SDLRenderer& renderer)
  79. {
  80. TTF_Font* const font = font_;
  81. int window_width = 0, window_height = 0;
  82. SDL_GetRendererOutputSize(renderer, &window_width, &window_height);
  83. int y = window_height-static_cast<int>(scroll_y_);
  84. if (y<=-static_cast<int>(scroll_size_)) {
  85. // everything is now outside the screen at the top
  86. done_ = true;
  87. }
  88. for (auto const& item: scroll_items_) {
  89. std::visit([&renderer, &y, window_width, font]<typename T>(T const& it) {
  90. int h = 0;
  91. if constexpr (std::is_same_v<T, char const*>) {
  92. auto const surface = TTF_RenderUTF8_Solid(font, it, {255, 255, 255, SDL_ALPHA_OPAQUE});
  93. auto const texture = SDL_CreateTextureFromSurface(renderer, surface);
  94. SDL_FreeSurface(surface);
  95. int w;
  96. SDL_QueryTexture(texture, nullptr, nullptr, &w, &h);
  97. SDL_Rect const rect{.x = (window_width-w)/2, .y = y, .w = w, .h = h};
  98. SDL_RenderCopy(renderer, texture, nullptr, &rect);
  99. SDL_DestroyTexture(texture);
  100. }
  101. else if constexpr (std::is_same_v<T, External>) {
  102. int w, logo_h, text_h;
  103. SDL_QueryTexture(it.texture_, nullptr, nullptr, &w, &logo_h);
  104. SDL_Rect const logo_rect{.x = (window_width-w)/2, .y = y, .w = w, .h = logo_h};
  105. SDL_RenderCopy(renderer, it.texture_, nullptr, &logo_rect);
  106. auto const surface = TTF_RenderUTF8_Solid(font, it.text_, {255, 255, 255, SDL_ALPHA_OPAQUE});
  107. auto const texture = SDL_CreateTextureFromSurface(renderer, surface);
  108. SDL_FreeSurface(surface);
  109. SDL_QueryTexture(texture, nullptr, nullptr, &w, &text_h);
  110. SDL_Rect const text_rect{.x = (window_width-w)/2, .y = y+logo_h+INNER_ITEM_PADDING, .w = w, .h = text_h};
  111. SDL_RenderCopy(renderer, texture, nullptr, &text_rect);
  112. SDL_DestroyTexture(texture);
  113. h = logo_h+INNER_ITEM_PADDING+text_h;
  114. }
  115. else if constexpr (std::is_same_v<T, SDL_Texture*>) {
  116. int w;
  117. SDL_QueryTexture(it, nullptr, nullptr, &w, &h);
  118. SDL_Rect const rect{.x = (window_width-w)/2, .y = y, .w = w, .h = h};
  119. SDL_RenderCopy(renderer, it, nullptr, &rect);
  120. }
  121. y += h+ITEM_PADDING;
  122. }, item);
  123. }
  124. }