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"
64 static Surface* bkg_title;
66 //static Surface* img_choose_subset;
69 static Timer random_timer;
73 static GameSession* titlesession;
74 static CodeController* controller;
76 static std::vector<LevelSubset*> contrib_subsets;
77 static LevelSubset* current_contrib_subset = 0;
78 static int current_subset = -1;
80 /* If the demo was stopped - because game started, level
81 editor was excuted, etc - call this when you get back
86 player_status->reset();
87 titlesession->get_current_sector()->activate("main");
88 titlesession->set_current();
90 //frame_rate.update();
93 void update_load_save_game_menu(Menu* menu)
95 printf("update loadsavemenu.\n");
96 for(int i = 1; i < 6; ++i) {
97 MenuItem& item = menu->get_item_by_id(i);
98 item.kind = MN_ACTION;
99 item.change_text(slotinfo(i));
103 void free_contrib_menu()
105 for(std::vector<LevelSubset*>::iterator i = contrib_subsets.begin();
106 i != contrib_subsets.end(); ++i)
109 contrib_subsets.clear();
110 contrib_menu->clear();
111 current_contrib_subset = 0;
115 void generate_contrib_menu()
117 /** Generating contrib levels list by making use of Level Subset */
118 std::vector<std::string> level_subsets;
119 char** files = PHYSFS_enumerateFiles("levels/");
120 for(const char* const* filename = files; *filename != 0; ++filename) {
121 std::string filepath = std::string("levels/") + *filename;
122 if(PHYSFS_isDirectory(filepath.c_str()))
123 level_subsets.push_back(filepath);
125 PHYSFS_freeList(files);
129 contrib_menu->add_label(_("Contrib Levels"));
130 contrib_menu->add_hl();
133 for (std::vector<std::string>::iterator it = level_subsets.begin();
134 it != level_subsets.end(); ++it) {
136 std::auto_ptr<LevelSubset> subset (new LevelSubset());
138 if(subset->hide_from_contribs) {
141 contrib_menu->add_submenu(subset->title, contrib_subset_menu, i++);
142 contrib_subsets.push_back(subset.release());
143 } catch(std::exception& e) {
145 std::cerr << "Couldn't parse levelset info for '"
146 << *it << "': " << e.what() << "\n";
151 contrib_menu->add_hl();
152 contrib_menu->add_back(_("Back"));
155 std::string get_level_name(const std::string& filename)
159 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
161 const lisp::Lisp* level = root->get_lisp("supertux-level");
166 level->get("name", name);
168 } catch(std::exception& e) {
169 std::cerr << "Problem getting name of '" << filename << "'.\n";
174 void check_levels_contrib_menu()
176 int index = contrib_menu->check();
180 LevelSubset& subset = * (contrib_subsets[index]);
182 if(subset.has_worldmap) {
183 WorldMapNS::WorldMap worldmap;
184 worldmap.set_map_filename(subset.get_worldmap_filename());
185 sound_manager->stop_music();
189 DrawingContext context;
190 context.draw_text(white_text, "Loading...",
191 Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT/2), CENTER_ALLIGN, LAYER_FOREGROUND1);
192 context.do_drawing();
194 // TODO: slots should be available for contrib maps
195 worldmap.loadgame("save/" + subset.name + "-slot1.stsg");
196 worldmap.display(); // run the map
198 Menu::set_current(main_menu);
200 } else if (current_subset != index) {
201 current_subset = index;
202 LevelSubset& subset = * (contrib_subsets[index]);
204 current_contrib_subset = ⊂
206 contrib_subset_menu->clear();
208 contrib_subset_menu->add_label(subset.title);
209 contrib_subset_menu->add_hl();
211 for (int i = 0; i < subset.get_num_levels(); ++i)
213 /** get level's title */
214 std::string filename = subset.get_level_filename(i);
215 std::string title = get_level_name(filename);
216 contrib_subset_menu->add_entry(i, title);
219 contrib_subset_menu->add_hl();
220 contrib_subset_menu->add_back(_("Back"));
222 titlesession->get_current_sector()->activate("main");
223 titlesession->set_current();
227 void check_contrib_subset_menu()
229 int index = contrib_subset_menu->check();
231 if (contrib_subset_menu->get_item_by_id(index).kind == MN_ACTION) {
232 sound_manager->stop_music();
234 current_contrib_subset->get_level_filename(index), ST_GL_PLAY);
236 player_status->reset();
237 Menu::set_current(main_menu);
243 void draw_demo(float elapsed_time)
245 static float last_tux_x_pos = -1;
246 static float last_tux_y_pos = -1;
247 Sector* sector = titlesession->get_current_sector();
248 Player* tux = sector->player;
250 sector->play_music(LEVEL_MUSIC);
252 controller->update();
253 controller->press(Controller::RIGHT);
255 if(random_timer.check() ||
256 (walking && fabsf(last_tux_x_pos - tux->get_pos().x)) < .1) {
259 if(!walking && fabsf(tux->get_pos().y - last_tux_y_pos) < .1) {
260 random_timer.start(float(rand() % 3000 + 3000) / 1000.);
265 controller->press(Controller::JUMP);
266 last_tux_x_pos = tux->get_pos().x;
267 last_tux_y_pos = tux->get_pos().y;
269 // Wrap around at the end of the level back to the beginnig
270 if(sector->solids->get_width() * 32 - 320 < tux->get_pos().x) {
271 sector->activate("main");
272 sector->camera->reset(tux->get_pos());
275 sector->update(elapsed_time);
276 sector->draw(*titlesession->context);
279 /* --- TITLE SCREEN --- */
283 //LevelEditor* leveleditor;
284 controller = new CodeController();
286 titlesession = new GameSession("levels/misc/menu.stl", ST_GL_DEMO_GAME);
289 bkg_title = new Surface("images/background/arctis.jpg");
290 logo = new Surface("images/engine/menu/logo.png");
291 //img_choose_subset = new Surface("images/status/choose-level-subset.png");
293 titlesession->get_current_sector()->activate("main");
294 titlesession->set_current();
296 Player* player = titlesession->get_current_sector()->player;
297 player->set_controller(controller);
299 /* --- Main title loop: --- */
302 random_timer.start(float(rand() % 2000 + 2000) / 1000.0);
304 Uint32 lastticks = SDL_GetTicks();
306 Menu::set_current(main_menu);
307 DrawingContext& context = *titlesession->context;
311 // Calculate the movement-factor
312 Uint32 ticks = SDL_GetTicks();
313 float elapsed_time = float(ticks - lastticks) / 1000.;
314 game_time += elapsed_time;
317 if(elapsed_time > .04)
320 /* Lower the speed so that Tux doesn't jump too hectically throught
325 main_controller->update();
326 while (SDL_PollEvent(&event)) {
327 if (Menu::current()) {
328 Menu::current()->event(event);
330 main_controller->process_event(event);
331 if (event.type == SDL_QUIT)
332 throw graceful_shutdown();
335 /* Draw the background: */
336 draw_demo(elapsed_time);
338 if (Menu::current() == main_menu)
339 context.draw_surface(logo, Vector(SCREEN_WIDTH/2 - logo->get_width()/2, 30),
340 LAYER_FOREGROUND1+1);
342 context.draw_text(white_small_text, " SuperTux " PACKAGE_VERSION "\n",
343 Vector(0, SCREEN_HEIGHT - 50), LEFT_ALLIGN, LAYER_FOREGROUND1);
344 context.draw_text(white_small_text,
346 "Copyright (c) 2006 SuperTux Devel Team\n"
347 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to\n"
348 "redistribute it under certain conditions; see the file COPYING for details.\n"
350 Vector(0, SCREEN_HEIGHT - 50 + white_small_text->get_height() + 5),
351 LEFT_ALLIGN, LAYER_FOREGROUND1);
353 /* Don't draw menu, if quit is true */
354 Menu* menu = Menu::current();
360 if(menu == main_menu)
362 switch (main_menu->check())
365 // Start Game, ie. goto the slots menu
366 update_load_save_game_menu(load_game_menu);
368 case MNID_LEVELS_CONTRIB:
370 generate_contrib_menu();
373 case MNID_LEVELEDITOR: {
374 LevelEdtiro* leveleditor = new LevelEditor();
377 Menu::set_current(main_menu);
383 sound_manager->stop_music();
385 sound_manager->play_music("music/credits.ogg");
386 display_text_file("credits.txt");
387 sound_manager->stop_music();
389 Menu::set_current(main_menu);
391 case MNID_QUITMAINMENU:
396 else if(menu == options_menu)
398 process_options_menu();
400 else if(menu == load_game_menu)
402 if(event.key.keysym.sym == SDLK_DELETE)
404 int slot = menu->get_active_item_id();
405 std::stringstream stream;
407 std::string str = _("Are you sure you want to delete slot") + stream.str() + "?";
409 if(confirm_dialog(bkg_title, str.c_str())) {
410 str = "save/slot" + stream.str() + ".stsg";
411 printf("Removing: %s\n",str.c_str());
412 PHYSFS_delete(str.c_str());
415 update_load_save_game_menu(load_game_menu);
416 Menu::set_current(main_menu);
419 else if (process_load_game_menu())
424 else if(menu == contrib_menu)
426 check_levels_contrib_menu();
428 else if (menu == contrib_subset_menu)
430 check_contrib_subset_menu();
434 // reopen menu of user closed it (so that the app doesn't close when user
435 // accidently hit ESC)
436 if(Menu::current() == 0) {
437 Menu::set_current(main_menu);
440 context.do_drawing();
441 sound_manager->update();
443 //frame_rate.update();
454 //delete img_choose_subset;