title_screen.cpp 9.4 KB

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