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;
70 static Timer random_timer;
74 static GameSession* titlesession;
75 static CodeController* controller;
77 static std::vector<LevelSubset*> contrib_subsets;
78 static LevelSubset* current_contrib_subset = 0;
79 static int current_subset = -1;
81 /* If the demo was stopped - because game started, level
82 editor was excuted, etc - call this when you get back
87 player_status->reset();
88 titlesession->get_current_sector()->activate("main");
89 titlesession->set_current();
91 //frame_rate.update();
94 void update_load_save_game_menu(Menu* menu)
96 msg_debug("update loadsavemenu");
97 for(int i = 1; i < 6; ++i) {
98 MenuItem& item = menu->get_item_by_id(i);
99 item.kind = MN_ACTION;
100 item.change_text(slotinfo(i));
104 void free_contrib_menu()
106 for(std::vector<LevelSubset*>::iterator i = contrib_subsets.begin();
107 i != contrib_subsets.end(); ++i)
110 contrib_subsets.clear();
111 contrib_menu->clear();
112 current_contrib_subset = 0;
116 void generate_contrib_menu()
118 /** Generating contrib levels list by making use of Level Subset */
119 std::vector<std::string> level_subsets;
120 char** files = PHYSFS_enumerateFiles("levels/");
121 for(const char* const* filename = files; *filename != 0; ++filename) {
122 std::string filepath = std::string("levels/") + *filename;
123 if(PHYSFS_isDirectory(filepath.c_str()))
124 level_subsets.push_back(filepath);
126 PHYSFS_freeList(files);
130 contrib_menu->add_label(_("Contrib Levels"));
131 contrib_menu->add_hl();
134 for (std::vector<std::string>::iterator it = level_subsets.begin();
135 it != level_subsets.end(); ++it) {
137 std::auto_ptr<LevelSubset> subset (new LevelSubset());
139 if(subset->hide_from_contribs) {
142 contrib_menu->add_submenu(subset->title, contrib_subset_menu, i++);
143 contrib_subsets.push_back(subset.release());
144 } catch(std::exception& e) {
146 msg_warning("Couldn't parse levelset info for '"
147 << *it << "': " << e.what() << "");
152 contrib_menu->add_hl();
153 contrib_menu->add_back(_("Back"));
156 std::string get_level_name(const std::string& filename)
160 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
162 const lisp::Lisp* level = root->get_lisp("supertux-level");
167 level->get("name", name);
169 } catch(std::exception& e) {
170 msg_warning("Problem getting name of '" << filename << "'.");
175 void check_levels_contrib_menu()
177 int index = contrib_menu->check();
181 LevelSubset& subset = * (contrib_subsets[index]);
183 if(subset.has_worldmap) {
184 WorldMapNS::WorldMap worldmap;
185 worldmap.set_map_filename(subset.get_worldmap_filename());
186 sound_manager->stop_music();
190 DrawingContext context;
191 context.draw_text(white_text, "Loading...",
192 Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT/2), CENTER_ALLIGN, LAYER_FOREGROUND1);
193 context.do_drawing();
195 // TODO: slots should be available for contrib maps
196 worldmap.loadgame("save/" + subset.name + "-slot1.stsg");
197 worldmap.display(); // run the map
199 Menu::set_current(main_menu);
201 } else if (current_subset != index) {
202 current_subset = index;
203 LevelSubset& subset = * (contrib_subsets[index]);
205 current_contrib_subset = ⊂
207 contrib_subset_menu->clear();
209 contrib_subset_menu->add_label(subset.title);
210 contrib_subset_menu->add_hl();
212 for (int i = 0; i < subset.get_num_levels(); ++i)
214 /** get level's title */
215 std::string filename = subset.get_level_filename(i);
216 std::string title = get_level_name(filename);
217 contrib_subset_menu->add_entry(i, title);
220 contrib_subset_menu->add_hl();
221 contrib_subset_menu->add_back(_("Back"));
223 titlesession->get_current_sector()->activate("main");
224 titlesession->set_current();
228 void check_contrib_subset_menu()
230 int index = contrib_subset_menu->check();
232 if (contrib_subset_menu->get_item_by_id(index).kind == MN_ACTION) {
233 sound_manager->stop_music();
235 current_contrib_subset->get_level_filename(index), ST_GL_PLAY);
237 player_status->reset();
238 Menu::set_current(main_menu);
244 void draw_demo(float elapsed_time)
246 static float last_tux_x_pos = -1;
247 static float last_tux_y_pos = -1;
248 Sector* sector = titlesession->get_current_sector();
249 Player* tux = sector->player;
251 sector->play_music(LEVEL_MUSIC);
253 controller->update();
254 controller->press(Controller::RIGHT);
256 if(random_timer.check() ||
257 (walking && fabsf(last_tux_x_pos - tux->get_pos().x)) < .1) {
260 if(!walking && fabsf(tux->get_pos().y - last_tux_y_pos) < .1) {
261 random_timer.start(float(rand() % 3000 + 3000) / 1000.);
266 controller->press(Controller::JUMP);
267 last_tux_x_pos = tux->get_pos().x;
268 last_tux_y_pos = tux->get_pos().y;
270 // Wrap around at the end of the level back to the beginnig
271 if(sector->solids->get_width() * 32 - 320 < tux->get_pos().x) {
272 sector->activate("main");
273 sector->camera->reset(tux->get_pos());
276 sector->update(elapsed_time);
277 sector->draw(*titlesession->context);
280 /* --- TITLE SCREEN --- */
284 //LevelEditor* leveleditor;
285 controller = new CodeController();
287 titlesession = new GameSession("levels/misc/menu.stl", ST_GL_DEMO_GAME);
290 bkg_title = new Surface("images/background/arctis.jpg");
291 logo = new Surface("images/engine/menu/logo.png");
292 //img_choose_subset = new Surface("images/status/choose-level-subset.png");
294 titlesession->get_current_sector()->activate("main");
295 titlesession->set_current();
297 Player* player = titlesession->get_current_sector()->player;
298 player->set_controller(controller);
300 /* --- Main title loop: --- */
303 random_timer.start(float(rand() % 2000 + 2000) / 1000.0);
305 Uint32 lastticks = SDL_GetTicks();
307 Menu::set_current(main_menu);
308 DrawingContext& context = *titlesession->context;
312 // Calculate the movement-factor
313 Uint32 ticks = SDL_GetTicks();
314 float elapsed_time = float(ticks - lastticks) / 1000.;
315 game_time += elapsed_time;
318 if(elapsed_time > .04)
321 /* Lower the speed so that Tux doesn't jump too hectically throught
326 main_controller->update();
327 while (SDL_PollEvent(&event)) {
328 if (Menu::current()) {
329 Menu::current()->event(event);
331 main_controller->process_event(event);
332 if (event.type == SDL_QUIT)
333 throw graceful_shutdown();
336 /* Draw the background: */
337 draw_demo(elapsed_time);
339 if (Menu::current() == main_menu)
340 context.draw_surface(logo, Vector(SCREEN_WIDTH/2 - logo->get_width()/2, 30),
341 LAYER_FOREGROUND1+1);
343 context.draw_text(white_small_text, " SuperTux " PACKAGE_VERSION "\n",
344 Vector(0, SCREEN_HEIGHT - 50), LEFT_ALLIGN, LAYER_FOREGROUND1);
345 context.draw_text(white_small_text,
347 "Copyright (c) 2006 SuperTux Devel Team\n"
348 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to\n"
349 "redistribute it under certain conditions; see the file COPYING for details.\n"
351 Vector(0, SCREEN_HEIGHT - 50 + white_small_text->get_height() + 5),
352 LEFT_ALLIGN, LAYER_FOREGROUND1);
354 /* Don't draw menu, if quit is true */
355 Menu* menu = Menu::current();
361 if(menu == main_menu)
363 switch (main_menu->check())
366 // Start Game, ie. goto the slots menu
367 update_load_save_game_menu(load_game_menu);
369 case MNID_LEVELS_CONTRIB:
371 generate_contrib_menu();
374 case MNID_LEVELEDITOR: {
375 LevelEdtiro* leveleditor = new LevelEditor();
378 Menu::set_current(main_menu);
384 sound_manager->stop_music();
386 sound_manager->play_music("music/credits.ogg");
387 display_text_file("credits.txt");
388 sound_manager->stop_music();
390 Menu::set_current(main_menu);
392 case MNID_QUITMAINMENU:
397 else if(menu == options_menu)
399 process_options_menu();
401 else if(menu == load_game_menu)
403 if(event.key.keysym.sym == SDLK_DELETE)
405 int slot = menu->get_active_item_id();
406 std::stringstream stream;
408 std::string str = _("Are you sure you want to delete slot") + stream.str() + "?";
410 if(confirm_dialog(bkg_title, str.c_str())) {
411 str = "save/slot" + stream.str() + ".stsg";
412 msg_debug("Removing: " << str);
413 PHYSFS_delete(str.c_str());
416 update_load_save_game_menu(load_game_menu);
417 Menu::set_current(main_menu);
420 else if (process_load_game_menu())
425 else if(menu == contrib_menu)
427 check_levels_contrib_menu();
429 else if (menu == contrib_subset_menu)
431 check_contrib_subset_menu();
435 // reopen menu of user closed it (so that the app doesn't close when user
436 // accidently hit ESC)
437 if(Menu::current() == 0) {
438 Menu::set_current(main_menu);
441 context.do_drawing();
442 sound_manager->update();
444 //frame_rate.update();
455 //delete img_choose_subset;