Renamed MainLoop to ScreenManager
[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_manager.hpp"
22 #include "lisp/parser.hpp"
23 #include "object/camera.hpp"
24 #include "object/player.hpp"
25 #include "supertux/fadeout.hpp"
26 #include "supertux/gameconfig.hpp"
27 #include "supertux/globals.hpp"
28 #include "supertux/screen_manager.hpp"
29 #include "supertux/menu/addon_menu.hpp"
30 #include "supertux/menu/contrib_world_menu.hpp"
31 #include "supertux/menu/contrib_menu.hpp"
32 #include "supertux/menu/main_menu.hpp"
33 #include "supertux/resources.hpp"
34 #include "supertux/sector.hpp"
35 #include "supertux/textscroller.hpp"
36 #include "supertux/world.hpp"
37 #include "util/file_system.hpp"
38 #include "util/gettext.hpp"
39 #include "util/reader.hpp"
40 #include "video/drawing_context.hpp"
41
42 TitleScreen::TitleScreen() :
43   main_menu(),
44   contrib_menu(),
45   contrib_world_menu(),
46   addons_menu(),
47   main_world(),
48   contrib_worlds(),
49   current_world(),
50   frame(),
51   controller(),
52   titlesession()
53 {
54   controller.reset(new CodeController());
55   titlesession.reset(new GameSession("levels/misc/menu.stl"));
56
57   Player* player = titlesession->get_current_sector()->player;
58   player->set_controller(controller.get());
59   player->set_speedlimit(230); //MAX_WALK_XM
60
61   generate_main_menu();
62
63   frame = std::auto_ptr<Surface>(new Surface("images/engine/menu/frame.png"));
64 }
65
66 std::string
67 TitleScreen::get_level_name(const std::string& filename)
68 {
69   try {
70     lisp::Parser parser;
71     const lisp::Lisp* root = parser.parse(filename);
72
73     const lisp::Lisp* level = root->get_lisp("supertux-level");
74     if(!level)
75       return "";
76
77     std::string name;
78     level->get("name", name);
79     return name;
80   } catch(std::exception& e) {
81     log_warning << "Problem getting name of '" << filename << "': "
82                 << e.what() << std::endl;
83     return "";
84   }
85 }
86
87 void
88 TitleScreen::check_levels_contrib_menu()
89 {
90   current_world = contrib_menu->get_current_world();
91   
92   if (current_world)
93   {
94     if (!current_world->is_levelset) 
95     {
96       start_game();
97     }
98     else 
99     {
100       contrib_world_menu.reset(new ContribWorldMenu(*current_world));
101       MenuManager::push_current(contrib_world_menu.get());
102     }
103   }
104 }
105
106 void
107 TitleScreen::make_tux_jump()
108 {
109   static bool jumpWasReleased = true;
110   Sector* sector  = titlesession->get_current_sector();
111   Player* tux = sector->player;
112
113   controller->update();
114   controller->press(Controller::RIGHT);
115
116   // Check if we should press the jump button
117   Rect lookahead = tux->get_bbox();
118   lookahead.p2.x += 96;
119   bool pathBlocked = !sector->is_free_of_statics(lookahead);
120   if ((pathBlocked && jumpWasReleased) || !tux->on_ground()) {
121     controller->press(Controller::JUMP);
122     jumpWasReleased = false;
123   } else {
124     jumpWasReleased = true;
125   }
126
127   // Wrap around at the end of the level back to the beginning
128   if(sector->get_width() - 320 < tux->get_pos().x) {
129     sector->activate("main");
130     sector->camera->reset(tux->get_pos());
131   }
132 }
133
134 void
135 TitleScreen::generate_main_menu()
136 {
137   main_menu.reset(new MainMenu());
138 }
139
140 TitleScreen::~TitleScreen()
141 {
142 }
143
144 void
145 TitleScreen::setup()
146 {
147   player_status->reset();
148
149   Sector* sector = titlesession->get_current_sector();
150   if(Sector::current() != sector) {
151     sector->play_music(LEVEL_MUSIC);
152     sector->activate(sector->player->get_pos());
153   }
154
155   MenuManager::set_current(main_menu.get());
156 }
157
158 void
159 TitleScreen::leave()
160 {
161   Sector* sector = titlesession->get_current_sector();
162   sector->deactivate();
163   MenuManager::set_current(NULL);
164 }
165
166 void
167 TitleScreen::draw(DrawingContext& context)
168 {
169   Sector* sector  = titlesession->get_current_sector();
170   sector->draw(context);
171
172   // FIXME: Add something to scale the frame to the resolution of the screen
173   context.draw_surface(frame.get(), Vector(0,0),LAYER_FOREGROUND1);
174
175   context.draw_text(Resources::small_font, "SuperTux " PACKAGE_VERSION "\n",
176                     Vector(5, SCREEN_HEIGHT - 50), ALIGN_LEFT, LAYER_FOREGROUND1);
177   context.draw_text(Resources::small_font,
178                     _(
179                       "Copyright (c) 2007 SuperTux Devel Team\n"
180                       "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to\n"
181                       "redistribute it under certain conditions; see the file COPYING for details.\n"
182                       ),
183                     Vector(5, SCREEN_HEIGHT - 50 + Resources::small_font->get_height() + 5),
184                     ALIGN_LEFT, LAYER_FOREGROUND1);
185 }
186
187 void
188 TitleScreen::update(float elapsed_time)
189 {
190   g_screen_manager->set_speed(0.6f);
191   Sector* sector  = titlesession->get_current_sector();
192   sector->update(elapsed_time);
193
194   make_tux_jump();
195
196   Menu* menu = MenuManager::current();
197   if(menu) {
198     if(menu == main_menu.get()) {
199       switch (main_menu->check()) {
200         case MNID_STARTGAME:
201           // Start Game, ie. goto the slots menu
202           if(main_world.get() == NULL) {
203             main_world.reset(new World());
204             main_world->load("levels/world1/info");
205           }
206           current_world = main_world.get();
207           start_game();
208           break;
209
210         case MNID_LEVELS_CONTRIB:
211           // Contrib Menu
212           contrib_menu.reset(new ContribMenu());
213           MenuManager::push_current(contrib_menu.get());
214           break;
215
216         case MNID_ADDONS:
217           // Add-ons Menu
218           addons_menu.reset(new AddonMenu());
219           MenuManager::push_current(addons_menu.get());
220           break;
221
222         case MNID_CREDITS:
223           MenuManager::set_current(NULL);
224           g_screen_manager->push_screen(new TextScroller("credits.txt"),
225                                    new FadeOut(0.5));
226           break;
227
228         case MNID_QUITMAINMENU:
229           g_screen_manager->quit(new FadeOut(0.25));
230           sound_manager->stop_music(0.25);
231           break;
232       }
233     } else if(menu == contrib_menu.get()) {
234       check_levels_contrib_menu();
235     } else if(menu == addons_menu.get()) {
236       addons_menu->check_menu();
237     } else if (menu == contrib_world_menu.get()) {
238       contrib_world_menu->check_menu();
239     }
240   }
241
242   // reopen menu if user closed it (so that the app doesn't close when user
243   // accidently hit ESC)
244   if(MenuManager::current() == 0 && g_screen_manager->has_no_pending_fadeout()) {
245     generate_main_menu();
246     MenuManager::set_current(main_menu.get());
247   }
248 }
249
250 void
251 TitleScreen::start_game()
252 {
253   MenuManager::set_current(NULL);
254   std::string basename = current_world->get_basedir();
255   basename = basename.substr(0, basename.length()-1);
256   std::string worlddirname = FileSystem::basename(basename);
257   std::ostringstream stream;
258   stream << "profile" << g_config->profile << "/" << worlddirname << ".stsg";
259   std::string slotfile = stream.str();
260
261   try {
262     current_world->set_savegame_filename(slotfile);
263     current_world->run();
264   } catch(std::exception& e) {
265     log_fatal << "Couldn't start world: " << e.what() << std::endl;
266   }
267 }
268
269 /* EOF */