SplashState.cxx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "SplashState.hxx"
  2. #include "AssetManager.hxx"
  3. #include "GameStateManager.hxx"
  4. using namespace std::chrono_literals;
  5. void SplashState::update(GameStateManager& gsm, std::chrono::milliseconds delta_time)
  6. {
  7. time_in_state_ += delta_time;
  8. auto const key_state = SDL_GetKeyboardState(nullptr);
  9. if (time_in_state_ > 13'000ms || key_state[SDL_SCANCODE_SPACE] || key_state[SDL_SCANCODE_RETURN])
  10. gsm.replace_state(GameStates::MainMenu);
  11. }
  12. void SplashState::render(SDLRenderer& renderer)
  13. {
  14. SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
  15. SDL_RenderClear(renderer);
  16. if (time_in_state_ >= 2'000ms && time_in_state_ < 11'000ms)
  17. {
  18. if (time_in_state_ < 4'000ms)
  19. {
  20. auto const progress = static_cast<float>(time_in_state_.count() - 2'000) / 2'000.0f;
  21. auto const alpha = static_cast<int>(std::lerp(SDL_ALPHA_TRANSPARENT, SDL_ALPHA_OPAQUE, progress));
  22. SDL_SetTextureAlphaMod(logo_, alpha);
  23. }
  24. else if (time_in_state_ >= 4'000ms && time_in_state_ < 9'000ms)
  25. {
  26. SDL_SetTextureAlphaMod(logo_, SDL_ALPHA_OPAQUE);
  27. }
  28. else if (time_in_state_ >= 9'000ms)
  29. {
  30. auto const progress = static_cast<float>(time_in_state_.count() - 9'000) / 2'000.0f;
  31. auto const alpha = static_cast<int>(std::lerp(SDL_ALPHA_OPAQUE, SDL_ALPHA_TRANSPARENT, progress));
  32. SDL_SetTextureAlphaMod(logo_, alpha);
  33. }
  34. int screen_w, screen_h;
  35. SDL_GetRendererOutputSize(renderer, &screen_w, &screen_h);
  36. int logo_w, logo_h;
  37. SDL_QueryTexture(logo_, nullptr, nullptr, &logo_w, &logo_h);
  38. float const logo_aspect = static_cast<float>(logo_w) / static_cast<float>(logo_h);
  39. float const screen_aspect = static_cast<float>(screen_w - 20) / static_cast<float>(screen_h - 20);
  40. int put_w, put_h;
  41. if (logo_aspect > screen_aspect) {
  42. put_w = (screen_w - 20);
  43. put_h = static_cast<int>(static_cast<float>(put_w) / logo_aspect);
  44. } else {
  45. put_h = screen_h - 20;
  46. put_w = static_cast<int>(static_cast<float>(put_h) * logo_aspect);
  47. }
  48. SDL_Rect const logo_rect = {
  49. .x = (screen_w - put_w) / 2,
  50. .y = (screen_h - put_h) / 2,
  51. .w = put_w,
  52. .h = put_h
  53. };
  54. SDL_RenderCopy(renderer, logo_, nullptr, &logo_rect);
  55. }
  56. SDL_RenderPresent(renderer);
  57. }
  58. void SplashState::on_enter()
  59. {
  60. time_in_state_ = 0ms;
  61. logo_ = AssetManager::instance().get_texture_asset("logo.jpg");
  62. SDL_SetTextureBlendMode(logo_, SDL_BLENDMODE_BLEND);
  63. }