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
31 #include <SDL_image.h>
34 #include <sys/types.h>
39 #include "app/globals.h"
41 #include "video/screen.h"
42 #include "video/surface.h"
43 #include "high_scores.h"
45 #include "special/timer.h"
46 #include "special/frame_rate.h"
47 #include "app/setup.h"
49 #include "level_subset.h"
52 #include "leveleditor.h"
58 #include "resources.h"
59 #include "special/base.h"
60 #include "app/gettext.h"
64 static Surface* bkg_title;
66 static Surface* img_choose_subset;
69 static Timer 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(double frame_ratio)
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())
288 tux->key_event((SDLKey) keymap.jump,UP);
290 tux->key_event((SDLKey) keymap.jump,DOWN);
294 random_timer.start(rand() % 3000 + 3000);
298 // Wrap around at the end of the level back to the beginnig
299 if(world->solids->get_width() * 32 - 320 < tux->base.x)
302 world->camera->reset(Vector(tux->base.x, tux->base.y));
305 tux->can_jump = true;
306 float last_tux_x_pos = tux->base.x;
307 world->action(frame_ratio);
310 // disabled for now, since with the new jump code we easily get deadlocks
311 // Jump if tux stays in the same position for one loop, ie. if he is
312 // stuck behind a wall
313 if (last_tux_x_pos == tux->base.x)
318 world->draw(*titlesession->context);
321 /* --- TITLE SCREEN --- */
325 LevelEditor* leveleditor;
327 random_timer.init(true);
330 titlesession = new GameSession("misc/menu.stl", ST_GL_DEMO_GAME);
333 bkg_title = new Surface(datadir + "/images/background/arctis.jpg", false);
334 logo = new Surface(datadir + "/images/title/logo.png", true);
335 img_choose_subset = new Surface(datadir + "/images/status/choose-level-subset.png", true);
337 /* Generating contrib maps by only using a string_list */
338 worldmap_list = FileSystem::dfiles("levels/worldmap", "", "icyisland.stwm");
340 titlesession->get_current_sector()->activate();
341 titlesession->set_current();
343 /* --- Main title loop: --- */
346 frame_rate.set_frame_limit(false);
348 random_timer.start(rand() % 2000 + 2000);
350 Menu::set_current(main_menu);
351 DrawingContext& context = *titlesession->context;
352 while (Menu::current())
354 // if we spent to much time on a menu entry
355 frame_rate.smooth_hanger();
357 // Calculate the movement-factor
358 double frame_ratio = frame_rate.get();
360 if(frame_ratio > 1.5) /* Quick hack to correct the unprecise CPU clocks a little bit. */
361 frame_ratio = 1.5 + (frame_ratio - 1.5) * 0.85;
362 /* Lower the frame_ratio that Tux doesn't jump to hectically throught the demo. */
366 while (SDL_PollEvent(&event))
370 Menu::current()->event(event);
372 // FIXME: QUIT signal should be handled more generic, not locally
373 if (event.type == SDL_QUIT)
374 Menu::set_current(0);
377 /* Draw the background: */
378 draw_demo(frame_ratio);
381 if (Menu::current() == main_menu)
382 context.draw_surface(logo, Vector(screen->w/2 - logo->w/2, 30),
383 LAYER_FOREGROUND1+1);
385 context.draw_text(white_small_text, " SuperTux " VERSION "\n", Vector(0, screen->h - 70), LEFT_ALLIGN, LAYER_FOREGROUND1);
386 context.draw_text(white_small_text,
387 _("Copyright (c) 2003 SuperTux Devel Team\n"
388 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you\n"
389 "are welcome to redistribute it under certain conditions; see the file COPYING\n"
390 "for details.\n"), Vector(0, screen->h - 70 + white_small_text->get_height()), LEFT_ALLIGN, LAYER_FOREGROUND1);
392 /* Don't draw menu, if quit is true */
393 Menu* menu = Menu::current();
399 if(menu == main_menu)
401 switch (main_menu->check())
404 // Start Game, ie. goto the slots menu
405 update_load_save_game_menu(load_game_menu);
407 case MNID_LEVELS_CONTRIB:
409 puts("Entering contrib menu");
410 generate_contrib_menu();
412 case MNID_LEVELEDITOR:
413 leveleditor = new LevelEditor();
416 Menu::set_current(main_menu);
421 display_text_file("CREDITS", SCROLL_SPEED_CREDITS, white_big_text , white_text, white_small_text, blue_text );
423 Menu::set_current(main_menu);
425 case MNID_QUITMAINMENU:
426 Menu::set_current(0);
430 else if(menu == options_menu)
432 process_options_menu();
434 else if(menu == load_game_menu)
436 if(event.key.keysym.sym == SDLK_DELETE)
438 int slot = menu->get_active_item_id();
439 std::stringstream stream;
441 std::string str = _("Are you sure you want to delete slot") + stream.str() + "?";
443 if(confirm_dialog(bkg_title, str.c_str()))
445 str = st_save_dir + "/slot" + stream.str() + ".stsg";
446 printf("Removing: %s\n",str.c_str());
450 update_load_save_game_menu(load_game_menu);
451 Menu::set_current(main_menu);
454 else if (process_load_game_menu())
459 else if(menu == contrib_menu)
461 check_levels_contrib_menu();
463 else if (menu == contrib_subset_menu)
465 check_contrib_subset_menu();
469 mouse_cursor->draw(context);
471 context.do_drawing();
482 worldmap_list.clear();
486 delete img_choose_subset;