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.h"
38 #include "video/surface.h"
41 #include "lisp/lisp.h"
42 #include "lisp/parser.h"
44 #include "level_subset.h"
45 #include "game_session.h"
47 #include "leveleditor.h"
48 #include "player_status.h"
51 #include "object/tilemap.h"
52 #include "object/camera.h"
53 #include "object/player.h"
54 #include "resources.h"
57 #include "textscroller.h"
58 #include "file_system.h"
59 #include "control/joystickkeyboardcontroller.h"
60 #include "control/codecontroller.h"
63 static Surface* bkg_title;
65 //static Surface* img_choose_subset;
68 static Timer random_timer;
72 static GameSession* titlesession;
73 static CodeController* controller;
75 static std::vector<LevelSubset*> contrib_subsets;
76 static LevelSubset* current_contrib_subset = 0;
77 static int current_subset = -1;
79 /* If the demo was stopped - because game started, level
80 editor was excuted, etc - call this when you get back
85 player_status.reset();
86 titlesession->get_current_sector()->activate("main");
87 titlesession->set_current();
89 //frame_rate.update();
92 void update_load_save_game_menu(Menu* menu)
94 printf("update loadsavemenu.\n");
95 for(int i = 1; i < 6; ++i) {
96 MenuItem& item = menu->get_item_by_id(i);
97 item.kind = MN_ACTION;
98 item.change_text(slotinfo(i));
102 void free_contrib_menu()
104 for(std::vector<LevelSubset*>::iterator i = contrib_subsets.begin();
105 i != contrib_subsets.end(); ++i)
108 contrib_subsets.clear();
109 contrib_menu->clear();
110 current_contrib_subset = 0;
114 void generate_contrib_menu()
116 /** Generating contrib levels list by making use of Level Subset */
117 std::vector<std::string> level_subsets;
118 char** files = PHYSFS_enumerateFiles("levels/");
119 for(const char* const* filename = files; *filename != 0; ++filename) {
120 std::string filepath = std::string("levels/") + *filename;
121 if(PHYSFS_isDirectory(filepath.c_str()))
122 level_subsets.push_back(filepath);
124 PHYSFS_freeList(files);
128 contrib_menu->add_label(_("Contrib Levels"));
129 contrib_menu->add_hl();
132 for (std::vector<std::string>::iterator it = level_subsets.begin();
133 it != level_subsets.end(); ++it) {
135 std::auto_ptr<LevelSubset> subset (new LevelSubset());
137 if(subset->hide_from_contribs) {
140 contrib_menu->add_submenu(subset->title, contrib_subset_menu, i++);
141 contrib_subsets.push_back(subset.release());
142 } catch(std::exception& e) {
144 std::cerr << "Couldn't parse levelset info for '"
145 << *it << "': " << e.what() << "\n";
150 contrib_menu->add_hl();
151 contrib_menu->add_back(_("Back"));
154 std::string get_level_name(const std::string& filename)
158 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
160 const lisp::Lisp* level = root->get_lisp("supertux-level");
165 level->get("name", name);
167 } catch(std::exception& e) {
168 std::cerr << "Problem getting name of '" << filename << "'.\n";
173 void check_levels_contrib_menu()
175 int index = contrib_menu->check();
179 LevelSubset& subset = * (contrib_subsets[index]);
181 if(subset.has_worldmap) {
182 WorldMapNS::WorldMap worldmap;
183 worldmap.set_map_filename(subset.get_worldmap_filename());
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) {
231 current_contrib_subset->get_level_filename(index), ST_GL_PLAY);
233 player_status.reset();
234 Menu::set_current(main_menu);
240 void draw_demo(float elapsed_time)
242 static float last_tux_x_pos = -1;
243 static float last_tux_y_pos = -1;
244 Sector* sector = titlesession->get_current_sector();
245 Player* tux = sector->player;
247 sector->play_music(LEVEL_MUSIC);
249 controller->update();
250 controller->press(Controller::RIGHT);
252 if(random_timer.check() ||
253 (walking && fabsf(last_tux_x_pos - tux->get_pos().x)) < .1) {
256 if(!walking && fabsf(tux->get_pos().y - last_tux_y_pos) < .1) {
257 random_timer.start(float(rand() % 3000 + 3000) / 1000.);
262 controller->press(Controller::JUMP);
263 last_tux_x_pos = tux->get_pos().x;
264 last_tux_y_pos = tux->get_pos().y;
266 // Wrap around at the end of the level back to the beginnig
267 if(sector->solids->get_width() * 32 - 320 < tux->get_pos().x) {
268 sector->activate("main");
269 sector->camera->reset(tux->get_pos());
272 sector->update(elapsed_time);
273 sector->draw(*titlesession->context);
276 /* --- TITLE SCREEN --- */
280 //LevelEditor* leveleditor;
281 MusicRef credits_music;
282 controller = new CodeController();
284 titlesession = new GameSession("levels/misc/menu.stl", ST_GL_DEMO_GAME);
287 bkg_title = new Surface("images/background/arctis.jpg", false);
288 logo = new Surface("images/engine/menu/logo.png", true);
289 //img_choose_subset = new Surface("images/status/choose-level-subset.png", true);
291 titlesession->get_current_sector()->activate("main");
292 titlesession->set_current();
294 Player* player = titlesession->get_current_sector()->player;
295 player->set_controller(controller);
297 /* --- Main title loop: --- */
300 random_timer.start(float(rand() % 2000 + 2000) / 1000.0);
302 Uint32 lastticks = SDL_GetTicks();
304 Menu::set_current(main_menu);
305 DrawingContext& context = *titlesession->context;
309 // Calculate the movement-factor
310 Uint32 ticks = SDL_GetTicks();
311 float elapsed_time = float(ticks - lastticks) / 1000.;
312 global_time += elapsed_time;
315 if(elapsed_time > .04)
318 /* Lower the speed so that Tux doesn't jump too hectically throught
323 main_controller->update();
324 while (SDL_PollEvent(&event)) {
325 if (Menu::current()) {
326 Menu::current()->event(event);
328 main_controller->process_event(event);
329 if (event.type == SDL_QUIT)
330 throw std::runtime_error("Received window close");
333 /* Draw the background: */
334 draw_demo(elapsed_time);
336 if (Menu::current() == main_menu)
337 context.draw_surface(logo, Vector(SCREEN_WIDTH/2 - logo->w/2, 30),
338 LAYER_FOREGROUND1+1);
340 context.draw_text(white_small_text, " SuperTux " PACKAGE_VERSION "\n",
341 Vector(0, SCREEN_HEIGHT - 50), LEFT_ALLIGN, LAYER_FOREGROUND1);
342 context.draw_text(white_small_text,
344 "Copyright (c) 2005 SuperTux Devel Team\n"
345 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to\n"
346 "redistribute it under certain conditions; see the file COPYING for details.\n"
348 Vector(0, SCREEN_HEIGHT - 50 + white_small_text->get_height() + 5),
349 LEFT_ALLIGN, LAYER_FOREGROUND1);
351 /* Don't draw menu, if quit is true */
352 Menu* menu = Menu::current();
358 if(menu == main_menu)
360 switch (main_menu->check())
363 // Start Game, ie. goto the slots menu
364 update_load_save_game_menu(load_game_menu);
366 case MNID_LEVELS_CONTRIB:
368 generate_contrib_menu();
371 case MNID_LEVELEDITOR: {
372 LevelEdtiro* leveleditor = new LevelEditor();
375 Menu::set_current(main_menu);
382 credits_music = sound_manager->load_music(
383 "/music/credits.ogg");
384 sound_manager->play_music(credits_music);
385 display_text_file("credits.txt");
387 Menu::set_current(main_menu);
389 case MNID_QUITMAINMENU:
394 else if(menu == options_menu)
396 process_options_menu();
398 else if(menu == load_game_menu)
400 if(event.key.keysym.sym == SDLK_DELETE)
402 int slot = menu->get_active_item_id();
403 std::stringstream stream;
405 std::string str = _("Are you sure you want to delete slot") + stream.str() + "?";
407 if(confirm_dialog(bkg_title, str.c_str())) {
408 str = "save/slot" + stream.str() + ".stsg";
409 printf("Removing: %s\n",str.c_str());
410 PHYSFS_delete(str.c_str());
413 update_load_save_game_menu(load_game_menu);
414 Menu::set_current(main_menu);
417 else if (process_load_game_menu())
422 else if(menu == contrib_menu)
424 check_levels_contrib_menu();
426 else if (menu == contrib_subset_menu)
428 check_contrib_subset_menu();
432 // reopen menu of user closed it (so that the app doesn't close when user
433 // accidently hit ESC)
434 if(Menu::current() == 0) {
435 Menu::set_current(main_menu);
438 context.do_drawing();
440 //frame_rate.update();
451 //delete img_choose_subset;