X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fbackground.cpp;h=1effc167131416e66587134ce5eedc873338b8e2;hb=3369ed08e4b7126af560dc5f9c3442fc76b43ab4;hp=5ebb6c546a4bc7f4b51bdb22fef9f2dd94d1d384;hpb=42398ef94da66fa3eb3d42aeae45c9076cae99ca;p=supertux.git diff --git a/src/background.cpp b/src/background.cpp index 5ebb6c546..1effc1671 100644 --- a/src/background.cpp +++ b/src/background.cpp @@ -19,16 +19,58 @@ #include "background.h" #include "globals.h" -#include "viewport.h" -#include "display_manager.h" +#include "camera.h" +#include "screen/drawing_context.h" +#include "lispwriter.h" -Background::Background(DisplayManager& displaymanager) +Background::Background() + : type(INVALID), image(0) { - displaymanager.add_drawable(this, LAYER_BACKGROUND0); +} + +Background::Background(LispReader& reader) + : type(INVALID), image(0) +{ + if(reader.read_string("image", imagefile) + && reader.read_float("speed", speed)) { + set_image(imagefile, speed); + } + + int tr, tg, tb, br, bg, bb; + if(reader.read_int("top_red", tr) && reader.read_int("top_green", tg) + && reader.read_int("top_blue", tb) && reader.read_int("bottom_red", br) + && reader.read_int("bottom_green", br) + && reader.read_int("bottom_blue", bb)) { + set_gradient(Color(tr, tg, tb), Color(br, bg, bb)); + } } 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) { + writer.write_int("top_red", gradient_top.red); + writer.write_int("top_green", gradient_top.green); + writer.write_int("top_blue", gradient_top.blue); + writer.write_int("bottom_red", gradient_bottom.red); + writer.write_int("bottom_green", gradient_bottom.green); + writer.write_int("bottom_blue", gradient_bottom.blue); + } + + writer.end_list("background"); } void @@ -37,34 +79,40 @@ 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, IGNORE_ALPHA); } void Background::set_gradient(Color top, Color bottom) { - bgtype = BACKGROUND_GRADIENT; + type = GRADIENT; gradient_top = top; gradient_bottom = bottom; } 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.)) + if(type == GRADIENT) { + context.draw_gradient(gradient_top, gradient_bottom, LAYER_BACKGROUND0); + } else if(type == IMAGE) { + int sx = int(-context.get_translation().x * speed) % image->w - image->w; - int sy = int(-viewport.get_translation().y * float(speed/100.)) + 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_BACKGROUND0); + context.pop_transform(); } }