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

114 lines
3.5 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/ease.hpp"
  23. #include "animation/timeline.hpp"
  24. #include "application.hpp"
  25. #include "render/passes/clear-pass.hpp"
  26. namespace game {
  27. namespace state {
  28. namespace splash {
  29. void enter(game::context* ctx)
  30. {
  31. ctx->ui_clear_pass->set_cleared_buffers(true, true, false);
  32. // Add splash billboard to UI scene
  33. ctx->ui_scene->add_object(ctx->splash_billboard);
  34. // Setup timing
  35. const float splash_fade_in_duration = 0.5f;
  36. const float splash_hang_duration = 2.0f;
  37. const float splash_fade_out_duration = 0.5f;
  38. // Start fade in
  39. ctx->fade_transition->transition(splash_fade_in_duration, true, ease<float>::in_quad);
  40. // Crate fade out function
  41. auto fade_out = [ctx, splash_fade_out_duration]()
  42. {
  43. ctx->fade_transition->transition(splash_fade_out_duration, false, ease<float>::out_quad);
  44. };
  45. // Create change state function
  46. auto change_state = [ctx]()
  47. {
  48. application::state next_state;
  49. next_state.name = "title";
  50. next_state.enter = std::bind(game::state::title::enter, ctx);
  51. next_state.exit = std::bind(game::state::title::exit, ctx);
  52. ctx->app->change_state(next_state);
  53. };
  54. // Schedule fade out and change state events
  55. timeline* timeline = ctx->timeline;
  56. float t = timeline->get_position();
  57. timeline::sequence splash_sequence =
  58. {
  59. {t + splash_fade_in_duration + splash_hang_duration, fade_out},
  60. {t + splash_fade_in_duration + splash_hang_duration + splash_fade_out_duration, change_state}
  61. };
  62. timeline->add_sequence(splash_sequence);
  63. // Set up splash skipper
  64. ctx->input_listener->set_callback
  65. (
  66. [ctx](const event_base& event)
  67. {
  68. auto id = event.get_event_type_id();
  69. if (id != mouse_moved_event::event_type_id && id != mouse_wheel_scrolled_event::event_type_id && id != gamepad_axis_moved_event::event_type_id)
  70. {
  71. ctx->timeline->clear();
  72. ctx->fade_transition->get_animation()->stop();
  73. ctx->rasterizer->set_clear_color(0.0f, 0.0f, 0.0f, 1.0f);
  74. ctx->rasterizer->clear_framebuffer(true, false, false);
  75. ctx->app->swap_buffers();
  76. application::state next_state;
  77. next_state.name = "title";
  78. next_state.enter = std::bind(game::state::title::enter, ctx);
  79. next_state.exit = std::bind(game::state::title::exit, ctx);
  80. ctx->app->change_state(next_state);
  81. }
  82. }
  83. );
  84. ctx->input_listener->set_enabled(true);
  85. }
  86. void exit(game::context* ctx)
  87. {
  88. // Disable splash skipper
  89. ctx->input_listener->set_enabled(false);
  90. ctx->input_listener->set_callback(nullptr);
  91. // Remove splash billboard from UI scene
  92. ctx->ui_scene->remove_object(ctx->splash_billboard);
  93. ctx->ui_clear_pass->set_cleared_buffers(false, true, false);
  94. }
  95. } // namespace splash
  96. } // namespace state
  97. } // namespace game