added (temporary) dispenser sprites
[supertux.git] / src / background.cpp
index 5ebb6c5..56506e5 100644 (file)
 //  You should have received a copy of the GNU General Public License
 //  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 "background.h"
+#include "app/globals.h"
+#include "camera.h"
+#include "video/drawing_context.h"
+#include "utils/lispwriter.h"
 
-#include "globals.h"
-#include "viewport.h"
-#include "display_manager.h"
+Background::Background()
+  : type(INVALID), layer(LAYER_BACKGROUND0), image(0)
+{
+}
 
-Background::Background(DisplayManager& displaymanager)
+Background::Background(LispReader& reader)
+  : type(INVALID), layer(LAYER_BACKGROUND0), image(0)
 {
-  displaymanager.add_drawable(this, LAYER_BACKGROUND0);
+  reader.read_int("layer", layer);
+  if(reader.read_string("image", imagefile) 
+      && reader.read_float("speed", speed)) {
+    set_image(imagefile, speed);
+  }
+
+  std::vector <unsigned int> bkgd_top_color, bkgd_bottom_color;
+  if(reader.read_int_vector("top_color", bkgd_top_color) &&
+     reader.read_int_vector("bottom_color", bkgd_bottom_color))
+    set_gradient(Color(bkgd_top_color), Color(bkgd_bottom_color));
 }
 
 Background::~Background()
 {
+  delete image;
+}
+
+void
+Background::write(LispWriter& writer)
+{
+  if(type == INVALID)
+    return;
+    
+  writer.start_list("background");
+
+  if(type == IMAGE) {
+    writer.write_string("image", imagefile);
+    writer.write_float("speed", speed);
+  } else if(type == GRADIENT) {
+    std::vector <unsigned int> bkgd_top_color, bkgd_bottom_color;
+    bkgd_top_color.push_back(gradient_top.red);
+    bkgd_top_color.push_back(gradient_top.green);
+    bkgd_top_color.push_back(gradient_top.blue);
+    bkgd_bottom_color.push_back(gradient_top.red);
+    bkgd_bottom_color.push_back(gradient_top.green);
+    bkgd_bottom_color.push_back(gradient_top.blue);
+    writer.write_int_vector("top_color", bkgd_top_color);
+    writer.write_int_vector("bottom_color", bkgd_bottom_color);
+  }
+  writer.write_int("layer", layer);
+  
+  writer.end_list("background");
 }
 
 void
@@ -37,34 +83,54 @@ Background::action(float)
 }
 
 void
-Background::set_image(Surface* image, float speed)
+Background::set_image(const std::string& name, float speed)
 {
-  bgtype = BACKGROUND_IMAGE;
-  this->image = image;
+  this->type = IMAGE;
+  this->imagefile = name;
   this->speed = speed;
+
+  delete image;
+  image = new Surface(datadir + "/images/background/" + name, false);
 }
 
 void
 Background::set_gradient(Color top, Color bottom)
 {
-  bgtype = BACKGROUND_GRADIENT;
+  type = GRADIENT;
   gradient_top = top;
   gradient_bottom = bottom;
+
+  delete image;
+  image = new Surface(top, bottom, screen->w, screen->h);
 }
 
 void
-Background::draw(ViewPort& viewport, int )
+Background::draw(DrawingContext& context)
 {
-  if(bgtype == BACKGROUND_GRADIENT) {
-    drawgradient(gradient_top, gradient_bottom);
-  } else if(bgtype == BACKGROUND_IMAGE) {
-    int sx = int(-viewport.get_translation().x * float(speed/100.))
-      % image->w - image->w;
-    int sy = int(-viewport.get_translation().y * float(speed/100.))
-      % image->h - image->h;
+  if(type == GRADIENT) {
+    /* In case we are using OpenGL just draw the gradient, else (software mode)
+        use the cache. */
+    if(use_gl)
+      context.draw_gradient(gradient_top, gradient_bottom, layer);
+    else
+      {
+      context.push_transform();
+      context.set_translation(Vector(0, 0));
+      context.draw_surface(image, Vector(0, 0), layer);
+      context.pop_transform();
+      }
+  } else if(type == IMAGE) {
+    if(!image)
+      return;
+    
+    int sx = int(-context.get_translation().x * speed) % image->w - image->w;
+    int sy = int(-context.get_translation().y * speed) % image->h - image->h;
+    context.push_transform();
+    context.set_translation(Vector(0, 0));
     for(int x = sx; x < screen->w; x += image->w)
       for(int y = sy; y < screen->h; y += image->h)
-        image->draw(x, y);
+        context.draw_surface(image, Vector(x, y), layer);
+    context.pop_transform();
   }
 }