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

183 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. #include "math/linear-algebra.hpp"
  32. #include <iostream>
  33. namespace game {
  34. namespace state {
  35. splash::splash(game::context& ctx):
  36. game::state::base(ctx)
  37. {
  38. ctx.logger->push_task("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_flags(MATERIAL_FLAG_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. double splash_fade_in_duration = 0.0;
  61. double splash_duration = 0.0;
  62. double splash_fade_out_duration = 0.0;
  63. if (ctx.config->contains("splash_fade_in_duration"))
  64. splash_fade_in_duration = (*ctx.config)["splash_fade_in_duration"].get<double>();
  65. if (ctx.config->contains("splash_duration"))
  66. splash_duration = (*ctx.config)["splash_duration"].get<double>();
  67. if (ctx.config->contains("splash_fade_out_duration"))
  68. splash_fade_out_duration = (*ctx.config)["splash_fade_out_duration"].get<double>();
  69. // Construct splash fade in animation
  70. splash_fade_in_animation.set_interpolator(ease<float>::out_cubic);
  71. animation_channel<float>* splash_fade_in_opacity_channel = splash_fade_in_animation.add_channel(0);
  72. splash_fade_in_opacity_channel->insert_keyframe({0.0, 0.0f});
  73. splash_fade_in_opacity_channel->insert_keyframe({splash_fade_in_duration, 1.0f});
  74. splash_fade_in_opacity_channel->insert_keyframe({splash_fade_in_duration + splash_duration, 1.0f});
  75. // Build splash fade out animation
  76. splash_fade_out_animation.set_interpolator(ease<float>::out_cubic);
  77. animation_channel<float>* splash_fade_out_opacity_channel = splash_fade_out_animation.add_channel(0);
  78. splash_fade_out_opacity_channel->insert_keyframe({0.0, 1.0f});
  79. splash_fade_out_opacity_channel->insert_keyframe({splash_fade_out_duration, 0.0f});
  80. // Setup animation frame callbacks
  81. auto set_splash_opacity = [splash_tint](int channel, const float& opacity)
  82. {
  83. splash_tint->set_value(float4{1, 1, 1, opacity});
  84. };
  85. splash_fade_in_animation.set_frame_callback(set_splash_opacity);
  86. splash_fade_out_animation.set_frame_callback(set_splash_opacity);
  87. // Trigger splash fade out animation when splash fade in animation ends
  88. splash_fade_in_animation.set_end_callback
  89. (
  90. [this]()
  91. {
  92. this->splash_fade_out_animation.play();
  93. }
  94. );
  95. // Trigger a state change when the splash fade out animation ends
  96. splash_fade_out_animation.set_end_callback
  97. (
  98. [&ctx]()
  99. {
  100. // Queue change to main menu state
  101. ctx.function_queue.push
  102. (
  103. [&ctx]()
  104. {
  105. ctx.state_machine.pop();
  106. ctx.state_machine.emplace(new game::state::main_menu(ctx, true));
  107. }
  108. );
  109. }
  110. );
  111. // Add splash fade animations to animator
  112. ctx.animator->add_animation(&splash_fade_in_animation);
  113. ctx.animator->add_animation(&splash_fade_out_animation);
  114. // Start splash fade in animation
  115. splash_fade_in_animation.play();
  116. // Set up splash skipper
  117. ctx.input_listener->set_callback
  118. (
  119. [&ctx](const event_base& event)
  120. {
  121. auto id = event.get_event_type_id();
  122. if (id != mouse_moved_event::event_type_id && id != mouse_wheel_scrolled_event::event_type_id && id != gamepad_axis_moved_event::event_type_id)
  123. {
  124. // Black out screen
  125. ctx.rasterizer->set_clear_color(0.0f, 0.0f, 0.0f, 1.0f);
  126. ctx.rasterizer->clear_framebuffer(true, false, false);
  127. ctx.app->swap_buffers();
  128. // Change to main menu state
  129. ctx.state_machine.pop();
  130. ctx.state_machine.emplace(new game::state::main_menu(ctx, true));
  131. }
  132. }
  133. );
  134. ctx.input_listener->set_enabled(true);
  135. ctx.logger->pop_task(EXIT_SUCCESS);
  136. }
  137. splash::~splash()
  138. {
  139. ctx.logger->push_task("Exiting splash state");
  140. // Disable splash skipper
  141. ctx.input_listener->set_enabled(false);
  142. ctx.input_listener->set_callback(nullptr);
  143. // Remove splash fade animations from animator
  144. ctx.animator->remove_animation(&splash_fade_in_animation);
  145. ctx.animator->remove_animation(&splash_fade_out_animation);
  146. // Remove splash billboard from UI scene
  147. ctx.ui_scene->remove_object(&splash_billboard);
  148. // Unload splash texture
  149. ctx.resource_manager->unload("splash.tex");
  150. // Disable color buffer clearing in UI pass
  151. ctx.ui_clear_pass->set_cleared_buffers(false, true, false);
  152. ctx.logger->pop_task(EXIT_SUCCESS);
  153. }
  154. } // namespace state
  155. } // namespace game