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 void update_load_save_game_menu(Menu* pmenu)
85 for(int i = 2; i < 7; ++i)
87 // FIXME: Insert a real savegame struct/class here instead of
89 std::string tmp = slotinfo(i - 1);
90 pmenu->item[i].kind = MN_ACTION;
91 pmenu->item[i].change_text(tmp.c_str());
95 void free_contrib_menu()
97 for(std::vector<LevelSubset*>::iterator i = contrib_subsets.begin();
98 i != contrib_subsets.end(); ++i)
101 contrib_subsets.clear();
102 contrib_menu->clear();
105 void generate_contrib_menu()
107 /** Generating contrib levels list by making use of Level Subset */
108 std::set<std::string> level_subsets = FileSystem::dsubdirs("/levels", "info");
112 contrib_menu->additem(MN_LABEL,_("Contrib Levels"),0,0);
113 contrib_menu->additem(MN_HL,"",0,0);
116 for(std::set<std::string>::iterator it = worldmap_list.begin(); it != worldmap_list.end(); ++it)
118 WorldMapNS::WorldMap worldmap;
119 worldmap.loadmap((*it).c_str());
120 contrib_menu->additem(MN_ACTION, worldmap.get_world_title(),0,0, i);
124 contrib_menu->additem(MN_HL,"",0,0);
126 first_level_index = i;
127 for (std::set<std::string>::iterator it = level_subsets.begin(); it != level_subsets.end(); ++it)
129 LevelSubset* subset = new LevelSubset();
131 if(subset->hide_from_contribs)
136 contrib_menu->additem(MN_GOTO, subset->title, 0, contrib_subset_menu, i);
137 contrib_subsets.push_back(subset);
141 contrib_menu->additem(MN_HL,"",0,0);
142 contrib_menu->additem(MN_BACK,_("Back"),0,0);
144 level_subsets.clear();
147 void check_levels_contrib_menu()
149 static int current_subset = -1;
151 int index = contrib_menu->check();
155 if((unsigned)index < worldmap_list.size())
157 WorldMapNS::WorldMap worldmap;
158 std::set<std::string>::iterator it = worldmap_list.begin();
159 for(int i = index; i > 0; --i)
162 std::string map_filename = *it;
164 worldmap.set_map_filename(map_filename);
166 // hack to erase the extension
167 unsigned int ext_pos = it->find_last_of(".");
168 if(ext_pos != std::string::npos)
169 map_filename.erase(ext_pos, map_filename.size() - ext_pos);
171 // TODO: slots should be available for contrib maps
172 worldmap.loadgame(st_save_dir + "/" + map_filename + "-slot1.stsg");
174 worldmap.display(); // run the map
176 Menu::set_current(main_menu);
178 else if (index < (int)contrib_subsets.size() + first_level_index)
180 index -= first_level_index;
181 if (current_subset != index)
183 current_subset = index;
184 // FIXME: This shouln't be busy looping
185 LevelSubset& subset = * (contrib_subsets[index]);
187 current_contrib_subset = ⊂
189 contrib_subset_menu->clear();
191 contrib_subset_menu->additem(MN_LABEL, subset.title, 0,0);
192 contrib_subset_menu->additem(MN_HL,"",0,0);
194 for (int i = 0; i < subset.get_num_levels(); ++i)
196 /** get level's title */
197 std::string level_title = "<no title>";
199 LispReader* reader = LispReader::load(subset.get_level_filename(i), "supertux-level");
202 std::cerr << "Error: Could not open level file. Ignoring...\n";
206 reader->read_string("name", level_title, true);
209 contrib_subset_menu->additem(MN_ACTION, level_title, 0, 0, i);
212 contrib_subset_menu->additem(MN_HL,"",0,0);
213 contrib_subset_menu->additem(MN_BACK, _("Back"), 0, 0);
215 titlesession->get_current_sector()->activate();
216 titlesession->set_current();
221 void check_contrib_subset_menu()
223 int index = contrib_subset_menu->check();
226 if (contrib_subset_menu->get_item_by_id(index).kind == MN_ACTION)
228 std::cout << "Starting level: " << index << std::endl;
231 current_contrib_subset->get_level_filename(index), ST_GL_PLAY);
233 player_status.reset();
234 Menu::set_current(main_menu);
235 titlesession->get_current_sector()->activate();
236 titlesession->set_current();
241 /* If the demo was stopped - because game started, level
242 editor was excuted, etc - call this when you get back
247 // FIXME: shouldn't be needed if GameSession
248 // didn't relay on global variables
249 titlesession->get_current_sector()->activate();
250 titlesession->set_current();
255 void draw_demo(double frame_ratio)
257 Sector* world = titlesession->get_current_sector();
258 Player* tux = world->player;
260 world->play_music(LEVEL_MUSIC);
262 global_frame_counter++;
263 tux->key_event((SDLKey) keymap.right,DOWN);
265 if(random_timer.check())
268 tux->key_event((SDLKey) keymap.jump,UP);
270 tux->key_event((SDLKey) keymap.jump,DOWN);
274 random_timer.start(rand() % 3000 + 3000);
278 // Wrap around at the end of the level back to the beginnig
279 if(world->solids->get_width() * 32 - 320 < tux->base.x)
282 world->camera->reset(Vector(tux->base.x, tux->base.y));
285 tux->can_jump = true;
286 float last_tux_x_pos = tux->base.x;
287 world->action(frame_ratio);
290 // disabled for now, since with the new jump code we easily get deadlocks
291 // Jump if tux stays in the same position for one loop, ie. if he is
292 // stuck behind a wall
293 if (last_tux_x_pos == tux->base.x)
298 world->draw(*titlesession->context);
301 /* --- TITLE SCREEN --- */
305 LevelEditor* leveleditor;
307 random_timer.init(true);
310 titlesession = new GameSession(datadir + "/levels/misc/menu.stl", ST_GL_DEMO_GAME);
313 bkg_title = new Surface(datadir + "/images/background/arctis.jpg", false);
314 logo = new Surface(datadir + "/images/title/logo.png", true);
315 img_choose_subset = new Surface(datadir + "/images/status/choose-level-subset.png", true);
317 /* Generating contrib maps by only using a string_list */
318 worldmap_list = FileSystem::dfiles("levels/worldmap", "", "icyisland.stwm");
320 titlesession->get_current_sector()->activate();
321 titlesession->set_current();
323 /* --- Main title loop: --- */
326 frame_rate.set_frame_limit(false);
328 random_timer.start(rand() % 2000 + 2000);
330 Menu::set_current(main_menu);
331 DrawingContext& context = *titlesession->context;
332 while (Menu::current())
334 // if we spent to much time on a menu entry
335 frame_rate.smooth_hanger();
337 // Calculate the movement-factor
338 double frame_ratio = frame_rate.get();
340 if(frame_ratio > 1.5) /* Quick hack to correct the unprecise CPU clocks a little bit. */
341 frame_ratio = 1.5 + (frame_ratio - 1.5) * 0.85;
342 /* Lower the frame_ratio that Tux doesn't jump to hectically throught the demo. */
346 while (SDL_PollEvent(&event))
350 Menu::current()->event(event);
352 // FIXME: QUIT signal should be handled more generic, not locally
353 if (event.type == SDL_QUIT)
354 Menu::set_current(0);
357 /* Draw the background: */
358 draw_demo(frame_ratio);
361 if (Menu::current() == main_menu)
362 context.draw_surface(logo, Vector(screen->w/2 - logo->w/2, 30),
363 LAYER_FOREGROUND1+1);
365 context.draw_text(white_small_text, " SuperTux " VERSION "\n", Vector(0, screen->h - 70), LEFT_ALLIGN, LAYER_FOREGROUND1);
366 context.draw_text(white_small_text,
367 _("Copyright (c) 2003 SuperTux Devel Team\n"
368 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you\n"
369 "are welcome to redistribute it under certain conditions; see the file COPYING\n"
370 "for details.\n"), Vector(0, screen->h - 70 + white_small_text->get_height()), LEFT_ALLIGN, LAYER_FOREGROUND1);
372 /* Don't draw menu, if quit is true */
373 Menu* menu = Menu::current();
379 if(menu == main_menu)
381 switch (main_menu->check())
384 // Start Game, ie. goto the slots menu
385 update_load_save_game_menu(load_game_menu);
387 case MNID_LEVELS_CONTRIB:
389 puts("Entering contrib menu");
390 generate_contrib_menu();
392 case MNID_LEVELEDITOR:
393 leveleditor = new LevelEditor();
396 Menu::set_current(main_menu);
400 display_text_file("CREDITS", SCROLL_SPEED_CREDITS, white_big_text , white_text, white_small_text, blue_text );
401 Menu::set_current(main_menu);
403 case MNID_QUITMAINMENU:
404 Menu::set_current(0);
408 else if(menu == options_menu)
410 process_options_menu();
412 else if(menu == load_game_menu)
414 if(event.key.keysym.sym == SDLK_DELETE)
416 int slot = menu->get_active_item_id();
417 std::stringstream stream;
419 std::string str = _("Are you sure you want to delete slot") + stream.str() + "?";
421 if(confirm_dialog(bkg_title, str.c_str()))
423 str = st_save_dir + "/slot" + stream.str() + ".stsg";
424 printf("Removing: %s\n",str.c_str());
428 update_load_save_game_menu(load_game_menu);
429 Menu::set_current(main_menu);
432 else if (process_load_game_menu())
437 else if(menu == contrib_menu)
439 check_levels_contrib_menu();
441 else if (menu == contrib_subset_menu)
443 check_contrib_subset_menu();
447 mouse_cursor->draw(context);
449 context.do_drawing();
460 worldmap_list.clear();
464 delete img_choose_subset;