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"
63 static Surface* bkg_title;
65 static Surface* img_choose_subset;
68 static Timer random_timer;
72 static GameSession* titlesession;
74 static std::vector<LevelSubset*> contrib_subsets;
75 static LevelSubset* current_contrib_subset = 0;
77 static std::set<std::string> worldmap_list;
79 static LevelEditor* leveleditor;
81 void update_load_save_game_menu(Menu* pmenu)
83 for(int i = 2; i < 7; ++i)
85 // FIXME: Insert a real savegame struct/class here instead of
87 std::string tmp = slotinfo(i - 1);
88 pmenu->item[i].kind = MN_ACTION;
89 pmenu->item[i].change_text(tmp.c_str());
93 void free_contrib_menu()
95 for(std::vector<LevelSubset*>::iterator i = contrib_subsets.begin();
96 i != contrib_subsets.end(); ++i)
99 contrib_subsets.clear();
100 contrib_menu->clear();
103 void generate_contrib_menu()
106 /** Generating contrib levels list by making use of Level Subset */
107 std::set<std::string> level_subsets = FileSystem::dsubdirs("/levels", "info");
111 contrib_menu->additem(MN_LABEL,_("Contrib Levels"),0,0);
112 contrib_menu->additem(MN_HL,"",0,0);
115 for (std::set<std::string>::iterator it = level_subsets.begin(); it != level_subsets.end(); ++it)
117 LevelSubset* subset = new LevelSubset();
118 subset->load((*it).c_str());
119 contrib_menu->additem(MN_GOTO, subset->title.c_str(), i,
120 contrib_subset_menu);
121 contrib_subsets.push_back(subset);
126 for(std::set<std::string>::iterator it = worldmap_list.begin(); it != worldmap_list.end(); ++it)
128 WorldMapNS::WorldMap worldmap;
129 worldmap.loadmap((*it).c_str());
130 contrib_menu->additem(MN_ACTION, worldmap.get_world_title(),0,0, i + level_subsets.size());
134 contrib_menu->additem(MN_HL,"",0,0);
135 contrib_menu->additem(MN_BACK,_("Back"),0,0);
137 level_subsets.clear();
140 void check_levels_contrib_menu()
142 static int current_subset = -1;
144 int index = contrib_menu->check();
148 if (index < (int)contrib_subsets.size())
150 if (current_subset != index)
152 current_subset = index;
153 // FIXME: This shouln't be busy looping
154 LevelSubset& subset = * (contrib_subsets[index]);
156 current_contrib_subset = ⊂
158 contrib_subset_menu->clear();
160 contrib_subset_menu->additem(MN_LABEL, subset.title, 0,0);
161 contrib_subset_menu->additem(MN_HL,"",0,0);
163 for (int i = 0; i < subset.get_num_levels(); ++i)
165 /** get level's title */
166 std::string level_title = "<no title>";
168 LispReader* reader = LispReader::load(subset.get_level_filename(i), "supertux-level");
171 std::cerr << "Error: Could not open level file. Ignoring...\n";
175 reader->read_string("name", level_title, true);
178 contrib_subset_menu->additem(MN_ACTION, level_title, 0, 0, i);
181 contrib_subset_menu->additem(MN_HL,"",0,0);
182 contrib_subset_menu->additem(MN_BACK, _("Back"), 0, 0);
184 titlesession->get_current_sector()->activate();
185 titlesession->set_current();
188 else if((unsigned)index < worldmap_list.size() + (int)contrib_subsets.size())
190 WorldMapNS::WorldMap worldmap;
191 std::set<std::string>::iterator it = worldmap_list.begin();
192 for(int i = index - contrib_subsets.size(); i > 0; --i)
194 worldmap.loadmap((*it));
197 Menu::set_current(main_menu);
201 void check_contrib_subset_menu()
203 int index = contrib_subset_menu->check();
206 if (contrib_subset_menu->get_item_by_id(index).kind == MN_ACTION)
208 std::cout << "Starting level: " << index << std::endl;
211 current_contrib_subset->get_level_filename(index), ST_GL_PLAY);
213 player_status.reset();
214 Menu::set_current(main_menu);
215 titlesession->get_current_sector()->activate();
216 titlesession->set_current();
221 void draw_demo(double frame_ratio)
223 Sector* world = titlesession->get_current_sector();
224 Player* tux = world->player;
226 world->play_music(LEVEL_MUSIC);
228 global_frame_counter++;
229 tux->key_event((SDLKey) keymap.right,DOWN);
231 if(random_timer.check())
234 tux->key_event((SDLKey) keymap.jump,UP);
236 tux->key_event((SDLKey) keymap.jump,DOWN);
240 random_timer.start(rand() % 3000 + 3000);
244 // Wrap around at the end of the level back to the beginnig
245 if(world->solids->get_width() * 32 - 320 < tux->base.x)
250 tux->can_jump = true;
251 float last_tux_x_pos = tux->base.x;
252 world->action(frame_ratio);
255 // disabled for now, since with the new jump code we easily get deadlocks
256 // Jump if tux stays in the same position for one loop, ie. if he is
257 // stuck behind a wall
258 if (last_tux_x_pos == tux->base.x)
263 world->draw(*titlesession->context);
266 /* --- TITLE SCREEN --- */
269 random_timer.init(true);
275 titlesession = new GameSession(datadir + "/levels/misc/menu.stl", ST_GL_DEMO_GAME);
278 bkg_title = new Surface(datadir + "/images/background/arctis.jpg", false);
279 logo = new Surface(datadir + "/images/title/logo.png", true);
280 img_choose_subset = new Surface(datadir + "/images/status/choose-level-subset.png", true);
282 /* Generating contrib maps by only using a string_list */
283 worldmap_list = FileSystem::dfiles("levels/worldmap", "", "icyisland.stwm");
285 titlesession->get_current_sector()->activate();
286 titlesession->set_current();
288 /* --- Main title loop: --- */
291 FrameRate frame_rate(100);
292 frame_rate.set_frame_limit(false);
294 random_timer.start(rand() % 2000 + 2000);
296 Menu::set_current(main_menu);
297 DrawingContext& context = *titlesession->context;
298 while (Menu::current())
300 // if we spent to much time on a menu entry
301 frame_rate.smooth_hanger();
303 // Calculate the movement-factor
304 double frame_ratio = frame_rate.get();
306 if(frame_ratio > 1.5) /* Quick hack to correct the unprecise CPU clocks a little bit. */
307 frame_ratio = 1.5 + (frame_ratio - 1.5) * 0.85;
308 /* Lower the frame_ratio that Tux doesn't jump to hectically throught the demo. */
312 while (SDL_PollEvent(&event))
316 Menu::current()->event(event);
318 // FIXME: QUIT signal should be handled more generic, not locally
319 if (event.type == SDL_QUIT)
320 Menu::set_current(0);
323 /* Draw the background: */
324 draw_demo(frame_ratio);
327 if (Menu::current() == main_menu)
328 context.draw_surface(logo, Vector(screen->w/2 - logo->w/2, 30),
329 LAYER_FOREGROUND1+1);
331 context.draw_text(white_small_text, " SuperTux " VERSION "\n", Vector(0, screen->h - 70), LAYER_FOREGROUND1);
332 context.draw_text(white_small_text,
333 _("Copyright (c) 2003 SuperTux Devel Team\n"
334 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you\n"
335 "are welcome to redistribute it under certain conditions; see the file COPYING\n"
336 "for details.\n"), Vector(0, screen->h - 70 + white_small_text->get_height()), LAYER_FOREGROUND1);
338 /* Don't draw menu, if quit is true */
339 Menu* menu = Menu::current();
345 if(menu == main_menu)
347 switch (main_menu->check())
350 // Start Game, ie. goto the slots menu
351 update_load_save_game_menu(load_game_menu);
353 case MNID_LEVELS_CONTRIB:
355 puts("Entering contrib menu");
356 generate_contrib_menu();
358 case MNID_LEVELEDITOR:
359 leveleditor = new LevelEditor();
362 Menu::set_current(main_menu);
366 display_text_file("CREDITS", SCROLL_SPEED_CREDITS, white_big_text , white_text, white_small_text, blue_text );
367 Menu::set_current(main_menu);
369 case MNID_QUITMAINMENU:
370 Menu::set_current(0);
374 else if(menu == options_menu)
376 process_options_menu();
378 else if(menu == load_game_menu)
380 if(event.key.keysym.sym == SDLK_DELETE)
382 int slot = menu->get_active_item_id();
383 std::stringstream stream;
385 std::string str = _("Are you sure you want to delete slot") + stream.str() + "?";
387 if(confirm_dialog(bkg_title, str.c_str()))
389 str = st_save_dir + "/slot" + stream.str() + ".stsg";
390 printf("Removing: %s\n",str.c_str());
394 update_load_save_game_menu(load_game_menu);
395 Menu::set_current(main_menu);
398 else if (process_load_game_menu())
400 // FIXME: shouldn't be needed if GameSession doesn't relay on global variables
401 titlesession->get_current_sector()->activate();
402 titlesession->set_current();
403 //titletux.level_begin();
407 else if(menu == contrib_menu)
409 check_levels_contrib_menu();
411 else if (menu == contrib_subset_menu)
413 check_contrib_subset_menu();
417 mouse_cursor->draw(context);
419 context.do_drawing();
430 worldmap_list.clear();
434 delete img_choose_subset;