restore trunk
[supertux.git] / src / video / surface.hpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 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
19 //  02111-1307, USA.
20 #ifndef __SURFACE_HPP__
21 #define __SURFACE_HPP__
22
23 #include <config.h>
24
25 #include <string>
26 #include <SDL.h>
27 #include "math/vector.hpp"
28 #include "texture.hpp"
29 #include "video_systems.hpp"
30
31 /**
32  * A rectangular image.
33  * The class basically holds a reference to a texture with additional UV
34  * coordinates that specify a rectangular area on this texture
35  */
36 class Surface
37 {
38 private:
39   Texture* texture;
40   void *surface_data;
41   int x;
42   int y;
43   int w;
44   int h;
45   bool flipx;
46
47 public:
48   Surface(const std::string& file) :
49     texture(texture_manager->get(file)),
50     x(0), y(0), w(0), h(0),
51     flipx(false)
52   {
53     texture->ref();
54     w = texture->get_image_width();
55     h = texture->get_image_height();
56     surface_data = new_surface_data(*this);
57   }
58
59   Surface(const std::string& file, int x, int y, int w, int h) :
60     texture(texture_manager->get(file)),
61     x(x), y(y), w(w), h(h),
62     flipx(false)
63   {
64     texture->ref();
65     surface_data = new_surface_data(*this);
66   }
67
68   Surface(const Surface& other) :
69     texture(other.texture),
70     x(other.x), y(other.y),
71     w(other.w), h(other.h),
72     flipx(false)
73   {
74     texture->ref();
75     surface_data = new_surface_data(*this);
76   }
77
78   ~Surface()
79   {
80     free_surface_data(surface_data);
81     texture->unref();
82   }
83
84   /** flip the surface horizontally */
85   void hflip()
86   {
87     flipx = !flipx;
88   }
89
90   bool get_flipx() const
91   {
92     return flipx;
93   }
94
95   const Surface& operator= (const Surface& other)
96   {
97     other.texture->ref();
98     texture->unref();
99     texture = other.texture;
100     x = other.x;
101     y = other.y;
102     w = other.w;
103     h = other.h;
104     return *this;
105   }
106
107   Texture *get_texture() const
108   {
109     return texture;
110   }
111
112   void *get_surface_data() const
113   {
114     return surface_data;
115   }
116
117   int get_x() const
118   {
119     return x;
120   }
121
122   int get_y() const
123   {
124     return y;
125   }
126
127   int get_width() const
128   {
129     return w;
130   }
131
132   int get_height() const
133   {
134     return h;
135   }
136
137   Vector get_position() const
138   { return Vector(get_x(), get_y()); }
139
140   /**
141    * returns a vector containing width and height
142    */
143   Vector get_size() const
144   { return Vector(get_width(), get_height()); }
145 };
146
147 #endif