5f1caf00cc3b6316fd72e9c1976c4099021741a7
[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, const Rect& rect)
34 {
35   return std::auto_ptr<Surface>(new Surface(file, rect));
36 }
37
38 Surface::Surface(const std::string& file) :
39   texture(texture_manager->get(file)),
40   surface_data(),
41   rect(0, 0, 
42       Size(texture->get_image_width(),
43            texture->get_image_height())),
44   flipx(false)
45 {
46   texture->ref();
47   surface_data = VideoSystem::new_surface_data(*this);
48 }
49
50 Surface::Surface(const std::string& file, const Rect& rect_) :
51   texture(texture_manager->get(file)),
52   surface_data(),
53   rect(rect_),
54   flipx(false)
55 {
56   texture->ref();
57   surface_data = VideoSystem::new_surface_data(*this);
58 }
59
60 Surface::Surface(const Surface& rhs) :
61   texture(rhs.texture),
62   surface_data(),
63   rect(rhs.rect),
64   flipx(false)
65 {
66   texture->ref();
67   surface_data = VideoSystem::new_surface_data(*this);
68 }
69
70 const Surface& 
71 Surface::operator=(const Surface& rhs)
72 {
73   rhs.texture->ref();
74   texture->unref();
75   texture = rhs.texture;
76   rect = rhs.rect;
77   return *this;
78 }
79
80 Surface::~Surface()
81 {
82   VideoSystem::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 SurfaceData* 
104 Surface::get_surface_data() const
105 {
106   return surface_data;
107 }
108
109 int
110 Surface::get_x() const
111 {
112   return rect.left;
113 }
114
115 int
116 Surface::get_y() const
117 {
118   return rect.top;
119 }
120
121 int 
122 Surface::get_width() const
123 {
124   return rect.get_width();
125 }
126
127 int 
128 Surface::get_height() const
129 {
130   return rect.get_height();
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 */