1 // $Id: gl_renderer.cpp 5063 2007-05-27 11:32:00Z matzeb $
4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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.
28 #include <SDL_image.h>
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"
42 #include "gameconfig.hpp"
43 #include "texture.hpp"
44 #include "texture_manager.hpp"
45 #include "obstack/obstackpp.hpp"
46 #define LIGHTMAP_DIV 5
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,
58 if(effect & HORIZONTAL_FLIP)
59 std::swap(uv_left, uv_right);
61 if(effect & VERTICAL_FLIP)
62 std::swap(uv_top, uv_bottom);
66 glBlendFunc(blend.sfactor, blend.dfactor);
67 glColor4f(color.red, color.green, color.blue, color.alpha * alpha);
69 glTexCoord2f(uv_left, uv_top);
70 glVertex2f(left, top);
72 glTexCoord2f(uv_right, uv_top);
73 glVertex2f(right, top);
75 glTexCoord2f(uv_right, uv_bottom);
76 glVertex2f(right, bottom);
78 glTexCoord2f(uv_left, uv_bottom);
79 glVertex2f(left, bottom);
84 float center_x = (left + right) / 2;
85 float center_y = (top + bottom) / 2;
87 float sa = sinf(angle/180.0f*M_PI);
88 float ca = cosf(angle/180.0f*M_PI);
96 glBlendFunc(blend.sfactor, blend.dfactor);
97 glColor4f(color.red, color.green, color.blue, color.alpha * alpha);
99 glTexCoord2f(uv_left, uv_top);
100 glVertex2f(left*ca - top*sa + center_x,
101 left*sa + top*ca + center_y);
103 glTexCoord2f(uv_right, uv_top);
104 glVertex2f(right*ca - top*sa + center_x,
105 right*sa + top*ca + center_y);
107 glTexCoord2f(uv_right, uv_bottom);
108 glVertex2f(right*ca - bottom*sa + center_x,
109 right*sa + bottom*ca + center_y);
111 glTexCoord2f(uv_left, uv_bottom);
112 glVertex2f(left*ca - bottom*sa + center_x,
113 left*sa + bottom*ca + center_y);
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);
129 ::Renderer::instance_ = this;
131 #if SDL_MAJOR_VERSION > 1 || SDL_MINOR_VERSION > 2 || (SDL_MINOR_VERSION == 2 && SDL_PATCHLEVEL >= 10)
132 // unfortunately only newer SDLs have these infos.
133 // This must be called before SDL_SetVideoMode() or it will return
134 // the window size instead of the desktop size.
135 const SDL_VideoInfo *info = SDL_GetVideoInfo();
138 desktop_width = info->current_w;
139 desktop_height = info->current_h;
143 if(texture_manager != 0)
144 texture_manager->save_textures();
146 if(config->try_vsync) {
147 /* we want vsync for smooth scrolling */
148 SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
151 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
153 // Hu? 16bit rendering?
154 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
155 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
156 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
158 int flags = SDL_OPENGL;
161 if(config->use_fullscreen)
163 flags |= SDL_FULLSCREEN;
164 width = config->fullscreen_width;
165 height = config->fullscreen_height;
169 flags |= SDL_RESIZABLE;
170 width = config->window_width;
171 height = config->window_height;
175 SDL_Surface *screen = SDL_SetVideoMode(width, height, bpp, flags);
177 std::stringstream msg;
178 msg << "Couldn't set video mode (" << width << "x" << height
179 << "-" << bpp << "bpp): " << SDL_GetError();
180 throw std::runtime_error(msg.str());
183 // setup opengl state and transform
184 glDisable(GL_DEPTH_TEST);
185 glDisable(GL_CULL_FACE);
186 glEnable(GL_TEXTURE_2D);
188 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
190 glViewport(0, 0, screen->w, screen->h);
191 glMatrixMode(GL_PROJECTION);
194 // logical resolution here not real monitor resolution
195 glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
197 glMatrixMode(GL_MODELVIEW);
199 glTranslatef(0, 0, 0);
201 check_gl_error("Setting up view matrices");
204 if(texture_manager == 0)
205 texture_manager = new TextureManager();
207 texture_manager->reload_textures();
210 Renderer::~Renderer()
215 Renderer::draw_surface(const DrawingRequest& request)
217 const Surface* surface = (const Surface*) request.request_data;
218 GL::Texture *gltexture = dynamic_cast<GL::Texture *>(surface->get_texture());
219 GL::SurfaceData *surface_data = reinterpret_cast<GL::SurfaceData *>(surface->get_surface_data());
221 glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
222 intern_draw(request.pos.x, request.pos.y,
223 request.pos.x + surface->get_width(),
224 request.pos.y + surface->get_height(),
225 surface_data->get_uv_left(),
226 surface_data->get_uv_top(),
227 surface_data->get_uv_right(),
228 surface_data->get_uv_bottom(),
233 request.drawing_effect);
237 Renderer::draw_surface_part(const DrawingRequest& request)
239 const SurfacePartRequest* surfacepartrequest
240 = (SurfacePartRequest*) request.request_data;
241 const Surface *surface = surfacepartrequest->surface;
242 GL::Texture *gltexture = dynamic_cast<GL::Texture *>(surface->get_texture());
243 GL::SurfaceData *surface_data = reinterpret_cast<GL::SurfaceData *>(surface->get_surface_data());
245 float uv_width = surface_data->get_uv_right() - surface_data->get_uv_left();
246 float uv_height = surface_data->get_uv_bottom() - surface_data->get_uv_top();
248 float uv_left = surface_data->get_uv_left() + (uv_width * surfacepartrequest->source.x) / surface->get_width();
249 float uv_top = surface_data->get_uv_top() + (uv_height * surfacepartrequest->source.y) / surface->get_height();
250 float uv_right = surface_data->get_uv_left() + (uv_width * (surfacepartrequest->source.x + surfacepartrequest->size.x)) / surface->get_width();
251 float uv_bottom = surface_data->get_uv_top() + (uv_height * (surfacepartrequest->source.y + surfacepartrequest->size.y)) / surface->get_height();
253 glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
254 intern_draw(request.pos.x, request.pos.y,
255 request.pos.x + surfacepartrequest->size.x,
256 request.pos.y + surfacepartrequest->size.y,
263 Color(1.0, 1.0, 1.0),
265 request.drawing_effect);
269 Renderer::draw_gradient(const DrawingRequest& request)
271 const GradientRequest* gradientrequest
272 = (GradientRequest*) request.request_data;
273 const Color& top = gradientrequest->top;
274 const Color& bottom = gradientrequest->bottom;
276 glDisable(GL_TEXTURE_2D);
278 glColor4f(top.red, top.green, top.blue, top.alpha);
280 glVertex2f(SCREEN_WIDTH, 0);
281 glColor4f(bottom.red, bottom.green, bottom.blue, bottom.alpha);
282 glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
283 glVertex2f(0, SCREEN_HEIGHT);
285 glEnable(GL_TEXTURE_2D);
286 glColor4f(1, 1, 1, 1);
290 Renderer::draw_filled_rect(const DrawingRequest& request)
292 const FillRectRequest* fillrectrequest
293 = (FillRectRequest*) request.request_data;
295 if (fillrectrequest->radius != 0.0f)
298 // Keep radius in the limits, so that we get a circle instead of
300 float radius = std::min(fillrectrequest->radius,
301 std::min(fillrectrequest->size.x/2,
302 fillrectrequest->size.y/2));
305 Rect irect(request.pos.x + radius,
306 request.pos.y + radius,
307 request.pos.x + fillrectrequest->size.x - radius,
308 request.pos.y + fillrectrequest->size.y - radius);
310 glDisable(GL_TEXTURE_2D);
311 glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
312 fillrectrequest->color.blue, fillrectrequest->color.alpha);
316 glBegin(GL_QUAD_STRIP);
317 for(int i = 0; i <= n; ++i)
319 float x = sinf(i * (M_PI/2) / n) * radius;
320 float y = cosf(i * (M_PI/2) / n) * radius;
322 glVertex2f(irect.get_left() - x, irect.get_top() - y);
323 glVertex2f(irect.get_right() + x, irect.get_top() - y);
325 for(int i = 0; i <= n; ++i)
327 float x = cosf(i * (M_PI/2) / n) * radius;
328 float y = sinf(i * (M_PI/2) / n) * radius;
330 glVertex2f(irect.get_left() - x, irect.get_bottom() + y);
331 glVertex2f(irect.get_right() + x, irect.get_bottom() + y);
334 glEnable(GL_TEXTURE_2D);
335 glColor4f(1, 1, 1, 1);
339 float x = request.pos.x;
340 float y = request.pos.y;
341 float w = fillrectrequest->size.x;
342 float h = fillrectrequest->size.y;
344 glDisable(GL_TEXTURE_2D);
345 glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
346 fillrectrequest->color.blue, fillrectrequest->color.alpha);
351 glVertex2f(x+w, y+h);
354 glEnable(GL_TEXTURE_2D);
355 glColor4f(1, 1, 1, 1);
360 Renderer::draw_inverse_ellipse(const DrawingRequest& request)
362 const InverseEllipseRequest* ellipse = (InverseEllipseRequest*)request.request_data;
364 glDisable(GL_TEXTURE_2D);
365 glColor4f(ellipse->color.red, ellipse->color.green,
366 ellipse->color.blue, ellipse->color.alpha);
368 float x = request.pos.x;
369 float y = request.pos.y;
370 float w = ellipse->size.x/2.0f;
371 float h = ellipse->size.y/2.0f;
373 glBegin(GL_TRIANGLES);
376 glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
377 glVertex2f(0, SCREEN_HEIGHT);
381 glVertex2f(SCREEN_WIDTH, 0);
386 glVertex2f(SCREEN_WIDTH, 0);
387 glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
392 glVertex2f(0, SCREEN_HEIGHT);
398 for(int i = 0; i < slices; ++i)
400 float ex1 = sinf(M_PI/2 / slices * i) * w;
401 float ey1 = cosf(M_PI/2 / slices * i) * h;
403 float ex2 = sinf(M_PI/2 / slices * (i+1)) * w;
404 float ey2 = cosf(M_PI/2 / slices * (i+1)) * h;
406 glBegin(GL_TRIANGLES);
409 glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
410 glVertex2f(x + ex1, y + ey1);
411 glVertex2f(x + ex2, y + ey2);
415 glVertex2f(x - ex1, y - ey1);
416 glVertex2f(x - ex2, y - ey2);
419 glVertex2f(SCREEN_WIDTH, 0);
420 glVertex2f(x + ex1, y - ey1);
421 glVertex2f(x + ex2, y - ey2);
424 glVertex2f(0, SCREEN_HEIGHT);
425 glVertex2f(x - ex1, y + ey1);
426 glVertex2f(x - ex2, y + ey2);
430 glEnable(GL_TEXTURE_2D);
431 glColor4f(1, 1, 1, 1);
435 Renderer::do_take_screenshot()
437 // [Christoph] TODO: Yes, this method also takes care of the actual disk I/O. Split it?
439 SDL_Surface *shot_surf;
440 // create surface to hold screenshot
441 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
442 shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x00FF0000, 0x0000FF00, 0x000000FF, 0);
444 shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0);
447 log_warning << "Could not create RGB Surface to contain screenshot" << std::endl;
451 // read pixels into array
452 char* pixels = new char[3 * SCREEN_WIDTH * SCREEN_HEIGHT];
454 log_warning << "Could not allocate memory to store screenshot" << std::endl;
455 SDL_FreeSurface(shot_surf);
458 glReadPixels(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, pixels);
460 // copy array line-by-line
461 for (int i = 0; i < SCREEN_HEIGHT; i++) {
462 char* src = pixels + (3 * SCREEN_WIDTH * (SCREEN_HEIGHT - i - 1));
463 if(SDL_MUSTLOCK(shot_surf))
465 SDL_LockSurface(shot_surf);
467 char* dst = ((char*)shot_surf->pixels) + i * shot_surf->pitch;
468 memcpy(dst, src, 3 * SCREEN_WIDTH);
469 if(SDL_MUSTLOCK(shot_surf))
471 SDL_UnlockSurface(shot_surf);
479 static const std::string writeDir = PHYSFS_getWriteDir();
480 static const std::string dirSep = PHYSFS_getDirSeparator();
481 static const std::string baseName = "screenshot";
482 static const std::string fileExt = ".bmp";
483 std::string fullFilename;
484 for (int num = 0; num < 1000; num++) {
485 std::ostringstream oss;
487 oss << std::setw(3) << std::setfill('0') << num;
489 std::string fileName = oss.str();
490 fullFilename = writeDir + dirSep + fileName;
491 if (!PHYSFS_exists(fileName.c_str())) {
492 SDL_SaveBMP(shot_surf, fullFilename.c_str());
493 log_debug << "Wrote screenshot to \"" << fullFilename << "\"" << std::endl;
494 SDL_FreeSurface(shot_surf);
498 log_warning << "Did not save screenshot, because all files up to \"" << fullFilename << "\" already existed" << std::endl;
499 SDL_FreeSurface(shot_surf);
505 assert_gl("drawing");
506 SDL_GL_SwapBuffers();
510 Renderer::resize(int w, int h)
512 SDL_SetVideoMode(w, h, 0, SDL_OPENGL | SDL_RESIZABLE);
514 config->window_width = w;
515 config->window_height = h;
521 Renderer::apply_config()
523 std::cout << "Applying Config:"
524 << "\n Desktop: " << desktop_width << "x" << desktop_height
525 << "\n Window: " << config->window_width << "x" << config->window_height
526 << "\n FullRes: " << config->fullscreen_width << "x" << config->fullscreen_height
527 << "\n Aspect: " << config->aspect_width << ":" << config->aspect_height
528 << "\n Magnif: " << config->magnification
532 float target_aspect = float(config->aspect_width) / float(config->aspect_height);
533 float desktop_aspect = 4.0f / 3.0f; // random default fallback guess
535 if (desktop_width != -1 && desktop_height != -1)
537 desktop_aspect = float(desktop_width) / float(desktop_height);
540 if (config->use_fullscreen)
542 w = config->fullscreen_width;
543 h = config->fullscreen_height;
544 desktop_aspect = float(w) / float(h);
548 w = config->window_width;
549 h = config->window_height;
552 if (target_aspect > 1.0f)
554 SCREEN_WIDTH = w * (target_aspect / desktop_aspect);
560 SCREEN_HEIGHT = h * (target_aspect / desktop_aspect);
563 SCREEN_WIDTH /= config->magnification;
564 SCREEN_HEIGHT /= config->magnification;
566 int max_width = 1600; // FIXME: Maybe 1920 is ok too
567 int max_height = 1200;
571 // This scales SCREEN_WIDTH/SCREEN_HEIGHT so that they never excede
572 // max_width/max_height
573 if (SCREEN_WIDTH > max_width || SCREEN_HEIGHT > max_height)
575 float scale1 = float(max_width)/SCREEN_WIDTH;
576 float scale2 = float(max_height)/SCREEN_HEIGHT;
577 float scale = scale1 < scale2 ? scale1 : scale2;
578 SCREEN_WIDTH *= scale;
579 SCREEN_HEIGHT *= scale;
582 glViewport(0, 0, w, h);
586 // This works by adding black borders around the screen to limit
587 // SCREEN_WIDTH/SCREEN_HEIGHT to max_width/max_height
591 if (SCREEN_WIDTH > max_width)
593 nw *= float(max_width)/SCREEN_WIDTH;
594 SCREEN_WIDTH = max_width;
597 if (SCREEN_HEIGHT > max_height)
599 nh *= float(max_height)/SCREEN_HEIGHT;
600 SCREEN_HEIGHT = max_height;
603 glClear(GL_COLOR_BUFFER_BIT);
605 std::cout << (w-nw)/2 << " "
607 << nw << "x" << nh << std::endl;
608 glViewport(std::max(0, (w-nw)/2),
609 std::max(0, (h-nh)/2),
614 std::cout << " -> " << SCREEN_WIDTH << "x" << SCREEN_HEIGHT << std::endl;
617 glMatrixMode(GL_PROJECTION);
619 glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);