Refactored video/ subsystem to make adding other methods of rendering (in particular...
[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
30 /**
31  * A rectangular image.
32  * The class basically holds a reference to a texture with additional UV
33  * coordinates that specify a rectangular area on this texture
34  */
35 class Surface
36 {
37 private:
38   Texture* texture;
39   int x;
40   int y;
41   int w;
42   int h;
43   bool flipx;
44
45 public:
46   Surface(const std::string& file) :
47     texture(texture_manager->get(file)),
48     x(0), y(0), w(0), h(0),
49     flipx(false)
50   {
51     texture->ref();
52     w = texture->get_image_width();
53     h = texture->get_image_height();
54   }
55
56   Surface(const std::string& file, int x, int y, int w, int h) :
57     texture(texture_manager->get(file)),
58     x(x), y(y), w(w), h(h),
59     flipx(false)
60   {
61     texture->ref();
62   }
63
64   Surface(const Surface& other) :
65     texture(other.texture),
66     x(other.x), y(other.y),
67     w(other.w), h(other.h),
68     flipx(false)
69   {
70     texture->ref();
71   }
72
73   ~Surface()
74   {
75     texture->unref();
76   }
77
78   /** flip the surface horizontally */
79   void hflip()
80   {
81     flipx = !flipx;
82   }
83
84   bool get_flipx() const
85   {
86     return flipx;
87   }
88
89   const Surface& operator= (const Surface& other)
90   {
91     other.texture->ref();
92     texture->unref();
93     texture = other.texture;
94     x = other.x;
95     y = other.y;
96     w = other.w;
97     h = other.h;
98     return *this;
99   }
100
101   Texture *get_texture() const
102   {
103     return texture;
104   }
105
106   int get_x() const
107   {
108     return x;
109   }
110
111   int get_y() const
112   {
113     return y;
114   }
115
116   int get_width() const
117   {
118     return w;
119   }
120
121   int get_height() const
122   {
123     return h;
124   }
125
126   Vector get_position() const
127   { return Vector(get_x(), get_y()); }
128
129   /**
130    * returns a vector containing width and height
131    */
132   Vector get_size() const
133   { return Vector(get_width(), get_height()); }
134
135   float get_uv_left() const
136   {
137     return (float) (x + (flipx ? w : 0)) / texture->get_texture_width();
138   }
139
140   float get_uv_top() const
141   {
142     return (float) y / texture->get_texture_height();
143   }
144
145   float get_uv_right() const
146   {
147     return (float) (x + (flipx ? 0 : w)) / texture->get_texture_width();
148   }
149
150   float get_uv_bottom() const
151   {
152     return (float) (y + h) / texture->get_texture_height();
153   }
154
155   //void draw_part(float src_x, float src_y, float dst_x, float dst_y,
156   //               float width, float height, float alpha,
157   //               DrawingEffect effect) const;
158 };
159
160 #endif