-renamed ViewPort to Camera
[supertux.git] / src / level.cpp
index e7ecdfd..3a55398 100644 (file)
@@ -220,7 +220,7 @@ Level::init_defaults()
   name       = "UnNamed";
   author     = "UnNamed";
   song_title = "Mortimers_chipdisko.mod";
-  bkgd_image = "arctis.png";
+  bkgd_image = "arctis.jpg";
   width      = 0;
   height     = 0;
   start_pos_x = 100;
@@ -465,54 +465,55 @@ Level::save(const std::string& subset, int level, World* world)
   LispWriter writer(out);
 
   /* Write header: */
-  writer.writeComment("SuperTux level made using the built-in leveleditor");
-  writer.startList("supertux-level");
-
-  writer.writeInt("version", 1);
-  writer.writeString("name", name);
-  writer.writeString("author", author);
-  writer.writeString("music", song_title);
-  writer.writeString("background", bkgd_image);
-  writer.writeString("particle_system", particle_system);
-  writer.writeInt("bkgd_speed", bkgd_speed);
-  writer.writeInt("bkgd_red_top", bkgd_top.red);
-  writer.writeInt("bkgd_green_top", bkgd_top.green);
-  writer.writeInt("bkgd_blue_top", bkgd_top.blue);
-  writer.writeInt("bkgd_red_bottom", bkgd_bottom.red);
-  writer.writeInt("bkgd_green_bottom", bkgd_bottom.green);
-  writer.writeInt("bkgd_blue_bottom", bkgd_bottom.blue);
-  writer.writeInt("time", time_left);
-  writer.writeInt("width", width);
-  writer.writeInt("height", height);
-  writer.writeBool("back_scrolling", back_scrolling);
-  writer.writeFloat("hor_autoscroll_speed", hor_autoscroll_speed);
-  writer.writeFloat("gravity", gravity);
-
-  writer.writeIntVector("background-tm", bg_tiles);
-  writer.writeIntVector("interactive-tm", ia_tiles);
-  writer.writeIntVector("foreground-tm", fg_tiles);
-
-  writer.startList("reset-points");
+  writer.write_comment("SuperTux level made using the built-in leveleditor");
+  writer.start_list("supertux-level");
+
+  writer.write_int("version", 1);
+  writer.write_string("name", name);
+  writer.write_string("author", author);
+  writer.write_string("music", song_title);
+  writer.write_string("background", bkgd_image);
+  writer.write_string("particle_system", particle_system);
+  writer.write_int("bkgd_speed", bkgd_speed);
+  writer.write_int("bkgd_red_top", bkgd_top.red);
+  writer.write_int("bkgd_green_top", bkgd_top.green);
+  writer.write_int("bkgd_blue_top", bkgd_top.blue);
+  writer.write_int("bkgd_red_bottom", bkgd_bottom.red);
+  writer.write_int("bkgd_green_bottom", bkgd_bottom.green);
+  writer.write_int("bkgd_blue_bottom", bkgd_bottom.blue);
+  writer.write_int("time", time_left);
+  writer.write_int("width", width);
+  writer.write_int("height", height);
+  writer.write_bool("back_scrolling", back_scrolling);
+  writer.write_float("hor_autoscroll_speed", hor_autoscroll_speed);
+  writer.write_float("gravity", gravity);
+
+  writer.write_int_vector("background-tm", bg_tiles);
+  writer.write_int_vector("interactive-tm", ia_tiles);
+  writer.write_int_vector("foreground-tm", fg_tiles);
+
+  writer.start_list("reset-points");
   for(std::vector<ResetPoint>::iterator i = reset_points.begin();
       i != reset_points.end(); ++i) {
-    writer.startList("point");
-    writer.writeInt("x", i->x);
-    writer.writeInt("y", i->y);
+    writer.start_list("point");
+    writer.write_int("x", i->x);
+    writer.write_int("y", i->y);
+    writer.end_list("point");
   }
-  writer.endList("reset-points");
+  writer.end_list("reset-points");
 
   // write objects
-  writer.startList("objects");
+  writer.start_list("objects");
   // pick all objects that can be written into a levelfile
-  for(std::vector<_GameObject*>::iterator it = world->gameobjects.begin();
+  for(std::vector<GameObject*>::iterator it = world->gameobjects.begin();
       it != world->gameobjects.end(); ++it) {
     Serializable* serializable = dynamic_cast<Serializable*> (*it);
     if(serializable)
       serializable->write(writer);
   }
-  writer.endList("objects");
+  writer.end_list("objects");
 
-  writer.endList("supertux-level");
+  writer.end_list("supertux-level");
   out.close();
 }
 
@@ -566,27 +567,39 @@ void Level::load_image(Surface** ptexture, string theme,const  char * file, int
 void 
 Level::resize(int new_width, int new_height)
 {
-  // first: resize height
-  ia_tiles.resize(new_height * width, 0);
-  bg_tiles.resize(new_height * width, 0);
-  fg_tiles.resize(new_height * width, 0);
-  height = new_height;
+  if(new_width < width) {
+    // remap tiles for new width
+    for(int y = 0; y < height && y < new_height; ++y) {
+      for(int x = 0; x < new_width; ++x) {
+        ia_tiles[y * new_width + x] = ia_tiles[y * width + x];
+        bg_tiles[y * new_width + x] = bg_tiles[y * width + x];
+        fg_tiles[y * new_width + x] = fg_tiles[y * width + x];
+      }
+    }
+  }
 
-  // remap horizontal tiles for new width
-  int np = 0;
-  for(int y = 0; y < height; ++y) {
-    for(int x = 0; x < new_width && x < width; ++x) {
-      ia_tiles[np] = ia_tiles[y * width + x];
-      bg_tiles[np] = bg_tiles[y * width + x];
-      fg_tiles[np] = fg_tiles[y * width + x];
-      np++;
+  ia_tiles.resize(new_width * new_height);
+  bg_tiles.resize(new_width * new_height);
+  fg_tiles.resize(new_width * new_height); 
+
+  if(new_width > width) {
+    // remap tiles
+    for(int y = std::min(height, new_height)-1; y >= 0; --y) {
+      for(int x = new_width-1; x >= 0; --x) {
+        if(x >= width) {
+          ia_tiles[y * new_width + x] = 0;
+          bg_tiles[y * new_width + x] = 0;
+          fg_tiles[y * new_width + x] = 0;
+        } else {
+          ia_tiles[y * new_width + x] = ia_tiles[y * width + x];
+          bg_tiles[y * new_width + x] = bg_tiles[y * width + x];
+          fg_tiles[y * new_width + x] = fg_tiles[y * width + x];
+        }
+      }
     }
   }
 
-  ia_tiles.resize(new_height * new_width);
-  bg_tiles.resize(new_height * new_width);
-  fg_tiles.resize(new_height * new_width); 
-  
+  height = new_height;
   width = new_width;
 }
 
@@ -613,25 +626,6 @@ Level::change(float x, float y, int tm, unsigned int c)
     }
 }
 
-void Level::draw_bg()
-{
-  if(img_bkgd)
-    {
-    // Tile background horizontally
-    int sx = (int)((float)scroll_x * ((float)bkgd_speed/100.0f)) % img_bkgd->w;
-    int sy = (int)((float)scroll_y * ((float)bkgd_speed/100.0f)) % img_bkgd->h;
-    for (int x = 0; (x-1)*img_bkgd->w <= screen->w; x++)
-      for (int y = 0; (y-1)*img_bkgd->h <= screen->h; y++)
-        img_bkgd->draw_part(x == 0 ? sx : 0, y == 0 ? sy : 0,
-                   x == 0 ? 0 : (img_bkgd->w * x) - sx, y == 0 ? 0 : (img_bkgd->h * y) - sy,
-                   x == 0 ? img_bkgd->w - sx : img_bkgd->w, y == 0 ? img_bkgd->h - sy : img_bkgd->h);
-    }
-  else
-    {
-    drawgradient(bkgd_top, bkgd_bottom);
-    }
-}
-
 void
 Level::load_song()
 {