#include "msg.hpp"
Background::Background()
- : type(INVALID), layer(LAYER_BACKGROUND0), image(0)
+ : type(INVALID), layer(LAYER_BACKGROUND0), image_top(0), image(0), image_bottom(0)
{
}
Background::Background(const lisp::Lisp& reader)
- : type(INVALID), layer(LAYER_BACKGROUND0), image(0)
+ : type(INVALID), layer(LAYER_BACKGROUND0), image_top(0), image(0), image_bottom(0)
{
+ // 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);
+ reader.get("speed-y", speed_y);
+ if (reader.get("image-top", imagefile_top)) image_top = new Surface(imagefile_top);
+ if (reader.get("image-bottom", imagefile_bottom)) image_bottom = new Surface(imagefile_bottom);
} else {
std::vector<float> bkgd_top_color, bkgd_bottom_color;
if(reader.get_vector("top_color", bkgd_top_color) &&
writer.start_list("background");
if(type == IMAGE) {
+ if (image_top) writer.write_string("image-top", imagefile_top);
writer.write_string("image", imagefile);
+ if (image_bottom) writer.write_string("image-bottom", imagefile_bottom);
writer.write_float("speed", speed);
+ writer.write_float("speed-y", speed_y);
} else if(type == GRADIENT) {
std::vector<float> bkgd_top_color, bkgd_bottom_color;
bkgd_top_color.push_back(gradient_top.red);
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;
+ 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)
+ for(int x = sx; x < SCREEN_WIDTH; x += w) {
+ for(int y = sy; y < SCREEN_HEIGHT; y += h) {
+ if (image_top && (y < center_image_py)) {
+ context.draw_surface(image_top, Vector(x, y), layer);
+ continue;
+ }
+ if (image_bottom && (y >= bottom_image_py)) {
+ context.draw_surface(image_bottom, Vector(x, y), layer);
+ continue;
+ }
context.draw_surface(image, Vector(x, y), layer);
+ }
+ }
context.pop_transform();
}
}