4 // Copyright (C) 2000 Bill Kendrick <bill@newbreedsoftware.com>
5 // Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
33 #include <SDL_image.h>
37 #include "video/screen.hpp"
38 #include "video/surface.hpp"
39 #include "audio/sound_manager.hpp"
40 #include "gui/menu.hpp"
42 #include "lisp/lisp.hpp"
43 #include "lisp/parser.hpp"
45 #include "level_subset.hpp"
46 #include "game_session.hpp"
47 #include "worldmap.hpp"
48 #include "player_status.hpp"
51 #include "object/tilemap.hpp"
52 #include "object/camera.hpp"
53 #include "object/player.hpp"
54 #include "resources.hpp"
55 #include "gettext.hpp"
57 #include "textscroller.hpp"
58 #include "file_system.hpp"
59 #include "control/joystickkeyboardcontroller.hpp"
60 #include "control/codecontroller.hpp"
62 #include "exceptions.hpp"
65 static Surface* bkg_title;
67 //static Surface* img_choose_subset;
71 static GameSession* titlesession;
72 static CodeController* controller;
74 static std::vector<LevelSubset*> contrib_subsets;
75 static LevelSubset* current_contrib_subset = 0;
76 static int current_subset = -1;
78 /* If the demo was stopped - because game started, level
79 editor was excuted, etc - call this when you get back
84 player_status->reset();
85 titlesession->get_current_sector()->activate("main");
86 titlesession->set_current();
88 //frame_rate.update();
91 void update_load_save_game_menu(Menu* menu)
93 msg_debug("update loadsavemenu");
94 for(int i = 1; i < 6; ++i) {
95 MenuItem& item = menu->get_item_by_id(i);
96 item.kind = MN_ACTION;
97 item.change_text(slotinfo(i));
101 void free_contrib_menu()
103 for(std::vector<LevelSubset*>::iterator i = contrib_subsets.begin();
104 i != contrib_subsets.end(); ++i)
107 contrib_subsets.clear();
108 contrib_menu->clear();
109 current_contrib_subset = 0;
113 void generate_contrib_menu()
115 /** Generating contrib levels list by making use of Level Subset */
116 std::vector<std::string> level_subsets;
117 char** files = PHYSFS_enumerateFiles("levels/");
118 for(const char* const* filename = files; *filename != 0; ++filename) {
119 std::string filepath = std::string("levels/") + *filename;
120 if(PHYSFS_isDirectory(filepath.c_str()))
121 level_subsets.push_back(filepath);
123 PHYSFS_freeList(files);
127 contrib_menu->add_label(_("Contrib Levels"));
128 contrib_menu->add_hl();
131 for (std::vector<std::string>::iterator it = level_subsets.begin();
132 it != level_subsets.end(); ++it) {
134 std::auto_ptr<LevelSubset> subset (new LevelSubset());
136 if(subset->hide_from_contribs) {
139 contrib_menu->add_submenu(subset->title, contrib_subset_menu, i++);
140 contrib_subsets.push_back(subset.release());
141 } catch(std::exception& e) {
143 msg_warning("Couldn't parse levelset info for '"
144 << *it << "': " << e.what() << "");
149 contrib_menu->add_hl();
150 contrib_menu->add_back(_("Back"));
153 std::string get_level_name(const std::string& filename)
157 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
159 const lisp::Lisp* level = root->get_lisp("supertux-level");
164 level->get("name", name);
166 } catch(std::exception& e) {
167 msg_warning("Problem getting name of '" << filename << "'.");
172 void check_levels_contrib_menu()
174 int index = contrib_menu->check();
178 LevelSubset& subset = * (contrib_subsets[index]);
180 if(subset.has_worldmap) {
181 WorldMapNS::WorldMap worldmap;
182 worldmap.set_map_filename(subset.get_worldmap_filename());
183 sound_manager->stop_music();
187 DrawingContext context;
188 context.draw_text(white_text, "Loading...",
189 Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT/2), CENTER_ALLIGN, LAYER_FOREGROUND1);
190 context.do_drawing();
192 // TODO: slots should be available for contrib maps
193 worldmap.loadgame("save/" + subset.name + "-slot1.stsg");
194 worldmap.display(); // run the map
196 Menu::set_current(main_menu);
198 } else if (current_subset != index) {
199 current_subset = index;
200 LevelSubset& subset = * (contrib_subsets[index]);
202 current_contrib_subset = ⊂
204 contrib_subset_menu->clear();
206 contrib_subset_menu->add_label(subset.title);
207 contrib_subset_menu->add_hl();
209 for (int i = 0; i < subset.get_num_levels(); ++i)
211 /** get level's title */
212 std::string filename = subset.get_level_filename(i);
213 std::string title = get_level_name(filename);
214 contrib_subset_menu->add_entry(i, title);
217 contrib_subset_menu->add_hl();
218 contrib_subset_menu->add_back(_("Back"));
220 titlesession->get_current_sector()->activate("main");
221 titlesession->set_current();
225 void check_contrib_subset_menu()
227 int index = contrib_subset_menu->check();
229 if (contrib_subset_menu->get_item_by_id(index).kind == MN_ACTION) {
230 sound_manager->stop_music();
232 current_contrib_subset->get_level_filename(index), ST_GL_PLAY);
234 player_status->reset();
235 Menu::set_current(main_menu);
241 void draw_demo(float elapsed_time)
243 static Timer randomWaitTimer;
244 static Timer jumpPushTimer;
245 static Timer jumpRecoverTimer;
246 static float last_tux_x_pos = -1;
247 static float last_tux_y_pos = -1;
249 Sector* sector = titlesession->get_current_sector();
250 Player* tux = sector->player;
252 sector->play_music(LEVEL_MUSIC);
254 controller->update();
255 controller->press(Controller::RIGHT);
257 // Determine how far we moved since last frame
258 float dx = fabsf(last_tux_x_pos - tux->get_pos().x);
259 float dy = fabsf(last_tux_y_pos - tux->get_pos().y);
261 // Calculate space to check for obstacles
262 Rect lookahead = Rect(tux->get_bbox());
263 lookahead.move(Vector(lookahead.get_width()*2,0));
265 // Check if we should press the jump button
266 bool randomJump = !randomWaitTimer.started();
267 bool mayJump = !jumpRecoverTimer.started();
268 bool notMoving = (dx+dy < 0.1);
269 bool pathBlocked = !sector->is_free_space(lookahead);
270 if ((notMoving || pathBlocked || randomJump) && mayJump) {
271 float jumpDuration = float(rand() % 200 + 500) / 1000.0;
272 jumpPushTimer.start(jumpDuration);
273 jumpRecoverTimer.start(jumpDuration+0.1);
274 randomWaitTimer.start(float(rand() % 3000 + 3000) / 1000.0);
277 // Keep jump button pressed
278 if (jumpPushTimer.started()) controller->press(Controller::JUMP);
280 // Remember last position, so we can determine if we moved
281 last_tux_x_pos = tux->get_pos().x;
282 last_tux_y_pos = tux->get_pos().y;
284 // Wrap around at the end of the level back to the beginnig
285 if(sector->solids->get_width() * 32 - 320 < tux->get_pos().x) {
286 sector->activate("main");
287 sector->camera->reset(tux->get_pos());
290 sector->update(elapsed_time);
291 sector->draw(*titlesession->context);
294 /* --- TITLE SCREEN --- */
297 //LevelEditor* leveleditor;
298 controller = new CodeController();
300 titlesession = new GameSession("levels/misc/menu.stl", ST_GL_DEMO_GAME);
303 bkg_title = new Surface("images/background/arctis.jpg");
304 logo = new Surface("images/engine/menu/logo.png");
305 //img_choose_subset = new Surface("images/status/choose-level-subset.png");
307 titlesession->get_current_sector()->activate("main");
308 titlesession->set_current();
310 Player* player = titlesession->get_current_sector()->player;
311 player->set_controller(controller);
313 /* --- Main title loop: --- */
316 Uint32 lastticks = SDL_GetTicks();
318 Menu::set_current(main_menu);
319 DrawingContext& context = *titlesession->context;
323 // Calculate the movement-factor
324 Uint32 ticks = SDL_GetTicks();
325 float elapsed_time = float(ticks - lastticks) / 1000.;
326 game_time += elapsed_time;
329 if(elapsed_time > .04)
332 /* Lower the speed so that Tux doesn't jump too hectically throught
337 main_controller->update();
338 while (SDL_PollEvent(&event)) {
339 if (Menu::current()) {
340 Menu::current()->event(event);
342 main_controller->process_event(event);
343 if (event.type == SDL_QUIT)
344 throw graceful_shutdown();
347 /* Draw the background: */
348 draw_demo(elapsed_time);
350 if (Menu::current() == main_menu)
351 context.draw_surface(logo, Vector(SCREEN_WIDTH/2 - logo->get_width()/2, 30),
352 LAYER_FOREGROUND1+1);
354 context.draw_text(white_small_text, " SuperTux " PACKAGE_VERSION "\n",
355 Vector(0, SCREEN_HEIGHT - 50), LEFT_ALLIGN, LAYER_FOREGROUND1);
356 context.draw_text(white_small_text,
358 "Copyright (c) 2006 SuperTux Devel Team\n"
359 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to\n"
360 "redistribute it under certain conditions; see the file COPYING for details.\n"
362 Vector(0, SCREEN_HEIGHT - 50 + white_small_text->get_height() + 5),
363 LEFT_ALLIGN, LAYER_FOREGROUND1);
365 /* Don't draw menu, if quit is true */
366 Menu* menu = Menu::current();
372 if(menu == main_menu)
374 switch (main_menu->check())
377 // Start Game, ie. goto the slots menu
378 update_load_save_game_menu(load_game_menu);
380 case MNID_LEVELS_CONTRIB:
382 generate_contrib_menu();
385 case MNID_LEVELEDITOR: {
386 LevelEdtiro* leveleditor = new LevelEditor();
389 Menu::set_current(main_menu);
395 sound_manager->stop_music();
397 sound_manager->play_music("music/credits.ogg");
398 display_text_file("credits.txt");
399 sound_manager->stop_music();
401 Menu::set_current(main_menu);
403 case MNID_QUITMAINMENU:
408 else if(menu == options_menu)
410 process_options_menu();
412 else if(menu == load_game_menu)
414 if(event.key.keysym.sym == SDLK_DELETE)
416 int slot = menu->get_active_item_id();
417 std::stringstream stream;
419 std::string str = _("Are you sure you want to delete slot") + stream.str() + "?";
421 if(confirm_dialog(bkg_title, str.c_str())) {
422 str = "save/slot" + stream.str() + ".stsg";
423 msg_debug("Removing: " << str);
424 PHYSFS_delete(str.c_str());
427 update_load_save_game_menu(load_game_menu);
428 Menu::set_current(main_menu);
431 else if (process_load_game_menu())
436 else if(menu == contrib_menu)
438 check_levels_contrib_menu();
440 else if (menu == contrib_subset_menu)
442 check_contrib_subset_menu();
446 // reopen menu of user closed it (so that the app doesn't close when user
447 // accidently hit ESC)
448 if(Menu::current() == 0) {
449 Menu::set_current(main_menu);
452 context.do_drawing();
453 sound_manager->update();
455 //frame_rate.update();
466 //delete img_choose_subset;