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 ::Renderer::instance_ = this;
139 #if SDL_MAJOR_VERSION > 1 || SDL_MINOR_VERSION > 2 || (SDL_MINOR_VERSION == 2 && SDL_PATCHLEVEL >= 10)
140 // unfortunately only newer SDLs have these infos.
141 // This must be called before SDL_SetVideoMode() or it will return
142 // the window size instead of the desktop size.
143 const SDL_VideoInfo *info = SDL_GetVideoInfo();
146 desktop_width = info->current_w;
147 desktop_height = info->current_h;
151 if(texture_manager != 0)
152 texture_manager->save_textures();
155 //FIXME: This is not supported on the GP2X
156 if(config->try_vsync) {
157 /* we want vsync for smooth scrolling */
158 SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
162 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
164 // FIXME: Hu? 16bit rendering?
165 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
166 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
167 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
169 int flags = SDL_OPENGL;
173 if(config->use_fullscreen)
175 flags |= SDL_FULLSCREEN;
176 width = config->fullscreen_width;
177 height = config->fullscreen_height;
181 flags |= SDL_RESIZABLE;
182 width = config->window_width;
183 height = config->window_height;
187 SDL_Surface *screen = SDL_SetVideoMode(width, height, bpp, flags);
190 std::stringstream msg;
191 msg << "Couldn't set video mode (" << width << "x" << height
192 << "-" << bpp << "bpp): " << SDL_GetError();
193 throw std::runtime_error(msg.str());
196 // setup opengl state and transform
197 glDisable(GL_DEPTH_TEST);
198 glDisable(GL_CULL_FACE);
199 glEnable(GL_TEXTURE_2D);
201 glEnableClientState(GL_VERTEX_ARRAY);
202 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
203 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
205 // Init the projection matrix, viewport and stuff
208 if(texture_manager == 0)
209 texture_manager = new TextureManager();
211 texture_manager->reload_textures();
214 Renderer::~Renderer()
219 Renderer::draw_surface(const DrawingRequest& request)
221 const Surface* surface = (const Surface*) request.request_data;
222 GL::Texture *gltexture = dynamic_cast<GL::Texture *>(surface->get_texture());
223 GL::SurfaceData *surface_data = reinterpret_cast<GL::SurfaceData *>(surface->get_surface_data());
225 glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
226 intern_draw(request.pos.x, request.pos.y,
227 request.pos.x + surface->get_width(),
228 request.pos.y + surface->get_height(),
229 surface_data->get_uv_left(),
230 surface_data->get_uv_top(),
231 surface_data->get_uv_right(),
232 surface_data->get_uv_bottom(),
237 request.drawing_effect);
241 Renderer::draw_surface_part(const DrawingRequest& request)
243 const SurfacePartRequest* surfacepartrequest
244 = (SurfacePartRequest*) request.request_data;
245 const Surface *surface = surfacepartrequest->surface;
246 GL::Texture *gltexture = dynamic_cast<GL::Texture *>(surface->get_texture());
247 GL::SurfaceData *surface_data = reinterpret_cast<GL::SurfaceData *>(surface->get_surface_data());
249 float uv_width = surface_data->get_uv_right() - surface_data->get_uv_left();
250 float uv_height = surface_data->get_uv_bottom() - surface_data->get_uv_top();
252 float uv_left = surface_data->get_uv_left() + (uv_width * surfacepartrequest->source.x) / surface->get_width();
253 float uv_top = surface_data->get_uv_top() + (uv_height * surfacepartrequest->source.y) / surface->get_height();
254 float uv_right = surface_data->get_uv_left() + (uv_width * (surfacepartrequest->source.x + surfacepartrequest->size.x)) / surface->get_width();
255 float uv_bottom = surface_data->get_uv_top() + (uv_height * (surfacepartrequest->source.y + surfacepartrequest->size.y)) / surface->get_height();
257 glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
258 intern_draw(request.pos.x, request.pos.y,
259 request.pos.x + surfacepartrequest->size.x,
260 request.pos.y + surfacepartrequest->size.y,
267 Color(1.0, 1.0, 1.0),
269 request.drawing_effect);
273 Renderer::draw_gradient(const DrawingRequest& request)
275 const GradientRequest* gradientrequest
276 = (GradientRequest*) request.request_data;
277 const Color& top = gradientrequest->top;
278 const Color& bottom = gradientrequest->bottom;
280 glDisable(GL_TEXTURE_2D);
281 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
282 glEnableClientState(GL_COLOR_ARRAY);
287 SCREEN_WIDTH, SCREEN_HEIGHT,
290 glVertexPointer(2, GL_FLOAT, 0, vertices);
293 top.red, top.green, top.blue, top.alpha,
294 top.red, top.green, top.blue, top.alpha,
295 bottom.red, bottom.green, bottom.blue, bottom.alpha,
296 bottom.red, bottom.green, bottom.blue, bottom.alpha,
298 glColorPointer(4, GL_FLOAT, 0, colors);
300 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
302 glDisableClientState(GL_COLOR_ARRAY);
303 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
305 glEnable(GL_TEXTURE_2D);
306 glColor4f(1, 1, 1, 1);
310 Renderer::draw_filled_rect(const DrawingRequest& request)
312 const FillRectRequest* fillrectrequest
313 = (FillRectRequest*) request.request_data;
315 if (fillrectrequest->radius != 0.0f)
318 // Keep radius in the limits, so that we get a circle instead of
320 float radius = std::min(fillrectrequest->radius,
321 std::min(fillrectrequest->size.x/2,
322 fillrectrequest->size.y/2));
325 Rect irect(request.pos.x + radius,
326 request.pos.y + radius,
327 request.pos.x + fillrectrequest->size.x - radius,
328 request.pos.y + fillrectrequest->size.y - radius);
330 glDisable(GL_TEXTURE_2D);
331 glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
332 fillrectrequest->color.blue, fillrectrequest->color.alpha);
336 //FIXME: This doesn't work with OpenGL ES
337 glBegin(GL_QUAD_STRIP);
338 for(int i = 0; i <= n; ++i)
340 float x = sinf(i * (M_PI/2) / n) * radius;
341 float y = cosf(i * (M_PI/2) / n) * radius;
343 glVertex2f(irect.get_left() - x, irect.get_top() - y);
344 glVertex2f(irect.get_right() + x, irect.get_top() - y);
346 for(int i = 0; i <= n; ++i)
348 float x = cosf(i * (M_PI/2) / n) * radius;
349 float y = sinf(i * (M_PI/2) / n) * radius;
351 glVertex2f(irect.get_left() - x, irect.get_bottom() + y);
352 glVertex2f(irect.get_right() + x, irect.get_bottom() + y);
355 glEnable(GL_TEXTURE_2D);
356 glColor4f(1, 1, 1, 1);
360 float x = request.pos.x;
361 float y = request.pos.y;
362 float w = fillrectrequest->size.x;
363 float h = fillrectrequest->size.y;
365 glDisable(GL_TEXTURE_2D);
366 glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
367 fillrectrequest->color.blue, fillrectrequest->color.alpha);
368 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
376 glVertexPointer(2, GL_FLOAT, 0, vertices);
378 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
380 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
381 glEnable(GL_TEXTURE_2D);
382 glColor4f(1, 1, 1, 1);
387 Renderer::draw_inverse_ellipse(const DrawingRequest& request)
389 const InverseEllipseRequest* ellipse = (InverseEllipseRequest*)request.request_data;
391 glDisable(GL_TEXTURE_2D);
392 glColor4f(ellipse->color.red, ellipse->color.green,
393 ellipse->color.blue, ellipse->color.alpha);
395 float x = request.pos.x;
396 float y = request.pos.y;
397 float w = ellipse->size.x/2.0f;
398 float h = ellipse->size.y/2.0f;
400 static const int slices = 16;
401 static const int points = (slices+1) * 12;
403 float vertices[points * 2];
407 vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
408 vertices[p++] = 0; vertices[p++] = SCREEN_HEIGHT;
409 vertices[p++] = x; vertices[p++] = y+h;
412 vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
413 vertices[p++] = 0; vertices[p++] = 0;
414 vertices[p++] = x; vertices[p++] = y-h;
417 vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
418 vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
419 vertices[p++] = x+w; vertices[p++] = y;
422 vertices[p++] = 0; vertices[p++] = 0;
423 vertices[p++] = 0; vertices[p++] = SCREEN_HEIGHT;
424 vertices[p++] = x-w; vertices[p++] = y;
426 for(int i = 0; i < slices; ++i)
428 float ex1 = sinf(M_PI/2 / slices * i) * w;
429 float ey1 = cosf(M_PI/2 / slices * i) * h;
431 float ex2 = sinf(M_PI/2 / slices * (i+1)) * w;
432 float ey2 = cosf(M_PI/2 / slices * (i+1)) * h;
435 vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
436 vertices[p++] = x + ex1; vertices[p++] = y + ey1;
437 vertices[p++] = x + ex2; vertices[p++] = y + ey2;
440 vertices[p++] = 0; vertices[p++] = 0;
441 vertices[p++] = x - ex1; vertices[p++] = y - ey1;
442 vertices[p++] = x - ex2; vertices[p++] = y - ey2;
445 vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
446 vertices[p++] = x + ex1; vertices[p++] = y - ey1;
447 vertices[p++] = x + ex2; vertices[p++] = y - ey2;
450 vertices[p++] = 0; vertices[p++] = SCREEN_HEIGHT;
451 vertices[p++] = x - ex1; vertices[p++] = y + ey1;
452 vertices[p++] = x - ex2; vertices[p++] = y + ey2;
455 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
456 glVertexPointer(2, GL_FLOAT, 0, vertices);
458 glDrawArrays(GL_TRIANGLES, 0, points);
460 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
462 glEnable(GL_TEXTURE_2D);
463 glColor4f(1, 1, 1, 1);
467 Renderer::do_take_screenshot()
469 // [Christoph] TODO: Yes, this method also takes care of the actual disk I/O. Split it?
471 SDL_Surface *shot_surf;
472 // create surface to hold screenshot
473 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
474 shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x00FF0000, 0x0000FF00, 0x000000FF, 0);
476 shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0);
479 log_warning << "Could not create RGB Surface to contain screenshot" << std::endl;
483 // read pixels into array
484 char* pixels = new char[3 * SCREEN_WIDTH * SCREEN_HEIGHT];
486 log_warning << "Could not allocate memory to store screenshot" << std::endl;
487 SDL_FreeSurface(shot_surf);
490 glReadPixels(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, pixels);
492 // copy array line-by-line
493 for (int i = 0; i < SCREEN_HEIGHT; i++) {
494 char* src = pixels + (3 * SCREEN_WIDTH * (SCREEN_HEIGHT - i - 1));
495 if(SDL_MUSTLOCK(shot_surf))
497 SDL_LockSurface(shot_surf);
499 char* dst = ((char*)shot_surf->pixels) + i * shot_surf->pitch;
500 memcpy(dst, src, 3 * SCREEN_WIDTH);
501 if(SDL_MUSTLOCK(shot_surf))
503 SDL_UnlockSurface(shot_surf);
511 static const std::string writeDir = PHYSFS_getWriteDir();
512 static const std::string dirSep = PHYSFS_getDirSeparator();
513 static const std::string baseName = "screenshot";
514 static const std::string fileExt = ".bmp";
515 std::string fullFilename;
516 for (int num = 0; num < 1000; num++) {
517 std::ostringstream oss;
519 oss << std::setw(3) << std::setfill('0') << num;
521 std::string fileName = oss.str();
522 fullFilename = writeDir + dirSep + fileName;
523 if (!PHYSFS_exists(fileName.c_str())) {
524 SDL_SaveBMP(shot_surf, fullFilename.c_str());
525 log_debug << "Wrote screenshot to \"" << fullFilename << "\"" << std::endl;
526 SDL_FreeSurface(shot_surf);
530 log_warning << "Did not save screenshot, because all files up to \"" << fullFilename << "\" already existed" << std::endl;
531 SDL_FreeSurface(shot_surf);
537 assert_gl("drawing");
538 SDL_GL_SwapBuffers();
542 Renderer::resize(int w, int h)
544 // This causes the screen to go black, which is annoying, but seems
545 // unavoidable with SDL at the moment
546 SDL_SetVideoMode(w, h, 0, SDL_OPENGL | SDL_RESIZABLE);
548 config->window_width = w;
549 config->window_height = h;
555 Renderer::apply_config()
559 std::cout << "Applying Config:"
560 << "\n Desktop: " << desktop_width << "x" << desktop_height
561 << "\n Window: " << config->window_width << "x" << config->window_height
562 << "\n FullRes: " << config->fullscreen_width << "x" << config->fullscreen_height
563 << "\n Aspect: " << config->aspect_width << ":" << config->aspect_height
564 << "\n Magnif: " << config->magnification
569 float target_aspect = float(desktop_width) / desktop_height;
571 if (config->aspect_width != 0 && config->aspect_height != 0)
572 target_aspect = float(config->aspect_width) / float(config->aspect_height);
574 float desktop_aspect = 4.0f / 3.0f; // random default fallback guess
576 if (desktop_width != -1 && desktop_height != -1)
578 desktop_aspect = float(desktop_width) / float(desktop_height);
581 // Get the screen width
582 if (config->use_fullscreen)
584 w = config->fullscreen_width;
585 h = config->fullscreen_height;
586 desktop_aspect = float(w) / float(h);
590 w = config->window_width;
591 h = config->window_height;
594 if (target_aspect > 1.0f)
596 SCREEN_WIDTH = static_cast<int>(w * (target_aspect / desktop_aspect));
597 SCREEN_HEIGHT = static_cast<int>(h);
601 SCREEN_WIDTH = static_cast<int>(w);
602 SCREEN_HEIGHT = static_cast<int>(h * (target_aspect / desktop_aspect));
605 int max_width = 1600; // FIXME: Maybe 1920 is ok too
606 int max_height = 1200;
608 if (config->magnification == 0.0f) // Magic value that means 'minfill'
610 // This scales SCREEN_WIDTH/SCREEN_HEIGHT so that they never excede
611 // max_width/max_height
612 if (SCREEN_WIDTH > max_width || SCREEN_HEIGHT > max_height)
614 float scale1 = float(max_width)/SCREEN_WIDTH;
615 float scale2 = float(max_height)/SCREEN_HEIGHT;
616 float scale = (scale1 < scale2) ? scale1 : scale2;
617 SCREEN_WIDTH = static_cast<int>(SCREEN_WIDTH * scale);
618 SCREEN_HEIGHT = static_cast<int>(SCREEN_HEIGHT * scale);
621 glViewport(0, 0, w, h);
625 SCREEN_WIDTH = static_cast<int>(SCREEN_WIDTH / config->magnification);
626 SCREEN_HEIGHT = static_cast<int>(SCREEN_HEIGHT / config->magnification);
628 // This works by adding black borders around the screen to limit
629 // SCREEN_WIDTH/SCREEN_HEIGHT to max_width/max_height
633 if (SCREEN_WIDTH > max_width)
635 nw = static_cast<int>((float) nw * float(max_width)/SCREEN_WIDTH);
636 SCREEN_WIDTH = static_cast<int>(max_width);
639 if (SCREEN_HEIGHT > max_height)
641 nh = static_cast<int>((float) nh * float(max_height)/SCREEN_HEIGHT);
642 SCREEN_HEIGHT = static_cast<int>(max_height);
645 // Clear both buffers so that we get a clean black border without junk
646 glClear(GL_COLOR_BUFFER_BIT);
647 SDL_GL_SwapBuffers();
648 glClear(GL_COLOR_BUFFER_BIT);
649 SDL_GL_SwapBuffers();
652 std::cout << (w-nw)/2 << " "
654 << nw << "x" << nh << std::endl;
656 glViewport(std::max(0, (w-nw)/2),
657 std::max(0, (h-nh)/2),
663 std::cout << " -> " << SCREEN_WIDTH << "x" << SCREEN_HEIGHT << std::endl;
665 glMatrixMode(GL_PROJECTION);
668 glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1);
670 glMatrixMode(GL_MODELVIEW);
672 glTranslatef(0, 0, 0);
673 check_gl_error("Setting up view matrices");