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
33 #include <SDL_image.h>
37 #include "video/screen.hpp"
38 #include "video/surface.hpp"
39 #include "audio/sound_manager.hpp"
40 #include "gui/menu.hpp"
42 #include "lisp/lisp.hpp"
43 #include "lisp/parser.hpp"
45 #include "level_subset.hpp"
46 #include "game_session.hpp"
47 #include "worldmap.hpp"
48 #include "player_status.hpp"
51 #include "object/tilemap.hpp"
52 #include "object/camera.hpp"
53 #include "object/player.hpp"
54 #include "resources.hpp"
55 #include "gettext.hpp"
57 #include "textscroller.hpp"
58 #include "file_system.hpp"
59 #include "control/joystickkeyboardcontroller.hpp"
60 #include "control/codecontroller.hpp"
63 static Surface* bkg_title;
65 //static Surface* img_choose_subset;
68 static Timer random_timer;
72 static GameSession* titlesession;
73 static CodeController* controller;
75 static std::vector<LevelSubset*> contrib_subsets;
76 static LevelSubset* current_contrib_subset = 0;
77 static int current_subset = -1;
79 /* If the demo was stopped - because game started, level
80 editor was excuted, etc - call this when you get back
85 player_status->reset();
86 titlesession->get_current_sector()->activate("main");
87 titlesession->set_current();
89 //frame_rate.update();
92 void update_load_save_game_menu(Menu* menu)
94 printf("update loadsavemenu.\n");
95 for(int i = 1; i < 6; ++i) {
96 MenuItem& item = menu->get_item_by_id(i);
97 item.kind = MN_ACTION;
98 item.change_text(slotinfo(i));
102 void free_contrib_menu()
104 for(std::vector<LevelSubset*>::iterator i = contrib_subsets.begin();
105 i != contrib_subsets.end(); ++i)
108 contrib_subsets.clear();
109 contrib_menu->clear();
110 current_contrib_subset = 0;
114 void generate_contrib_menu()
116 /** Generating contrib levels list by making use of Level Subset */
117 std::vector<std::string> level_subsets;
118 char** files = PHYSFS_enumerateFiles("levels/");
119 for(const char* const* filename = files; *filename != 0; ++filename) {
120 std::string filepath = std::string("levels/") + *filename;
121 if(PHYSFS_isDirectory(filepath.c_str()))
122 level_subsets.push_back(filepath);
124 PHYSFS_freeList(files);
128 contrib_menu->add_label(_("Contrib Levels"));
129 contrib_menu->add_hl();
132 for (std::vector<std::string>::iterator it = level_subsets.begin();
133 it != level_subsets.end(); ++it) {
135 std::auto_ptr<LevelSubset> subset (new LevelSubset());
137 if(subset->hide_from_contribs) {
140 contrib_menu->add_submenu(subset->title, contrib_subset_menu, i++);
141 contrib_subsets.push_back(subset.release());
142 } catch(std::exception& e) {
144 std::cerr << "Couldn't parse levelset info for '"
145 << *it << "': " << e.what() << "\n";
150 contrib_menu->add_hl();
151 contrib_menu->add_back(_("Back"));
154 std::string get_level_name(const std::string& filename)
158 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
160 const lisp::Lisp* level = root->get_lisp("supertux-level");
165 level->get("name", name);
167 } catch(std::exception& e) {
168 std::cerr << "Problem getting name of '" << filename << "'.\n";
173 void check_levels_contrib_menu()
175 int index = contrib_menu->check();
179 LevelSubset& subset = * (contrib_subsets[index]);
181 if(subset.has_worldmap) {
182 WorldMapNS::WorldMap worldmap;
183 worldmap.set_map_filename(subset.get_worldmap_filename());
184 sound_manager->stop_music();
188 DrawingContext context;
189 context.draw_text(white_text, "Loading...",
190 Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT/2), CENTER_ALLIGN, LAYER_FOREGROUND1);
191 context.do_drawing();
193 // TODO: slots should be available for contrib maps
194 worldmap.loadgame("save/" + subset.name + "-slot1.stsg");
195 worldmap.display(); // run the map
197 Menu::set_current(main_menu);
199 } else if (current_subset != index) {
200 current_subset = index;
201 LevelSubset& subset = * (contrib_subsets[index]);
203 current_contrib_subset = ⊂
205 contrib_subset_menu->clear();
207 contrib_subset_menu->add_label(subset.title);
208 contrib_subset_menu->add_hl();
210 for (int i = 0; i < subset.get_num_levels(); ++i)
212 /** get level's title */
213 std::string filename = subset.get_level_filename(i);
214 std::string title = get_level_name(filename);
215 contrib_subset_menu->add_entry(i, title);
218 contrib_subset_menu->add_hl();
219 contrib_subset_menu->add_back(_("Back"));
221 titlesession->get_current_sector()->activate("main");
222 titlesession->set_current();
226 void check_contrib_subset_menu()
228 int index = contrib_subset_menu->check();
230 if (contrib_subset_menu->get_item_by_id(index).kind == MN_ACTION) {
231 sound_manager->stop_music();
233 current_contrib_subset->get_level_filename(index), ST_GL_PLAY);
235 player_status->reset();
236 Menu::set_current(main_menu);
242 void draw_demo(float elapsed_time)
244 static float last_tux_x_pos = -1;
245 static float last_tux_y_pos = -1;
246 Sector* sector = titlesession->get_current_sector();
247 Player* tux = sector->player;
249 sector->play_music(LEVEL_MUSIC);
251 controller->update();
252 controller->press(Controller::RIGHT);
254 if(random_timer.check() ||
255 (walking && fabsf(last_tux_x_pos - tux->get_pos().x)) < .1) {
258 if(!walking && fabsf(tux->get_pos().y - last_tux_y_pos) < .1) {
259 random_timer.start(float(rand() % 3000 + 3000) / 1000.);
264 controller->press(Controller::JUMP);
265 last_tux_x_pos = tux->get_pos().x;
266 last_tux_y_pos = tux->get_pos().y;
268 // Wrap around at the end of the level back to the beginnig
269 if(sector->solids->get_width() * 32 - 320 < tux->get_pos().x) {
270 sector->activate("main");
271 sector->camera->reset(tux->get_pos());
274 sector->update(elapsed_time);
275 sector->draw(*titlesession->context);
278 /* --- TITLE SCREEN --- */
282 //LevelEditor* leveleditor;
283 controller = new CodeController();
285 titlesession = new GameSession("levels/misc/menu.stl", ST_GL_DEMO_GAME);
288 bkg_title = new Surface("images/background/arctis.jpg", false);
289 logo = new Surface("images/engine/menu/logo.png", true);
290 //img_choose_subset = new Surface("images/status/choose-level-subset.png", true);
292 titlesession->get_current_sector()->activate("main");
293 titlesession->set_current();
295 Player* player = titlesession->get_current_sector()->player;
296 player->set_controller(controller);
298 /* --- Main title loop: --- */
301 random_timer.start(float(rand() % 2000 + 2000) / 1000.0);
303 Uint32 lastticks = SDL_GetTicks();
305 Menu::set_current(main_menu);
306 DrawingContext& context = *titlesession->context;
310 // Calculate the movement-factor
311 Uint32 ticks = SDL_GetTicks();
312 float elapsed_time = float(ticks - lastticks) / 1000.;
313 game_time += elapsed_time;
316 if(elapsed_time > .04)
319 /* Lower the speed so that Tux doesn't jump too hectically throught
324 main_controller->update();
325 while (SDL_PollEvent(&event)) {
326 if (Menu::current()) {
327 Menu::current()->event(event);
329 main_controller->process_event(event);
330 if (event.type == SDL_QUIT)
331 throw std::runtime_error("Received window close");
334 /* Draw the background: */
335 draw_demo(elapsed_time);
337 if (Menu::current() == main_menu)
338 context.draw_surface(logo, Vector(SCREEN_WIDTH/2 - logo->w/2, 30),
339 LAYER_FOREGROUND1+1);
341 context.draw_text(white_small_text, " SuperTux " PACKAGE_VERSION "\n",
342 Vector(0, SCREEN_HEIGHT - 50), LEFT_ALLIGN, LAYER_FOREGROUND1);
343 context.draw_text(white_small_text,
345 "Copyright (c) 2005 SuperTux Devel Team\n"
346 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to\n"
347 "redistribute it under certain conditions; see the file COPYING for details.\n"
349 Vector(0, SCREEN_HEIGHT - 50 + white_small_text->get_height() + 5),
350 LEFT_ALLIGN, LAYER_FOREGROUND1);
352 /* Don't draw menu, if quit is true */
353 Menu* menu = Menu::current();
359 if(menu == main_menu)
361 switch (main_menu->check())
364 // Start Game, ie. goto the slots menu
365 update_load_save_game_menu(load_game_menu);
367 case MNID_LEVELS_CONTRIB:
369 generate_contrib_menu();
372 case MNID_LEVELEDITOR: {
373 LevelEdtiro* leveleditor = new LevelEditor();
376 Menu::set_current(main_menu);
382 sound_manager->stop_music();
384 sound_manager->play_music("music/credits.ogg");
385 display_text_file("credits.txt");
386 sound_manager->stop_music();
388 Menu::set_current(main_menu);
390 case MNID_QUITMAINMENU:
395 else if(menu == options_menu)
397 process_options_menu();
399 else if(menu == load_game_menu)
401 if(event.key.keysym.sym == SDLK_DELETE)
403 int slot = menu->get_active_item_id();
404 std::stringstream stream;
406 std::string str = _("Are you sure you want to delete slot") + stream.str() + "?";
408 if(confirm_dialog(bkg_title, str.c_str())) {
409 str = "save/slot" + stream.str() + ".stsg";
410 printf("Removing: %s\n",str.c_str());
411 PHYSFS_delete(str.c_str());
414 update_load_save_game_menu(load_game_menu);
415 Menu::set_current(main_menu);
418 else if (process_load_game_menu())
423 else if(menu == contrib_menu)
425 check_levels_contrib_menu();
427 else if (menu == contrib_subset_menu)
429 check_contrib_subset_menu();
433 // reopen menu of user closed it (so that the app doesn't close when user
434 // accidently hit ESC)
435 if(Menu::current() == 0) {
436 Menu::set_current(main_menu);
439 context.do_drawing();
440 sound_manager->update();
442 //frame_rate.update();
453 //delete img_choose_subset;