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