Cleaned up MenuManager some more, some ownership issues remain, so things will crash...
[supertux.git] / src / supertux / title_screen.cpp
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 "audio/sound_manager.hpp"
21 #include "gui/menu.hpp"
22 #include "gui/menu_manager.hpp"
23 #include "lisp/parser.hpp"
24 #include "object/camera.hpp"
25 #include "object/player.hpp"
26 #include "supertux/fadeout.hpp"
27 #include "supertux/gameconfig.hpp"
28 #include "supertux/globals.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 "supertux/textscroller.hpp"
34 #include "supertux/world.hpp"
35 #include "util/file_system.hpp"
36 #include "util/gettext.hpp"
37 #include "util/reader.hpp"
38 #include "video/drawing_context.hpp"
39
40 #include <sstream>
41 #include <version.h>
42
43 TitleScreen::TitleScreen(PlayerStatus* player_status) :
44   frame(),
45   controller(),
46   titlesession()
47 {
48   controller.reset(new CodeController());
49   titlesession.reset(new GameSession("levels/misc/menu.stl", player_status));
50
51   Player* player = titlesession->get_current_sector()->player;
52   player->set_controller(controller.get());
53   player->set_speedlimit(230); //MAX_WALK_XM
54
55   frame = Surface::create("images/engine/menu/frame.png");
56   copyright_text = "SuperTux " PACKAGE_VERSION "\n" +
57     _("Copyright") + " (c) 2003-2014 SuperTux Devel Team\n" +
58     _("This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to\n"
59       "redistribute it under certain conditions; see the file COPYING for details.\n"
60    );
61 }
62
63 std::string
64 TitleScreen::get_level_name(const std::string& filename)
65 {
66   try {
67     lisp::Parser parser;
68     const lisp::Lisp* root = parser.parse(filename);
69
70     const lisp::Lisp* level = root->get_lisp("supertux-level");
71     if(!level)
72       return "";
73
74     std::string name;
75     level->get("name", name);
76     return name;
77   } catch(std::exception& e) {
78     log_warning << "Problem getting name of '" << filename << "': "
79                 << e.what() << std::endl;
80     return "";
81   }
82 }
83
84 void
85 TitleScreen::make_tux_jump()
86 {
87   static bool jumpWasReleased = true;
88   Sector* sector  = titlesession->get_current_sector();
89   Player* tux = sector->player;
90
91   controller->update();
92   controller->press(Controller::RIGHT);
93
94   // Check if we should press the jump button
95   Rectf lookahead = tux->get_bbox();
96   lookahead.p2.x += 96;
97   bool pathBlocked = !sector->is_free_of_statics(lookahead);
98   if ((pathBlocked && jumpWasReleased) || !tux->on_ground()) {
99     controller->press(Controller::JUMP);
100     jumpWasReleased = false;
101   } else {
102     jumpWasReleased = true;
103   }
104
105   // Wrap around at the end of the level back to the beginning
106   if(sector->get_width() - 320 < tux->get_pos().x) {
107     sector->activate("main");
108     sector->camera->reset(tux->get_pos());
109   }
110 }
111
112 TitleScreen::~TitleScreen()
113 {
114 }
115
116 void
117 TitleScreen::setup()
118 {
119   Sector* sector = titlesession->get_current_sector();
120   if(Sector::current() != sector) {
121     sector->play_music(LEVEL_MUSIC);
122     sector->activate(sector->player->get_pos());
123   }
124
125   MenuManager::instance().set_menu(MenuStorage::MAIN_MENU);
126 }
127
128 void
129 TitleScreen::leave()
130 {
131   Sector* sector = titlesession->get_current_sector();
132   sector->deactivate();
133   MenuManager::instance().clear_menu_stack();
134 }
135
136 void
137 TitleScreen::draw(DrawingContext& context)
138 {
139   Sector* sector  = titlesession->get_current_sector();
140   sector->draw(context);
141
142   // FIXME: Add something to scale the frame to the resolution of the screen
143   //context.draw_surface(frame, Vector(0,0),LAYER_FOREGROUND1);
144
145   context.draw_text(Resources::small_font,
146                     copyright_text,
147                     Vector(5, SCREEN_HEIGHT - 50),
148                     ALIGN_LEFT, LAYER_FOREGROUND1);
149 }
150
151 void
152 TitleScreen::update(float elapsed_time)
153 {
154   g_screen_manager->set_speed(0.6f);
155   Sector* sector  = titlesession->get_current_sector();
156   sector->update(elapsed_time);
157
158   make_tux_jump();
159
160   if (Menu* menu = MenuManager::instance().current())
161   {
162     menu->check_menu();
163   }
164
165   // reopen menu if user closed it (so that the app doesn't close when user
166   // accidently hit ESC)
167   if(!MenuManager::instance().is_active() && g_screen_manager->has_no_pending_fadeout())
168   {
169     MenuManager::instance().set_menu(MenuStorage::MAIN_MENU);
170   }
171 }
172
173 void
174 TitleScreen::start_game(std::unique_ptr<World> world)
175 {
176   MenuManager::instance().clear_menu_stack();
177
178   std::string basename = world->get_basedir();
179   basename = basename.substr(0, basename.length()-1);
180   std::string worlddirname = FileSystem::basename(basename);
181   std::ostringstream stream;
182   stream << "profile" << g_config->profile << "/" << worlddirname << ".stsg";
183   std::string slotfile = stream.str();
184
185   try
186   {
187     world->set_savegame_filename(slotfile);
188     world->run();
189   }
190   catch(std::exception& e)
191   {
192     log_fatal << "Couldn't start world: " << e.what() << std::endl;
193   }
194 }
195
196 /* EOF */