Copyright update to 2015
[supertux.git] / src / supertux / title_screen.cpp
index b1db0f7..7b70f45 100644 (file)
 //  You should have received a copy of the GNU General Public License
 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-#include <version.h>
-
 #include "supertux/title_screen.hpp"
 
-#include <algorithm>
-#include <physfs.h>
-
-#include "addon/addon_manager.hpp"
 #include "audio/sound_manager.hpp"
 #include "gui/menu.hpp"
 #include "gui/menu_manager.hpp"
-#include "gui/menu_item.hpp"
 #include "lisp/parser.hpp"
 #include "object/camera.hpp"
 #include "object/player.hpp"
 #include "supertux/fadeout.hpp"
 #include "supertux/gameconfig.hpp"
 #include "supertux/globals.hpp"
-#include "supertux/mainloop.hpp"
 #include "supertux/menu/menu_storage.hpp"
-#include "supertux/menu/addon_menu.hpp"
-#include "supertux/menu/contrib_world_menu.hpp"
-#include "supertux/menu/contrib_menu.hpp"
-#include "supertux/menu/main_menu.hpp"
-#include "supertux/menu/options_menu.hpp"
 #include "supertux/resources.hpp"
+#include "supertux/screen_manager.hpp"
 #include "supertux/sector.hpp"
 #include "supertux/textscroller.hpp"
 #include "supertux/world.hpp"
 #include "util/reader.hpp"
 #include "video/drawing_context.hpp"
 
-TitleScreen::TitleScreen() :
-  main_menu(),
-  contrib_menu(),
-  contrib_world_menu(),
-  main_world(),
-  contrib_worlds(),
-  addons_menu(),
-  addons(),
-  current_world(),
+#include <sstream>
+#include <version.h>
+
+TitleScreen::TitleScreen(Savegame& savegame) :
   frame(),
   controller(),
-  titlesession()
+  titlesession(),
+  copyright_text()
 {
   controller.reset(new CodeController());
-  titlesession.reset(new GameSession("levels/misc/menu.stl"));
+  titlesession.reset(new GameSession("levels/misc/menu.stl", savegame));
 
   Player* player = titlesession->get_current_sector()->player;
   player->set_controller(controller.get());
   player->set_speedlimit(230); //MAX_WALK_XM
 
-  generate_main_menu();
-
-  frame = std::auto_ptr<Surface>(new Surface("images/engine/menu/frame.png"));
-}
-
-std::string
-TitleScreen::get_level_name(const std::string& filename)
-{
-  try {
-    lisp::Parser parser;
-    const 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) {
-    log_warning << "Problem getting name of '" << filename << "': "
-                << e.what() << std::endl;
-    return "";
-  }
-}
-
-void
-TitleScreen::check_levels_contrib_menu()
-{
-  current_world = contrib_menu->get_current_world();
-  
-  if (current_world)
-  {
-    if (!current_world->is_levelset) 
-    {
-      start_game();
-    }
-    else 
-    {
-      contrib_world_menu.reset(new ContribWorldMenu(*current_world));
-      MenuManager::push_current(contrib_world_menu.get());
-    }
-  }
-}
-
-namespace {
-bool generate_addons_menu_sorter(const Addon* a1, const Addon* a2)
-{
-  return a1->title < a2->title;
-}
-}
-
-void
-TitleScreen::generate_addons_menu()
-{
-  AddonManager& adm = AddonManager::get_instance();
-
-  // refresh list of addons
-  addons = adm.get_addons();
-  
-  // sort list
-  std::sort(addons.begin(), addons.end(), generate_addons_menu_sorter);
-
-  // (re)generate menu
-  addons_menu.reset(new AddonMenu(addons));
-}
-
-void
-TitleScreen::check_addons_menu()
-{
-  int index = addons_menu->check();
-  if (index == -1) return;
-
-  // check if "Check Online" was chosen
-  if (index == 0) {
-    try {
-      AddonManager::get_instance().check_online();
-      generate_addons_menu();
-      MenuManager::set_current(addons_menu.get());
-      addons_menu->set_active_item(index);
-    } 
-    catch (std::runtime_error e) {
-      log_warning << "Check for available Add-ons failed: " << e.what() << std::endl;
-    }
-    return;
-  }
-
-  // if one of the Addons listed was chosen, take appropriate action
-  if ((index >= ADDON_LIST_START_ID) && (index < ADDON_LIST_START_ID) + addons.size()) {
-    Addon& addon = *addons[index - ADDON_LIST_START_ID];
-    if (!addon.installed) {
-      try {
-        AddonManager::get_instance().install(&addon);
-      } 
-      catch (std::runtime_error e) {
-        log_warning << "Installing Add-on failed: " << e.what() << std::endl;
-      }
-      addons_menu->set_toggled(index, addon.loaded);
-    } else if (!addon.loaded) {
-      try {
-        AddonManager::get_instance().enable(&addon);
-      } 
-      catch (std::runtime_error e) {
-        log_warning << "Enabling Add-on failed: " << e.what() << std::endl;
-      }
-      addons_menu->set_toggled(index, addon.loaded);
-    } else {
-      try {
-        AddonManager::get_instance().disable(&addon);
-      } 
-      catch (std::runtime_error e) {
-        log_warning << "Disabling Add-on failed: " << e.what() << std::endl;
-      }
-      addons_menu->set_toggled(index, addon.loaded);
-    }
-  }
+  frame = Surface::create("images/engine/menu/frame.png");
+  copyright_text = "SuperTux " PACKAGE_VERSION "\n" +
+    _("Copyright") + " (c) 2003-2015 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"
+   );
 }
 
 void
@@ -198,7 +72,7 @@ TitleScreen::make_tux_jump()
   controller->press(Controller::RIGHT);
 
   // Check if we should press the jump button
-  Rect lookahead = tux->get_bbox();
+  Rectf lookahead = tux->get_bbox();
   lookahead.p2.x += 96;
   bool pathBlocked = !sector->is_free_of_statics(lookahead);
   if ((pathBlocked && jumpWasReleased) || !tux->on_ground()) {
@@ -215,12 +89,6 @@ TitleScreen::make_tux_jump()
   }
 }
 
-void
-TitleScreen::generate_main_menu()
-{
-  main_menu.reset(new MainMenu());
-}
-
 TitleScreen::~TitleScreen()
 {
 }
@@ -228,15 +96,13 @@ TitleScreen::~TitleScreen()
 void
 TitleScreen::setup()
 {
-  player_status->reset();
-
   Sector* sector = titlesession->get_current_sector();
   if(Sector::current() != sector) {
     sector->play_music(LEVEL_MUSIC);
     sector->activate(sector->player->get_pos());
   }
 
-  MenuManager::set_current(main_menu.get());
+  MenuManager::instance().set_menu(MenuStorage::MAIN_MENU);
 }
 
 void
@@ -244,7 +110,7 @@ TitleScreen::leave()
 {
   Sector* sector = titlesession->get_current_sector();
   sector->deactivate();
-  MenuManager::set_current(NULL);
+  MenuManager::instance().clear_menu_stack();
 }
 
 void
@@ -253,100 +119,31 @@ TitleScreen::draw(DrawingContext& context)
   Sector* sector  = titlesession->get_current_sector();
   sector->draw(context);
 
-  // FIXME: Add something to scale the frame to the resolution of the screen
-  context.draw_surface(frame.get(), Vector(0,0),LAYER_FOREGROUND1);
+  context.draw_surface_part(frame,
+                            Rectf(0, 0, frame->get_width(), frame->get_height()),
+                            Rectf(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT),
+                            LAYER_FOREGROUND1);
 
-  context.draw_text(Resources::small_font, "SuperTux " PACKAGE_VERSION "\n",
-                    Vector(5, SCREEN_HEIGHT - 50), ALIGN_LEFT, LAYER_FOREGROUND1);
   context.draw_text(Resources::small_font,
-                    _(
-                      "Copyright (c) 2007 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(5, SCREEN_HEIGHT - 50 + Resources::small_font->get_height() + 5),
+                    copyright_text,
+                    Vector(5, SCREEN_HEIGHT - 50),
                     ALIGN_LEFT, LAYER_FOREGROUND1);
 }
 
 void
 TitleScreen::update(float elapsed_time)
 {
-  g_main_loop->set_speed(0.6f);
+  ScreenManager::current()->set_speed(0.6f);
   Sector* sector  = titlesession->get_current_sector();
   sector->update(elapsed_time);
 
   make_tux_jump();
 
-  Menu* menu = MenuManager::current();
-  if(menu) {
-    if(menu == main_menu.get()) {
-      switch (main_menu->check()) {
-        case MNID_STARTGAME:
-          // Start Game, ie. goto the slots menu
-          if(main_world.get() == NULL) {
-            main_world.reset(new World());
-            main_world->load("levels/world1/info");
-          }
-          current_world = main_world.get();
-          start_game();
-          break;
-
-        case MNID_LEVELS_CONTRIB:
-          // Contrib Menu
-          contrib_menu.reset(new ContribMenu());
-          MenuManager::push_current(contrib_menu.get());
-          break;
-
-        case MNID_ADDONS:
-          // Add-ons Menu
-          generate_addons_menu();
-          MenuManager::push_current(addons_menu.get());
-          break;
-
-        case MNID_CREDITS:
-          MenuManager::set_current(NULL);
-          g_main_loop->push_screen(new TextScroller("credits.txt"),
-                                   new FadeOut(0.5));
-          break;
-
-        case MNID_QUITMAINMENU:
-          g_main_loop->quit(new FadeOut(0.25));
-          sound_manager->stop_music(0.25);
-          break;
-      }
-    } else if(menu == contrib_menu.get()) {
-      check_levels_contrib_menu();
-    } else if(menu == addons_menu.get()) {
-      check_addons_menu();
-    } else if (menu == contrib_world_menu.get()) {
-      contrib_world_menu->check_menu();
-    }
-  }
-
   // reopen menu if user closed it (so that the app doesn't close when user
   // accidently hit ESC)
-  if(MenuManager::current() == 0 && g_main_loop->has_no_pending_fadeout()) {
-    generate_main_menu();
-    MenuManager::set_current(main_menu.get());
-  }
-}
-
-void
-TitleScreen::start_game()
-{
-  MenuManager::set_current(NULL);
-  std::string basename = current_world->get_basedir();
-  basename = basename.substr(0, basename.length()-1);
-  std::string worlddirname = FileSystem::basename(basename);
-  std::ostringstream stream;
-  stream << "profile" << g_config->profile << "/" << worlddirname << ".stsg";
-  std::string slotfile = stream.str();
-
-  try {
-    current_world->set_savegame_filename(slotfile);
-    current_world->run();
-  } catch(std::exception& e) {
-    log_fatal << "Couldn't start world: " << e.what() << std::endl;
+  if(!MenuManager::instance().is_active() && !ScreenManager::current()->has_pending_fadeout())
+  {
+    MenuManager::instance().set_menu(MenuStorage::MAIN_MENU);
   }
 }