SplashState.cxx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. if (logo_==nullptr) {
  15. // right now this is required here as we need the renderer and the main thread
  16. logo_ = SDL_CreateTextureFromSurface(
  17. renderer,
  18. AssetManager::instance().get_image_asset("logo.jpg")
  19. );
  20. SDL_SetTextureBlendMode(logo_, SDL_BLENDMODE_BLEND);
  21. }
  22. SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
  23. SDL_RenderClear(renderer);
  24. int w, h;
  25. SDL_GetRendererOutputSize(renderer, &w, &h);
  26. if (time_in_state_>=2'000ms && time_in_state_<4'000ms) {
  27. auto const progress = static_cast<float>(time_in_state_.count()-2'000)/2'000.0f;
  28. auto const alpha = static_cast<int>(std::lerp(SDL_ALPHA_TRANSPARENT, SDL_ALPHA_OPAQUE, progress));
  29. SDL_SetTextureAlphaMod(logo_, alpha);
  30. SDL_RenderCopy(renderer, logo_, nullptr, nullptr);
  31. }
  32. else if (time_in_state_>=4'000ms && time_in_state_<9'000ms) {
  33. SDL_SetTextureAlphaMod(logo_, SDL_ALPHA_OPAQUE);
  34. SDL_RenderCopy(renderer, logo_, nullptr, nullptr);
  35. }
  36. else if (time_in_state_>=9'000ms && time_in_state_<11'000ms) {
  37. auto const progress = static_cast<float>(time_in_state_.count()-11'000)/2'000.0f;
  38. auto const alpha = static_cast<int>(std::lerp(SDL_ALPHA_OPAQUE, SDL_ALPHA_TRANSPARENT, progress));
  39. SDL_SetTextureAlphaMod(logo_, alpha);
  40. SDL_RenderCopy(renderer, logo_, nullptr, nullptr);
  41. }
  42. SDL_RenderPresent(renderer);
  43. }
  44. void SplashState::on_enter()
  45. {
  46. time_in_state_ = 0ms;
  47. logo_ = nullptr;
  48. }
  49. void SplashState::on_leave()
  50. {
  51. SDL_DestroyTexture(logo_);
  52. logo_ = nullptr;
  53. }