Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / video / surface.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "video/surface.hpp"
18
19 #include <config.h>
20
21 #include <SDL.h>
22
23 #include "video/texture.hpp"
24 #include "video/video_systems.hpp"
25
26 Surface::Surface(const std::string& file) :
27   texture(texture_manager->get(file)),
28   surface_data(),
29   x(0), y(0), w(0), h(0),
30   flipx(false)
31 {
32   texture->ref();
33   w = texture->get_image_width();
34   h = texture->get_image_height();
35   surface_data = new_surface_data(*this);
36 }
37
38 Surface::Surface(const std::string& file, int x, int y, int w, int h) :
39   texture(texture_manager->get(file)),
40   surface_data(),
41   x(x), y(y), w(w), h(h),
42   flipx(false)
43 {
44   texture->ref();
45   surface_data = new_surface_data(*this);
46 }
47
48 Surface::Surface(const Surface& other) :
49   texture(other.texture),
50   surface_data(),
51   x(other.x), y(other.y),
52   w(other.w), h(other.h),
53   flipx(false)
54 {
55   texture->ref();
56   surface_data = new_surface_data(*this);
57 }
58
59 const Surface& 
60 Surface::operator= (const Surface& other)
61 {
62   other.texture->ref();
63   texture->unref();
64   texture = other.texture;
65   x = other.x;
66   y = other.y;
67   w = other.w;
68   h = other.h;
69   return *this;
70 }
71
72 Surface::~Surface()
73 {
74   free_surface_data(surface_data);
75   texture->unref();
76 }
77
78 /** flip the surface horizontally */
79 void Surface::hflip()
80 {
81   flipx = !flipx;
82 }
83
84 bool Surface::get_flipx() const
85 {
86   return flipx;
87 }
88
89 Texture* 
90 Surface::get_texture() const
91 {
92   return texture;
93 }
94
95 void* 
96 Surface::get_surface_data() const
97 {
98   return surface_data;
99 }
100
101 int
102 Surface::get_x() const
103 {
104   return x;
105 }
106
107 int
108 Surface::get_y() const
109 {
110   return y;
111 }
112
113 int 
114 Surface::get_width() const
115 {
116   return w;
117 }
118
119 int 
120 Surface::get_height() const
121 {
122   return h;
123 }
124
125 Vector
126 Surface::get_position() const
127 {
128   return Vector(get_x(), get_y()); 
129 }
130
131 Vector
132 Surface::get_size() const
133 {
134   return Vector(get_width(), get_height()); 
135 }
136
137 /* EOF */