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

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