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;
78 static std::set<std::string> worldmap_list;
80 static LevelEditor* leveleditor;
82 void update_load_save_game_menu(Menu* pmenu)
84 for(int i = 2; i < 7; ++i)
86 // FIXME: Insert a real savegame struct/class here instead of
88 std::string tmp = slotinfo(i - 1);
89 pmenu->item[i].kind = MN_ACTION;
90 pmenu->item[i].change_text(tmp.c_str());
94 void free_contrib_menu()
96 for(std::vector<LevelSubset*>::iterator i = contrib_subsets.begin();
97 i != contrib_subsets.end(); ++i)
100 contrib_subsets.clear();
101 contrib_menu->clear();
104 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);
114 for (std::set<std::string>::iterator it = level_subsets.begin(); it != level_subsets.end(); ++it)
116 LevelSubset* subset = new LevelSubset();
117 subset->load((*it).c_str());
118 contrib_menu->additem(MN_GOTO, subset->title, 0, contrib_subset_menu, i);
119 contrib_subsets.push_back(subset);
123 i = level_subsets.size();
124 for(std::set<std::string>::iterator it = worldmap_list.begin(); it != worldmap_list.end(); ++it)
126 WorldMapNS::WorldMap worldmap;
127 worldmap.loadmap((*it).c_str());
128 contrib_menu->additem(MN_ACTION, worldmap.get_world_title(),0,0, i);
132 contrib_menu->additem(MN_HL,"",0,0);
133 contrib_menu->additem(MN_BACK,_("Back"),0,0);
135 level_subsets.clear();
138 void check_levels_contrib_menu()
140 static int current_subset = -1;
142 int index = contrib_menu->check();
146 if (index < (int)contrib_subsets.size())
148 if (current_subset != index)
150 current_subset = index;
151 // FIXME: This shouln't be busy looping
152 LevelSubset& subset = * (contrib_subsets[index]);
154 current_contrib_subset = ⊂
156 contrib_subset_menu->clear();
158 contrib_subset_menu->additem(MN_LABEL, subset.title, 0,0);
159 contrib_subset_menu->additem(MN_HL,"",0,0);
161 for (int i = 0; i < subset.get_num_levels(); ++i)
163 /** get level's title */
164 std::string level_title = "<no title>";
166 LispReader* reader = LispReader::load(subset.get_level_filename(i), "supertux-level");
169 std::cerr << "Error: Could not open level file. Ignoring...\n";
173 reader->read_string("name", level_title, true);
176 contrib_subset_menu->additem(MN_ACTION, level_title, 0, 0, i);
179 contrib_subset_menu->additem(MN_HL,"",0,0);
180 contrib_subset_menu->additem(MN_BACK, _("Back"), 0, 0);
182 titlesession->get_current_sector()->activate();
183 titlesession->set_current();
186 else if((unsigned)index < worldmap_list.size() + (int)contrib_subsets.size())
188 WorldMapNS::WorldMap worldmap;
189 std::set<std::string>::iterator it = worldmap_list.begin();
190 for(int i = index - contrib_subsets.size(); i > 0; --i)
192 worldmap.loadmap((*it));
195 Menu::set_current(main_menu);
199 void check_contrib_subset_menu()
201 int index = contrib_subset_menu->check();
204 if (contrib_subset_menu->get_item_by_id(index).kind == MN_ACTION)
206 std::cout << "Starting level: " << index << std::endl;
209 current_contrib_subset->get_level_filename(index), ST_GL_PLAY);
211 player_status.reset();
212 Menu::set_current(main_menu);
213 titlesession->get_current_sector()->activate();
214 titlesession->set_current();
219 void draw_demo(double frame_ratio)
221 Sector* world = titlesession->get_current_sector();
222 Player* tux = world->player;
224 world->play_music(LEVEL_MUSIC);
226 global_frame_counter++;
227 tux->key_event((SDLKey) keymap.right,DOWN);
229 if(random_timer.check())
232 tux->key_event((SDLKey) keymap.jump,UP);
234 tux->key_event((SDLKey) keymap.jump,DOWN);
238 random_timer.start(rand() % 3000 + 3000);
242 // Wrap around at the end of the level back to the beginnig
243 if(world->solids->get_width() * 32 - 320 < tux->base.x)
246 world->camera->reset(Vector(tux->base.x, tux->base.y));
249 tux->can_jump = true;
250 float last_tux_x_pos = tux->base.x;
251 world->action(frame_ratio);
254 // disabled for now, since with the new jump code we easily get deadlocks
255 // Jump if tux stays in the same position for one loop, ie. if he is
256 // stuck behind a wall
257 if (last_tux_x_pos == tux->base.x)
262 world->draw(*titlesession->context);
265 /* --- TITLE SCREEN --- */
268 random_timer.init(true);
274 titlesession = new GameSession(datadir + "/levels/misc/menu.stl", ST_GL_DEMO_GAME);
277 bkg_title = new Surface(datadir + "/images/background/arctis.jpg", false);
278 logo = new Surface(datadir + "/images/title/logo.png", true);
279 img_choose_subset = new Surface(datadir + "/images/status/choose-level-subset.png", true);
281 /* Generating contrib maps by only using a string_list */
282 worldmap_list = FileSystem::dfiles("levels/worldmap", "", "icyisland.stwm");
284 titlesession->get_current_sector()->activate();
285 titlesession->set_current();
287 /* --- Main title loop: --- */
290 FrameRate frame_rate(100);
291 frame_rate.set_frame_limit(false);
293 random_timer.start(rand() % 2000 + 2000);
295 Menu::set_current(main_menu);
296 DrawingContext& context = *titlesession->context;
297 while (Menu::current())
299 // if we spent to much time on a menu entry
300 frame_rate.smooth_hanger();
302 // Calculate the movement-factor
303 double frame_ratio = frame_rate.get();
305 if(frame_ratio > 1.5) /* Quick hack to correct the unprecise CPU clocks a little bit. */
306 frame_ratio = 1.5 + (frame_ratio - 1.5) * 0.85;
307 /* Lower the frame_ratio that Tux doesn't jump to hectically throught the demo. */
311 while (SDL_PollEvent(&event))
315 Menu::current()->event(event);
317 // FIXME: QUIT signal should be handled more generic, not locally
318 if (event.type == SDL_QUIT)
319 Menu::set_current(0);
322 /* Draw the background: */
323 draw_demo(frame_ratio);
326 if (Menu::current() == main_menu)
327 context.draw_surface(logo, Vector(screen->w/2 - logo->w/2, 30),
328 LAYER_FOREGROUND1+1);
330 context.draw_text(white_small_text, " SuperTux " VERSION "\n", Vector(0, screen->h - 70), LEFT_ALLIGN, LAYER_FOREGROUND1);
331 context.draw_text(white_small_text,
332 _("Copyright (c) 2003 SuperTux Devel Team\n"
333 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you\n"
334 "are welcome to redistribute it under certain conditions; see the file COPYING\n"
335 "for details.\n"), Vector(0, screen->h - 70 + white_small_text->get_height()), LEFT_ALLIGN, LAYER_FOREGROUND1);
337 /* Don't draw menu, if quit is true */
338 Menu* menu = Menu::current();
344 if(menu == main_menu)
346 switch (main_menu->check())
349 // Start Game, ie. goto the slots menu
350 update_load_save_game_menu(load_game_menu);
352 case MNID_LEVELS_CONTRIB:
354 puts("Entering contrib menu");
355 generate_contrib_menu();
357 case MNID_LEVELEDITOR:
358 leveleditor = new LevelEditor();
361 Menu::set_current(main_menu);
365 display_text_file("CREDITS", SCROLL_SPEED_CREDITS, white_big_text , white_text, white_small_text, blue_text );
366 Menu::set_current(main_menu);
368 case MNID_QUITMAINMENU:
369 Menu::set_current(0);
373 else if(menu == options_menu)
375 process_options_menu();
377 else if(menu == load_game_menu)
379 if(event.key.keysym.sym == SDLK_DELETE)
381 int slot = menu->get_active_item_id();
382 std::stringstream stream;
384 std::string str = _("Are you sure you want to delete slot") + stream.str() + "?";
386 if(confirm_dialog(bkg_title, str.c_str()))
388 str = st_save_dir + "/slot" + stream.str() + ".stsg";
389 printf("Removing: %s\n",str.c_str());
393 update_load_save_game_menu(load_game_menu);
394 Menu::set_current(main_menu);
397 else if (process_load_game_menu())
399 // FIXME: shouldn't be needed if GameSession doesn't relay on global variables
400 titlesession->get_current_sector()->activate();
401 titlesession->set_current();
402 //titletux.level_begin();
406 else if(menu == contrib_menu)
408 check_levels_contrib_menu();
410 else if (menu == contrib_subset_menu)
412 check_contrib_subset_menu();
416 mouse_cursor->draw(context);
418 context.do_drawing();
429 worldmap_list.clear();
433 delete img_choose_subset;