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
32 #include <SDL_image.h>
35 #include <sys/types.h>
40 #include "app/globals.h"
42 #include "video/screen.h"
43 #include "video/surface.h"
46 #include "special/frame_rate.h"
47 #include "app/setup.h"
48 #include "lisp/lisp.h"
49 #include "lisp/parser.h"
51 #include "level_subset.h"
54 #include "leveleditor.h"
58 #include "object/tilemap.h"
59 #include "object/camera.h"
60 #include "object/player.h"
61 #include "resources.h"
62 #include "app/gettext.h"
65 static Surface* bkg_title;
67 static Surface* img_choose_subset;
70 static Timer2 random_timer;
74 static GameSession* titlesession;
76 static std::vector<LevelSubset*> contrib_subsets;
77 static LevelSubset* current_contrib_subset = 0;
78 static int first_level_index;
80 static std::set<std::string> worldmap_list;
82 static FrameRate frame_rate(100);
84 /* If the demo was stopped - because game started, level
85 editor was excuted, etc - call this when you get back
90 // FIXME: shouldn't be needed if GameSession
91 // didn't relay on global variables
92 titlesession->get_current_sector()->activate();
93 titlesession->set_current();
98 void update_load_save_game_menu(Menu* pmenu)
100 for(int i = 2; i < 7; ++i)
102 // FIXME: Insert a real savegame struct/class here instead of
103 // doing string vodoo
104 std::string tmp = slotinfo(i - 1);
105 pmenu->item[i].kind = MN_ACTION;
106 pmenu->item[i].change_text(tmp.c_str());
110 void free_contrib_menu()
112 for(std::vector<LevelSubset*>::iterator i = contrib_subsets.begin();
113 i != contrib_subsets.end(); ++i)
116 contrib_subsets.clear();
117 contrib_menu->clear();
120 void generate_contrib_menu()
122 /** Generating contrib levels list by making use of Level Subset */
123 std::set<std::string> level_subsets = FileSystem::dsubdirs("/levels", "info");
127 contrib_menu->additem(MN_LABEL,_("Contrib Levels"),0,0);
128 contrib_menu->additem(MN_HL,"",0,0);
131 for(std::set<std::string>::iterator it = worldmap_list.begin();
132 it != worldmap_list.end(); ++it) {
133 WorldMapNS::WorldMap worldmap;
134 worldmap.loadmap((*it).c_str());
135 contrib_menu->additem(MN_ACTION, worldmap.get_world_title(),0,0, i);
139 contrib_menu->additem(MN_HL,"",0,0);
141 first_level_index = i;
142 for (std::set<std::string>::iterator it = level_subsets.begin(); it != level_subsets.end(); ++it)
144 LevelSubset* subset = new LevelSubset();
146 if(subset->hide_from_contribs)
151 contrib_menu->additem(MN_GOTO, subset->title, 0, contrib_subset_menu, i);
152 contrib_subsets.push_back(subset);
156 contrib_menu->additem(MN_HL,"",0,0);
157 contrib_menu->additem(MN_BACK,_("Back"),0,0);
159 level_subsets.clear();
162 std::string get_level_name(const std::string& filename)
166 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
168 const lisp::Lisp* level = root->get_lisp("supertux-level");
173 level->get("name", name);
175 } catch(std::exception& e) {
176 std::cerr << "Problem getting name of '" << filename << "'.\n";
181 void check_levels_contrib_menu()
183 static int current_subset = -1;
185 int index = contrib_menu->check();
189 if((unsigned)index < worldmap_list.size())
191 WorldMapNS::WorldMap worldmap;
192 std::set<std::string>::iterator it = worldmap_list.begin();
193 for(int i = index; i > 0; --i)
196 std::string map_filename = *it;
200 DrawingContext context;
201 context.draw_text(white_text, "Loading...",
202 Vector(screen->w/2, screen->h/2), CENTER_ALLIGN, LAYER_FOREGROUND1);
203 context.do_drawing();
205 worldmap.set_map_filename(map_filename);
207 // hack to erase the extension
208 unsigned int ext_pos = it->find_last_of(".");
209 if(ext_pos != std::string::npos)
210 map_filename.erase(ext_pos, map_filename.size() - ext_pos);
212 // TODO: slots should be available for contrib maps
213 worldmap.loadgame(st_save_dir + "/" + map_filename + "-slot1.stsg");
215 worldmap.display(); // run the map
217 Menu::set_current(main_menu);
220 else if (index < (int)contrib_subsets.size() + first_level_index)
222 index -= first_level_index;
223 if (current_subset != index)
225 current_subset = index;
226 // FIXME: This shouln't be busy looping
227 LevelSubset& subset = * (contrib_subsets[index]);
229 current_contrib_subset = ⊂
231 contrib_subset_menu->clear();
233 contrib_subset_menu->additem(MN_LABEL, subset.title, 0,0);
234 contrib_subset_menu->additem(MN_HL,"",0,0);
236 for (int i = 0; i < subset.get_num_levels(); ++i)
238 /** get level's title */
239 std::string filename = subset.get_level_filename(i);
240 std::string title = get_level_name(filename);
241 contrib_subset_menu->additem(MN_ACTION, title, 0, 0, i);
244 contrib_subset_menu->additem(MN_HL,"",0,0);
245 contrib_subset_menu->additem(MN_BACK, _("Back"), 0, 0);
247 titlesession->get_current_sector()->activate();
248 titlesession->set_current();
253 void check_contrib_subset_menu()
255 int index = contrib_subset_menu->check();
258 if (contrib_subset_menu->get_item_by_id(index).kind == MN_ACTION)
260 std::cout << "Starting level: " << index << std::endl;
263 current_contrib_subset->get_level_filename(index), ST_GL_PLAY);
265 player_status.reset();
266 Menu::set_current(main_menu);
272 void draw_demo(float elapsed_time)
274 Sector* world = titlesession->get_current_sector();
275 Player* tux = world->player;
277 world->play_music(LEVEL_MUSIC);
279 tux->key_event((SDLKey) keymap.right,DOWN);
281 if(random_timer.check()) {
282 random_timer.start(float(rand() % 3000 + 3000) / 1000.);
286 tux->key_event((SDLKey) keymap.jump,UP);
288 tux->key_event((SDLKey) keymap.jump,DOWN);
291 // Wrap around at the end of the level back to the beginnig
292 if(world->solids->get_width() * 32 - 320 < tux->get_pos().x)
295 world->camera->reset(tux->get_pos());
298 tux->can_jump = true;
299 float last_tux_x_pos = tux->get_pos().x;
300 world->action(elapsed_time);
303 // disabled for now, since with the new jump code we easily get deadlocks
304 // Jump if tux stays in the same position for one loop, ie. if he is
305 // stuck behind a wall
306 if (last_tux_x_pos == tux->get_pos().x)
311 world->draw(*titlesession->context);
314 /* --- TITLE SCREEN --- */
318 LevelEditor* leveleditor;
322 titlesession = new GameSession(get_resource_filename("levels/misc/menu.stl"),
326 bkg_title = new Surface(datadir + "/images/background/arctis.jpg", false);
327 logo = new Surface(datadir + "/images/title/logo.png", true);
328 img_choose_subset = new Surface(datadir + "/images/status/choose-level-subset.png", true);
330 /* Generating contrib maps by only using a string_list */
331 worldmap_list = FileSystem::dfiles("levels/worldmap", "", "icyisland.stwm");
333 titlesession->get_current_sector()->activate();
334 titlesession->set_current();
336 /* --- Main title loop: --- */
339 random_timer.start(float(rand() % 2000 + 2000) / 1000.0);
341 Uint32 lastticks = SDL_GetTicks();
343 Menu::set_current(main_menu);
344 DrawingContext& context = *titlesession->context;
345 while (Menu::current())
347 // Calculate the movement-factor
348 Uint32 ticks = SDL_GetTicks();
349 float elapsed_time = float(ticks - lastticks) / 1000.;
350 global_time += elapsed_time;
353 if(elapsed_time > .04)
356 /* Lower the speed so that Tux doesn't jump too hectically throught
361 while (SDL_PollEvent(&event))
365 Menu::current()->event(event);
367 // FIXME: QUIT signal should be handled more generic, not locally
368 if (event.type == SDL_QUIT)
369 Menu::set_current(0);
372 /* Draw the background: */
373 draw_demo(elapsed_time);
376 if (Menu::current() == main_menu)
377 context.draw_surface(logo, Vector(screen->w/2 - logo->w/2, 30),
378 LAYER_FOREGROUND1+1);
380 context.draw_text(white_small_text, " SuperTux " PACKAGE_VERSION "\n",
381 Vector(0, screen->h - 70), LEFT_ALLIGN, LAYER_FOREGROUND1);
382 context.draw_text(white_small_text,
383 _("Copyright (c) 2003 SuperTux Devel Team\n"
384 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you\n"
385 "are welcome to redistribute it under certain conditions; see the file COPYING\n"
386 "for details.\n"), Vector(0, screen->h - 70 + white_small_text->get_height()), LEFT_ALLIGN, LAYER_FOREGROUND1);
388 /* Don't draw menu, if quit is true */
389 Menu* menu = Menu::current();
395 if(menu == main_menu)
397 switch (main_menu->check())
400 // Start Game, ie. goto the slots menu
401 update_load_save_game_menu(load_game_menu);
403 case MNID_LEVELS_CONTRIB:
405 puts("Entering contrib menu");
406 generate_contrib_menu();
408 case MNID_LEVELEDITOR:
409 leveleditor = new LevelEditor();
412 Menu::set_current(main_menu);
417 display_text_file("credits.txt", SCROLL_SPEED_CREDITS, white_big_text , white_text, white_small_text, blue_text );
419 Menu::set_current(main_menu);
421 case MNID_QUITMAINMENU:
422 Menu::set_current(0);
426 else if(menu == options_menu)
428 process_options_menu();
430 else if(menu == load_game_menu)
432 if(event.key.keysym.sym == SDLK_DELETE)
434 int slot = menu->get_active_item_id();
435 std::stringstream stream;
437 std::string str = _("Are you sure you want to delete slot") + stream.str() + "?";
439 if(confirm_dialog(bkg_title, str.c_str()))
441 str = st_save_dir + "/slot" + stream.str() + ".stsg";
442 printf("Removing: %s\n",str.c_str());
446 update_load_save_game_menu(load_game_menu);
447 Menu::set_current(main_menu);
450 else if (process_load_game_menu())
455 else if(menu == contrib_menu)
457 check_levels_contrib_menu();
459 else if (menu == contrib_subset_menu)
461 check_contrib_subset_menu();
465 mouse_cursor->draw(context);
467 context.do_drawing();
477 worldmap_list.clear();
481 delete img_choose_subset;