💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
5.7 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "game/states/splash.hpp"
  20. #include "game/states/main-menu.hpp"
  21. #include "animation/screen-transition.hpp"
  22. #include "animation/animation.hpp"
  23. #include "animation/animator.hpp"
  24. #include "animation/ease.hpp"
  25. #include "application.hpp"
  26. #include "render/passes/clear-pass.hpp"
  27. namespace game {
  28. namespace state {
  29. namespace splash {
  30. void enter(game::context* ctx)
  31. {
  32. ctx->ui_clear_pass->set_cleared_buffers(true, true, false);
  33. // Load animation timing configuration
  34. double splash_fade_in_duration = 0.0;
  35. double splash_duration = 0.0;
  36. double splash_fade_out_duration = 0.0;
  37. if (ctx->config->contains("splash_fade_in_duration"))
  38. splash_fade_in_duration = (*ctx->config)["splash_fade_in_duration"].get<double>();
  39. if (ctx->config->contains("splash_duration"))
  40. splash_duration = (*ctx->config)["splash_duration"].get<double>();
  41. if (ctx->config->contains("splash_fade_out_duration"))
  42. splash_fade_out_duration = (*ctx->config)["splash_fade_out_duration"].get<double>();
  43. // Build splash fade in animation
  44. ctx->splash_fade_in_animation = new animation<float>();
  45. animation_channel<float>* splash_fade_in_opacity_channel = ctx->splash_fade_in_animation->add_channel(0);
  46. ctx->splash_fade_in_animation->set_interpolator(ease<float>::out_cubic);
  47. splash_fade_in_opacity_channel->insert_keyframe({0.0, 0.0f});
  48. splash_fade_in_opacity_channel->insert_keyframe({splash_fade_in_duration, 1.0f});
  49. splash_fade_in_opacity_channel->insert_keyframe({splash_fade_in_duration + splash_duration, 1.0f});
  50. // Build splash fade out animation
  51. ctx->splash_fade_out_animation = new animation<float>();
  52. animation_channel<float>* splash_fade_out_opacity_channel = ctx->splash_fade_out_animation->add_channel(0);
  53. ctx->splash_fade_out_animation->set_interpolator(ease<float>::out_cubic);
  54. splash_fade_out_opacity_channel->insert_keyframe({0.0, 1.0f});
  55. splash_fade_out_opacity_channel->insert_keyframe({splash_fade_out_duration, 0.0f});
  56. // Setup animation frame callbacks
  57. auto set_splash_opacity = [ctx](int channel, const float& opacity)
  58. {
  59. static_cast<render::material_property<float4>*>(ctx->splash_billboard_material->get_property("tint"))->set_value(float4{1, 1, 1, opacity});
  60. };
  61. ctx->splash_fade_in_animation->set_frame_callback(set_splash_opacity);
  62. ctx->splash_fade_out_animation->set_frame_callback(set_splash_opacity);
  63. // Reset splash color when animation starts
  64. ctx->splash_fade_in_animation->set_start_callback
  65. (
  66. [ctx]()
  67. {
  68. static_cast<render::material_property<float4>*>(ctx->splash_billboard_material->get_property("tint"))->set_value(float4{1, 1, 1, 0});
  69. ctx->splash_billboard_material->update_tweens();
  70. }
  71. );
  72. // Trigger splash fade out animation when splash fade in animation ends
  73. ctx->splash_fade_in_animation->set_end_callback
  74. (
  75. [ctx]()
  76. {
  77. ctx->splash_fade_out_animation->play();
  78. }
  79. );
  80. // Trigger a state change when the splash fade out animation ends
  81. ctx->splash_fade_out_animation->set_end_callback
  82. (
  83. [ctx]()
  84. {
  85. application::state next_state;
  86. next_state.name = "main_menu";
  87. next_state.enter = std::bind(game::state::main_menu::enter, ctx, true);
  88. next_state.exit = std::bind(game::state::main_menu::exit, ctx);
  89. ctx->app->queue_state(next_state);
  90. }
  91. );
  92. // Add splash fade animations to animator
  93. ctx->animator->add_animation(ctx->splash_fade_in_animation);
  94. ctx->animator->add_animation(ctx->splash_fade_out_animation);
  95. // Start splash fade in animation
  96. ctx->splash_fade_in_animation->play();
  97. // Set up splash skipper
  98. ctx->input_listener->set_callback
  99. (
  100. [ctx](const event_base& event)
  101. {
  102. auto id = event.get_event_type_id();
  103. if (id != mouse_moved_event::event_type_id && id != mouse_wheel_scrolled_event::event_type_id && id != gamepad_axis_moved_event::event_type_id)
  104. {
  105. // Black out screen
  106. ctx->rasterizer->set_clear_color(0.0f, 0.0f, 0.0f, 1.0f);
  107. ctx->rasterizer->clear_framebuffer(true, false, false);
  108. ctx->app->swap_buffers();
  109. // Change state
  110. application::state next_state;
  111. next_state.name = "main_menu";
  112. next_state.enter = std::bind(game::state::main_menu::enter, ctx, true);
  113. next_state.exit = std::bind(game::state::main_menu::exit, ctx);
  114. ctx->app->queue_state(next_state);
  115. }
  116. }
  117. );
  118. ctx->input_listener->set_enabled(true);
  119. // Add splash billboard to UI scene
  120. ctx->ui_scene->add_object(ctx->splash_billboard);
  121. }
  122. void exit(game::context* ctx)
  123. {
  124. // Remove splash billboard from UI scene
  125. ctx->ui_scene->remove_object(ctx->splash_billboard);
  126. // Disable splash skipper
  127. ctx->input_listener->set_enabled(false);
  128. ctx->input_listener->set_callback(nullptr);
  129. // Destruct splash fade animations
  130. ctx->animator->remove_animation(ctx->splash_fade_in_animation);
  131. ctx->animator->remove_animation(ctx->splash_fade_out_animation);
  132. delete ctx->splash_fade_in_animation;
  133. delete ctx->splash_fade_out_animation;
  134. ctx->splash_fade_in_animation = nullptr;
  135. ctx->splash_fade_out_animation = nullptr;
  136. ctx->ui_clear_pass->set_cleared_buffers(false, true, false);
  137. }
  138. } // namespace splash
  139. } // namespace state
  140. } // namespace game