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