fix tiles being 1 pixel off their correct position
[supertux.git] / src / background.cpp
1 //  $Id$
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2004 Matthias Braun <matze@braunis.de
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 #include "background.h"
20
21 #include "globals.h"
22 #include "camera.h"
23 #include "screen/drawing_context.h"
24
25 Background::Background()
26   : type(INVALID), image(0)
27 {
28 }
29
30 Background::~Background()
31 {
32   delete image;
33 }
34
35 void
36 Background::action(float)
37 {
38 }
39
40 void
41 Background::set_image(const std::string& name, float speed)
42 {
43   type = IMAGE;
44   this->speed = speed;
45
46   delete image;
47   image = new Surface(datadir + "/images/background/" + name, IGNORE_ALPHA);
48 }
49
50 void
51 Background::set_gradient(Color top, Color bottom)
52 {
53   type = GRADIENT;
54   gradient_top = top;
55   gradient_bottom = bottom;
56 }
57
58 void
59 Background::draw(DrawingContext& context)
60 {
61   if(type == GRADIENT) {
62     context.draw_gradient(gradient_top, gradient_bottom, LAYER_BACKGROUND0);
63   } else if(type == IMAGE) {
64     int sx = int(-context.get_translation().x * float(speed/100.))
65       % image->w - image->w;
66     int sy = int(-context.get_translation().y * float(speed/100.))
67       % image->h - image->h;
68     context.push_transform();
69     context.set_translation(Vector(0, 0));
70     for(int x = sx; x < screen->w; x += image->w)
71       for(int y = sy; y < screen->h; y += image->h)
72         context.draw_surface(image, Vector(x, y), LAYER_BACKGROUND0);
73     context.pop_transform();
74   }
75 }
76