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

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