title_screen.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. // SuperTux
  2. // Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
  3. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  4. // 2023 Vankata453
  5. //
  6. // This program 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. // This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
  18. #include "supertux/title_screen.hpp"
  19. #include <version.h>
  20. #include "gui/menu_manager.hpp"
  21. #include "object/camera.hpp"
  22. #include "object/music_object.hpp"
  23. #include "object/player.hpp"
  24. #include "sdk/integration.hpp"
  25. #include "supertux/gameconfig.hpp"
  26. #include "supertux/game_session.hpp"
  27. #include "supertux/globals.hpp"
  28. #include "supertux/level.hpp"
  29. #include "supertux/menu/menu_storage.hpp"
  30. #include "supertux/profile.hpp"
  31. #include "supertux/profile_manager.hpp"
  32. #include "supertux/resources.hpp"
  33. #include "supertux/savegame.hpp"
  34. #include "supertux/screen_manager.hpp"
  35. #include "supertux/sector.hpp"
  36. #include "supertux/world.hpp"
  37. #include "video/compositor.hpp"
  38. #include "video/drawing_context.hpp"
  39. #include "video/surface.hpp"
  40. #include "video/video_system.hpp"
  41. static const std::string DEFAULT_TITLE_LEVEL = "levels/misc/menu.stl";
  42. static const std::string TITLE_MUSIC = "music/misc/theme.music";
  43. static const std::string CHRISTMAS_TITLE_MUSIC = "music/misc/christmas_theme.music";
  44. TitleScreen::TitleScreen(Savegame& savegame, bool christmas) :
  45. m_savegame(savegame),
  46. m_christmas(christmas),
  47. m_logo(Surface::from_file("images/engine/menu/" + std::string(LOGO_FILE))),
  48. m_santahat(christmas ? Surface::from_file("images/engine/menu/logo_santahat.png") : nullptr),
  49. m_frame(Surface::from_file("images/engine/menu/frame.png")),
  50. m_controller(new CodeController()),
  51. m_titlesession(),
  52. m_copyright_text(),
  53. m_videosystem_name(VideoSystem::current()->get_name()),
  54. m_jump_was_released(false)
  55. {
  56. refresh_copyright_text();
  57. }
  58. void
  59. TitleScreen::setup()
  60. {
  61. refresh_level();
  62. MenuManager::instance().set_menu(MenuStorage::MAIN_MENU);
  63. ScreenManager::current()->set_screen_fade(std::make_unique<FadeToBlack>(FadeToBlack::FADEIN, 0.25f));
  64. }
  65. void
  66. TitleScreen::leave()
  67. {
  68. m_titlesession->get_current_sector().deactivate();
  69. MenuManager::instance().clear_menu_stack();
  70. }
  71. void
  72. TitleScreen::refresh_level()
  73. {
  74. bool level_init = false;
  75. if (g_config->custom_title_levels)
  76. {
  77. /** Check the savegame of the previously entered world on the current profile.
  78. If there is a title screen level set, use it. Otherwise, look for such level in the world's properties.
  79. If a level still isn't specified, or it cannot be entered, use the default. */
  80. std::string title_level;
  81. const std::string last_world = ProfileManager::current()->get_current_profile().get_last_world();
  82. if (!last_world.empty())
  83. {
  84. const std::string savegame_title_level = Savegame::from_current_profile(last_world, true)->get_player_status().title_level;
  85. if (savegame_title_level.empty())
  86. {
  87. const auto world = World::from_directory("levels/" + last_world);
  88. if (world)
  89. title_level = world->get_title_level();
  90. }
  91. else
  92. {
  93. title_level = savegame_title_level;
  94. }
  95. }
  96. if (title_level.empty())
  97. title_level = DEFAULT_TITLE_LEVEL;
  98. /** Attempt to switch to the custom title level, using the default one as a fallback. */
  99. if (!m_titlesession || m_titlesession->get_level_file() != title_level)
  100. {
  101. std::unique_ptr<GameSession> new_session;
  102. try
  103. {
  104. new_session = std::make_unique<GameSession>(title_level, m_savegame, nullptr, true);
  105. }
  106. catch (const std::exception& err)
  107. {
  108. log_warning << "Error loading custom title screen level '" << title_level << "': " << err.what() << std::endl;
  109. if (!m_titlesession || m_titlesession->get_level_file() != DEFAULT_TITLE_LEVEL)
  110. new_session = std::make_unique<GameSession>(DEFAULT_TITLE_LEVEL, m_savegame, nullptr, true);
  111. }
  112. if (new_session)
  113. {
  114. m_titlesession = std::move(new_session);
  115. level_init = true;
  116. }
  117. }
  118. }
  119. else if (!m_titlesession || m_titlesession->get_level_file() != DEFAULT_TITLE_LEVEL)
  120. {
  121. m_titlesession = std::make_unique<GameSession>(DEFAULT_TITLE_LEVEL, m_savegame, nullptr, true);
  122. level_init = true;
  123. }
  124. /** Initialize the main sector. */
  125. Sector& sector = m_titlesession->get_current_sector();
  126. if (level_init || Sector::current() != &sector)
  127. setup_sector(sector);
  128. }
  129. void
  130. TitleScreen::setup_sector(Sector& sector)
  131. {
  132. auto& music = sector.get_singleton_by_type<MusicObject>();
  133. music.set_music(m_christmas ? CHRISTMAS_TITLE_MUSIC : TITLE_MUSIC);
  134. music.resume_music(true);
  135. Player& player = *(sector.get_players()[0]);
  136. // sector.activate(Vector) expects position calculated for big tux, but tux
  137. // might be small on the title screen
  138. sector.activate(player.get_pos() - Vector(0.f, player.is_big() ? 0.f : 32.f));
  139. player.set_controller(m_controller.get());
  140. player.set_speedlimit(230.f);
  141. }
  142. void
  143. TitleScreen::draw(Compositor& compositor)
  144. {
  145. auto& context = compositor.make_context();
  146. m_titlesession->get_current_sector().draw(context);
  147. context.color().draw_surface(m_logo,
  148. Vector(context.get_width() / 2 - static_cast<float>(m_logo->get_width()) / 2,
  149. context.get_height() / 2 - static_cast<float>(m_logo->get_height()) / 2 - 200.f),
  150. LAYER_GUI + 1);
  151. if (m_christmas)
  152. {
  153. context.color().draw_surface(m_santahat,
  154. Vector(context.get_width() / 2 - static_cast<float>(m_santahat->get_width()) / 2 + 35.f,
  155. context.get_height() / 2 - static_cast<float>(m_santahat->get_height()) / 2 - 255.f),
  156. LAYER_GUI + 2);
  157. }
  158. context.color().draw_surface_scaled(m_frame, context.get_rect(), LAYER_GUI + 3);
  159. context.color().draw_text(Resources::small_font,
  160. m_copyright_text,
  161. Vector(5.0f, context.get_height() - 50.0f),
  162. ALIGN_LEFT, LAYER_GUI + 4);
  163. context.color().draw_text(Resources::small_font,
  164. m_videosystem_name,
  165. Vector(context.get_width() - 5.0f,
  166. context.get_height() - 14.0f),
  167. ALIGN_RIGHT, LAYER_GUI + 4);
  168. }
  169. void
  170. TitleScreen::update(float dt_sec, const Controller& controller)
  171. {
  172. ScreenManager::current()->set_speed(0.6f);
  173. update_level(dt_sec);
  174. // Re-open main menu, if it was closed
  175. if (!MenuManager::instance().is_active() && !ScreenManager::current()->has_pending_fadeout())
  176. MenuManager::instance().set_menu(MenuStorage::MAIN_MENU);
  177. }
  178. void
  179. TitleScreen::update_level(float dt_sec)
  180. {
  181. using RaycastResult = CollisionSystem::RaycastResult;
  182. Sector& sector = m_titlesession->get_current_sector();
  183. Player& player = *(sector.get_players()[0]);
  184. // Restart level if Tux is dying
  185. if (player.is_dying())
  186. {
  187. m_titlesession->restart_level(true, true);
  188. setup_sector(m_titlesession->get_current_sector());
  189. return;
  190. }
  191. BIND_SECTOR(sector);
  192. sector.update(dt_sec);
  193. m_controller->update();
  194. m_controller->press(Control::RIGHT);
  195. // Check if we should press the jump button
  196. const Rectf& bbox = player.get_bbox();
  197. const Vector eye(bbox.get_right(), bbox.get_top() + bbox.get_height() / 2);
  198. const Vector end(eye.x + 46.f, eye.y);
  199. RaycastResult result = sector.get_first_line_intersection(eye, end, false, player.get_collision_object());
  200. bool shouldjump = result.is_valid;
  201. if (shouldjump)
  202. {
  203. if (auto tile = std::get_if<const Tile*>(&result.hit))
  204. shouldjump = !(*tile)->is_slope();
  205. else if (auto obj = std::get_if<CollisionObject*>(&result.hit))
  206. shouldjump = ((*obj)->get_group() == COLGROUP_STATIC ||
  207. (*obj)->get_group() == COLGROUP_MOVING_STATIC);
  208. }
  209. if (player.m_fall_mode == Player::FallMode::JUMPING ||
  210. (m_jump_was_released && shouldjump))
  211. {
  212. m_controller->press(Control::JUMP);
  213. m_jump_was_released = false;
  214. }
  215. else
  216. {
  217. m_jump_was_released = true;
  218. }
  219. // Wrap around at the end of the level back to the beginning
  220. if (sector.get_width() - 320.f < player.get_pos().x)
  221. {
  222. sector.activate("main");
  223. sector.get_camera().reset(player.get_pos());
  224. }
  225. }
  226. void
  227. TitleScreen::refresh_copyright_text()
  228. {
  229. // cppcheck-suppress unknownMacro
  230. m_copyright_text = "SuperTux " PACKAGE_VERSION "\n" +
  231. _("Copyright") + " (c) 2003-2024 SuperTux Devel Team\n" +
  232. _("This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to\n"
  233. "redistribute it under certain conditions; see the license file for details.\n");
  234. }
  235. void
  236. TitleScreen::set_frame(const std::string& image)
  237. {
  238. m_frame = Surface::from_file(image);
  239. }
  240. IntegrationStatus
  241. TitleScreen::get_status() const
  242. {
  243. IntegrationStatus status;
  244. status.m_details.push_back("In main menu");
  245. return status;
  246. }
  247. /* EOF */