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

188 lines
6.2 KiB

  1. /*
  2. * Copyright (C) 2023 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/log.hpp"
  29. #include "resources/resource-manager.hpp"
  30. #include "render/material-flags.hpp"
  31. #include "math/linear-algebra.hpp"
  32. namespace game {
  33. namespace state {
  34. splash::splash(game::context& ctx):
  35. game::state::base(ctx),
  36. skipped(false)
  37. {
  38. debug::log::trace("Entering splash state...");
  39. // Enable color buffer clearing in UI pass
  40. ctx.ui_clear_pass->set_cleared_buffers(true, true, false);
  41. // Load splash texture
  42. const gl::texture_2d* splash_texture = ctx.resource_manager->load<gl::texture_2d>("splash.tex");
  43. // Get splash texture dimensions
  44. auto splash_dimensions = splash_texture->get_dimensions();
  45. // Construct splash billboard material
  46. splash_billboard_material.set_blend_mode(render::blend_mode::translucent);
  47. splash_billboard_material.set_shader_program(ctx.resource_manager->load<gl::shader_program>("ui-element-textured.glsl"));
  48. splash_billboard_material.add_property<const gl::texture_2d*>("background")->set_value(splash_texture);
  49. render::material_property<float4>* splash_tint = splash_billboard_material.add_property<float4>("tint");
  50. splash_tint->set_value(float4{1, 1, 1, 0});
  51. splash_billboard_material.update_tweens();
  52. // Construct splash billboard
  53. splash_billboard.set_material(&splash_billboard_material);
  54. splash_billboard.set_scale({(float)std::get<0>(splash_dimensions) * 0.5f, (float)std::get<1>(splash_dimensions) * 0.5f, 1.0f});
  55. splash_billboard.set_translation({0.0f, 0.0f, 0.0f});
  56. splash_billboard.update_tweens();
  57. // Add splash billboard to UI scene
  58. ctx.ui_scene->add_object(&splash_billboard);
  59. // Load animation timing configuration
  60. const double splash_fade_in_duration = 0.5;
  61. const double splash_duration = 2.0;
  62. const double splash_fade_out_duration = 0.5;
  63. // Construct splash fade in animation
  64. splash_fade_in_animation.set_interpolator(ease<float>::out_cubic);
  65. animation_channel<float>* splash_fade_in_opacity_channel = splash_fade_in_animation.add_channel(0);
  66. splash_fade_in_opacity_channel->insert_keyframe({0.0, 0.0f});
  67. splash_fade_in_opacity_channel->insert_keyframe({splash_fade_in_duration, 1.0f});
  68. splash_fade_in_opacity_channel->insert_keyframe({splash_fade_in_duration + splash_duration, 1.0f});
  69. // Build splash fade out animation
  70. splash_fade_out_animation.set_interpolator(ease<float>::out_cubic);
  71. animation_channel<float>* splash_fade_out_opacity_channel = splash_fade_out_animation.add_channel(0);
  72. splash_fade_out_opacity_channel->insert_keyframe({0.0, 1.0f});
  73. splash_fade_out_opacity_channel->insert_keyframe({splash_fade_out_duration, 0.0f});
  74. // Setup animation frame callbacks
  75. auto set_splash_opacity = [splash_tint](int channel, const float& opacity)
  76. {
  77. splash_tint->set_value(float4{1, 1, 1, opacity});
  78. };
  79. splash_fade_in_animation.set_frame_callback(set_splash_opacity);
  80. splash_fade_out_animation.set_frame_callback(set_splash_opacity);
  81. // Trigger splash fade out animation when splash fade in animation ends
  82. splash_fade_in_animation.set_end_callback
  83. (
  84. [this]()
  85. {
  86. this->splash_fade_out_animation.play();
  87. }
  88. );
  89. // Trigger a state change when the splash fade out animation ends
  90. splash_fade_out_animation.set_end_callback
  91. (
  92. [&ctx]()
  93. {
  94. // Queue change to main menu state
  95. ctx.function_queue.push
  96. (
  97. [&ctx]()
  98. {
  99. ctx.state_machine.pop();
  100. ctx.state_machine.emplace(new game::state::main_menu(ctx, true));
  101. }
  102. );
  103. }
  104. );
  105. // Add splash fade animations to animator
  106. ctx.animator->add_animation(&splash_fade_in_animation);
  107. ctx.animator->add_animation(&splash_fade_out_animation);
  108. // Start splash fade in animation
  109. splash_fade_in_animation.play();
  110. // Set up splash skipper
  111. input_mapped_subscription = ctx.input_mapper.get_input_mapped_channel().subscribe
  112. (
  113. [this](const auto& event)
  114. {
  115. auto mapping_type = event.mapping->get_mapping_type();
  116. if (!this->skipped &&
  117. mapping_type != input::mapping_type::gamepad_axis &&
  118. mapping_type != input::mapping_type::mouse_motion &&
  119. mapping_type != input::mapping_type::mouse_scroll)
  120. {
  121. this->skipped = true;
  122. this->ctx.function_queue.emplace
  123. (
  124. [&ctx = this->ctx]()
  125. {
  126. // Black out screen
  127. ctx.rasterizer->set_clear_color(0.0f, 0.0f, 0.0f, 1.0f);
  128. ctx.rasterizer->clear_framebuffer(true, false, false);
  129. ctx.app->swap_buffers();
  130. // Change to main menu state
  131. ctx.state_machine.pop();
  132. ctx.state_machine.emplace(new game::state::main_menu(ctx, true));
  133. }
  134. );
  135. }
  136. }
  137. );
  138. ctx.input_mapper.connect(ctx.app->get_device_manager().get_event_queue());
  139. debug::log::trace("Entered splash state");
  140. }
  141. splash::~splash()
  142. {
  143. debug::log::trace("Exiting splash state...");
  144. // Disable splash skipper
  145. ctx.input_mapper.disconnect();
  146. // Remove splash fade animations from animator
  147. ctx.animator->remove_animation(&splash_fade_in_animation);
  148. ctx.animator->remove_animation(&splash_fade_out_animation);
  149. // Remove splash billboard from UI scene
  150. ctx.ui_scene->remove_object(&splash_billboard);
  151. // Unload splash texture
  152. ctx.resource_manager->unload("splash.tex");
  153. // Disable color buffer clearing in UI pass
  154. ctx.ui_clear_pass->set_cleared_buffers(false, true, false);
  155. debug::log::trace("Exited splash state");
  156. }
  157. } // namespace state
  158. } // namespace game