Replaced rectangular shrinkfade with a circular one
[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 "gl_surface_data.hpp"
37 #include "drawing_context.hpp"
38 #include "drawing_request.hpp"
39 #include "surface.hpp"
40 #include "font.hpp"
41 #include "main.hpp"
42 #include "gameconfig.hpp"
43 #include "texture.hpp"
44 #include "texture_manager.hpp"
45 #include "obstack/obstackpp.hpp"
46 #define LIGHTMAP_DIV 5
47
48 namespace 
49 {
50 inline void intern_draw(float left, float top, float right, float bottom,
51                         float uv_left, float uv_top,
52                         float uv_right, float uv_bottom,
53                         float angle, float alpha,
54                         const Color& color,
55                         const Blend& blend,
56                         DrawingEffect effect)
57 {
58   if(effect & HORIZONTAL_FLIP)
59     std::swap(uv_left, uv_right);
60  
61   if(effect & VERTICAL_FLIP) 
62     std::swap(uv_top, uv_bottom);
63  
64   if (angle == 0.0f)
65     { // unrotated blit
66       glBlendFunc(blend.sfactor, blend.dfactor);
67       glColor4f(color.red, color.green, color.blue, color.alpha * alpha);
68       glBegin(GL_QUADS);
69       glTexCoord2f(uv_left, uv_top);
70       glVertex2f(left, top);
71
72       glTexCoord2f(uv_right, uv_top);
73       glVertex2f(right, top);
74
75       glTexCoord2f(uv_right, uv_bottom);
76       glVertex2f(right, bottom);
77
78       glTexCoord2f(uv_left, uv_bottom);
79       glVertex2f(left, bottom);
80       glEnd();
81     }
82   else
83     { // rotated blit
84       float center_x = (left + right) / 2;
85       float center_y = (top + bottom) / 2;
86
87       float sa = sinf(angle/180.0f*M_PI);
88       float ca = cosf(angle/180.0f*M_PI);
89
90       left  -= center_x;
91       right -= center_x;
92
93       top    -= center_y;
94       bottom -= center_y;
95
96       glBlendFunc(blend.sfactor, blend.dfactor);
97       glColor4f(color.red, color.green, color.blue, color.alpha * alpha);
98       glBegin(GL_QUADS);
99       glTexCoord2f(uv_left, uv_top);
100       glVertex2f(left*ca - top*sa + center_x,
101                  left*sa + top*ca + center_y);
102
103       glTexCoord2f(uv_right, uv_top);
104       glVertex2f(right*ca - top*sa + center_x,
105                  right*sa + top*ca + center_y);
106
107       glTexCoord2f(uv_right, uv_bottom);
108       glVertex2f(right*ca - bottom*sa + center_x,
109                  right*sa + bottom*ca + center_y);
110
111       glTexCoord2f(uv_left, uv_bottom);
112       glVertex2f(left*ca - bottom*sa + center_x,
113                  left*sa + bottom*ca + center_y);
114       glEnd();
115     }
116
117   // FIXME: find a better way to restore the blend mode
118   glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
119   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
120 }
121 }
122
123 namespace GL
124 {
125   Renderer::Renderer()
126   {
127     if(texture_manager != 0)
128       texture_manager->save_textures();
129
130     if(config->try_vsync) {
131       /* we want vsync for smooth scrolling */
132     SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
133     }
134
135     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
136     SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
137     SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
138     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
139
140     int flags = SDL_OPENGL;
141     if(config->use_fullscreen)
142       flags |= SDL_FULLSCREEN;
143     int width = config->screenwidth;
144     int height = config->screenheight;
145     int bpp = 0;
146
147     SDL_Surface *screen = SDL_SetVideoMode(width, height, bpp, flags);
148     if(screen == 0) {
149       std::stringstream msg;
150       msg << "Couldn't set video mode (" << width << "x" << height
151           << "-" << bpp << "bpp): " << SDL_GetError();
152       throw std::runtime_error(msg.str());
153     }
154
155     // setup opengl state and transform
156     glDisable(GL_DEPTH_TEST);
157     glDisable(GL_CULL_FACE);
158     glEnable(GL_TEXTURE_2D);
159     glEnable(GL_BLEND);
160     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
161
162     glViewport(0, 0, screen->w, screen->h);
163     glMatrixMode(GL_PROJECTION);
164     glLoadIdentity();
165     // logical resolution here not real monitor resolution
166     glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
167     glMatrixMode(GL_MODELVIEW);
168     glLoadIdentity();
169     glTranslatef(0, 0, 0);
170
171     check_gl_error("Setting up view matrices");
172
173
174     if(texture_manager == 0)
175       texture_manager = new TextureManager();
176     else
177       texture_manager->reload_textures();
178   }
179
180   Renderer::~Renderer()
181   {
182   }
183
184   void
185   Renderer::draw_surface(const DrawingRequest& request)
186   {
187     const Surface* surface = (const Surface*) request.request_data;
188     GL::Texture *gltexture = dynamic_cast<GL::Texture *>(surface->get_texture());
189     GL::SurfaceData *surface_data = reinterpret_cast<GL::SurfaceData *>(surface->get_surface_data());
190
191     glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
192     intern_draw(request.pos.x, request.pos.y,
193                 request.pos.x + surface->get_width(),
194                 request.pos.y + surface->get_height(),
195                 surface_data->get_uv_left(),
196                 surface_data->get_uv_top(),
197                 surface_data->get_uv_right(),
198                 surface_data->get_uv_bottom(),
199                 request.angle,
200                 request.alpha,
201                 request.color,
202                 request.blend,
203                 request.drawing_effect);
204   }
205
206   void
207   Renderer::draw_surface_part(const DrawingRequest& request)
208   {
209     const SurfacePartRequest* surfacepartrequest
210       = (SurfacePartRequest*) request.request_data;
211     const Surface *surface = surfacepartrequest->surface;
212     GL::Texture *gltexture = dynamic_cast<GL::Texture *>(surface->get_texture());
213     GL::SurfaceData *surface_data = reinterpret_cast<GL::SurfaceData *>(surface->get_surface_data());
214
215     float uv_width = surface_data->get_uv_right() - surface_data->get_uv_left();
216     float uv_height = surface_data->get_uv_bottom() - surface_data->get_uv_top();
217
218     float uv_left = surface_data->get_uv_left() + (uv_width * surfacepartrequest->source.x) / surface->get_width();
219     float uv_top = surface_data->get_uv_top() + (uv_height * surfacepartrequest->source.y) / surface->get_height();
220     float uv_right = surface_data->get_uv_left() + (uv_width * (surfacepartrequest->source.x + surfacepartrequest->size.x)) / surface->get_width();
221     float uv_bottom = surface_data->get_uv_top() + (uv_height * (surfacepartrequest->source.y + surfacepartrequest->size.y)) / surface->get_height();
222
223     glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
224     intern_draw(request.pos.x, request.pos.y,
225                 request.pos.x + surfacepartrequest->size.x,
226                 request.pos.y + surfacepartrequest->size.y,
227                 uv_left,
228                 uv_top,
229                 uv_right,
230                 uv_bottom,
231                 0.0,
232                 request.alpha,
233                 Color(1.0, 1.0, 1.0),
234                 Blend(),
235                 request.drawing_effect);
236   }
237
238   void
239   Renderer::draw_gradient(const DrawingRequest& request)
240   {
241     const GradientRequest* gradientrequest 
242       = (GradientRequest*) request.request_data;
243     const Color& top = gradientrequest->top;
244     const Color& bottom = gradientrequest->bottom;
245
246     glDisable(GL_TEXTURE_2D);
247     glBegin(GL_QUADS);
248     glColor4f(top.red, top.green, top.blue, top.alpha);
249     glVertex2f(0, 0);
250     glVertex2f(SCREEN_WIDTH, 0);
251     glColor4f(bottom.red, bottom.green, bottom.blue, bottom.alpha);
252     glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
253     glVertex2f(0, SCREEN_HEIGHT);
254     glEnd();
255     glEnable(GL_TEXTURE_2D);
256     glColor4f(1, 1, 1, 1);
257   }
258
259   void
260   Renderer::draw_filled_rect(const DrawingRequest& request)
261   {
262     const FillRectRequest* fillrectrequest
263       = (FillRectRequest*) request.request_data;
264
265     if (fillrectrequest->radius != 0.0f)
266       {
267         // draw round rect
268         // Keep radius in the limits, so that we get a circle instead of
269         // just graphic junk
270         float radius = std::min(fillrectrequest->radius,
271                                 std::min(fillrectrequest->size.x/2,
272                                          fillrectrequest->size.y/2));
273
274         // inner rectangle
275         Rect irect(request.pos.x    + radius,
276                    request.pos.y    + radius,
277                    request.pos.x + fillrectrequest->size.x - radius,
278                    request.pos.y + fillrectrequest->size.y - radius);
279
280         glDisable(GL_TEXTURE_2D);
281         glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
282                   fillrectrequest->color.blue, fillrectrequest->color.alpha);
283
284
285         int n = 8;
286         glBegin(GL_QUAD_STRIP);
287         for(int i = 0; i <= n; ++i)
288           {
289             float x = sinf(i * (M_PI/2) / n) * radius;
290             float y = cosf(i * (M_PI/2) / n) * radius;
291
292             glVertex2f(irect.get_left()  - x, irect.get_top() - y);
293             glVertex2f(irect.get_right() + x, irect.get_top() - y);
294           }
295         for(int i = 0; i <= n; ++i)
296           {
297             float x = cosf(i * (M_PI/2) / n) * radius;
298             float y = sinf(i * (M_PI/2) / n) * radius;
299
300             glVertex2f(irect.get_left()  - x, irect.get_bottom() + y);
301             glVertex2f(irect.get_right() + x, irect.get_bottom() + y);
302           }
303         glEnd();
304         glEnable(GL_TEXTURE_2D);
305         glColor4f(1, 1, 1, 1);
306       }
307     else
308       {
309         float x = request.pos.x;
310         float y = request.pos.y;
311         float w = fillrectrequest->size.x;
312         float h = fillrectrequest->size.y;
313
314         glDisable(GL_TEXTURE_2D);
315         glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
316                   fillrectrequest->color.blue, fillrectrequest->color.alpha);
317
318         glBegin(GL_QUADS);
319         glVertex2f(x, y);
320         glVertex2f(x+w, y);
321         glVertex2f(x+w, y+h);
322         glVertex2f(x, y+h);
323         glEnd();
324         glEnable(GL_TEXTURE_2D);
325         glColor4f(1, 1, 1, 1);
326       }
327   }
328
329   void
330   Renderer::draw_inverse_ellipse(const DrawingRequest& request)
331   {
332     const InverseEllipseRequest* ellipse = (InverseEllipseRequest*)request.request_data;
333
334     glDisable(GL_TEXTURE_2D);
335     glColor4f(ellipse->color.red,  ellipse->color.green,
336               ellipse->color.blue, ellipse->color.alpha);
337     
338     float x = request.pos.x;
339     float y = request.pos.y;
340     float w = ellipse->size.x/2.0f;
341     float h = ellipse->size.y/2.0f;
342
343     glBegin(GL_TRIANGLES);
344     
345     // Bottom
346     glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
347     glVertex2f(0, SCREEN_HEIGHT);
348     glVertex2f(x, y+h);
349
350     // Top
351     glVertex2f(SCREEN_WIDTH, 0);
352     glVertex2f(0, 0);
353     glVertex2f(x, y-h);
354
355     // Left
356     glVertex2f(SCREEN_WIDTH, 0);
357     glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
358     glVertex2f(x+w, y);
359
360     // Right
361     glVertex2f(0, 0);
362     glVertex2f(0, SCREEN_HEIGHT);
363     glVertex2f(x-w, y);
364
365     glEnd();
366         
367     int slices = 8;
368     for(int i = 0; i < slices; ++i)
369       {
370         float ex1 = sinf(M_PI/2 / slices * i) * w;
371         float ey1 = cosf(M_PI/2 / slices * i) * h;
372
373         float ex2 = sinf(M_PI/2 / slices * (i+1)) * w;
374         float ey2 = cosf(M_PI/2 / slices * (i+1)) * h;
375
376         glBegin(GL_TRIANGLES);
377
378         // Bottom/Right
379         glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
380         glVertex2f(x + ex1, y + ey1);
381         glVertex2f(x + ex2, y + ey2);
382
383         // Top/Left
384         glVertex2f(0, 0);
385         glVertex2f(x - ex1, y - ey1);
386         glVertex2f(x - ex2, y - ey2);
387
388         // Top/Right
389         glVertex2f(SCREEN_WIDTH, 0);
390         glVertex2f(x + ex1, y - ey1);
391         glVertex2f(x + ex2, y - ey2);
392
393         // Bottom/Left
394         glVertex2f(0, SCREEN_HEIGHT);
395         glVertex2f(x - ex1, y + ey1);
396         glVertex2f(x - ex2, y + ey2);
397
398         glEnd();
399       }
400     glEnable(GL_TEXTURE_2D);
401     glColor4f(1, 1, 1, 1);    
402   }
403
404   void 
405   Renderer::do_take_screenshot()
406   {
407     // [Christoph] TODO: Yes, this method also takes care of the actual disk I/O. Split it?
408
409     SDL_Surface *shot_surf;
410     // create surface to hold screenshot
411     #if SDL_BYTEORDER == SDL_BIG_ENDIAN
412     shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x00FF0000, 0x0000FF00, 0x000000FF, 0);
413     #else
414     shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0);
415     #endif
416     if (!shot_surf) {
417       log_warning << "Could not create RGB Surface to contain screenshot" << std::endl;
418       return;
419     }
420
421     // read pixels into array
422     char* pixels = new char[3 * SCREEN_WIDTH * SCREEN_HEIGHT];
423     if (!pixels) {
424       log_warning << "Could not allocate memory to store screenshot" << std::endl;
425       SDL_FreeSurface(shot_surf);
426       return;
427     }
428     glReadPixels(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, pixels);
429
430     // copy array line-by-line
431     for (int i = 0; i < SCREEN_HEIGHT; i++) {
432       char* src = pixels + (3 * SCREEN_WIDTH * (SCREEN_HEIGHT - i - 1));
433       if(SDL_MUSTLOCK(shot_surf))
434       {
435         SDL_LockSurface(shot_surf);
436       }
437       char* dst = ((char*)shot_surf->pixels) + i * shot_surf->pitch;
438       memcpy(dst, src, 3 * SCREEN_WIDTH);
439       if(SDL_MUSTLOCK(shot_surf))
440       {
441         SDL_UnlockSurface(shot_surf);
442       }
443     }
444
445     // free array
446     delete[](pixels);
447
448     // save screenshot
449     static const std::string writeDir = PHYSFS_getWriteDir();
450     static const std::string dirSep = PHYSFS_getDirSeparator();
451     static const std::string baseName = "screenshot";
452     static const std::string fileExt = ".bmp";
453     std::string fullFilename;
454     for (int num = 0; num < 1000; num++) {
455       std::ostringstream oss;
456       oss << baseName;
457       oss << std::setw(3) << std::setfill('0') << num;
458       oss << fileExt;
459       std::string fileName = oss.str();
460       fullFilename = writeDir + dirSep + fileName;
461       if (!PHYSFS_exists(fileName.c_str())) {
462         SDL_SaveBMP(shot_surf, fullFilename.c_str());
463         log_debug << "Wrote screenshot to \"" << fullFilename << "\"" << std::endl;
464         SDL_FreeSurface(shot_surf);
465         return;
466       }
467     }
468     log_warning << "Did not save screenshot, because all files up to \"" << fullFilename << "\" already existed" << std::endl;
469     SDL_FreeSurface(shot_surf);
470   }
471
472   void
473   Renderer::flip()
474   {
475     assert_gl("drawing");
476     SDL_GL_SwapBuffers();
477   }
478 }
479
480 #endif