62d810e5842f8e3982c21c111cfb22b242cfe33e
[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   copyright_text()
48 {
49   controller.reset(new CodeController());
50   titlesession.reset(new GameSession("levels/misc/menu.stl", player_status));
51
52   Player* player = titlesession->get_current_sector()->player;
53   player->set_controller(controller.get());
54   player->set_speedlimit(230); //MAX_WALK_XM
55
56   frame = Surface::create("images/engine/menu/frame.png");
57   copyright_text = "SuperTux " PACKAGE_VERSION "\n" +
58     _("Copyright") + " (c) 2003-2014 SuperTux Devel Team\n" +
59     _("This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to\n"
60       "redistribute it under certain conditions; see the file COPYING for details.\n"
61    );
62 }
63
64 std::string
65 TitleScreen::get_level_name(const std::string& filename)
66 {
67   try {
68     lisp::Parser parser;
69     const lisp::Lisp* root = parser.parse(filename);
70
71     const lisp::Lisp* level = root->get_lisp("supertux-level");
72     if(!level)
73       return "";
74
75     std::string name;
76     level->get("name", name);
77     return name;
78   } catch(std::exception& e) {
79     log_warning << "Problem getting name of '" << filename << "': "
80                 << e.what() << std::endl;
81     return "";
82   }
83 }
84
85 void
86 TitleScreen::make_tux_jump()
87 {
88   static bool jumpWasReleased = true;
89   Sector* sector  = titlesession->get_current_sector();
90   Player* tux = sector->player;
91
92   controller->update();
93   controller->press(Controller::RIGHT);
94
95   // Check if we should press the jump button
96   Rectf lookahead = tux->get_bbox();
97   lookahead.p2.x += 96;
98   bool pathBlocked = !sector->is_free_of_statics(lookahead);
99   if ((pathBlocked && jumpWasReleased) || !tux->on_ground()) {
100     controller->press(Controller::JUMP);
101     jumpWasReleased = false;
102   } else {
103     jumpWasReleased = true;
104   }
105
106   // Wrap around at the end of the level back to the beginning
107   if(sector->get_width() - 320 < tux->get_pos().x) {
108     sector->activate("main");
109     sector->camera->reset(tux->get_pos());
110   }
111 }
112
113 TitleScreen::~TitleScreen()
114 {
115 }
116
117 void
118 TitleScreen::setup()
119 {
120   Sector* sector = titlesession->get_current_sector();
121   if(Sector::current() != sector) {
122     sector->play_music(LEVEL_MUSIC);
123     sector->activate(sector->player->get_pos());
124   }
125
126   MenuManager::instance().set_menu(MenuStorage::MAIN_MENU);
127 }
128
129 void
130 TitleScreen::leave()
131 {
132   Sector* sector = titlesession->get_current_sector();
133   sector->deactivate();
134   MenuManager::instance().clear_menu_stack();
135 }
136
137 void
138 TitleScreen::draw(DrawingContext& context)
139 {
140   Sector* sector  = titlesession->get_current_sector();
141   sector->draw(context);
142
143   // FIXME: Add something to scale the frame to the resolution of the screen
144   //context.draw_surface(frame, Vector(0,0),LAYER_FOREGROUND1);
145
146   context.draw_text(Resources::small_font,
147                     copyright_text,
148                     Vector(5, SCREEN_HEIGHT - 50),
149                     ALIGN_LEFT, LAYER_FOREGROUND1);
150 }
151
152 void
153 TitleScreen::update(float elapsed_time)
154 {
155   g_screen_manager->set_speed(0.6f);
156   Sector* sector  = titlesession->get_current_sector();
157   sector->update(elapsed_time);
158
159   make_tux_jump();
160
161   MenuManager::instance().check_menu();
162
163   // reopen menu if user closed it (so that the app doesn't close when user
164   // accidently hit ESC)
165   if(!MenuManager::instance().is_active() && g_screen_manager->has_no_pending_fadeout())
166   {
167     MenuManager::instance().set_menu(MenuStorage::MAIN_MENU);
168   }
169 }
170
171 /* EOF */