SplashState.cxx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. if (time_in_state_<4'000ms) {
  18. auto const progress = static_cast<float>(time_in_state_.count()-2'000)/2'000.0f;
  19. auto const alpha = static_cast<int>(std::lerp(SDL_ALPHA_TRANSPARENT, SDL_ALPHA_OPAQUE, progress));
  20. SDL_SetTextureAlphaMod(logo_, alpha);
  21. }
  22. else if (time_in_state_>=4'000ms && time_in_state_<9'000ms) {
  23. SDL_SetTextureAlphaMod(logo_, SDL_ALPHA_OPAQUE);
  24. }
  25. else if (time_in_state_>=9'000ms) {
  26. auto const progress = static_cast<float>(time_in_state_.count()-9'000)/2'000.0f;
  27. auto const alpha = static_cast<int>(std::lerp(SDL_ALPHA_OPAQUE, SDL_ALPHA_TRANSPARENT, progress));
  28. SDL_SetTextureAlphaMod(logo_, alpha);
  29. }
  30. int screen_w, screen_h;
  31. SDL_GetRendererOutputSize(renderer, &screen_w, &screen_h);
  32. int logo_w, logo_h;
  33. SDL_QueryTexture(logo_, nullptr, nullptr, &logo_w, &logo_h);
  34. float const logo_aspect = static_cast<float>(logo_w)/static_cast<float>(logo_h);
  35. float const screen_aspect = static_cast<float>(screen_w-20)/static_cast<float>(screen_h-20);
  36. int put_w, put_h;
  37. if (logo_aspect>screen_aspect) {
  38. put_w = (screen_w-20);
  39. put_h = static_cast<int>(static_cast<float>(put_w)/logo_aspect);
  40. }
  41. else {
  42. put_h = screen_h-20;
  43. put_w = static_cast<int>(static_cast<float>(put_h)*logo_aspect);
  44. }
  45. SDL_Rect const logo_rect = {
  46. .x = (screen_w-put_w)/2,
  47. .y = (screen_h-put_h)/2,
  48. .w = put_w,
  49. .h = put_h
  50. };
  51. SDL_RenderCopy(renderer, logo_, nullptr, &logo_rect);
  52. }
  53. SDL_RenderPresent(renderer);
  54. }
  55. void SplashState::on_enter()
  56. {
  57. time_in_state_ = 0ms;
  58. logo_ = AssetManager::instance().get_texture_asset("logo.jpg");
  59. SDL_SetTextureBlendMode(logo_, SDL_BLENDMODE_BLEND);
  60. }