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
48 #ifdef GL_VERSION_ES_CM_1_0
49 # define glOrtho glOrthof
55 inline void intern_draw(float left, float top, float right, float bottom,
56 float uv_left, float uv_top,
57 float uv_right, float uv_bottom,
58 float angle, float alpha,
63 if(effect & HORIZONTAL_FLIP)
64 std::swap(uv_left, uv_right);
66 if(effect & VERTICAL_FLIP)
67 std::swap(uv_top, uv_bottom);
70 glBlendFunc(blend.sfactor, blend.dfactor);
71 glColor4f(color.red, color.green, color.blue, color.alpha * alpha);
80 glVertexPointer(2, GL_FLOAT, 0, vertices);
88 glTexCoordPointer(2, GL_FLOAT, 0, uvs);
90 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
93 float center_x = (left + right) / 2;
94 float center_y = (top + bottom) / 2;
96 float sa = sinf(angle/180.0f*M_PI);
97 float ca = cosf(angle/180.0f*M_PI);
106 left*ca - top*sa + center_x, left*sa + top*ca + center_y,
107 right*ca - top*sa + center_x, right*sa + top*ca + center_y,
108 right*ca - bottom*sa + center_x, right*sa + bottom*ca + center_y,
109 left*ca - bottom*sa + center_x, left*sa + bottom*ca + center_y
111 glVertexPointer(2, GL_FLOAT, 0, vertices);
119 glTexCoordPointer(2, GL_FLOAT, 0, uvs);
121 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
124 // FIXME: find a better way to restore the blend mode
125 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
126 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
137 #if SDL_MAJOR_VERSION > 1 || SDL_MINOR_VERSION > 2 || (SDL_MINOR_VERSION == 2 && SDL_PATCHLEVEL >= 10)
138 // unfortunately only newer SDLs have these infos.
139 // This must be called before SDL_SetVideoMode() or it will return
140 // the window size instead of the desktop size.
141 const SDL_VideoInfo *info = SDL_GetVideoInfo();
144 desktop_width = info->current_w;
145 desktop_height = info->current_h;
149 if(texture_manager != 0)
150 texture_manager->save_textures();
152 #ifdef SDL_GL_SWAP_CONTROL
153 if(config->try_vsync) {
154 /* we want vsync for smooth scrolling */
155 SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
159 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
161 // FIXME: Hu? 16bit rendering?
162 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
163 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
164 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
166 int flags = SDL_OPENGL;
170 if(config->use_fullscreen)
172 flags |= SDL_FULLSCREEN;
173 width = config->fullscreen_width;
174 height = config->fullscreen_height;
178 flags |= SDL_RESIZABLE;
179 width = config->window_width;
180 height = config->window_height;
184 SDL_Surface *screen = SDL_SetVideoMode(width, height, bpp, flags);
187 std::stringstream msg;
188 msg << "Couldn't set video mode (" << width << "x" << height
189 << "-" << bpp << "bpp): " << SDL_GetError();
190 throw std::runtime_error(msg.str());
193 // setup opengl state and transform
194 glDisable(GL_DEPTH_TEST);
195 glDisable(GL_CULL_FACE);
196 glEnable(GL_TEXTURE_2D);
198 glEnableClientState(GL_VERTEX_ARRAY);
199 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
200 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
202 // Init the projection matrix, viewport and stuff
205 if(texture_manager == 0)
206 texture_manager = new TextureManager();
208 texture_manager->reload_textures();
211 Renderer::~Renderer()
216 Renderer::draw_surface(const DrawingRequest& request)
218 const Surface* surface = (const Surface*) request.request_data;
219 GL::Texture *gltexture = dynamic_cast<GL::Texture *>(surface->get_texture());
220 GL::SurfaceData *surface_data = reinterpret_cast<GL::SurfaceData *>(surface->get_surface_data());
222 glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
223 intern_draw(request.pos.x, request.pos.y,
224 request.pos.x + surface->get_width(),
225 request.pos.y + surface->get_height(),
226 surface_data->get_uv_left(),
227 surface_data->get_uv_top(),
228 surface_data->get_uv_right(),
229 surface_data->get_uv_bottom(),
234 request.drawing_effect);
238 Renderer::draw_surface_part(const DrawingRequest& request)
240 const SurfacePartRequest* surfacepartrequest
241 = (SurfacePartRequest*) request.request_data;
242 const Surface *surface = surfacepartrequest->surface;
243 GL::Texture *gltexture = dynamic_cast<GL::Texture *>(surface->get_texture());
244 GL::SurfaceData *surface_data = reinterpret_cast<GL::SurfaceData *>(surface->get_surface_data());
246 float uv_width = surface_data->get_uv_right() - surface_data->get_uv_left();
247 float uv_height = surface_data->get_uv_bottom() - surface_data->get_uv_top();
249 float uv_left = surface_data->get_uv_left() + (uv_width * surfacepartrequest->source.x) / surface->get_width();
250 float uv_top = surface_data->get_uv_top() + (uv_height * surfacepartrequest->source.y) / surface->get_height();
251 float uv_right = surface_data->get_uv_left() + (uv_width * (surfacepartrequest->source.x + surfacepartrequest->size.x)) / surface->get_width();
252 float uv_bottom = surface_data->get_uv_top() + (uv_height * (surfacepartrequest->source.y + surfacepartrequest->size.y)) / surface->get_height();
254 glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
255 intern_draw(request.pos.x, request.pos.y,
256 request.pos.x + surfacepartrequest->size.x,
257 request.pos.y + surfacepartrequest->size.y,
266 request.drawing_effect);
270 Renderer::draw_gradient(const DrawingRequest& request)
272 const GradientRequest* gradientrequest
273 = (GradientRequest*) request.request_data;
274 const Color& top = gradientrequest->top;
275 const Color& bottom = gradientrequest->bottom;
277 glDisable(GL_TEXTURE_2D);
278 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
279 glEnableClientState(GL_COLOR_ARRAY);
284 SCREEN_WIDTH, SCREEN_HEIGHT,
287 glVertexPointer(2, GL_FLOAT, 0, vertices);
290 top.red, top.green, top.blue, top.alpha,
291 top.red, top.green, top.blue, top.alpha,
292 bottom.red, bottom.green, bottom.blue, bottom.alpha,
293 bottom.red, bottom.green, bottom.blue, bottom.alpha,
295 glColorPointer(4, GL_FLOAT, 0, colors);
297 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
299 glDisableClientState(GL_COLOR_ARRAY);
300 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
302 glEnable(GL_TEXTURE_2D);
303 glColor4f(1, 1, 1, 1);
307 Renderer::draw_filled_rect(const DrawingRequest& request)
309 const FillRectRequest* fillrectrequest
310 = (FillRectRequest*) request.request_data;
312 glDisable(GL_TEXTURE_2D);
313 glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
314 fillrectrequest->color.blue, fillrectrequest->color.alpha);
315 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
317 if (fillrectrequest->radius != 0.0f)
320 // Keep radius in the limits, so that we get a circle instead of
322 float radius = std::min(fillrectrequest->radius,
323 std::min(fillrectrequest->size.x/2,
324 fillrectrequest->size.y/2));
327 Rect irect(request.pos.x + radius,
328 request.pos.y + radius,
329 request.pos.x + fillrectrequest->size.x - radius,
330 request.pos.y + fillrectrequest->size.y - radius);
335 float vertices[(n+1) * 4 * 2];
337 for(int i = 0; i <= n; ++i)
339 float x = sinf(i * (M_PI/2) / n) * radius;
340 float y = cosf(i * (M_PI/2) / n) * radius;
342 vertices[p++] = irect.get_left() - x;
343 vertices[p++] = irect.get_top() - y;
345 vertices[p++] = irect.get_right() + x;
346 vertices[p++] = irect.get_top() - y;
349 for(int i = 0; i <= n; ++i)
351 float x = cosf(i * (M_PI/2) / n) * radius;
352 float y = sinf(i * (M_PI/2) / n) * radius;
354 vertices[p++] = irect.get_left() - x;
355 vertices[p++] = irect.get_bottom() + y;
357 vertices[p++] = irect.get_right() + x;
358 vertices[p++] = irect.get_bottom() + y;
361 glVertexPointer(2, GL_FLOAT, 0, vertices);
362 glDrawArrays(GL_TRIANGLE_STRIP, 0, sizeof(vertices)/sizeof(float)/2);
366 float x = request.pos.x;
367 float y = request.pos.y;
368 float w = fillrectrequest->size.x;
369 float h = fillrectrequest->size.y;
377 glVertexPointer(2, GL_FLOAT, 0, vertices);
379 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
382 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
383 glEnable(GL_TEXTURE_2D);
384 glColor4f(1, 1, 1, 1);
388 Renderer::draw_inverse_ellipse(const DrawingRequest& request)
390 const InverseEllipseRequest* ellipse = (InverseEllipseRequest*)request.request_data;
392 glDisable(GL_TEXTURE_2D);
393 glColor4f(ellipse->color.red, ellipse->color.green,
394 ellipse->color.blue, ellipse->color.alpha);
396 float x = request.pos.x;
397 float y = request.pos.y;
398 float w = ellipse->size.x/2.0f;
399 float h = ellipse->size.y/2.0f;
401 static const int slices = 16;
402 static const int points = (slices+1) * 12;
404 float vertices[points * 2];
408 vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
409 vertices[p++] = 0; vertices[p++] = SCREEN_HEIGHT;
410 vertices[p++] = x; vertices[p++] = y+h;
413 vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
414 vertices[p++] = 0; vertices[p++] = 0;
415 vertices[p++] = x; vertices[p++] = y-h;
418 vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
419 vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
420 vertices[p++] = x+w; vertices[p++] = y;
423 vertices[p++] = 0; vertices[p++] = 0;
424 vertices[p++] = 0; vertices[p++] = SCREEN_HEIGHT;
425 vertices[p++] = x-w; vertices[p++] = y;
427 for(int i = 0; i < slices; ++i)
429 float ex1 = sinf(M_PI/2 / slices * i) * w;
430 float ey1 = cosf(M_PI/2 / slices * i) * h;
432 float ex2 = sinf(M_PI/2 / slices * (i+1)) * w;
433 float ey2 = cosf(M_PI/2 / slices * (i+1)) * h;
436 vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
437 vertices[p++] = x + ex1; vertices[p++] = y + ey1;
438 vertices[p++] = x + ex2; vertices[p++] = y + ey2;
441 vertices[p++] = 0; vertices[p++] = 0;
442 vertices[p++] = x - ex1; vertices[p++] = y - ey1;
443 vertices[p++] = x - ex2; vertices[p++] = y - ey2;
446 vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
447 vertices[p++] = x + ex1; vertices[p++] = y - ey1;
448 vertices[p++] = x + ex2; vertices[p++] = y - ey2;
451 vertices[p++] = 0; vertices[p++] = SCREEN_HEIGHT;
452 vertices[p++] = x - ex1; vertices[p++] = y + ey1;
453 vertices[p++] = x - ex2; vertices[p++] = y + ey2;
456 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
457 glVertexPointer(2, GL_FLOAT, 0, vertices);
459 glDrawArrays(GL_TRIANGLES, 0, points);
461 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
463 glEnable(GL_TEXTURE_2D);
464 glColor4f(1, 1, 1, 1);
468 Renderer::do_take_screenshot()
470 // [Christoph] TODO: Yes, this method also takes care of the actual disk I/O. Split it?
472 SDL_Surface *shot_surf;
473 // create surface to hold screenshot
474 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
475 shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x00FF0000, 0x0000FF00, 0x000000FF, 0);
477 shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0);
480 log_warning << "Could not create RGB Surface to contain screenshot" << std::endl;
484 // read pixels into array
485 char* pixels = new char[3 * SCREEN_WIDTH * SCREEN_HEIGHT];
487 log_warning << "Could not allocate memory to store screenshot" << std::endl;
488 SDL_FreeSurface(shot_surf);
491 glReadPixels(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, pixels);
493 // copy array line-by-line
494 for (int i = 0; i < SCREEN_HEIGHT; i++) {
495 char* src = pixels + (3 * SCREEN_WIDTH * (SCREEN_HEIGHT - i - 1));
496 if(SDL_MUSTLOCK(shot_surf))
498 SDL_LockSurface(shot_surf);
500 char* dst = ((char*)shot_surf->pixels) + i * shot_surf->pitch;
501 memcpy(dst, src, 3 * SCREEN_WIDTH);
502 if(SDL_MUSTLOCK(shot_surf))
504 SDL_UnlockSurface(shot_surf);
512 static const std::string writeDir = PHYSFS_getWriteDir();
513 static const std::string dirSep = PHYSFS_getDirSeparator();
514 static const std::string baseName = "screenshot";
515 static const std::string fileExt = ".bmp";
516 std::string fullFilename;
517 for (int num = 0; num < 1000; num++) {
518 std::ostringstream oss;
520 oss << std::setw(3) << std::setfill('0') << num;
522 std::string fileName = oss.str();
523 fullFilename = writeDir + dirSep + fileName;
524 if (!PHYSFS_exists(fileName.c_str())) {
525 SDL_SaveBMP(shot_surf, fullFilename.c_str());
526 log_debug << "Wrote screenshot to \"" << fullFilename << "\"" << std::endl;
527 SDL_FreeSurface(shot_surf);
531 log_warning << "Did not save screenshot, because all files up to \"" << fullFilename << "\" already existed" << std::endl;
532 SDL_FreeSurface(shot_surf);
538 assert_gl("drawing");
539 SDL_GL_SwapBuffers();
543 Renderer::resize(int w, int h)
545 // This causes the screen to go black, which is annoying, but seems
546 // unavoidable with SDL at the moment
547 SDL_SetVideoMode(w, h, 0, SDL_OPENGL | SDL_RESIZABLE);
549 config->window_width = w;
550 config->window_height = h;
556 Renderer::apply_config()
560 std::cout << "Applying Config:"
561 << "\n Desktop: " << desktop_width << "x" << desktop_height
562 << "\n Window: " << config->window_width << "x" << config->window_height
563 << "\n FullRes: " << config->fullscreen_width << "x" << config->fullscreen_height
564 << "\n Aspect: " << config->aspect_width << ":" << config->aspect_height
565 << "\n Magnif: " << config->magnification
570 float target_aspect = float(desktop_width) / desktop_height;
572 if (config->aspect_width != 0 && config->aspect_height != 0)
573 target_aspect = float(config->aspect_width) / float(config->aspect_height);
575 float desktop_aspect = 4.0f / 3.0f; // random default fallback guess
577 if (desktop_width != -1 && desktop_height != -1)
579 desktop_aspect = float(desktop_width) / float(desktop_height);
582 // Get the screen width
583 if (config->use_fullscreen)
585 w = config->fullscreen_width;
586 h = config->fullscreen_height;
587 desktop_aspect = float(w) / float(h);
591 w = config->window_width;
592 h = config->window_height;
595 if (target_aspect > 1.0f)
597 SCREEN_WIDTH = static_cast<int>(w * (target_aspect / desktop_aspect));
598 SCREEN_HEIGHT = static_cast<int>(h);
602 SCREEN_WIDTH = static_cast<int>(w);
603 SCREEN_HEIGHT = static_cast<int>(h * (target_aspect / desktop_aspect));
606 int max_width = 1600; // FIXME: Maybe 1920 is ok too
607 int max_height = 1200;
609 if (config->magnification == 0.0f) // Magic value that means 'minfill'
611 // This scales SCREEN_WIDTH/SCREEN_HEIGHT so that they never excede
612 // max_width/max_height
613 if (SCREEN_WIDTH > max_width || SCREEN_HEIGHT > max_height)
615 float scale1 = float(max_width)/SCREEN_WIDTH;
616 float scale2 = float(max_height)/SCREEN_HEIGHT;
617 float scale = (scale1 < scale2) ? scale1 : scale2;
618 SCREEN_WIDTH = static_cast<int>(SCREEN_WIDTH * scale);
619 SCREEN_HEIGHT = static_cast<int>(SCREEN_HEIGHT * scale);
622 glViewport(0, 0, w, h);
626 SCREEN_WIDTH = static_cast<int>(SCREEN_WIDTH / config->magnification);
627 SCREEN_HEIGHT = static_cast<int>(SCREEN_HEIGHT / config->magnification);
629 // This works by adding black borders around the screen to limit
630 // SCREEN_WIDTH/SCREEN_HEIGHT to max_width/max_height
634 if (SCREEN_WIDTH > max_width)
636 nw = static_cast<int>((float) nw * float(max_width)/SCREEN_WIDTH);
637 SCREEN_WIDTH = static_cast<int>(max_width);
640 if (SCREEN_HEIGHT > max_height)
642 nh = static_cast<int>((float) nh * float(max_height)/SCREEN_HEIGHT);
643 SCREEN_HEIGHT = static_cast<int>(max_height);
646 // Clear both buffers so that we get a clean black border without junk
647 glClear(GL_COLOR_BUFFER_BIT);
648 SDL_GL_SwapBuffers();
649 glClear(GL_COLOR_BUFFER_BIT);
650 SDL_GL_SwapBuffers();
653 std::cout << (w-nw)/2 << " "
655 << nw << "x" << nh << std::endl;
657 glViewport(std::max(0, (w-nw)/2),
658 std::max(0, (h-nh)/2),
664 std::cout << " -> " << SCREEN_WIDTH << "x" << SCREEN_HEIGHT << std::endl;
666 glMatrixMode(GL_PROJECTION);
669 glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1);
671 glMatrixMode(GL_MODELVIEW);
673 glTranslatef(0, 0, 0);
674 check_gl_error("Setting up view matrices");