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"
44 #include "high_scores.h"
47 #include "special/frame_rate.h"
48 #include "app/setup.h"
50 #include "level_subset.h"
53 #include "leveleditor.h"
59 #include "resources.h"
60 #include "app/gettext.h"
64 static Surface* bkg_title;
66 static Surface* img_choose_subset;
69 static Timer2 random_timer;
73 static GameSession* titlesession;
75 static std::vector<LevelSubset*> contrib_subsets;
76 static LevelSubset* current_contrib_subset = 0;
77 static int first_level_index;
79 static std::set<std::string> worldmap_list;
81 static FrameRate frame_rate(100);
83 /* If the demo was stopped - because game started, level
84 editor was excuted, etc - call this when you get back
89 // FIXME: shouldn't be needed if GameSession
90 // didn't relay on global variables
91 titlesession->get_current_sector()->activate();
92 titlesession->set_current();
97 void update_load_save_game_menu(Menu* pmenu)
99 for(int i = 2; i < 7; ++i)
101 // FIXME: Insert a real savegame struct/class here instead of
102 // doing string vodoo
103 std::string tmp = slotinfo(i - 1);
104 pmenu->item[i].kind = MN_ACTION;
105 pmenu->item[i].change_text(tmp.c_str());
109 void free_contrib_menu()
111 for(std::vector<LevelSubset*>::iterator i = contrib_subsets.begin();
112 i != contrib_subsets.end(); ++i)
115 contrib_subsets.clear();
116 contrib_menu->clear();
119 void generate_contrib_menu()
121 /** Generating contrib levels list by making use of Level Subset */
122 std::set<std::string> level_subsets = FileSystem::dsubdirs("/levels", "info");
126 contrib_menu->additem(MN_LABEL,_("Contrib Levels"),0,0);
127 contrib_menu->additem(MN_HL,"",0,0);
130 for(std::set<std::string>::iterator it = worldmap_list.begin(); it != worldmap_list.end(); ++it)
132 WorldMapNS::WorldMap worldmap;
133 worldmap.loadmap((*it).c_str());
134 contrib_menu->additem(MN_ACTION, worldmap.get_world_title(),0,0, i);
138 contrib_menu->additem(MN_HL,"",0,0);
140 first_level_index = i;
141 for (std::set<std::string>::iterator it = level_subsets.begin(); it != level_subsets.end(); ++it)
143 LevelSubset* subset = new LevelSubset();
145 if(subset->hide_from_contribs)
150 contrib_menu->additem(MN_GOTO, subset->title, 0, contrib_subset_menu, i);
151 contrib_subsets.push_back(subset);
155 contrib_menu->additem(MN_HL,"",0,0);
156 contrib_menu->additem(MN_BACK,_("Back"),0,0);
158 level_subsets.clear();
161 void check_levels_contrib_menu()
163 static int current_subset = -1;
165 int index = contrib_menu->check();
169 if((unsigned)index < worldmap_list.size())
171 WorldMapNS::WorldMap worldmap;
172 std::set<std::string>::iterator it = worldmap_list.begin();
173 for(int i = index; i > 0; --i)
176 std::string map_filename = *it;
180 DrawingContext context;
181 context.draw_text(white_text, "Loading...",
182 Vector(screen->w/2, screen->h/2), CENTER_ALLIGN, LAYER_FOREGROUND1);
183 context.do_drawing();
185 worldmap.set_map_filename(map_filename);
187 // hack to erase the extension
188 unsigned int ext_pos = it->find_last_of(".");
189 if(ext_pos != std::string::npos)
190 map_filename.erase(ext_pos, map_filename.size() - ext_pos);
192 // TODO: slots should be available for contrib maps
193 worldmap.loadgame(st_save_dir + "/" + map_filename + "-slot1.stsg");
195 worldmap.display(); // run the map
197 Menu::set_current(main_menu);
200 else if (index < (int)contrib_subsets.size() + first_level_index)
202 index -= first_level_index;
203 if (current_subset != index)
205 current_subset = index;
206 // FIXME: This shouln't be busy looping
207 LevelSubset& subset = * (contrib_subsets[index]);
209 current_contrib_subset = ⊂
211 contrib_subset_menu->clear();
213 contrib_subset_menu->additem(MN_LABEL, subset.title, 0,0);
214 contrib_subset_menu->additem(MN_HL,"",0,0);
216 for (int i = 0; i < subset.get_num_levels(); ++i)
218 /** get level's title */
219 std::string level_title = "<no title>";
221 std::string filename = subset.get_level_filename(i);
222 std::string filepath;
223 filepath = st_dir + "/levels/" + filename;
224 if (access(filepath.c_str(), R_OK) != 0)
226 filepath = datadir + "/levels/" + filename;
227 if (access(filepath.c_str(), R_OK) != 0)
229 std::cerr << "Error: Level: couldn't find level: " << filename << std::endl;
234 LispReader* reader = LispReader::load(filepath, "supertux-level");
237 std::cerr << "Error: Could not open level file. Ignoring...\n";
241 reader->read_string("name", level_title, true);
244 contrib_subset_menu->additem(MN_ACTION, level_title, 0, 0, i);
247 contrib_subset_menu->additem(MN_HL,"",0,0);
248 contrib_subset_menu->additem(MN_BACK, _("Back"), 0, 0);
250 titlesession->get_current_sector()->activate();
251 titlesession->set_current();
256 void check_contrib_subset_menu()
258 int index = contrib_subset_menu->check();
261 if (contrib_subset_menu->get_item_by_id(index).kind == MN_ACTION)
263 std::cout << "Starting level: " << index << std::endl;
266 current_contrib_subset->get_level_filename(index), ST_GL_PLAY);
268 player_status.reset();
269 Menu::set_current(main_menu);
275 void draw_demo(float elapsed_time)
277 Sector* world = titlesession->get_current_sector();
278 Player* tux = world->player;
280 world->play_music(LEVEL_MUSIC);
282 global_frame_counter++;
283 tux->key_event((SDLKey) keymap.right,DOWN);
285 if(random_timer.check()) {
286 random_timer.start(float(rand() % 3000 + 3000) / 1000.);
290 tux->key_event((SDLKey) keymap.jump,UP);
292 tux->key_event((SDLKey) keymap.jump,DOWN);
295 // Wrap around at the end of the level back to the beginnig
296 if(world->solids->get_width() * 32 - 320 < tux->get_pos().x)
299 world->camera->reset(tux->get_pos());
302 tux->can_jump = true;
303 float last_tux_x_pos = tux->get_pos().x;
304 world->action(elapsed_time);
307 // disabled for now, since with the new jump code we easily get deadlocks
308 // Jump if tux stays in the same position for one loop, ie. if he is
309 // stuck behind a wall
310 if (last_tux_x_pos == tux->get_pos().x)
315 world->draw(*titlesession->context);
318 /* --- TITLE SCREEN --- */
322 LevelEditor* leveleditor;
326 titlesession = new GameSession("misc/menu.stl", ST_GL_DEMO_GAME);
329 bkg_title = new Surface(datadir + "/images/background/arctis.jpg", false);
330 logo = new Surface(datadir + "/images/title/logo.png", true);
331 img_choose_subset = new Surface(datadir + "/images/status/choose-level-subset.png", true);
333 /* Generating contrib maps by only using a string_list */
334 worldmap_list = FileSystem::dfiles("levels/worldmap", "", "icyisland.stwm");
336 titlesession->get_current_sector()->activate();
337 titlesession->set_current();
339 /* --- Main title loop: --- */
342 random_timer.start(float(rand() % 2000 + 2000) / 1000.0);
344 Uint32 lastticks = SDL_GetTicks();
346 Menu::set_current(main_menu);
347 DrawingContext& context = *titlesession->context;
348 while (Menu::current())
350 // Calculate the movement-factor
351 Uint32 ticks = SDL_GetTicks();
352 float elapsed_time = float(ticks - lastticks) / 1000.;
353 global_time += elapsed_time;
356 if(elapsed_time > .04)
359 /* Lower the speed so that Tux doesn't jump too hectically throught
364 while (SDL_PollEvent(&event))
368 Menu::current()->event(event);
370 // FIXME: QUIT signal should be handled more generic, not locally
371 if (event.type == SDL_QUIT)
372 Menu::set_current(0);
375 /* Draw the background: */
376 draw_demo(elapsed_time);
379 if (Menu::current() == main_menu)
380 context.draw_surface(logo, Vector(screen->w/2 - logo->w/2, 30),
381 LAYER_FOREGROUND1+1);
383 context.draw_text(white_small_text, " SuperTux " PACKAGE_VERSION "\n",
384 Vector(0, screen->h - 70), LEFT_ALLIGN, LAYER_FOREGROUND1);
385 context.draw_text(white_small_text,
386 _("Copyright (c) 2003 SuperTux Devel Team\n"
387 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you\n"
388 "are welcome to redistribute it under certain conditions; see the file COPYING\n"
389 "for details.\n"), Vector(0, screen->h - 70 + white_small_text->get_height()), LEFT_ALLIGN, LAYER_FOREGROUND1);
391 /* Don't draw menu, if quit is true */
392 Menu* menu = Menu::current();
398 if(menu == main_menu)
400 switch (main_menu->check())
403 // Start Game, ie. goto the slots menu
404 update_load_save_game_menu(load_game_menu);
406 case MNID_LEVELS_CONTRIB:
408 puts("Entering contrib menu");
409 generate_contrib_menu();
411 case MNID_LEVELEDITOR:
412 leveleditor = new LevelEditor();
415 Menu::set_current(main_menu);
420 display_text_file("credits.txt", SCROLL_SPEED_CREDITS, white_big_text , white_text, white_small_text, blue_text );
422 Menu::set_current(main_menu);
424 case MNID_QUITMAINMENU:
425 Menu::set_current(0);
429 else if(menu == options_menu)
431 process_options_menu();
433 else if(menu == load_game_menu)
435 if(event.key.keysym.sym == SDLK_DELETE)
437 int slot = menu->get_active_item_id();
438 std::stringstream stream;
440 std::string str = _("Are you sure you want to delete slot") + stream.str() + "?";
442 if(confirm_dialog(bkg_title, str.c_str()))
444 str = st_save_dir + "/slot" + stream.str() + ".stsg";
445 printf("Removing: %s\n",str.c_str());
449 update_load_save_game_menu(load_game_menu);
450 Menu::set_current(main_menu);
453 else if (process_load_game_menu())
458 else if(menu == contrib_menu)
460 check_levels_contrib_menu();
462 else if (menu == contrib_subset_menu)
464 check_contrib_subset_menu();
468 mouse_cursor->draw(context);
470 context.do_drawing();
480 worldmap_list.clear();
484 delete img_choose_subset;