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>
36 #include <sys/types.h>
41 #include "video/screen.h"
42 #include "video/surface.h"
45 #include "lisp/lisp.h"
46 #include "lisp/parser.h"
48 #include "level_subset.h"
49 #include "game_session.h"
51 #include "leveleditor.h"
52 #include "player_status.h"
55 #include "object/tilemap.h"
56 #include "object/camera.h"
57 #include "object/player.h"
58 #include "resources.h"
61 #include "textscroller.h"
62 #include "file_system.h"
63 #include "control/joystickkeyboardcontroller.h"
64 #include "control/codecontroller.h"
67 static Surface* bkg_title;
69 //static Surface* img_choose_subset;
72 static Timer random_timer;
76 static GameSession* titlesession;
77 static CodeController* controller;
79 static std::vector<LevelSubset*> contrib_subsets;
80 static LevelSubset* current_contrib_subset = 0;
82 /* If the demo was stopped - because game started, level
83 editor was excuted, etc - call this when you get back
88 player_status.reset();
89 titlesession->get_current_sector()->activate("main");
90 titlesession->set_current();
92 //frame_rate.update();
95 void update_load_save_game_menu(Menu* menu)
97 printf("update loadsavemenu.\n");
98 for(int i = 1; i < 6; ++i) {
99 MenuItem& item = menu->get_item_by_id(i);
100 item.kind = MN_ACTION;
101 item.change_text(slotinfo(i));
105 void free_contrib_menu()
107 for(std::vector<LevelSubset*>::iterator i = contrib_subsets.begin();
108 i != contrib_subsets.end(); ++i)
111 contrib_subsets.clear();
112 contrib_menu->clear();
115 void generate_contrib_menu()
117 /** Generating contrib levels list by making use of Level Subset */
118 std::set<std::string> level_subsets = FileSystem::dsubdirs("/levels", "info");
122 contrib_menu->add_label(_("Contrib Levels"));
123 contrib_menu->add_hl();
126 for (std::set<std::string>::iterator it = level_subsets.begin();
127 it != level_subsets.end(); ++it)
129 LevelSubset* subset = new LevelSubset();
131 if(subset->hide_from_contribs) {
135 contrib_menu->add_submenu(subset->title, contrib_subset_menu, i);
136 contrib_subsets.push_back(subset);
140 contrib_menu->add_hl();
141 contrib_menu->add_back(_("Back"));
143 level_subsets.clear();
146 std::string get_level_name(const std::string& filename)
150 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
152 const lisp::Lisp* level = root->get_lisp("supertux-level");
157 level->get("name", name);
159 } catch(std::exception& e) {
160 std::cerr << "Problem getting name of '" << filename << "'.\n";
165 void check_levels_contrib_menu()
167 static int current_subset = -1;
169 int index = contrib_menu->check();
173 LevelSubset& subset = * (contrib_subsets[index]);
175 if(subset.has_worldmap) {
176 WorldMapNS::WorldMap worldmap;
177 worldmap.set_map_filename(subset.get_worldmap_filename());
181 DrawingContext context;
182 context.draw_text(white_text, "Loading...",
183 Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT/2), CENTER_ALLIGN, LAYER_FOREGROUND1);
184 context.do_drawing();
186 // TODO: slots should be available for contrib maps
187 worldmap.loadgame(user_dir + "/save/" + subset.name + "-slot1.stsg");
188 worldmap.display(); // run the map
190 Menu::set_current(main_menu);
192 } else if (current_subset != index) {
193 current_subset = index;
194 LevelSubset& subset = * (contrib_subsets[index]);
196 current_contrib_subset = ⊂
198 contrib_subset_menu->clear();
200 contrib_subset_menu->add_label(subset.title);
201 contrib_subset_menu->add_hl();
203 for (int i = 0; i < subset.get_num_levels(); ++i)
205 /** get level's title */
206 std::string filename = subset.get_level_filename(i);
207 std::string title = get_level_name(filename);
208 contrib_subset_menu->add_entry(i, title);
211 contrib_subset_menu->add_hl();
212 contrib_subset_menu->add_back(_("Back"));
214 titlesession->get_current_sector()->activate("main");
215 titlesession->set_current();
219 void check_contrib_subset_menu()
221 int index = contrib_subset_menu->check();
223 if (contrib_subset_menu->get_item_by_id(index).kind == MN_ACTION) {
225 current_contrib_subset->get_level_filename(index), ST_GL_PLAY);
227 player_status.reset();
228 Menu::set_current(main_menu);
234 void draw_demo(float elapsed_time)
236 static float last_tux_x_pos = -1;
237 static float last_tux_y_pos = -1;
238 Sector* sector = titlesession->get_current_sector();
239 Player* tux = sector->player;
241 sector->play_music(LEVEL_MUSIC);
243 controller->update();
244 controller->press(Controller::RIGHT);
246 if(random_timer.check() ||
247 (walking && fabsf(last_tux_x_pos - tux->get_pos().x)) < .1) {
250 if(!walking && fabsf(tux->get_pos().y - last_tux_y_pos) < .1) {
251 random_timer.start(float(rand() % 3000 + 3000) / 1000.);
256 controller->press(Controller::JUMP);
257 last_tux_x_pos = tux->get_pos().x;
258 last_tux_y_pos = tux->get_pos().y;
260 // Wrap around at the end of the level back to the beginnig
261 if(sector->solids->get_width() * 32 - 320 < tux->get_pos().x) {
262 sector->activate("main");
263 sector->camera->reset(tux->get_pos());
266 sector->update(elapsed_time);
267 sector->draw(*titlesession->context);
270 /* --- TITLE SCREEN --- */
274 //LevelEditor* leveleditor;
275 MusicRef credits_music;
276 controller = new CodeController();
278 titlesession = new GameSession(get_resource_filename("levels/misc/menu.stl"),
282 bkg_title = new Surface(datadir + "/images/background/arctis.jpg", false);
283 logo = new Surface(datadir + "/images/engine/menu/logo.png", true);
284 //img_choose_subset = new Surface(datadir + "/images/status/choose-level-subset.png", true);
286 titlesession->get_current_sector()->activate("main");
287 titlesession->set_current();
289 Player* player = titlesession->get_current_sector()->player;
290 player->set_controller(controller);
292 /* --- Main title loop: --- */
295 random_timer.start(float(rand() % 2000 + 2000) / 1000.0);
297 Uint32 lastticks = SDL_GetTicks();
299 Menu::set_current(main_menu);
300 DrawingContext& context = *titlesession->context;
304 // Calculate the movement-factor
305 Uint32 ticks = SDL_GetTicks();
306 float elapsed_time = float(ticks - lastticks) / 1000.;
307 global_time += elapsed_time;
310 if(elapsed_time > .04)
313 /* Lower the speed so that Tux doesn't jump too hectically throught
318 main_controller->update();
319 while (SDL_PollEvent(&event)) {
320 if (Menu::current()) {
321 Menu::current()->event(event);
323 main_controller->process_event(event);
324 if (event.type == SDL_QUIT)
325 throw std::runtime_error("Received window close");
328 /* Draw the background: */
329 draw_demo(elapsed_time);
331 if (Menu::current() == main_menu)
332 context.draw_surface(logo, Vector(SCREEN_WIDTH/2 - logo->w/2, 30),
333 LAYER_FOREGROUND1+1);
335 context.draw_text(white_small_text, " SuperTux " PACKAGE_VERSION "\n",
336 Vector(0, SCREEN_HEIGHT - 50), LEFT_ALLIGN, LAYER_FOREGROUND1);
337 context.draw_text(white_small_text,
339 "Copyright (c) 2005 SuperTux Devel Team\n"
340 "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to\n"
341 "redistribute it under certain conditions; see the file COPYING for details.\n"
343 Vector(0, SCREEN_HEIGHT - 50 + white_small_text->get_height() + 5),
344 LEFT_ALLIGN, LAYER_FOREGROUND1);
346 /* Don't draw menu, if quit is true */
347 Menu* menu = Menu::current();
353 if(menu == main_menu)
355 switch (main_menu->check())
358 // Start Game, ie. goto the slots menu
359 update_load_save_game_menu(load_game_menu);
361 case MNID_LEVELS_CONTRIB:
363 generate_contrib_menu();
366 case MNID_LEVELEDITOR: {
367 LevelEdtiro* leveleditor = new LevelEditor();
370 Menu::set_current(main_menu);
377 credits_music = sound_manager->load_music(
378 get_resource_filename("/music/credits.ogg"));
379 sound_manager->play_music(credits_music);
380 display_text_file("credits.txt");
382 Menu::set_current(main_menu);
384 case MNID_QUITMAINMENU:
389 else if(menu == options_menu)
391 process_options_menu();
393 else if(menu == load_game_menu)
395 if(event.key.keysym.sym == SDLK_DELETE)
397 int slot = menu->get_active_item_id();
398 std::stringstream stream;
400 std::string str = _("Are you sure you want to delete slot") + stream.str() + "?";
402 if(confirm_dialog(bkg_title, str.c_str()))
404 str = user_dir + "/save/slot" + stream.str() + ".stsg";
405 printf("Removing: %s\n",str.c_str());
409 update_load_save_game_menu(load_game_menu);
410 Menu::set_current(main_menu);
413 else if (process_load_game_menu())
418 else if(menu == contrib_menu)
420 check_levels_contrib_menu();
422 else if (menu == contrib_subset_menu)
424 check_contrib_subset_menu();
428 // reopen menu of user closed it (so that the app doesn't close when user
429 // accidently hit ESC)
430 if(Menu::current() == 0) {
431 Menu::set_current(main_menu);
434 context.do_drawing();
436 //frame_rate.update();
447 //delete img_choose_subset;