Refactored video/ subsystem to make adding other methods of rendering (in particular...
[supertux.git] / src / video / gl_renderer.cpp
1 //  $Id: gl_renderer.cpp 5063 2007-05-27 11:32:00Z matzeb $
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 #include <config.h>
20
21 #ifdef HAVE_OPENGL
22
23 #include <functional>
24 #include <algorithm>
25 #include <cassert>
26 #include <math.h>
27 #include <iostream>
28 #include <SDL_image.h>
29 #include <sstream>
30 #include <iomanip>
31 #include <physfs.h>
32
33 #include "glutil.hpp"
34 #include "gl_renderer.hpp"
35 #include "gl_texture.hpp"
36 #include "drawing_context.hpp"
37 #include "drawing_request.hpp"
38 #include "surface.hpp"
39 #include "font.hpp"
40 #include "main.hpp"
41 #include "gameconfig.hpp"
42 #include "texture.hpp"
43 #include "texture_manager.hpp"
44 #include "obstack/obstackpp.hpp"
45 #define LIGHTMAP_DIV 5
46
47 namespace
48 {
49   inline void intern_draw(float left, float top, float right, float bottom,
50                                   float uv_left, float uv_top,
51                                   float uv_right, float uv_bottom,
52                                   float angle, float alpha,
53                                   const Color& color,
54                                   const Blend& blend,
55                                   DrawingEffect effect)
56   {
57     if(effect & HORIZONTAL_FLIP)
58       std::swap(uv_left, uv_right);
59     if(effect & VERTICAL_FLIP) {
60       std::swap(uv_top, uv_bottom);
61     }
62
63     float center_x = (left + right) / 2;
64     float center_y = (top + bottom) / 2;
65
66     float sa = sinf(angle/180.0f*M_PI);
67     float ca = cosf(angle/180.0f*M_PI);
68
69     left  -= center_x;
70     right -= center_x;
71
72     top    -= center_y;
73     bottom -= center_y;
74
75     glBlendFunc(blend.sfactor, blend.dfactor);
76     glColor4f(color.red, color.green, color.blue, color.alpha * alpha);
77     glBegin(GL_QUADS);
78     glTexCoord2f(uv_left, uv_top);
79     glVertex2f(left*ca - top*sa + center_x,
80                left*sa + top*ca + center_y);
81
82     glTexCoord2f(uv_right, uv_top);
83     glVertex2f(right*ca - top*sa + center_x,
84                right*sa + top*ca + center_y);
85
86     glTexCoord2f(uv_right, uv_bottom);
87     glVertex2f(right*ca - bottom*sa + center_x,
88                right*sa + bottom*ca + center_y);
89
90     glTexCoord2f(uv_left, uv_bottom);
91     glVertex2f(left*ca - bottom*sa + center_x,
92                left*sa + bottom*ca + center_y);
93     glEnd();
94
95     // FIXME: find a better way to restore the blend mode
96     glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
97     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
98   }
99 }
100
101 namespace GL
102 {
103   Renderer::Renderer()
104   {
105     if(texture_manager != 0)
106       texture_manager->save_textures();
107
108     if(config->try_vsync) {
109       /* we want vsync for smooth scrolling */
110     SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
111     }
112
113     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
114     SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
115     SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
116     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
117
118     int flags = SDL_OPENGL;
119     if(config->use_fullscreen)
120       flags |= SDL_FULLSCREEN;
121     int width = config->screenwidth;
122     int height = config->screenheight;
123     int bpp = 0;
124
125     SDL_Surface *screen = SDL_SetVideoMode(width, height, bpp, flags);
126     if(screen == 0) {
127       std::stringstream msg;
128       msg << "Couldn't set video mode (" << width << "x" << height
129           << "-" << bpp << "bpp): " << SDL_GetError();
130       throw std::runtime_error(msg.str());
131     }
132
133     // setup opengl state and transform
134     glDisable(GL_DEPTH_TEST);
135     glDisable(GL_CULL_FACE);
136     glEnable(GL_TEXTURE_2D);
137     glEnable(GL_BLEND);
138     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
139
140     glViewport(0, 0, screen->w, screen->h);
141     glMatrixMode(GL_PROJECTION);
142     glLoadIdentity();
143     // logical resolution here not real monitor resolution
144     glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
145     glMatrixMode(GL_MODELVIEW);
146     glLoadIdentity();
147     glTranslatef(0, 0, 0);
148
149     check_gl_error("Setting up view matrices");
150
151
152     if(texture_manager == 0)
153       texture_manager = new TextureManager();
154     else
155       texture_manager->reload_textures();
156   }
157
158   Renderer::~Renderer()
159   {
160   }
161
162   void
163   Renderer::draw_surface(const DrawingRequest& request)
164   {
165     const Surface* surface = (const Surface*) request.request_data;
166     GL::Texture *gltexture = dynamic_cast<GL::Texture *>(surface->get_texture());
167     glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
168     intern_draw(request.pos.x, request.pos.y,
169                 request.pos.x + surface->get_width(),
170                 request.pos.y + surface->get_height(),
171                 surface->get_uv_left(),
172                 surface->get_uv_top(),
173                 surface->get_uv_right(),
174                 surface->get_uv_bottom(),
175                 request.angle,
176                 request.alpha,
177                 request.color,
178                 request.blend,
179                 request.drawing_effect);
180   }
181
182   void
183   Renderer::draw_surface_part(const DrawingRequest& request)
184   {
185     const SurfacePartRequest* surfacepartrequest
186       = (SurfacePartRequest*) request.request_data;
187     const Surface *surface = surfacepartrequest->surface;
188     GL::Texture *gltexture = dynamic_cast<GL::Texture *>(surface->get_texture());
189
190     float uv_width = surface->get_uv_right() - surface->get_uv_left();
191     float uv_height = surface->get_uv_bottom() - surface->get_uv_top();
192
193     float uv_left = surface->get_uv_left() + (uv_width * surfacepartrequest->source.x) / surface->get_width();
194     float uv_top = surface->get_uv_top() + (uv_height * surfacepartrequest->source.y) / surface->get_height();
195     float uv_right = surface->get_uv_left() + (uv_width * (surfacepartrequest->source.x + surfacepartrequest->size.x)) / surface->get_width();
196     float uv_bottom = surface->get_uv_top() + (uv_height * (surfacepartrequest->source.y + surfacepartrequest->size.y)) / surface->get_height();
197
198     glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
199     intern_draw(request.pos.x, request.pos.y,
200                 request.pos.x + surfacepartrequest->size.x,
201                 request.pos.y + surfacepartrequest->size.y,
202                 uv_left,
203                 uv_top,
204                 uv_right,
205                 uv_bottom,
206                 0.0,
207                 request.alpha,
208                 Color(1.0, 1.0, 1.0),
209                 Blend(),
210                 request.drawing_effect);
211   }
212
213   void
214   Renderer::draw_gradient(const DrawingRequest& request)
215   {
216     const GradientRequest* gradientrequest 
217       = (GradientRequest*) request.request_data;
218     const Color& top = gradientrequest->top;
219     const Color& bottom = gradientrequest->bottom;
220
221     glDisable(GL_TEXTURE_2D);
222     glBegin(GL_QUADS);
223     glColor4f(top.red, top.green, top.blue, top.alpha);
224     glVertex2f(0, 0);
225     glVertex2f(SCREEN_WIDTH, 0);
226     glColor4f(bottom.red, bottom.green, bottom.blue, bottom.alpha);
227     glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
228     glVertex2f(0, SCREEN_HEIGHT);
229     glEnd();
230     glEnable(GL_TEXTURE_2D);
231     glColor4f(1, 1, 1, 1);
232   }
233
234   void
235   Renderer::draw_text(const DrawingRequest& request)
236   {
237     const TextRequest* textrequest = (TextRequest*) request.request_data;
238
239     textrequest->font->draw(this, textrequest->text, request.pos,
240         textrequest->alignment, request.drawing_effect, request.alpha);
241   }
242
243   void
244   Renderer::draw_filled_rect(const DrawingRequest& request)
245   {
246     const FillRectRequest* fillrectrequest
247       = (FillRectRequest*) request.request_data;
248
249     float x = request.pos.x;
250     float y = request.pos.y;
251     float w = fillrectrequest->size.x;
252     float h = fillrectrequest->size.y;
253
254     glDisable(GL_TEXTURE_2D);
255     glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
256               fillrectrequest->color.blue, fillrectrequest->color.alpha);
257
258     glBegin(GL_QUADS);
259     glVertex2f(x, y);
260     glVertex2f(x+w, y);
261     glVertex2f(x+w, y+h);
262     glVertex2f(x, y+h);
263     glEnd();
264     glEnable(GL_TEXTURE_2D);
265     glColor4f(1, 1, 1, 1);
266   }
267
268   void 
269   Renderer::do_take_screenshot()
270   {
271     // [Christoph] TODO: Yes, this method also takes care of the actual disk I/O. Split it?
272
273     SDL_Surface *shot_surf;
274     // create surface to hold screenshot
275     #if SDL_BYTEORDER == SDL_BIG_ENDIAN
276     shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x00FF0000, 0x0000FF00, 0x000000FF, 0);
277     #else
278     shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0);
279     #endif
280     if (!shot_surf) {
281       log_warning << "Could not create RGB Surface to contain screenshot" << std::endl;
282       return;
283     }
284
285     // read pixels into array
286     char* pixels = new char[3 * SCREEN_WIDTH * SCREEN_HEIGHT];
287     if (!pixels) {
288       log_warning << "Could not allocate memory to store screenshot" << std::endl;
289       SDL_FreeSurface(shot_surf);
290       return;
291     }
292     glReadPixels(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, pixels);
293
294     // copy array line-by-line
295     for (int i = 0; i < SCREEN_HEIGHT; i++) {
296       char* src = pixels + (3 * SCREEN_WIDTH * (SCREEN_HEIGHT - i - 1));
297       char* dst = ((char*)shot_surf->pixels) + i * shot_surf->pitch;
298       memcpy(dst, src, 3 * SCREEN_WIDTH);
299     }
300
301     // free array
302     delete[](pixels);
303
304     // save screenshot
305     static const std::string writeDir = PHYSFS_getWriteDir();
306     static const std::string dirSep = PHYSFS_getDirSeparator();
307     static const std::string baseName = "screenshot";
308     static const std::string fileExt = ".bmp";
309     std::string fullFilename;
310     for (int num = 0; num < 1000; num++) {
311       std::ostringstream oss;
312       oss << baseName;
313       oss << std::setw(3) << std::setfill('0') << num;
314       oss << fileExt;
315       std::string fileName = oss.str();
316       fullFilename = writeDir + dirSep + fileName;
317       if (!PHYSFS_exists(fileName.c_str())) {
318         SDL_SaveBMP(shot_surf, fullFilename.c_str());
319         log_debug << "Wrote screenshot to \"" << fullFilename << "\"" << std::endl;
320         SDL_FreeSurface(shot_surf);
321         return;
322       }
323     }
324     log_warning << "Did not save screenshot, because all files up to \"" << fullFilename << "\" already existed" << std::endl;
325     SDL_FreeSurface(shot_surf);
326   }
327
328   void
329   Renderer::flip()
330   {
331     assert_gl("drawing");
332     SDL_GL_SwapBuffers();
333   }
334 }
335
336 #endif