SplashState.cxx 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "SplashState.hxx"
  2. #include "AssetManager.hxx"
  3. #include "GameStateManager.hxx"
  4. using namespace std::chrono_literals;
  5. void SplashState::on_event(GameStateManager& gsm, SDL_Event const& evt)
  6. {
  7. if (evt.type==SDL_KEYUP) {
  8. switch (evt.key.keysym.scancode) {
  9. default:
  10. break;
  11. case SDL_SCANCODE_SPACE:
  12. [[fallthrough]];
  13. case SDL_SCANCODE_RETURN:
  14. [[fallthrough]];
  15. case SDL_SCANCODE_ESCAPE:
  16. gsm.replace_state(GameStates::MainMenu);
  17. break;
  18. }
  19. }
  20. }
  21. void SplashState::update(GameStateManager& gsm, std::chrono::milliseconds delta_time)
  22. {
  23. time_in_state_ += delta_time;
  24. if (time_in_state_>13'000ms)
  25. gsm.replace_state(GameStates::MainMenu);
  26. }
  27. void SplashState::render(SDLRenderer& renderer)
  28. {
  29. SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
  30. SDL_RenderClear(renderer);
  31. if (time_in_state_>=2'000ms && time_in_state_<11'000ms) {
  32. if (time_in_state_<4'000ms) {
  33. auto const progress = static_cast<float>(time_in_state_.count()-2'000)/2'000.0f;
  34. auto const alpha = static_cast<int>(std::lerp(SDL_ALPHA_TRANSPARENT, SDL_ALPHA_OPAQUE, progress));
  35. SDL_SetTextureAlphaMod(logo_, alpha);
  36. }
  37. else if (time_in_state_>=4'000ms && time_in_state_<9'000ms) {
  38. SDL_SetTextureAlphaMod(logo_, SDL_ALPHA_OPAQUE);
  39. }
  40. else if (time_in_state_>=9'000ms) {
  41. auto const progress = static_cast<float>(time_in_state_.count()-9'000)/2'000.0f;
  42. auto const alpha = static_cast<int>(std::lerp(SDL_ALPHA_OPAQUE, SDL_ALPHA_TRANSPARENT, progress));
  43. SDL_SetTextureAlphaMod(logo_, alpha);
  44. }
  45. int screen_w, screen_h;
  46. SDL_GetRendererOutputSize(renderer, &screen_w, &screen_h);
  47. int logo_w, logo_h;
  48. SDL_QueryTexture(logo_, nullptr, nullptr, &logo_w, &logo_h);
  49. float const logo_aspect = static_cast<float>(logo_w)/static_cast<float>(logo_h);
  50. float const screen_aspect = static_cast<float>(screen_w-20)/static_cast<float>(screen_h-20);
  51. int put_w, put_h;
  52. if (logo_aspect>screen_aspect) {
  53. put_w = (screen_w-20);
  54. put_h = static_cast<int>(static_cast<float>(put_w)/logo_aspect);
  55. }
  56. else {
  57. put_h = screen_h-20;
  58. put_w = static_cast<int>(static_cast<float>(put_h)*logo_aspect);
  59. }
  60. SDL_Rect const logo_rect = {
  61. .x = (screen_w-put_w)/2,
  62. .y = (screen_h-put_h)/2,
  63. .w = put_w,
  64. .h = put_h
  65. };
  66. SDL_RenderCopy(renderer, logo_, nullptr, &logo_rect);
  67. }
  68. SDL_RenderPresent(renderer);
  69. }
  70. void SplashState::on_enter(GameStateManager& gsm)
  71. {
  72. auto& am = AssetManager::instance();
  73. time_in_state_ = 0ms;
  74. logo_ = am.get_texture_asset("logo.jpg");
  75. SDL_SetTextureBlendMode(logo_, SDL_BLENDMODE_BLEND);
  76. SDL_SetWindowIcon(gsm.window(), am.get_surface_asset("snake-icon.png"));
  77. }