1// SuperTux
2// Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
3// Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4//
5// This program is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18#include "supertux/title_screen.hpp"
19
20#include <version.h>
21
22#include "gui/menu_manager.hpp"
23#include "object/camera.hpp"
24#include "object/music_object.hpp"
25#include "object/player.hpp"
26#include "supertux/fadetoblack.hpp"
27#include "supertux/game_session.hpp"
28#include "supertux/level.hpp"
29#include "supertux/menu/menu_storage.hpp"
30#include "supertux/resources.hpp"
31#include "supertux/screen_manager.hpp"
32#include "supertux/sector.hpp"
33#include "video/compositor.hpp"
34#include "video/drawing_context.hpp"
35#include "video/surface.hpp"
36#include "video/video_system.hpp"
37
38TitleScreen::TitleScreen(Savegame& savegame) :
39 m_frame(Surface::from_file("images/engine/menu/frame.png")),
40 m_controller(new CodeController()),
41 m_titlesession(new GameSession("levels/misc/menu.stl", savegame)),
42 m_copyright_text("SuperTux " PACKAGE_VERSION "\n" +
43 _("Copyright") + " (c) 2003-2018 SuperTux Devel Team\n" +
44 _("This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to\n"
45 "redistribute it under certain conditions; see the license file for details.\n"
46 )),
47 m_videosystem_name(VideoSystem::current()->get_name())
48{
49 Player& player = m_titlesession->get_current_sector().get_player();
50 player.set_controller(m_controller.get());
51 player.set_speedlimit(230); //MAX_WALK_XM
52}
53
54void
55TitleScreen::make_tux_jump()
56{
57 static bool jumpWasReleased = true;
58 Sector& sector = m_titlesession->get_current_sector();
59 Player& tux = sector.get_player();
60
61 m_controller->update();
62 m_controller->press(Control::RIGHT);
63
64 // Check if we should press the jump button
65 Rectf lookahead = tux.get_bbox();
66 lookahead.set_right(lookahead.get_right() + 96);
67 bool pathBlocked = !sector.is_free_of_statics(lookahead);
68 if ((pathBlocked && jumpWasReleased) || !tux.on_ground()) {
69 m_controller->press(Control::JUMP);
70 jumpWasReleased = false;
71 } else {
72 jumpWasReleased = true;
73 }
74
75 // Wrap around at the end of the level back to the beginning
76 if (sector.get_width() - 320 < tux.get_pos().x) {
77 sector.activate("main");
78 sector.get_camera().reset(tux.get_pos());
79 }
80}
81
82TitleScreen::~TitleScreen()
83{
84}
85
86void
87TitleScreen::setup()
88{
89 Sector& sector = m_titlesession->get_current_sector();
90 if (Sector::current() != &sector) {
91 auto& music = sector.get_singleton_by_type<MusicObject>();
92 music.play_music(LEVEL_MUSIC);
93 sector.activate(sector.get_player().get_pos());
94 }
95
96 MenuManager::instance().set_menu(MenuStorage::MAIN_MENU);
97 ScreenManager::current()->set_screen_fade(std::make_unique<FadeToBlack>(FadeToBlack::FADEIN, 0.25));
98}
99
100void
101TitleScreen::leave()
102{
103 Sector& sector = m_titlesession->get_current_sector();
104 sector.deactivate();
105 MenuManager::instance().clear_menu_stack();
106}
107
108void
109TitleScreen::draw(Compositor& compositor)
110{
111 auto& context = compositor.make_context();
112
113 Sector& sector = m_titlesession->get_current_sector();
114 sector.draw(context);
115
116 context.color().draw_surface_scaled(m_frame,
117 Rectf(0, 0, static_cast<float>(context.get_width()), static_cast<float>(context.get_height())),
118 LAYER_FOREGROUND1);
119
120 context.color().draw_text(Resources::small_font,
121 m_copyright_text,
122 Vector(5.0f, static_cast<float>(context.get_height()) - 50.0f),
123 ALIGN_LEFT, LAYER_FOREGROUND1);
124
125 context.color().draw_text(Resources::small_font,
126 m_videosystem_name,
127 Vector(static_cast<float>(context.get_width()) - 5.0f,
128 static_cast<float>(context.get_height()) - 14.0f),
129 ALIGN_RIGHT, LAYER_FOREGROUND1);
130}
131
132void
133TitleScreen::update(float dt_sec, const Controller& controller)
134{
135 ScreenManager::current()->set_speed(0.6f);
136 Sector& sector = m_titlesession->get_current_sector();
137 sector.update(dt_sec);
138
139 BIND_SECTOR(sector);
140 make_tux_jump();
141
142 // reopen menu if user closed it (so that the app doesn't close when user
143 // accidently hit ESC)
144 if (!MenuManager::instance().is_active() && !ScreenManager::current()->has_pending_fadeout())
145 {
146 MenuManager::instance().set_menu(MenuStorage::MAIN_MENU);
147 }
148}
149
150/* EOF */
151