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

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