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

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