b15d80b62fae97c9398f95bdc73b148361329c96
[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),
30   y(0), 
31   w(0), 
32   h(0),
33   flipx(false)
34 {
35   texture->ref();
36   w = texture->get_image_width();
37   h = texture->get_image_height();
38   surface_data = new_surface_data(*this);
39 }
40
41 Surface::Surface(const std::string& file, int x, int y, int w, int h) :
42   texture(texture_manager->get(file)),
43   surface_data(),
44   x(x), 
45   y(y),
46   w(w),
47   h(h),
48   flipx(false)
49 {
50   texture->ref();
51   surface_data = new_surface_data(*this);
52 }
53
54 Surface::Surface(const Surface& other) :
55   texture(other.texture),
56   surface_data(),
57   x(other.x), 
58   y(other.y),
59   w(other.w), 
60   h(other.h),
61   flipx(false)
62 {
63   texture->ref();
64   surface_data = new_surface_data(*this);
65 }
66
67 const Surface& 
68 Surface::operator=(const Surface& other)
69 {
70   other.texture->ref();
71   texture->unref();
72   texture = other.texture;
73   x = other.x;
74   y = other.y;
75   w = other.w;
76   h = other.h;
77   return *this;
78 }
79
80 Surface::~Surface()
81 {
82   free_surface_data(surface_data);
83   texture->unref();
84 }
85
86 /** flip the surface horizontally */
87 void Surface::hflip()
88 {
89   flipx = !flipx;
90 }
91
92 bool Surface::get_flipx() const
93 {
94   return flipx;
95 }
96
97 Texture* 
98 Surface::get_texture() const
99 {
100   return texture;
101 }
102
103 void* 
104 Surface::get_surface_data() const
105 {
106   return surface_data;
107 }
108
109 int
110 Surface::get_x() const
111 {
112   return x;
113 }
114
115 int
116 Surface::get_y() const
117 {
118   return y;
119 }
120
121 int 
122 Surface::get_width() const
123 {
124   return w;
125 }
126
127 int 
128 Surface::get_height() const
129 {
130   return h;
131 }
132
133 Vector
134 Surface::get_position() const
135 {
136   return Vector(get_x(), get_y()); 
137 }
138
139 Vector
140 Surface::get_size() const
141 {
142   return Vector(get_width(), get_height()); 
143 }
144
145 /* EOF */