2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "video/gl/gl_renderer.hpp"
24 #include "supertux/gameconfig.hpp"
25 #include "supertux/globals.hpp"
26 #include "video/drawing_request.hpp"
27 #include "video/gl/gl_surface_data.hpp"
28 #include "video/gl/gl_texture.hpp"
29 #define LIGHTMAP_DIV 5
31 #ifdef GL_VERSION_ES_CM_1_0
32 # define glOrtho glOrthof
38 inline void intern_draw(float left, float top, float right, float bottom,
39 float uv_left, float uv_top,
40 float uv_right, float uv_bottom,
41 float angle, float alpha,
46 if(effect & HORIZONTAL_FLIP)
47 std::swap(uv_left, uv_right);
49 if(effect & VERTICAL_FLIP)
50 std::swap(uv_top, uv_bottom);
53 glBlendFunc(blend.sfactor, blend.dfactor);
54 glColor4f(color.red, color.green, color.blue, color.alpha * alpha);
63 glVertexPointer(2, GL_FLOAT, 0, vertices);
71 glTexCoordPointer(2, GL_FLOAT, 0, uvs);
73 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
76 float center_x = (left + right) / 2;
77 float center_y = (top + bottom) / 2;
79 float sa = sinf(angle/180.0f*M_PI);
80 float ca = cosf(angle/180.0f*M_PI);
89 left*ca - top*sa + center_x, left*sa + top*ca + center_y,
90 right*ca - top*sa + center_x, right*sa + top*ca + center_y,
91 right*ca - bottom*sa + center_x, right*sa + bottom*ca + center_y,
92 left*ca - bottom*sa + center_x, left*sa + bottom*ca + center_y
94 glVertexPointer(2, GL_FLOAT, 0, vertices);
102 glTexCoordPointer(2, GL_FLOAT, 0, uvs);
104 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
107 // FIXME: find a better way to restore the blend mode
108 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
109 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
114 GLRenderer::GLRenderer() :
118 Renderer::instance_ = this;
120 #if SDL_MAJOR_VERSION > 1 || SDL_MINOR_VERSION > 2 || (SDL_MINOR_VERSION == 2 && SDL_PATCHLEVEL >= 10)
121 // unfortunately only newer SDLs have these infos.
122 // This must be called before SDL_SetVideoMode() or it will return
123 // the window size instead of the desktop size.
124 const SDL_VideoInfo *info = SDL_GetVideoInfo();
127 desktop_width = info->current_w;
128 desktop_height = info->current_h;
132 if(texture_manager != 0)
133 texture_manager->save_textures();
135 #ifdef SDL_GL_SWAP_CONTROL
136 if(config->try_vsync) {
137 /* we want vsync for smooth scrolling */
138 SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
142 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
144 // FIXME: Hu? 16bit rendering?
145 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
146 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
147 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
149 int flags = SDL_OPENGL;
153 if(g_config->use_fullscreen)
155 flags |= SDL_FULLSCREEN;
156 width = g_config->fullscreen_size.width;
157 height = g_config->fullscreen_size.height;
161 // flags |= SDL_RESIZABLE;
162 width = g_config->window_size.width;
163 height = g_config->window_size.height;
167 SDL_Surface *screen = SDL_SetVideoMode(width, height, bpp, flags);
170 std::stringstream msg;
171 msg << "Couldn't set video mode (" << width << "x" << height
172 << "-" << bpp << "bpp): " << SDL_GetError();
173 throw std::runtime_error(msg.str());
176 // setup opengl state and transform
177 glDisable(GL_DEPTH_TEST);
178 glDisable(GL_CULL_FACE);
179 glEnable(GL_TEXTURE_2D);
181 glEnableClientState(GL_VERTEX_ARRAY);
182 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
183 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
185 // Init the projection matrix, viewport and stuff
188 if(texture_manager == 0)
189 texture_manager = new TextureManager();
191 texture_manager->reload_textures();
193 #ifndef GL_VERSION_ES_CM_1_0
194 GLenum err = glewInit();
197 std::ostringstream out;
198 out << "GLRenderer: " << glewGetErrorString(err);
199 throw std::runtime_error(out.str());
201 log_info << "Using GLEW " << glewGetString(GLEW_VERSION) << std::endl;
205 GLRenderer::~GLRenderer()
210 GLRenderer::draw_surface(const DrawingRequest& request)
212 const Surface* surface = (const Surface*) request.request_data;
213 GLTexture *gltexture = dynamic_cast<GLTexture *>(surface->get_texture());
214 GLSurfaceData *surface_data = reinterpret_cast<GLSurfaceData *>(surface->get_surface_data());
216 glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
217 intern_draw(request.pos.x, request.pos.y,
218 request.pos.x + surface->get_width(),
219 request.pos.y + surface->get_height(),
220 surface_data->get_uv_left(),
221 surface_data->get_uv_top(),
222 surface_data->get_uv_right(),
223 surface_data->get_uv_bottom(),
228 request.drawing_effect);
232 GLRenderer::draw_surface_part(const DrawingRequest& request)
234 const SurfacePartRequest* surfacepartrequest
235 = (SurfacePartRequest*) request.request_data;
236 const Surface *surface = surfacepartrequest->surface;
237 GLTexture *gltexture = dynamic_cast<GLTexture *>(surface->get_texture());
238 GLSurfaceData *surface_data = reinterpret_cast<GLSurfaceData *>(surface->get_surface_data());
240 float uv_width = surface_data->get_uv_right() - surface_data->get_uv_left();
241 float uv_height = surface_data->get_uv_bottom() - surface_data->get_uv_top();
243 float uv_left = surface_data->get_uv_left() + (uv_width * surfacepartrequest->source.x) / surface->get_width();
244 float uv_top = surface_data->get_uv_top() + (uv_height * surfacepartrequest->source.y) / surface->get_height();
245 float uv_right = surface_data->get_uv_left() + (uv_width * (surfacepartrequest->source.x + surfacepartrequest->size.x)) / surface->get_width();
246 float uv_bottom = surface_data->get_uv_top() + (uv_height * (surfacepartrequest->source.y + surfacepartrequest->size.y)) / surface->get_height();
248 glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
249 intern_draw(request.pos.x, request.pos.y,
250 request.pos.x + surfacepartrequest->size.x,
251 request.pos.y + surfacepartrequest->size.y,
260 request.drawing_effect);
264 GLRenderer::draw_gradient(const DrawingRequest& request)
266 const GradientRequest* gradientrequest
267 = (GradientRequest*) request.request_data;
268 const Color& top = gradientrequest->top;
269 const Color& bottom = gradientrequest->bottom;
271 glDisable(GL_TEXTURE_2D);
272 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
273 glEnableClientState(GL_COLOR_ARRAY);
278 SCREEN_WIDTH, SCREEN_HEIGHT,
281 glVertexPointer(2, GL_FLOAT, 0, vertices);
284 top.red, top.green, top.blue, top.alpha,
285 top.red, top.green, top.blue, top.alpha,
286 bottom.red, bottom.green, bottom.blue, bottom.alpha,
287 bottom.red, bottom.green, bottom.blue, bottom.alpha,
289 glColorPointer(4, GL_FLOAT, 0, colors);
291 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
293 glDisableClientState(GL_COLOR_ARRAY);
294 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
296 glEnable(GL_TEXTURE_2D);
297 glColor4f(1, 1, 1, 1);
301 GLRenderer::draw_filled_rect(const DrawingRequest& request)
303 const FillRectRequest* fillrectrequest
304 = (FillRectRequest*) request.request_data;
306 glDisable(GL_TEXTURE_2D);
307 glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
308 fillrectrequest->color.blue, fillrectrequest->color.alpha);
309 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
311 if (fillrectrequest->radius != 0.0f)
314 // Keep radius in the limits, so that we get a circle instead of
316 float radius = std::min(fillrectrequest->radius,
317 std::min(fillrectrequest->size.x/2,
318 fillrectrequest->size.y/2));
321 Rect irect(request.pos.x + radius,
322 request.pos.y + radius,
323 request.pos.x + fillrectrequest->size.x - radius,
324 request.pos.y + fillrectrequest->size.y - radius);
328 std::vector<float> vertices((n+1) * 4 * 2);
330 for(int i = 0; i <= n; ++i)
332 float x = sinf(i * (M_PI/2) / n) * radius;
333 float y = cosf(i * (M_PI/2) / n) * radius;
335 vertices[p++] = irect.get_left() - x;
336 vertices[p++] = irect.get_top() - y;
338 vertices[p++] = irect.get_right() + x;
339 vertices[p++] = irect.get_top() - y;
342 for(int i = 0; i <= n; ++i)
344 float x = cosf(i * (M_PI/2) / n) * radius;
345 float y = sinf(i * (M_PI/2) / n) * radius;
347 vertices[p++] = irect.get_left() - x;
348 vertices[p++] = irect.get_bottom() + y;
350 vertices[p++] = irect.get_right() + x;
351 vertices[p++] = irect.get_bottom() + y;
354 glVertexPointer(2, GL_FLOAT, 0, &*vertices.begin());
355 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertices.size()/2);
359 float x = request.pos.x;
360 float y = request.pos.y;
361 float w = fillrectrequest->size.x;
362 float h = fillrectrequest->size.y;
370 glVertexPointer(2, GL_FLOAT, 0, vertices);
372 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
375 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
376 glEnable(GL_TEXTURE_2D);
377 glColor4f(1, 1, 1, 1);
381 GLRenderer::draw_inverse_ellipse(const DrawingRequest& request)
383 const InverseEllipseRequest* ellipse = (InverseEllipseRequest*)request.request_data;
385 glDisable(GL_TEXTURE_2D);
386 glColor4f(ellipse->color.red, ellipse->color.green,
387 ellipse->color.blue, ellipse->color.alpha);
389 float x = request.pos.x;
390 float y = request.pos.y;
391 float w = ellipse->size.x/2.0f;
392 float h = ellipse->size.y/2.0f;
394 static const int slices = 16;
395 static const int points = (slices+1) * 12;
397 float vertices[points * 2];
401 vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
402 vertices[p++] = 0; vertices[p++] = SCREEN_HEIGHT;
403 vertices[p++] = x; vertices[p++] = y+h;
406 vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
407 vertices[p++] = 0; vertices[p++] = 0;
408 vertices[p++] = x; vertices[p++] = y-h;
411 vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
412 vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
413 vertices[p++] = x+w; vertices[p++] = y;
416 vertices[p++] = 0; vertices[p++] = 0;
417 vertices[p++] = 0; vertices[p++] = SCREEN_HEIGHT;
418 vertices[p++] = x-w; vertices[p++] = y;
420 for(int i = 0; i < slices; ++i)
422 float ex1 = sinf(M_PI/2 / slices * i) * w;
423 float ey1 = cosf(M_PI/2 / slices * i) * h;
425 float ex2 = sinf(M_PI/2 / slices * (i+1)) * w;
426 float ey2 = cosf(M_PI/2 / slices * (i+1)) * h;
429 vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
430 vertices[p++] = x + ex1; vertices[p++] = y + ey1;
431 vertices[p++] = x + ex2; vertices[p++] = y + ey2;
434 vertices[p++] = 0; vertices[p++] = 0;
435 vertices[p++] = x - ex1; vertices[p++] = y - ey1;
436 vertices[p++] = x - ex2; vertices[p++] = y - ey2;
439 vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
440 vertices[p++] = x + ex1; vertices[p++] = y - ey1;
441 vertices[p++] = x + ex2; vertices[p++] = y - ey2;
444 vertices[p++] = 0; vertices[p++] = SCREEN_HEIGHT;
445 vertices[p++] = x - ex1; vertices[p++] = y + ey1;
446 vertices[p++] = x - ex2; vertices[p++] = y + ey2;
449 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
450 glVertexPointer(2, GL_FLOAT, 0, vertices);
452 glDrawArrays(GL_TRIANGLES, 0, points);
454 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
456 glEnable(GL_TEXTURE_2D);
457 glColor4f(1, 1, 1, 1);
461 GLRenderer::do_take_screenshot()
463 // [Christoph] TODO: Yes, this method also takes care of the actual disk I/O. Split it?
465 SDL_Surface *shot_surf;
466 // create surface to hold screenshot
467 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
468 shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x00FF0000, 0x0000FF00, 0x000000FF, 0);
470 shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0);
473 log_warning << "Could not create RGB Surface to contain screenshot" << std::endl;
477 // read pixels into array
478 char* pixels = new char[3 * SCREEN_WIDTH * SCREEN_HEIGHT];
480 log_warning << "Could not allocate memory to store screenshot" << std::endl;
481 SDL_FreeSurface(shot_surf);
484 glPixelStorei(GL_PACK_ALIGNMENT, 1);
485 glReadPixels(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, pixels);
487 // copy array line-by-line
488 for (int i = 0; i < SCREEN_HEIGHT; i++) {
489 char* src = pixels + (3 * SCREEN_WIDTH * (SCREEN_HEIGHT - i - 1));
490 if(SDL_MUSTLOCK(shot_surf))
492 SDL_LockSurface(shot_surf);
494 char* dst = ((char*)shot_surf->pixels) + i * shot_surf->pitch;
495 memcpy(dst, src, 3 * SCREEN_WIDTH);
496 if(SDL_MUSTLOCK(shot_surf))
498 SDL_UnlockSurface(shot_surf);
506 static const std::string writeDir = PHYSFS_getWriteDir();
507 static const std::string dirSep = PHYSFS_getDirSeparator();
508 static const std::string baseName = "screenshot";
509 static const std::string fileExt = ".bmp";
510 std::string fullFilename;
511 for (int num = 0; num < 1000; num++) {
512 std::ostringstream oss;
514 oss << std::setw(3) << std::setfill('0') << num;
516 std::string fileName = oss.str();
517 fullFilename = writeDir + dirSep + fileName;
518 if (!PHYSFS_exists(fileName.c_str())) {
519 SDL_SaveBMP(shot_surf, fullFilename.c_str());
520 log_debug << "Wrote screenshot to \"" << fullFilename << "\"" << std::endl;
521 SDL_FreeSurface(shot_surf);
525 log_warning << "Did not save screenshot, because all files up to \"" << fullFilename << "\" already existed" << std::endl;
526 SDL_FreeSurface(shot_surf);
532 assert_gl("drawing");
533 SDL_GL_SwapBuffers();
537 GLRenderer::resize(int w, int h)
539 // This causes the screen to go black, which is annoying, but seems
540 // unavoidable with SDL at the moment
541 SDL_SetVideoMode(w, h, 0, SDL_OPENGL /*| SDL_RESIZABLE*/);
543 g_config->window_size = Size(w, h);
549 GLRenderer::apply_config()
553 std::cout << "Applying Config:"
554 << "\n Desktop: " << desktop_width << "x" << desktop_height
555 << "\n Window: " << g_config->window_size
556 << "\n FullRes: " << g_config->fullscreen_size
557 << "\n Aspect: " << g_config->aspect_size
558 << "\n Magnif: " << g_config->magnification
562 float target_aspect = static_cast<float>(desktop_width) / static_cast<float>(desktop_height);
564 if (g_config->aspect_size != Size(0, 0))
566 target_aspect = float(g_config->aspect_size.width) / float(g_config->aspect_size.height);
569 float desktop_aspect = 4.0f / 3.0f; // random default fallback guess
571 if (desktop_width != -1 && desktop_height != -1)
573 desktop_aspect = float(desktop_width) / float(desktop_height);
578 // Get the screen width
579 if (g_config->use_fullscreen)
581 w = g_config->fullscreen_size.width;
582 h = g_config->fullscreen_size.height;
583 desktop_aspect = float(w) / float(h);
587 w = g_config->window_size.width;
588 h = g_config->window_size.height;
591 if (target_aspect > 1.0f)
593 SCREEN_WIDTH = static_cast<int>(w * (target_aspect / desktop_aspect));
594 SCREEN_HEIGHT = static_cast<int>(h);
598 SCREEN_WIDTH = static_cast<int>(w);
599 SCREEN_HEIGHT = static_cast<int>(h * (target_aspect / desktop_aspect));
602 int max_width = 1600; // FIXME: Maybe 1920 is ok too
603 int max_height = 1200;
605 if (g_config->magnification == 0.0f) // Magic value that means 'minfill'
607 // This scales SCREEN_WIDTH/SCREEN_HEIGHT so that they never excede
608 // max_width/max_height
609 if (SCREEN_WIDTH > max_width || SCREEN_HEIGHT > max_height)
611 float scale1 = float(max_width)/SCREEN_WIDTH;
612 float scale2 = float(max_height)/SCREEN_HEIGHT;
613 float scale = (scale1 < scale2) ? scale1 : scale2;
614 SCREEN_WIDTH = static_cast<int>(SCREEN_WIDTH * scale);
615 SCREEN_HEIGHT = static_cast<int>(SCREEN_HEIGHT * scale);
618 glViewport(0, 0, w, h);
622 SCREEN_WIDTH = static_cast<int>(SCREEN_WIDTH / g_config->magnification);
623 SCREEN_HEIGHT = static_cast<int>(SCREEN_HEIGHT / g_config->magnification);
625 // This works by adding black borders around the screen to limit
626 // SCREEN_WIDTH/SCREEN_HEIGHT to max_width/max_height
630 if (SCREEN_WIDTH > max_width)
632 nw = static_cast<int>((float) nw * float(max_width)/SCREEN_WIDTH);
633 SCREEN_WIDTH = static_cast<int>(max_width);
636 if (SCREEN_HEIGHT > max_height)
638 nh = static_cast<int>((float) nh * float(max_height)/SCREEN_HEIGHT);
639 SCREEN_HEIGHT = static_cast<int>(max_height);
642 // Clear both buffers so that we get a clean black border without junk
643 glClear(GL_COLOR_BUFFER_BIT);
644 SDL_GL_SwapBuffers();
645 glClear(GL_COLOR_BUFFER_BIT);
646 SDL_GL_SwapBuffers();
649 std::cout << (w-nw)/2 << " "
651 << nw << "x" << nh << std::endl;
653 glViewport(std::max(0, (w-nw)/2),
654 std::max(0, (h-nh)/2),
660 std::cout << " -> " << SCREEN_WIDTH << "x" << SCREEN_HEIGHT << std::endl;
662 glMatrixMode(GL_PROJECTION);
665 glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1);
667 glMatrixMode(GL_MODELVIEW);
669 glTranslatef(0, 0, 0);
670 check_gl_error("Setting up view matrices");