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

138 lines
4.9 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/title.hpp"
  20. #include "game/states/main-menu.hpp"
  21. #include "animation/screen-transition.hpp"
  22. #include "animation/ease.hpp"
  23. #include "animation/timeline.hpp"
  24. #include "application.hpp"
  25. #include "scene/text.hpp"
  26. #include "configuration.hpp"
  27. #include "render/passes/clear-pass.hpp"
  28. namespace game {
  29. namespace state {
  30. namespace title {
  31. void enter(game::context* ctx)
  32. {
  33. ctx->ui_clear_pass->set_cleared_buffers(true, true, false);
  34. // Setup timing
  35. const float title_fade_in_duration = 0.5f;
  36. const float title_fade_out_duration = 0.5f;
  37. // Start fade in
  38. ctx->fade_transition->transition(title_fade_in_duration, true, ease<float>::in_quad);
  39. // Crate fade out function
  40. auto fade_out = [ctx, title_fade_out_duration]()
  41. {
  42. ctx->fade_transition->transition(title_fade_out_duration, false, ease<float>::out_quad);
  43. };
  44. // Create change state function
  45. auto change_state = [ctx]()
  46. {
  47. application::state next_state;
  48. next_state.name = "main_menu";
  49. next_state.enter = std::bind(game::state::main_menu::enter, ctx);
  50. next_state.exit = std::bind(game::state::main_menu::exit, ctx);
  51. ctx->app->change_state(next_state);
  52. };
  53. // Set up title skipper
  54. ctx->input_listener->set_callback
  55. (
  56. [ctx](const event_base& event)
  57. {
  58. auto id = event.get_event_type_id();
  59. if (id != mouse_moved_event::event_type_id && id != mouse_wheel_scrolled_event::event_type_id && id != gamepad_axis_moved_event::event_type_id)
  60. {
  61. ctx->timeline->clear();
  62. ctx->fade_transition->get_animation()->stop();
  63. ctx->fade_transition->get_billboard()->set_active(false);
  64. ctx->rasterizer->set_clear_color(0.0f, 0.0f, 0.0f, 1.0f);
  65. ctx->rasterizer->clear_framebuffer(true, false, false);
  66. ctx->app->swap_buffers();
  67. application::state next_state;
  68. next_state.name = "main_menu";
  69. next_state.enter = std::bind(game::state::main_menu::enter, ctx);
  70. next_state.exit = std::bind(game::state::main_menu::exit, ctx);
  71. ctx->app->change_state(next_state);
  72. }
  73. }
  74. );
  75. ctx->input_listener->set_enabled(true);
  76. // Construct title text
  77. ctx->title_text = new scene::text();
  78. ctx->title_text->set_material(&ctx->title_font_material);
  79. ctx->title_text->set_font(&ctx->title_font);
  80. ctx->title_text->set_color({1.0f, 1.0f, 1.0f, 1.0f});
  81. ctx->title_text->set_content((*ctx->strings)["title"]);
  82. ctx->ui_scene->add_object(ctx->title_text);
  83. // Construct version string text
  84. ctx->title_version_text = new scene::text();
  85. ctx->title_version_text->set_material(&ctx->debug_font_material);
  86. ctx->title_version_text->set_font(&ctx->debug_font);
  87. ctx->title_version_text->set_color({1.0f, 1.0f, 1.0f, 1.0f});
  88. ctx->title_version_text->set_content(ANTKEEPER_VERSION_STRING);
  89. ctx->ui_scene->add_object(ctx->title_version_text);
  90. // Align title text
  91. const auto& title_aabb = static_cast<const geom::aabb<float>&>(ctx->title_text->get_local_bounds());
  92. float title_w = title_aabb.max_point.x - title_aabb.min_point.x;
  93. float title_h = title_aabb.max_point.y - title_aabb.min_point.y;
  94. ctx->title_text->set_translation({std::round(-title_w * 0.5f), std::round(-title_h * 0.5f), 0.0f});
  95. // Align version string
  96. const auto& version_aabb = static_cast<const geom::aabb<float>&>(ctx->title_version_text->get_local_bounds());
  97. float version_w = version_aabb.max_point.x - version_aabb.min_point.x;
  98. float version_h = version_aabb.max_point.y - version_aabb.min_point.y;
  99. const float version_padding = 12.0f;
  100. auto viewport = ctx->app->get_viewport_dimensions();
  101. ctx->title_version_text->set_translation({viewport[0] * 0.5f - version_w - version_padding, -viewport[1] * 0.5f + version_padding, 0.0f});
  102. }
  103. void exit(game::context* ctx)
  104. {
  105. // Remove and destruct title and version text
  106. ctx->ui_scene->remove_object(ctx->title_text);
  107. ctx->ui_scene->remove_object(ctx->title_version_text);
  108. delete ctx->title_text;
  109. delete ctx->title_version_text;
  110. ctx->title_text = nullptr;
  111. ctx->title_version_text = nullptr;
  112. // Disable title skipper
  113. ctx->input_listener->set_enabled(false);
  114. ctx->input_listener->set_callback(nullptr);
  115. ctx->ui_clear_pass->set_cleared_buffers(false, true, false);
  116. }
  117. } // namespace title
  118. } // namespace state
  119. } // namespace game