Fixed memory leak in TextureManager, surfaces didn't get SDL_FreeSurface()'ed
[supertux.git] / src / video / video_systems.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 <config.h>
18
19 #include "supertux/gameconfig.hpp"
20 #include "video/lightmap.hpp"
21 #include "video/renderer.hpp"
22 #include "video/sdl/sdl_lightmap.hpp"
23 #include "video/sdl/sdl_renderer.hpp"
24 #include "video/sdl/sdl_surface_data.hpp"
25 #include "video/sdl/sdl_texture.hpp"
26 #include "video/texture.hpp"
27 #include "video/video_systems.hpp"
28
29 #ifdef HAVE_OPENGL
30 #include "video/gl/gl_lightmap.hpp"
31 #include "video/gl/gl_renderer.hpp"
32 #include "video/gl/gl_surface_data.hpp"
33 #include "video/gl/gl_texture.hpp"
34 #endif
35
36 std::unique_ptr<Renderer>
37 VideoSystem::new_renderer()
38 {
39   switch(g_config->video)
40   {
41     case AUTO_VIDEO:
42 #ifdef HAVE_OPENGL
43       try {
44         log_info << "new GL renderer\n";
45         return std::unique_ptr<Renderer>(new GLRenderer());
46       } catch(std::runtime_error& e) {
47         log_warning << "Error creating GL renderer: "  << e.what() << std::endl;
48 #endif
49         log_warning << "new SDL renderer\n";
50         return std::unique_ptr<Renderer>(new SDLRenderer());
51 #ifdef HAVE_OPENGL
52       }
53     case OPENGL:
54       log_info << "new GL renderer\n";
55       return std::unique_ptr<Renderer>(new GLRenderer());
56 #endif
57     case PURE_SDL:
58       log_info << "new SDL renderer\n";
59       return std::unique_ptr<Renderer>(new SDLRenderer());
60     default:
61       assert(0 && "invalid video system in config");
62 #ifdef HAVE_OPENGL
63       log_info << "new GL renderer\n";
64       return std::unique_ptr<Renderer>(new GLRenderer());
65 #else
66       log_warning << "new SDL renderer\n";
67       return std::unique_ptr<Renderer>(new SDLRenderer());
68 #endif
69   }
70 }
71
72 std::unique_ptr<Lightmap>
73 VideoSystem::new_lightmap()
74 {
75   switch(g_config->video)
76   {
77     case AUTO_VIDEO:
78 #ifdef HAVE_OPENGL
79       return std::unique_ptr<Lightmap>(new GLLightmap());
80 #else
81       return std::unique_ptr<Lightmap>(new SDLLightmap());
82 #endif
83 #ifdef HAVE_OPENGL
84     case OPENGL:
85       return std::unique_ptr<Lightmap>(new GLLightmap());
86 #endif
87     case PURE_SDL:
88       return std::unique_ptr<Lightmap>(new SDLLightmap());
89     default:
90       assert(0 && "invalid video system in config");
91 #ifdef HAVE_OPENGL
92       return std::unique_ptr<Lightmap>(new GLLightmap());
93 #else
94       return std::unique_ptr<Lightmap>(new SDLLightmap());
95 #endif
96   }
97 }
98
99 TexturePtr
100 VideoSystem::new_texture(SDL_Surface *image)
101 {
102   switch(g_config->video)
103   {
104     case AUTO_VIDEO:
105 #ifdef HAVE_OPENGL
106       return TexturePtr(new GLTexture(image));
107 #else
108       return TexturePtr(new SDLTexture(image));
109 #endif
110 #ifdef HAVE_OPENGL
111     case OPENGL:
112       return TexturePtr(new GLTexture(image));
113 #endif
114     case PURE_SDL:
115       return TexturePtr(new SDLTexture(image));
116     default:
117       assert(0 && "invalid video system in config");
118 #ifdef HAVE_OPENGL
119       return TexturePtr(new GLTexture(image));
120 #else
121       return TexturePtr(new SDLTexture(image));
122 #endif
123   }
124 }
125
126 SurfaceData*
127 VideoSystem::new_surface_data(const Surface &surface)
128 {
129   switch(g_config->video)
130   {
131     case AUTO_VIDEO:
132 #ifdef HAVE_OPENGL
133       return new GLSurfaceData(surface);
134 #else
135       return new SDLSurfaceData(surface);
136 #endif
137 #ifdef HAVE_OPENGL
138     case OPENGL:
139       return new GLSurfaceData(surface);
140 #endif
141     case PURE_SDL:
142       return new SDLSurfaceData(surface);
143     default:
144       assert(0 && "invalid video system in config");
145 #ifdef HAVE_OPENGL
146       return new GLSurfaceData(surface);
147 #else
148       return new SDLSurfaceData(surface);
149 #endif
150   }
151 }
152
153 void
154 VideoSystem::free_surface_data(SurfaceData* surface_data)
155 {
156   if(surface_data == NULL)
157     return;
158
159   delete surface_data;
160 }
161
162 VideoSystem::Enum
163 VideoSystem::get_video_system(const std::string &video)
164 {
165   if(video == "auto")
166   {
167     return AUTO_VIDEO;
168   }
169 #ifdef HAVE_OPENGL
170   else if(video == "opengl")
171   {
172     return OPENGL;
173   }
174 #endif
175   else if(video == "sdl")
176   {
177     return PURE_SDL;
178   }
179   else
180   {
181     return AUTO_VIDEO;
182   }
183 }
184
185 std::string
186 VideoSystem::get_video_string(VideoSystem::Enum video)
187 {
188   switch(video)
189   {
190     case AUTO_VIDEO:
191       return "auto";
192     case OPENGL:
193       return "opengl";
194     case PURE_SDL:
195       return "sdl";
196     default:
197       assert(0 && "invalid video system in config");
198       return "auto";
199   }
200 }
201
202 /* EOF */