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