added some comments and the framework for platforms that travel back and forth on...
[supertux.git] / src / worldmap.cpp
index 872c285..36e0444 100644 (file)
 #include <sstream>
 #include <unistd.h>
 
-#include "gettext.h"
-#include "video/surface.h"
-#include "video/screen.h"
-#include "video/drawing_context.h"
-#include "sprite/sprite_manager.h"
-#include "audio/sound_manager.h"
-#include "lisp/parser.h"
-#include "lisp/lisp.h"
-#include "lisp/list_iterator.h"
-#include "lisp/writer.h"
-#include "game_session.h"
-#include "sector.h"
-#include "worldmap.h"
-#include "resources.h"
-#include "misc.h"
-#include "player_status.h"
-#include "textscroller.h"
-#include "main.h"
-#include "spawn_point.h"
-#include "file_system.h"
-#include "gui/menu.h"
-#include "gui/mousecursor.h"
-#include "control/joystickkeyboardcontroller.h"
-#include "object/background.h"
-#include "object/tilemap.h"
-#include "scripting/script_interpreter.h"
+#include "gettext.hpp"
+#include "video/surface.hpp"
+#include "video/screen.hpp"
+#include "video/drawing_context.hpp"
+#include "sprite/sprite_manager.hpp"
+#include "audio/sound_manager.hpp"
+#include "lisp/parser.hpp"
+#include "lisp/lisp.hpp"
+#include "lisp/list_iterator.hpp"
+#include "lisp/writer.hpp"
+#include "game_session.hpp"
+#include "sector.hpp"
+#include "worldmap.hpp"
+#include "resources.hpp"
+#include "misc.hpp"
+#include "player_status.hpp"
+#include "textscroller.hpp"
+#include "main.hpp"
+#include "spawn_point.hpp"
+#include "file_system.hpp"
+#include "gui/menu.hpp"
+#include "gui/mousecursor.hpp"
+#include "control/joystickkeyboardcontroller.hpp"
+#include "object/background.hpp"
+#include "object/tilemap.hpp"
+#include "scripting/script_interpreter.hpp"
 
 Menu* worldmap_menu  = 0;
 
@@ -352,17 +352,13 @@ WorldMap::WorldMap()
   tux = new Tux(this);
   add_object(tux);
     
-  leveldot_green
-    = new Surface(datadir + "/images/tiles/worldmap/leveldot_green.png", true);
-  leveldot_red
-    = new Surface(datadir + "/images/tiles/worldmap/leveldot_red.png", true);
-  messagedot
-    = new Surface(datadir + "/images/tiles/worldmap/messagedot.png", true);
-  teleporterdot
-    = new Surface(datadir + "/images/tiles/worldmap/teleporterdot.png", true);
+  leveldot_green= new Surface("images/tiles/worldmap/leveldot_green.png", true);
+  leveldot_red = new Surface("images/tiles/worldmap/leveldot_red.png", true);
+  messagedot = new Surface("images/tiles/worldmap/messagedot.png", true);
+  teleporterdot = new Surface("images/tiles/worldmap/teleporterdot.png", true);
 
   name = "<no title>";
-  music = "salcon.mod";
+  music = "salcon.ogg";
   intro_displayed = false;
 
   total_stats.reset();
@@ -415,12 +411,11 @@ WorldMap::load_map()
 
   try {
     lisp::Parser parser;
-    std::string filename = get_resource_filename(map_filename);
-    std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
+    std::auto_ptr<lisp::Lisp> root (parser.parse(map_filename));
 
     const lisp::Lisp* lisp = root->get_lisp("supertux-worldmap");
     if(!lisp)
-      throw new std::runtime_error("file isn't a supertux-worldmap file.");
+      throw std::runtime_error("file isn't a supertux-worldmap file.");
 
     clear_objects();
     lisp::ListIterator iter(lisp);
@@ -548,8 +543,7 @@ WorldMap::get_level_title(Level& level)
 
   try {
     lisp::Parser parser;
-    std::auto_ptr<lisp::Lisp> root (
-        parser.parse(get_resource_filename(levels_path + level.name)));
+    std::auto_ptr<lisp::Lisp> root (parser.parse(levels_path + level.name));
 
     const lisp::Lisp* level_lisp = root->get_lisp("supertux-level");
     if(!level_lisp)
@@ -719,7 +713,7 @@ WorldMap::update(float delta)
         if (special_tile->teleport_dest != Vector(-1,-1))
           {
           // TODO: an animation, camera scrolling or a fading would be a nice touch
-          sound_manager->play_sound("warp");
+          sound_manager->play("sounds/warp.ogg");
           tux->back_direction = D_NONE;
           tux->set_tile_pos(special_tile->teleport_dest);
           SDL_Delay(1000);
@@ -740,12 +734,13 @@ WorldMap::update(float delta)
 
       if (level->pos == tux->get_tile_pos())
         {
+          sound_manager->stop_music();
           PlayerStatus old_player_status = player_status;
 
           // do a shriking fade to the level
           shrink_fade(Vector((level->pos.x*32 + 16 + offset.x),
                              (level->pos.y*32 + 16 + offset.y)), 500);
-          GameSession session(get_resource_filename(levels_path + level->name),
+          GameSession session(levels_path + level->name,
                               ST_GL_LOAD_LEVEL_FILE, &level->statistics);
 
           switch (session.run())
@@ -834,7 +829,7 @@ WorldMap::update(float delta)
               break;
             }
 
-          sound_manager->play_music(song);
+          sound_manager->play_music(std::string("music/") + music);
           Menu::set_current(0);
           if (!savegame_file.empty())
             savegame(savegame_file);
@@ -843,11 +838,15 @@ WorldMap::update(float delta)
          the level (in case there is one), don't show anything */
       if(level_finished) {
         if (level->extro_script != "") {
-          ScriptInterpreter* interpreter = new ScriptInterpreter(levels_path);
-          std::istringstream in(level->extro_script);
-          interpreter->load_script(in, "level-extro-script");
-          interpreter->start_script();
-          add_object(interpreter);
+          try {
+            std::auto_ptr<ScriptInterpreter> interpreter 
+              (new ScriptInterpreter(levels_path));
+            std::istringstream in(level->extro_script);
+            interpreter->run_script(in, "level-extro-script");
+            add_object(interpreter.release());
+          } catch(std::exception& e) {
+            std::cerr << "Couldn't run level-extro-script:" << e.what() << "\n";
+          }
         }
 
         if (!level->next_worldmap.empty())
@@ -972,7 +971,7 @@ WorldMap::draw_status(DrawingContext& context)
         }
     }
   /* Display a passive message in the map, if needed */
-  if(passive_message_timer.check())
+  if(passive_message_timer.started())
     context.draw_text(gold_text, passive_message, 
             Vector(SCREEN_WIDTH/2, SCREEN_HEIGHT - white_text->get_height() - 60),
             CENTER_ALLIGN, LAYER_FOREGROUND1);
@@ -987,15 +986,19 @@ WorldMap::display()
 
   quit = false;
 
-  song = sound_manager->load_music(datadir +  "/music/" + music);
-  sound_manager->play_music(song);
+  sound_manager->play_music(std::string("music/") + music);
 
   if(!intro_displayed && intro_script != "") {
-    ScriptInterpreter* interpreter = new ScriptInterpreter(levels_path);
-    std::istringstream in(intro_script);
-    interpreter->load_script(in, "worldmap-intro-script");
-    interpreter->start_script();
-    add_object(interpreter);
+    try {
+      std::auto_ptr<ScriptInterpreter> interpreter 
+        (new ScriptInterpreter(levels_path));
+      std::istringstream in(intro_script);
+      interpreter->run_script(in, "worldmap-intro-script");
+      add_object(interpreter.release());
+    } catch(std::exception& e) {
+      std::cerr << "Couldn't execute worldmap-intro-script: "
+        << e.what() << "\n";
+    }
                                            
     intro_displayed = true;
   }
@@ -1005,7 +1008,7 @@ WorldMap::display()
   while(!quit) {
     Uint32 ticks = SDL_GetTicks();
     float elapsed_time = float(ticks - lastticks) / 1000;
-    global_time += elapsed_time;
+    game_time += elapsed_time;
     lastticks = ticks;
     
     // 40 fps minimum // TODO use same code as in GameSession here
@@ -1032,6 +1035,7 @@ WorldMap::display()
     context.pop_transform();
     get_input();
     update(elapsed_time);
+    sound_manager->update();
       
     if(Menu::current()) {
       Menu::current()->draw(context);
@@ -1047,8 +1051,7 @@ WorldMap::savegame(const std::string& filename)
   if(filename == "")
     return;
 
-  std::ofstream file(filename.c_str(), std::ios::out);
-  lisp::Writer writer(file);
+  lisp::Writer writer(filename);
 
   int nb_solved_levels = 0, total_levels = 0;
   for(Levels::iterator i = levels.begin(); i != levels.end(); ++i) {