first very unfinished and unpolished version of the intro
[supertux.git] / src / object / background.cpp
index 2d6cc41..025adbc 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 "background.hpp"
+#include "camera.hpp"
+#include "video/drawing_context.hpp"
+#include "lisp/lisp.hpp"
+#include "lisp/writer.hpp"
+#include "object_factory.hpp"
+#include "resources.hpp"
+#include "main.hpp"
 
 Background::Background()
   : type(INVALID), layer(LAYER_BACKGROUND0), image(0)
 {
 }
 
-Background::Background(LispReader& reader)
+Background::Background(const lisp::Lisp& reader)
   : type(INVALID), layer(LAYER_BACKGROUND0), image(0)
 {
-  reader.read_int("layer", layer);
-  if(reader.read_string("image", imagefile) 
-      && reader.read_float("speed", speed)) {
+  reader.get("layer", layer);
+  if(reader.get("image", imagefile) 
+      && reader.get("speed", speed)) {
     set_image(imagefile, speed);
+  } else {
+    std::vector<float> bkgd_top_color, bkgd_bottom_color;
+    if(reader.get_vector("top_color", bkgd_top_color) &&
+        reader.get_vector("bottom_color", bkgd_bottom_color))
+      set_gradient(Color(bkgd_top_color),
+                   Color(bkgd_bottom_color));
   }
-
-  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()
@@ -51,7 +54,7 @@ Background::~Background()
 }
 
 void
-Background::write(LispWriter& writer)
+Background::write(lisp::Writer& writer)
 {
   if(type == INVALID)
     return;
@@ -62,15 +65,15 @@ Background::write(LispWriter& writer)
     writer.write_string("image", imagefile);
     writer.write_float("speed", speed);
   } else if(type == GRADIENT) {
-    std::vector <unsigned int> bkgd_top_color, bkgd_bottom_color;
+    std::vector<float> 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);
+    bkgd_bottom_color.push_back(gradient_bottom.red);
+    bkgd_bottom_color.push_back(gradient_bottom.green);
+    bkgd_bottom_color.push_back(gradient_bottom.blue);
+    writer.write_float_vector("top_color", bkgd_top_color);
+    writer.write_float_vector("bottom_color", bkgd_bottom_color);
   }
   writer.write_int("layer", layer);
   
@@ -78,7 +81,7 @@ Background::write(LispWriter& writer)
 }
 
 void
-Background::action(float)
+Background::update(float)
 {
 }
 
@@ -90,7 +93,7 @@ Background::set_image(const std::string& name, float speed)
   this->speed = speed;
 
   delete image;
-  image = new Surface(datadir + "/images/background/" + name, false);
+  image = new Surface("images/background/" + name);
 }
 
 void
@@ -99,9 +102,16 @@ Background::set_gradient(Color top, Color bottom)
   type = GRADIENT;
   gradient_top = top;
   gradient_bottom = bottom;
+  
+  if (gradient_top.red > 1.0 || gradient_top.green > 1.0
+   || gradient_top.blue > 1.0 || gradient_top.alpha > 1.0)
+    std::cerr << "Warning: top gradient color has values above 1.0." << std::endl;
+  if (gradient_bottom.red > 1.0 || gradient_bottom.green > 1.0
+   || gradient_bottom.blue > 1.0 || gradient_bottom.alpha > 1.0)
+    std::cerr << "Warning: bottom gradient color has values above 1.0." << std::endl;
 
   delete image;
-  image = new Surface(top, bottom, screen->w, screen->h);
+  image = NULL;
 }
 
 void
@@ -110,20 +120,23 @@ Background::draw(DrawingContext& context)
   if(type == GRADIENT) {
     context.push_transform();
     context.set_translation(Vector(0, 0));
-    context.draw_surface(image, Vector(0, 0), layer);
+    context.draw_gradient(gradient_top, gradient_bottom, 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;
+    int w = (int) image->get_width();
+    int h = (int) image->get_height();
+    int sx = int(-context.get_translation().x * speed) % w - w;
+    int sy = int(-context.get_translation().y * speed) % h - 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)
+    for(int x = sx; x < SCREEN_WIDTH; x += w)
+      for(int y = sy; y < SCREEN_HEIGHT; y += h)
         context.draw_surface(image, Vector(x, y), layer);
     context.pop_transform();
   }
 }
 
+IMPLEMENT_FACTORY(Background, "background");