include fixes from ohnobinki, video_systems.cpp should fall back to SDL now if GL...
[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 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 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 new SDLRenderer();
51 #ifdef HAVE_OPENGL
52       }
53     case OPENGL:
54       log_info << "new GL renderer\n";
55       return new GLRenderer();
56 #endif
57     case PURE_SDL:
58       log_info << "new SDL renderer\n";
59       return 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 new GLRenderer();
65 #else
66       log_warning << "new SDL renderer\n";
67       return new SDLRenderer();
68 #endif
69   }
70 }
71
72 Lightmap*
73 VideoSystem::new_lightmap()
74 {
75   switch(g_config->video)
76   {
77     case AUTO_VIDEO:
78 #ifdef HAVE_OPENGL
79       return new GLLightmap();
80 #else
81       return new SDLLightmap();
82 #endif
83 #ifdef HAVE_OPENGL
84     case OPENGL:
85       return new GLLightmap();
86 #endif
87     case PURE_SDL:
88       return new SDLLightmap();
89     default:
90       assert(0 && "invalid video system in config");
91 #ifdef HAVE_OPENGL
92       return new GLLightmap();
93 #else
94       return 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   delete surface_data;
157 }
158
159 VideoSystem::Enum
160 VideoSystem::get_video_system(const std::string &video)
161 {
162   if(video == "auto")
163   {
164     return AUTO_VIDEO;
165   }
166 #ifdef HAVE_OPENGL
167   else if(video == "opengl")
168   {
169     return OPENGL;
170   }
171 #endif
172   else if(video == "sdl")
173   {
174     return PURE_SDL;
175   }
176   else
177   {
178     return AUTO_VIDEO;
179   }
180 }
181
182 std::string
183 VideoSystem::get_video_string(VideoSystem::Enum video)
184 {
185   switch(video)
186   {
187     case AUTO_VIDEO:
188       return "auto";
189     case OPENGL:
190       return "opengl";
191     case PURE_SDL:
192       return "sdl";
193     default:
194       assert(0 && "invalid video system in config");
195       return "auto";
196   }
197 }
198
199 /* EOF */