- Refactored worldmap a bit to reuse GameObject from the rest of the game
[supertux.git] / src / title.cpp
index 7ef9ff4..9afb667 100644 (file)
 //  along with this program; if not, write to the Free Software
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 //  02111-1307, USA.
+#include <config.h>
 
 #include <iostream>
+#include <sstream>
+#include <stdexcept>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
 #include <unistd.h>
+#include <cmath>
 #include <SDL.h>
 #include <SDL_image.h>
 
 #include <ctype.h>
 #endif
 
-#include "defines.h"
-#include "globals.h"
 #include "title.h"
-#include "screen/screen.h"
-#include "screen/surface.h"
-#include "high_scores.h"
-#include "menu.h"
+#include "video/screen.h"
+#include "video/surface.h"
+#include "gui/menu.h"
 #include "timer.h"
-#include "setup.h"
+#include "lisp/lisp.h"
+#include "lisp/parser.h"
 #include "level.h"
 #include "level_subset.h"
-#include "gameloop.h"
+#include "game_session.h"
 #include "worldmap.h"
 #include "leveleditor.h"
-#include "scene.h"
-#include "player.h"
-#include "math.h"
+#include "player_status.h"
 #include "tile.h"
 #include "sector.h"
-#include "tilemap.h"
+#include "object/tilemap.h"
+#include "object/camera.h"
+#include "object/player.h"
 #include "resources.h"
-#include "type.h"
 #include "gettext.h"
+#include "misc.h"
+#include "textscroller.h"
+#include "file_system.h"
+#include "control/joystickkeyboardcontroller.h"
+#include "control/codecontroller.h"
+#include "main.h"
 
 static Surface* bkg_title;
 static Surface* logo;
-static Surface* img_choose_subset;
+//static Surface* img_choose_subset;
 
 static bool walking;
 static Timer random_timer;
 
 static int frame;
-static unsigned int last_update_time;
-static unsigned int update_time;
 
 static GameSession* titlesession;
+static CodeController* controller;
 
 static std::vector<LevelSubset*> contrib_subsets;
 static LevelSubset* current_contrib_subset = 0;
 
-static string_list_type worldmap_list;
+/* If the demo was stopped - because game started, level
+   editor was excuted, etc - call this when you get back
+   to the title code.
+ */
+void resume_demo()
+{
+  player_status.reset();
+  titlesession->get_current_sector()->activate("main");
+  titlesession->set_current();
 
-static LevelEditor* leveleditor;
+  //frame_rate.update();
+}
+
+void update_load_save_game_menu(Menu* menu)
+{
+  printf("update loadsavemenu.\n");
+  for(int i = 1; i < 6; ++i) {
+    MenuItem& item = menu->get_item_by_id(i);
+    item.kind = MN_ACTION;
+    item.change_text(slotinfo(i));
+  }
+}
 
 void free_contrib_menu()
 {
@@ -89,27 +114,52 @@ void free_contrib_menu()
 
 void generate_contrib_menu()
 {
-  /** Generating contrib levels list by making use of Level Subset */
-  string_list_type level_subsets = dsubdirs("/levels", "info");
+  /** Generating contrib levels list by making use of Level Subset  */
+  std::set<std::string> level_subsets = FileSystem::dsubdirs("/levels", "info");
 
   free_contrib_menu();
 
-  contrib_menu->additem(MN_LABEL,_("Contrib Levels"),0,0);
-  contrib_menu->additem(MN_HL,"",0,0);
-
-  for (int i = 0; i < level_subsets.num_items; ++i)
+  contrib_menu->add_label(_("Contrib Levels"));
+  contrib_menu->add_hl();
+  
+  int i = 0;
+  for (std::set<std::string>::iterator it = level_subsets.begin();
+      it != level_subsets.end(); ++it)
     {
       LevelSubset* subset = new LevelSubset();
-      subset->load(level_subsets.item[i]);
-      contrib_menu->additem(MN_GOTO, subset->title.c_str(), i,
-          contrib_subset_menu, i+1);
+      subset->load(*it);
+      if(subset->hide_from_contribs) {
+        delete subset;
+        continue;
+      }
+      contrib_menu->add_submenu(subset->title, contrib_subset_menu, i);
       contrib_subsets.push_back(subset);
+      ++i;
     }
 
-  contrib_menu->additem(MN_HL,"",0,0);
-  contrib_menu->additem(MN_BACK,_("Back"),0,0);
+  contrib_menu->add_hl();
+  contrib_menu->add_back(_("Back"));
+  
+  level_subsets.clear();
+}
 
-  string_list_free(&level_subsets);
+std::string get_level_name(const std::string& filename)
+{
+  try {
+    lisp::Parser parser;
+    std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
+
+    const lisp::Lisp* level = root->get_lisp("supertux-level");
+    if(!level)
+      return "";
+
+    std::string name;
+    level->get("name", name);
+    return name;
+  } catch(std::exception& e) {
+    std::cerr << "Problem getting name of '" << filename << "'.\n";
+    return "";
+  }
 }
 
 void check_levels_contrib_menu()
@@ -117,211 +167,189 @@ void check_levels_contrib_menu()
   static int current_subset = -1;
 
   int index = contrib_menu->check();
-  if (index != -1)
-    {
-      index -= 1;
-      if (index >= 0 && index <= int(contrib_subsets.size()))
-        {
-          if (current_subset != index)
-            {
-              current_subset = index;
-              // FIXME: This shouln't be busy looping
-              LevelSubset& subset = * (contrib_subsets[index]);
-          
-              current_contrib_subset = &subset;
-
-              contrib_subset_menu->clear();
-
-              contrib_subset_menu->additem(MN_LABEL, subset.title, 0,0);
-              contrib_subset_menu->additem(MN_HL,"",0,0);
-              
-              for (int i = 0; i < subset.get_num_levels(); ++i)
-                {
-                  Level* level = new Level;
-                  level->load(subset.get_level_filename(i));
-                  contrib_subset_menu->additem(MN_ACTION, level->get_name(), 0, 0, i);
-                  delete level;
-                }
-              
-              contrib_subset_menu->additem(MN_HL,"",0,0);      
-              contrib_subset_menu->additem(MN_BACK, _("Back"), 0, 0);
+  if (index == -1)
+    return;
 
-              titlesession->get_current_sector()->activate();
-              titlesession->set_current();
-            }
-        }
-      else
-        {
-          // Back button
-        }
+  LevelSubset& subset = * (contrib_subsets[index]);
+  
+  if(subset.has_worldmap) {
+    WorldMapNS::WorldMap worldmap;
+    worldmap.set_map_filename(subset.get_worldmap_filename());
+
+    // some fading
+    fadeout(256);
+    DrawingContext context;
+    context.draw_text(white_text, "Loading...",
+        Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT/2), CENTER_ALLIGN, LAYER_FOREGROUND1);
+    context.do_drawing();
+
+    // TODO: slots should be available for contrib maps
+    worldmap.loadgame(user_dir + "/save/" + subset.name + "-slot1.stsg");
+    worldmap.display();  // run the map
+
+    Menu::set_current(main_menu);
+    resume_demo();
+  } else if (current_subset != index) {
+    current_subset = index;
+    LevelSubset& subset = * (contrib_subsets[index]);
+
+    current_contrib_subset = &subset;
+
+    contrib_subset_menu->clear();
+
+    contrib_subset_menu->add_label(subset.title);
+    contrib_subset_menu->add_hl();
+
+    for (int i = 0; i < subset.get_num_levels(); ++i)
+    {
+      /** get level's title */
+      std::string filename = subset.get_level_filename(i);
+      std::string title = get_level_name(filename);
+      contrib_subset_menu->add_entry(i, title);
     }
+
+    contrib_subset_menu->add_hl();
+    contrib_subset_menu->add_back(_("Back"));
+
+    titlesession->get_current_sector()->activate("main");
+    titlesession->set_current();
+  }
 }
 
 void check_contrib_subset_menu()
 {
   int index = contrib_subset_menu->check();
-  if (index != -1)
-    {
-      if (contrib_subset_menu->get_item_by_id(index).kind == MN_ACTION)
-        {
-          std::cout << "Starting level: " << index << std::endl;
-          
-          GameSession session(
-              current_contrib_subset->get_level_filename(index), ST_GL_PLAY);
-          session.run();
-          player_status.reset();
-          Menu::set_current(main_menu);
-          titlesession->get_current_sector()->activate();
-          titlesession->set_current();
-        }
-    }  
-}
-
-void check_contrib_worldmap_menu()
-{
-  int index = contrib_worldmap_menu->check();
-  if (index != -1)
-    {
-      WorldMapNS::WorldMap worldmap;
-      worldmap.loadmap(worldmap_list.item[index]);
-      worldmap.display();
-
+  if (index != -1) {
+    if (contrib_subset_menu->get_item_by_id(index).kind == MN_ACTION) {
+      GameSession session(
+          current_contrib_subset->get_level_filename(index), ST_GL_PLAY);
+      session.run();
+      player_status.reset();
       Menu::set_current(main_menu);
+      resume_demo();
     }
+  }  
 }
 
-void draw_demo(double frame_ratio)
+void draw_demo(float elapsed_time)
 {
-  Sector* world  = titlesession->get_current_sector();
-  Player* tux = world->player;
+  static float last_tux_x_pos = -1;
+  static float last_tux_y_pos = -1;
+  Sector* sector  = titlesession->get_current_sector();
+  Player* tux = sector->player;
 
-  world->play_music(LEVEL_MUSIC);
-  
-  global_frame_counter++;
-  tux->key_event((SDLKey) keymap.right,DOWN);
-  
-  if(random_timer.check())
-    {
-      if(walking)
-        tux->key_event((SDLKey) keymap.jump,UP);
-      else
-        tux->key_event((SDLKey) keymap.jump,DOWN);
-    }
-  else
-    {
-      random_timer.start(rand() % 3000 + 3000);
-      walking = !walking;
-    }
+  sector->play_music(LEVEL_MUSIC);
 
-  // Wrap around at the end of the level back to the beginnig
-  if(world->solids->get_width() * 32 - 320 < tux->base.x)
-    {
-      tux->level_begin();
-    }
-
-  tux->can_jump = true;
-  float last_tux_x_pos = tux->base.x;
-  world->action(frame_ratio);
+  controller->update();
+  controller->press(Controller::RIGHT);
   
+  if(random_timer.check() || 
+      (walking && fabsf(last_tux_x_pos - tux->get_pos().x)) < .1) {
+    walking = false;
+  } else {
+      if(!walking && fabsf(tux->get_pos().y - last_tux_y_pos) < .1) {
+        random_timer.start(float(rand() % 3000 + 3000) / 1000.);
+        walking = true;
+      }
+  }
+  if(!walking)
+    controller->press(Controller::JUMP);
+  last_tux_x_pos = tux->get_pos().x;
+  last_tux_y_pos = tux->get_pos().y;
 
-  // disabled for now, since with the new jump code we easily get deadlocks
-  // Jump if tux stays in the same position for one loop, ie. if he is
-  // stuck behind a wall
-  if (last_tux_x_pos == tux->base.x)
-    {
-      walking = false;
-    }
+  // Wrap around at the end of the level back to the beginnig
+  if(sector->solids->get_width() * 32 - 320 < tux->get_pos().x) {
+    sector->activate("main");
+    sector->camera->reset(tux->get_pos());
+  }
 
-  world->draw(*titlesession->context);
+  sector->update(elapsed_time);
+  sector->draw(*titlesession->context);
 }
 
 /* --- TITLE SCREEN --- */
-void title(void)
+void title()
 {
-  random_timer.init(true);
-
   walking = true;
+  //LevelEditor* leveleditor;
+  MusicRef credits_music;
+  controller = new CodeController();
 
-  st_pause_ticks_init();
-
-  titlesession = new GameSession(datadir + "/levels/misc/menu.stl", ST_GL_DEMO_GAME);
+  titlesession = new GameSession(get_resource_filename("levels/misc/menu.stl"),
+      ST_GL_DEMO_GAME);
 
   /* Load images: */
-  bkg_title = new Surface(datadir + "/images/background/arctis.jpg", IGNORE_ALPHA);
-  logo = new Surface(datadir + "/images/title/logo.png", USE_ALPHA);
-  img_choose_subset = new Surface(datadir + "/images/status/choose-level-subset.png", USE_ALPHA);
-
-  /* Generating contrib maps by only using a string_list */
-  worldmap_list = dfiles("levels/worldmap", NULL, NULL);
-
-  contrib_worldmap_menu->additem(MN_LABEL, _("Contrib Worlds"), 0,0);
-  contrib_worldmap_menu->additem(MN_HL, "", 0,0);
-  for(int i = 0; i < worldmap_list.num_items; i++)
-    {
-    WorldMapNS::WorldMap worldmap;
-    worldmap.loadmap(worldmap_list.item[i]);
-    contrib_worldmap_menu->additem(MN_ACTION, worldmap.get_world_title(),0,0,i);
-    }
-  contrib_worldmap_menu->additem(MN_HL,"",0,0);
-  contrib_worldmap_menu->additem(MN_BACK,"Back",0,0);
+  bkg_title = new Surface(datadir + "/images/background/arctis.jpg", false);
+  logo = new Surface(datadir + "/images/engine/menu/logo.png", true);
+  //img_choose_subset = new Surface(datadir + "/images/status/choose-level-subset.png", true);
 
-  titlesession->get_current_sector()->activate();
+  titlesession->get_current_sector()->activate("main");
   titlesession->set_current();
 
+  Player* player = titlesession->get_current_sector()->player;
+  player->set_controller(controller);
+
   /* --- Main title loop: --- */
   frame = 0;
 
-  update_time = st_get_ticks();
-  random_timer.start(rand() % 2000 + 2000);
+  random_timer.start(float(rand() % 2000 + 2000) / 1000.0);
 
+  Uint32 lastticks = SDL_GetTicks();
+  
   Menu::set_current(main_menu);
   DrawingContext& context = *titlesession->context;
-  while (Menu::current())
+  bool running = true;
+  while (running)
     {
-      // if we spent to much time on a menu entry
-      if( (update_time - last_update_time) > 1000)
-        update_time = last_update_time = st_get_ticks();
-
       // Calculate the movement-factor
-      double frame_ratio = ((double)(update_time-last_update_time))/((double)FRAME_RATE);
-      if(frame_ratio > 1.5) /* Quick hack to correct the unprecise CPU clocks a little bit. */
-        frame_ratio = 1.5 + (frame_ratio - 1.5) * 0.85;
-      /* Lower the frame_ratio that Tux doesn't jump to hectically throught the demo. */
-      frame_ratio /= 2;
+      Uint32 ticks = SDL_GetTicks();
+      float elapsed_time = float(ticks - lastticks) / 1000.;
+      global_time += elapsed_time;
+      lastticks = ticks;
+      // 40fps is minimum
+      if(elapsed_time > .04)
+        elapsed_time = .04;
+      
+      /* Lower the speed so that Tux doesn't jump too hectically throught
+         the demo. */
+      elapsed_time /= 2;
 
       SDL_Event event;
-      while (SDL_PollEvent(&event))
-        {
-          if (Menu::current())
-            {
-              Menu::current()->event(event);
-            }
-         // FIXME: QUIT signal should be handled more generic, not locally
-          if (event.type == SDL_QUIT)
-            Menu::set_current(0);
+      main_controller->update();
+      while (SDL_PollEvent(&event)) {
+        if (Menu::current()) {
+          Menu::current()->event(event);
         }
-
+        main_controller->process_event(event);
+        if (event.type == SDL_QUIT)
+          throw std::runtime_error("Received window close");
+      }
+  
       /* Draw the background: */
-      draw_demo(frame_ratio);
-     
+      draw_demo(elapsed_time);
+
       if (Menu::current() == main_menu)
-        context.draw_surface(logo, Vector(screen->w/2 - logo->w/2, 30),
+        context.draw_surface(logo, Vector(SCREEN_WIDTH/2 - logo->w/2, 30),
             LAYER_FOREGROUND1+1);
 
-      context.draw_text(white_small_text, " SuperTux " VERSION "\n", Vector(0, screen->h - 70), LAYER_FOREGROUND1);
+      context.draw_text(white_small_text, " SuperTux " PACKAGE_VERSION "\n",
+              Vector(0, SCREEN_HEIGHT - 50), LEFT_ALLIGN, LAYER_FOREGROUND1);
       context.draw_text(white_small_text,
-        _("Copyright (c) 2003 SuperTux Devel Team\n"
-          "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you\n"
-          "are welcome to redistribute it under certain conditions; see the file COPYING\n"
-          "for details.\n"), Vector(0, screen->h - 70 + white_small_text->get_height()), LAYER_FOREGROUND1);
+        _(
+"Copyright (c) 2005 SuperTux Devel Team\n"
+"This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to\n"
+"redistribute it under certain conditions; see the file COPYING for details.\n"
+        ),
+        Vector(0, SCREEN_HEIGHT - 50 + white_small_text->get_height() + 5),
+        LEFT_ALLIGN, LAYER_FOREGROUND1);
 
       /* Don't draw menu, if quit is true */
       Menu* menu = Menu::current();
       if(menu)
         {
           menu->draw(context);
-          menu->action();
-        
+          menu->update();
+         
           if(menu == main_menu)
             {
               switch (main_menu->check())
@@ -330,26 +358,31 @@ void title(void)
                   // Start Game, ie. goto the slots menu
                   update_load_save_game_menu(load_game_menu);
                   break;
-                case MNID_WORLDMAP_CONTRIB:
-                  break;
                 case MNID_LEVELS_CONTRIB:
                   // Contrib Menu
-                  puts("Entering contrib menu");
                   generate_contrib_menu();
                   break;
-                case MNID_LEVELEDITOR:
-                  leveleditor = new LevelEditor();
+#if 0
+                case MNID_LEVELEDITOR: {
+                  LevelEdtiro* leveleditor = new LevelEditor();
                   leveleditor->run();
                   delete leveleditor;
                   Menu::set_current(main_menu);
-                  update_time = st_get_ticks();
+                  resume_demo();
                   break;
+                }
+#endif
                 case MNID_CREDITS:
-                  display_text_file("CREDITS", bkg_title, SCROLL_SPEED_CREDITS);
+                  fadeout(500);
+                  credits_music = sound_manager->load_music(
+                    get_resource_filename("/music/credits.ogg"));
+                  sound_manager->play_music(credits_music);
+                  display_text_file("credits.txt");
+                  fadeout(500);
                   Menu::set_current(main_menu);
                   break;
                 case MNID_QUITMAINMENU:
-                  Menu::set_current(0);
+                  running = false;
                   break;
                 }
             }
@@ -362,27 +395,24 @@ void title(void)
               if(event.key.keysym.sym == SDLK_DELETE)
                 {
                 int slot = menu->get_active_item_id();
-                char str[1024];
-                sprintf(str,_("Are you sure you want to delete slot %d?"), slot);
+                std::stringstream stream;
+                stream << slot;
+                std::string str = _("Are you sure you want to delete slot") + stream.str() + "?";
                 
-                if(confirm_dialog(bkg_title, str))
+                if(confirm_dialog(bkg_title, str.c_str()))
                   {
-                  sprintf(str,"%s/slot%d.stsg", st_save_dir, slot);
-                  printf("Removing: %s\n",str);
-                  remove(str);
+                  str = user_dir + "/save/slot" + stream.str() + ".stsg";
+                  printf("Removing: %s\n",str.c_str());
+                  remove(str.c_str());
                   }
 
                 update_load_save_game_menu(load_game_menu);
                 Menu::set_current(main_menu);
-                update_time = st_get_ticks();
+                resume_demo();
                 }
               else if (process_load_game_menu())
                 {
-                  // FIXME: shouldn't be needed if GameSession doesn't relay on global variables
-                  titlesession->get_current_sector()->activate();
-                  titlesession->set_current();
-                  //titletux.level_begin();
-                  update_time = st_get_ticks();
+                  resume_demo();
                 }
             }
           else if(menu == contrib_menu)
@@ -393,23 +423,20 @@ void title(void)
             {
               check_contrib_subset_menu();
             }
-          else if(menu == contrib_worldmap_menu)
-            {
-              check_contrib_worldmap_menu();
-            }
         }
 
-      mouse_cursor->draw(context);
-     
+      // reopen menu of user closed it (so that the app doesn't close when user
+      // accidently hit ESC)
+      if(Menu::current() == 0) {
+        Menu::set_current(main_menu);
+      }
+
       context.do_drawing();
 
-      /* Set the time of the last update and the time of the current update */
-      last_update_time = update_time;
-      update_time = st_get_ticks();
+      //frame_rate.update();
 
       /* Pause: */
       frame++;
-      SDL_Delay(25);
     }
   /* Free surfaces: */
 
@@ -417,8 +444,5 @@ void title(void)
   delete titlesession;
   delete bkg_title;
   delete logo;
-  delete img_choose_subset;
+  //delete img_choose_subset;
 }
-
-// EOF //
-