X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fobject%2Fbackground.cpp;h=619645b63cade6522c698492c123511ab44772cc;hb=78ac7aef674f518549f96160c6354b589553f952;hp=50b01d09a3f8d3cc0404ffe1703af90a92004317;hpb=e3bb6e46812f108f093e9ad0751a945c34b18cd3;p=supertux.git diff --git a/src/object/background.cpp b/src/object/background.cpp index 50b01d09a..619645b63 100644 --- a/src/object/background.cpp +++ b/src/object/background.cpp @@ -1,12 +1,10 @@ -// $Id$ +// SuperTux +// Copyright (C) 2006 Matthias Braun // -// SuperTux - A Jump'n Run -// Copyright (C) 2004 Matthias Braun +// along with this program. If not, see . -#include "background.h" -#include "app/globals.h" -#include "camera.h" -#include "video/drawing_context.h" -#include "lisp/lisp.h" -#include "lisp/writer.h" -#include "object_factory.h" +#include "object/background.hpp" +#include "supertux/globals.hpp" +#include "supertux/object_factory.hpp" +#include "util/reader.hpp" -Background::Background() - : type(INVALID), layer(LAYER_BACKGROUND0), image(0) +Background::Background() : + layer(LAYER_BACKGROUND0), + imagefile_top(), + imagefile(), + imagefile_bottom(), + pos(), + speed(), + speed_y(), + image_top(), + image(), + image_bottom() { } -Background::Background(const lisp::Lisp& reader) - : type(INVALID), layer(LAYER_BACKGROUND0), image(0) +Background::Background(const Reader& reader) : + layer(LAYER_BACKGROUND0), + imagefile_top(), + imagefile(), + imagefile_bottom(), + pos(), + speed(), + speed_y(), + image_top(), + image(), + image_bottom() { + // read position, defaults to (0,0) + float px = 0; + float py = 0; + reader.get("x", px); + reader.get("y", py); + this->pos = Vector(px,py); + + speed = 1.0; + speed_y = 1.0; + reader.get("layer", layer); - if(reader.get("image", imagefile) - && reader.get("speed", speed)) { - set_image(imagefile, speed); - } + if(!reader.get("image", imagefile) || !reader.get("speed", speed)) + throw std::runtime_error("Must specify image and speed for background"); - std::vector 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)); + set_image(imagefile, speed); + reader.get("speed-y", speed_y); + if (reader.get("image-top", imagefile_top)) { + image_top.reset(new Surface(imagefile_top)); + } + if (reader.get("image-bottom", imagefile_bottom)) { + image_bottom.reset(new Surface(imagefile_bottom)); + } } Background::~Background() { - delete image; -} - -void -Background::write(lisp::Writer& 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 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 -Background::action(float) +Background::update(float) { } void Background::set_image(const std::string& name, float speed) { - 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) -{ - type = GRADIENT; - gradient_top = top; - gradient_bottom = bottom; - - delete image; - image = new Surface(top, bottom, screen->w, screen->h); + image.reset(new Surface(name)); } void 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.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) - context.draw_surface(image, Vector(x, y), layer); - context.pop_transform(); + if(image.get() == NULL) + return; + + int w = (int) image->get_width(); + int h = (int) image->get_height(); + int sx = int(pos.x-context.get_translation().x * speed) % w - w; + int sy = int(pos.y-context.get_translation().y * speed_y) % h - h; + int center_image_py = int(pos.y-context.get_translation().y * speed_y); + int bottom_image_py = int(pos.y-context.get_translation().y * speed_y) + h; + context.push_transform(); + context.set_translation(Vector(0, 0)); + for(int x = sx; x < SCREEN_WIDTH; x += w) { + for(int y = sy; y < SCREEN_HEIGHT; y += h) { + if (image_top.get() != NULL && (y < center_image_py)) { + context.draw_surface(image_top.get(), Vector(x, y), layer); + continue; + } + if (image_bottom.get() != NULL && (y >= bottom_image_py)) { + context.draw_surface(image_bottom.get(), Vector(x, y), layer); + continue; + } + context.draw_surface(image.get(), Vector(x, y), layer); + } } + context.pop_transform(); } IMPLEMENT_FACTORY(Background, "background"); + +/* EOF */