💿🐜 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.

161 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/title.hpp"
  21. #include "animation/screen-transition.hpp"
  22. #include "animation/animation.hpp"
  23. #include "animation/animator.hpp"
  24. #include "animation/ease.hpp"
  25. #include "animation/timeline.hpp"
  26. #include "application.hpp"
  27. #include "render/passes/clear-pass.hpp"
  28. namespace game {
  29. namespace state {
  30. namespace splash {
  31. void enter(game::context* ctx)
  32. {
  33. ctx->ui_clear_pass->set_cleared_buffers(true, true, false);
  34. // Load animation timing configuration
  35. double splash_fade_in_duration = 0.0;
  36. double splash_duration = 0.0;
  37. double splash_fade_out_duration = 0.0;
  38. if (ctx->config->contains("splash_fade_in_duration"))
  39. splash_fade_in_duration = (*ctx->config)["splash_fade_in_duration"].get<double>();
  40. if (ctx->config->contains("splash_duration"))
  41. splash_duration = (*ctx->config)["splash_duration"].get<double>();
  42. if (ctx->config->contains("splash_fade_out_duration"))
  43. splash_fade_out_duration = (*ctx->config)["splash_fade_out_duration"].get<double>();
  44. // Build splash fade in animation
  45. ctx->splash_fade_in_animation = new animation<float>();
  46. animation_channel<float>* splash_fade_in_opacity_channel = ctx->splash_fade_in_animation->add_channel(0);
  47. ctx->splash_fade_in_animation->set_interpolator(ease<float>::in_quad);
  48. splash_fade_in_opacity_channel->insert_keyframe({0.0, 0.0f});
  49. splash_fade_in_opacity_channel->insert_keyframe({splash_fade_in_duration, 1.0f});
  50. splash_fade_in_opacity_channel->insert_keyframe({splash_fade_in_duration + splash_duration, 1.0f});
  51. // Build splash fade out animation
  52. ctx->splash_fade_out_animation = new animation<float>();
  53. animation_channel<float>* splash_fade_out_opacity_channel = ctx->splash_fade_out_animation->add_channel(0);
  54. ctx->splash_fade_out_animation->set_interpolator(ease<float>::out_quad);
  55. splash_fade_out_opacity_channel->insert_keyframe({0.0, 1.0f});
  56. splash_fade_out_opacity_channel->insert_keyframe({splash_fade_out_duration, 0.0f});
  57. // Setup animation frame callbacks
  58. auto set_splash_opacity = [ctx](int channel, const float& opacity)
  59. {
  60. static_cast<render::material_property<float4>*>(ctx->splash_billboard_material->get_property("tint"))->set_value(float4{1, 1, 1, opacity});
  61. };
  62. ctx->splash_fade_in_animation->set_frame_callback(set_splash_opacity);
  63. ctx->splash_fade_out_animation->set_frame_callback(set_splash_opacity);
  64. // Reset splash color when animation starts
  65. ctx->splash_fade_in_animation->set_start_callback
  66. (
  67. [ctx]()
  68. {
  69. static_cast<render::material_property<float4>*>(ctx->splash_billboard_material->get_property("tint"))->set_value(float4{1, 1, 1, 0});
  70. ctx->splash_billboard_material->update_tweens();
  71. }
  72. );
  73. // Trigger splash fade out animation when splash fade in animation ends
  74. ctx->splash_fade_in_animation->set_end_callback
  75. (
  76. [ctx]()
  77. {
  78. ctx->splash_fade_out_animation->play();
  79. }
  80. );
  81. // Trigger a state change when the splash fade out animation ends
  82. ctx->splash_fade_out_animation->set_end_callback
  83. (
  84. [ctx]()
  85. {
  86. application::state next_state;
  87. next_state.name = "title";
  88. next_state.enter = std::bind(game::state::title::enter, ctx);
  89. next_state.exit = std::bind(game::state::title::exit, ctx);
  90. ctx->app->queue_state(next_state);
  91. }
  92. );
  93. // Add splash fade animations to animator
  94. ctx->animator->add_animation(ctx->splash_fade_in_animation);
  95. ctx->animator->add_animation(ctx->splash_fade_out_animation);
  96. // Start splash fade in animation
  97. ctx->splash_fade_in_animation->play();
  98. // Set up splash skipper
  99. ctx->input_listener->set_callback
  100. (
  101. [ctx](const event_base& event)
  102. {
  103. auto id = event.get_event_type_id();
  104. if (id != mouse_moved_event::event_type_id && id != mouse_wheel_scrolled_event::event_type_id && id != gamepad_axis_moved_event::event_type_id)
  105. {
  106. // Black out screen
  107. ctx->rasterizer->set_clear_color(0.0f, 0.0f, 0.0f, 1.0f);
  108. ctx->rasterizer->clear_framebuffer(true, false, false);
  109. ctx->app->swap_buffers();
  110. // Change state
  111. application::state next_state;
  112. next_state.name = "title";
  113. next_state.enter = std::bind(game::state::title::enter, ctx);
  114. next_state.exit = std::bind(game::state::title::exit, ctx);
  115. ctx->app->change_state(next_state);
  116. }
  117. }
  118. );
  119. ctx->input_listener->set_enabled(true);
  120. // Add splash billboard to UI scene
  121. ctx->ui_scene->add_object(ctx->splash_billboard);
  122. }
  123. void exit(game::context* ctx)
  124. {
  125. // Remove splash billboard from UI scene
  126. ctx->ui_scene->remove_object(ctx->splash_billboard);
  127. // Disable splash skipper
  128. ctx->input_listener->set_enabled(false);
  129. ctx->input_listener->set_callback(nullptr);
  130. // Destruct splash fade animations
  131. ctx->animator->remove_animation(ctx->splash_fade_in_animation);
  132. ctx->animator->remove_animation(ctx->splash_fade_out_animation);
  133. delete ctx->splash_fade_in_animation;
  134. delete ctx->splash_fade_out_animation;
  135. ctx->splash_fade_in_animation = nullptr;
  136. ctx->splash_fade_out_animation = nullptr;
  137. ctx->ui_clear_pass->set_cleared_buffers(false, true, false);
  138. }
  139. } // namespace splash
  140. } // namespace state
  141. } // namespace game