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
37 inline void intern_draw(float left, float top, float right, float bottom,
38 float uv_left, float uv_top,
39 float uv_right, float uv_bottom,
40 float angle, float alpha,
45 if(effect & HORIZONTAL_FLIP)
46 std::swap(uv_left, uv_right);
48 if(effect & VERTICAL_FLIP)
49 std::swap(uv_top, uv_bottom);
52 glBlendFunc(blend.sfactor, blend.dfactor);
53 glColor4f(color.red, color.green, color.blue, color.alpha * alpha);
62 glVertexPointer(2, GL_FLOAT, 0, vertices);
70 glTexCoordPointer(2, GL_FLOAT, 0, uvs);
72 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
75 float center_x = (left + right) / 2;
76 float center_y = (top + bottom) / 2;
78 float sa = sinf(angle/180.0f*M_PI);
79 float ca = cosf(angle/180.0f*M_PI);
88 left*ca - top*sa + center_x, left*sa + top*ca + center_y,
89 right*ca - top*sa + center_x, right*sa + top*ca + center_y,
90 right*ca - bottom*sa + center_x, right*sa + bottom*ca + center_y,
91 left*ca - bottom*sa + center_x, left*sa + bottom*ca + center_y
93 glVertexPointer(2, GL_FLOAT, 0, vertices);
101 glTexCoordPointer(2, GL_FLOAT, 0, uvs);
103 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
106 // FIXME: find a better way to restore the blend mode
107 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
108 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
113 GLRenderer::GLRenderer() :
114 desktop_size(-1, -1),
116 fullscreen_active(false)
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_size = Size(info->current_w, info->current_h);
131 if(texture_manager != 0)
132 texture_manager->save_textures();
134 #ifdef SDL_GL_SWAP_CONTROL
135 if(config->try_vsync) {
136 /* we want vsync for smooth scrolling */
137 SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
141 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
143 // FIXME: Hu? 16bit rendering?
144 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
145 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
146 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
148 if(g_config->use_fullscreen)
150 apply_video_mode(g_config->fullscreen_size, true);
154 apply_video_mode(g_config->window_size, false);
157 // setup opengl state and transform
158 glDisable(GL_DEPTH_TEST);
159 glDisable(GL_CULL_FACE);
160 glEnable(GL_TEXTURE_2D);
162 glEnableClientState(GL_VERTEX_ARRAY);
163 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
164 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
166 // Init the projection matrix, viewport and stuff
169 if(texture_manager == 0)
170 texture_manager = new TextureManager();
172 texture_manager->reload_textures();
174 #ifndef GL_VERSION_ES_CM_1_0
175 GLenum err = glewInit();
178 std::ostringstream out;
179 out << "GLRenderer: " << glewGetErrorString(err);
180 throw std::runtime_error(out.str());
182 log_info << "Using GLEW " << glewGetString(GLEW_VERSION) << std::endl;
183 log_info << "GL_ARB_texture_non_power_of_two: " << GL_ARB_texture_non_power_of_two << std::endl;
187 GLRenderer::~GLRenderer()
192 GLRenderer::draw_surface(const DrawingRequest& request)
194 const Surface* surface = (const Surface*) request.request_data;
195 GLTexture *gltexture = dynamic_cast<GLTexture *>(surface->get_texture());
196 GLSurfaceData *surface_data = reinterpret_cast<GLSurfaceData *>(surface->get_surface_data());
198 glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
199 intern_draw(request.pos.x, request.pos.y,
200 request.pos.x + surface->get_width(),
201 request.pos.y + surface->get_height(),
202 surface_data->get_uv_left(),
203 surface_data->get_uv_top(),
204 surface_data->get_uv_right(),
205 surface_data->get_uv_bottom(),
210 request.drawing_effect);
214 GLRenderer::draw_surface_part(const DrawingRequest& request)
216 const SurfacePartRequest* surfacepartrequest
217 = (SurfacePartRequest*) request.request_data;
218 const Surface *surface = surfacepartrequest->surface;
219 GLTexture *gltexture = dynamic_cast<GLTexture *>(surface->get_texture());
220 GLSurfaceData *surface_data = reinterpret_cast<GLSurfaceData *>(surface->get_surface_data());
222 float uv_width = surface_data->get_uv_right() - surface_data->get_uv_left();
223 float uv_height = surface_data->get_uv_bottom() - surface_data->get_uv_top();
225 float uv_left = surface_data->get_uv_left() + (uv_width * surfacepartrequest->source.x) / surface->get_width();
226 float uv_top = surface_data->get_uv_top() + (uv_height * surfacepartrequest->source.y) / surface->get_height();
227 float uv_right = surface_data->get_uv_left() + (uv_width * (surfacepartrequest->source.x + surfacepartrequest->size.x)) / surface->get_width();
228 float uv_bottom = surface_data->get_uv_top() + (uv_height * (surfacepartrequest->source.y + surfacepartrequest->size.y)) / surface->get_height();
230 glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
231 intern_draw(request.pos.x, request.pos.y,
232 request.pos.x + surfacepartrequest->size.x,
233 request.pos.y + surfacepartrequest->size.y,
242 request.drawing_effect);
246 GLRenderer::draw_gradient(const DrawingRequest& request)
248 const GradientRequest* gradientrequest
249 = (GradientRequest*) request.request_data;
250 const Color& top = gradientrequest->top;
251 const Color& bottom = gradientrequest->bottom;
253 glDisable(GL_TEXTURE_2D);
254 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
255 glEnableClientState(GL_COLOR_ARRAY);
260 SCREEN_WIDTH, SCREEN_HEIGHT,
263 glVertexPointer(2, GL_FLOAT, 0, vertices);
266 top.red, top.green, top.blue, top.alpha,
267 top.red, top.green, top.blue, top.alpha,
268 bottom.red, bottom.green, bottom.blue, bottom.alpha,
269 bottom.red, bottom.green, bottom.blue, bottom.alpha,
271 glColorPointer(4, GL_FLOAT, 0, colors);
273 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
275 glDisableClientState(GL_COLOR_ARRAY);
276 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
278 glEnable(GL_TEXTURE_2D);
279 glColor4f(1, 1, 1, 1);
283 GLRenderer::draw_filled_rect(const DrawingRequest& request)
285 const FillRectRequest* fillrectrequest
286 = (FillRectRequest*) request.request_data;
288 glDisable(GL_TEXTURE_2D);
289 glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
290 fillrectrequest->color.blue, fillrectrequest->color.alpha);
291 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
293 if (fillrectrequest->radius != 0.0f)
296 // Keep radius in the limits, so that we get a circle instead of
298 float radius = std::min(fillrectrequest->radius,
299 std::min(fillrectrequest->size.x/2,
300 fillrectrequest->size.y/2));
303 Rect irect(request.pos.x + radius,
304 request.pos.y + radius,
305 request.pos.x + fillrectrequest->size.x - radius,
306 request.pos.y + fillrectrequest->size.y - radius);
310 std::vector<float> vertices((n+1) * 4 * 2);
312 for(int i = 0; i <= n; ++i)
314 float x = sinf(i * (M_PI/2) / n) * radius;
315 float y = cosf(i * (M_PI/2) / n) * radius;
317 vertices[p++] = irect.get_left() - x;
318 vertices[p++] = irect.get_top() - y;
320 vertices[p++] = irect.get_right() + x;
321 vertices[p++] = irect.get_top() - y;
324 for(int i = 0; i <= n; ++i)
326 float x = cosf(i * (M_PI/2) / n) * radius;
327 float y = sinf(i * (M_PI/2) / n) * radius;
329 vertices[p++] = irect.get_left() - x;
330 vertices[p++] = irect.get_bottom() + y;
332 vertices[p++] = irect.get_right() + x;
333 vertices[p++] = irect.get_bottom() + y;
336 glVertexPointer(2, GL_FLOAT, 0, &*vertices.begin());
337 glDrawArrays(GL_TRIANGLE_STRIP, 0, vertices.size()/2);
341 float x = request.pos.x;
342 float y = request.pos.y;
343 float w = fillrectrequest->size.x;
344 float h = fillrectrequest->size.y;
352 glVertexPointer(2, GL_FLOAT, 0, vertices);
354 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
357 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
358 glEnable(GL_TEXTURE_2D);
359 glColor4f(1, 1, 1, 1);
363 GLRenderer::draw_inverse_ellipse(const DrawingRequest& request)
365 const InverseEllipseRequest* ellipse = (InverseEllipseRequest*)request.request_data;
367 glDisable(GL_TEXTURE_2D);
368 glColor4f(ellipse->color.red, ellipse->color.green,
369 ellipse->color.blue, ellipse->color.alpha);
371 float x = request.pos.x;
372 float y = request.pos.y;
373 float w = ellipse->size.x/2.0f;
374 float h = ellipse->size.y/2.0f;
376 static const int slices = 16;
377 static const int points = (slices+1) * 12;
379 float vertices[points * 2];
383 vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
384 vertices[p++] = 0; vertices[p++] = SCREEN_HEIGHT;
385 vertices[p++] = x; vertices[p++] = y+h;
388 vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
389 vertices[p++] = 0; vertices[p++] = 0;
390 vertices[p++] = x; vertices[p++] = y-h;
393 vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
394 vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
395 vertices[p++] = x+w; vertices[p++] = y;
398 vertices[p++] = 0; vertices[p++] = 0;
399 vertices[p++] = 0; vertices[p++] = SCREEN_HEIGHT;
400 vertices[p++] = x-w; vertices[p++] = y;
402 for(int i = 0; i < slices; ++i)
404 float ex1 = sinf(M_PI/2 / slices * i) * w;
405 float ey1 = cosf(M_PI/2 / slices * i) * h;
407 float ex2 = sinf(M_PI/2 / slices * (i+1)) * w;
408 float ey2 = cosf(M_PI/2 / slices * (i+1)) * h;
411 vertices[p++] = SCREEN_WIDTH; vertices[p++] = SCREEN_HEIGHT;
412 vertices[p++] = x + ex1; vertices[p++] = y + ey1;
413 vertices[p++] = x + ex2; vertices[p++] = y + ey2;
416 vertices[p++] = 0; vertices[p++] = 0;
417 vertices[p++] = x - ex1; vertices[p++] = y - ey1;
418 vertices[p++] = x - ex2; vertices[p++] = y - ey2;
421 vertices[p++] = SCREEN_WIDTH; vertices[p++] = 0;
422 vertices[p++] = x + ex1; vertices[p++] = y - ey1;
423 vertices[p++] = x + ex2; vertices[p++] = y - ey2;
426 vertices[p++] = 0; vertices[p++] = SCREEN_HEIGHT;
427 vertices[p++] = x - ex1; vertices[p++] = y + ey1;
428 vertices[p++] = x - ex2; vertices[p++] = y + ey2;
431 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
432 glVertexPointer(2, GL_FLOAT, 0, vertices);
434 glDrawArrays(GL_TRIANGLES, 0, points);
436 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
438 glEnable(GL_TEXTURE_2D);
439 glColor4f(1, 1, 1, 1);
443 GLRenderer::do_take_screenshot()
445 // [Christoph] TODO: Yes, this method also takes care of the actual disk I/O. Split it?
447 SDL_Surface *shot_surf;
448 // create surface to hold screenshot
449 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
450 shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x00FF0000, 0x0000FF00, 0x000000FF, 0);
452 shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0);
455 log_warning << "Could not create RGB Surface to contain screenshot" << std::endl;
459 // read pixels into array
460 char* pixels = new char[3 * SCREEN_WIDTH * SCREEN_HEIGHT];
462 log_warning << "Could not allocate memory to store screenshot" << std::endl;
463 SDL_FreeSurface(shot_surf);
466 glPixelStorei(GL_PACK_ALIGNMENT, 1);
467 glReadPixels(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, pixels);
469 // copy array line-by-line
470 for (int i = 0; i < SCREEN_HEIGHT; i++) {
471 char* src = pixels + (3 * SCREEN_WIDTH * (SCREEN_HEIGHT - i - 1));
472 if(SDL_MUSTLOCK(shot_surf))
474 SDL_LockSurface(shot_surf);
476 char* dst = ((char*)shot_surf->pixels) + i * shot_surf->pitch;
477 memcpy(dst, src, 3 * SCREEN_WIDTH);
478 if(SDL_MUSTLOCK(shot_surf))
480 SDL_UnlockSurface(shot_surf);
488 static const std::string writeDir = PHYSFS_getWriteDir();
489 static const std::string dirSep = PHYSFS_getDirSeparator();
490 static const std::string baseName = "screenshot";
491 static const std::string fileExt = ".bmp";
492 std::string fullFilename;
493 for (int num = 0; num < 1000; num++) {
494 std::ostringstream oss;
496 oss << std::setw(3) << std::setfill('0') << num;
498 std::string fileName = oss.str();
499 fullFilename = writeDir + dirSep + fileName;
500 if (!PHYSFS_exists(fileName.c_str())) {
501 SDL_SaveBMP(shot_surf, fullFilename.c_str());
502 log_debug << "Wrote screenshot to \"" << fullFilename << "\"" << std::endl;
503 SDL_FreeSurface(shot_surf);
507 log_warning << "Did not save screenshot, because all files up to \"" << fullFilename << "\" already existed" << std::endl;
508 SDL_FreeSurface(shot_surf);
514 assert_gl("drawing");
515 SDL_GL_SwapBuffers();
519 GLRenderer::resize(int w, int h)
521 // This causes the screen to go black, which is annoying, but seems
522 // unavoidable with SDL at the moment
523 SDL_SetVideoMode(w, h, 0, SDL_OPENGL /*| SDL_RESIZABLE*/);
525 g_config->window_size = Size(w, h);
531 GLRenderer::apply_config()
535 std::cout << "Applying Config:"
536 << "\n Desktop: " << desktop_size.width << "x" << desktop_size.height
537 << "\n Window: " << g_config->window_size
538 << "\n FullRes: " << g_config->fullscreen_size
539 << "\n Aspect: " << g_config->aspect_size
540 << "\n Magnif: " << g_config->magnification
544 float target_aspect = static_cast<float>(desktop_size.width) / static_cast<float>(desktop_size.height);
545 if (g_config->aspect_size != Size(0, 0))
547 target_aspect = float(g_config->aspect_size.width) / float(g_config->aspect_size.height);
550 float desktop_aspect = 4.0f / 3.0f; // random default fallback guess
551 if (desktop_size.width != -1 && desktop_size.height != -1)
553 desktop_aspect = float(desktop_size.width) / float(desktop_size.height);
558 // Get the screen width
559 if (g_config->use_fullscreen)
561 screen_size = g_config->fullscreen_size;
562 desktop_aspect = float(screen_size.width) / float(screen_size.height);
566 screen_size = g_config->window_size;
569 apply_video_mode(screen_size, g_config->use_fullscreen);
571 if (target_aspect > 1.0f)
573 SCREEN_WIDTH = static_cast<int>(screen_size.width * (target_aspect / desktop_aspect));
574 SCREEN_HEIGHT = static_cast<int>(screen_size.height);
578 SCREEN_WIDTH = static_cast<int>(screen_size.width);
579 SCREEN_HEIGHT = static_cast<int>(screen_size.height * (target_aspect / desktop_aspect));
582 Size max_size(1600, 1200); // FIXME: Maybe 1920 is ok too
584 if (g_config->magnification == 0.0f) // Magic value that means 'minfill'
586 // This scales SCREEN_WIDTH/SCREEN_HEIGHT so that they never excede
587 // max_size.width/max_size.height
588 if (SCREEN_WIDTH > max_size.width || SCREEN_HEIGHT > max_size.height)
590 float scale1 = float(max_size.width)/SCREEN_WIDTH;
591 float scale2 = float(max_size.height)/SCREEN_HEIGHT;
592 float scale = (scale1 < scale2) ? scale1 : scale2;
593 SCREEN_WIDTH = static_cast<int>(SCREEN_WIDTH * scale);
594 SCREEN_HEIGHT = static_cast<int>(SCREEN_HEIGHT * scale);
597 glViewport(0, 0, screen_size.width, screen_size.height);
601 SCREEN_WIDTH = static_cast<int>(SCREEN_WIDTH / g_config->magnification);
602 SCREEN_HEIGHT = static_cast<int>(SCREEN_HEIGHT / g_config->magnification);
604 // This works by adding black borders around the screen to limit
605 // SCREEN_WIDTH/SCREEN_HEIGHT to max_size.width/max_size.height
606 Size new_size = screen_size;
608 if (SCREEN_WIDTH > max_size.width)
610 new_size.width = static_cast<int>((float) new_size.width * float(max_size.width)/SCREEN_WIDTH);
611 SCREEN_WIDTH = static_cast<int>(max_size.width);
614 if (SCREEN_HEIGHT > max_size.height)
616 new_size.height = static_cast<int>((float) new_size.height * float(max_size.height)/SCREEN_HEIGHT);
617 SCREEN_HEIGHT = static_cast<int>(max_size.height);
620 // Clear both buffers so that we get a clean black border without junk
621 glClear(GL_COLOR_BUFFER_BIT);
622 SDL_GL_SwapBuffers();
623 glClear(GL_COLOR_BUFFER_BIT);
624 SDL_GL_SwapBuffers();
626 glViewport(std::max(0, (screen_size.width - new_size.width) / 2),
627 std::max(0, (screen_size.height - new_size.height) / 2),
628 std::min(new_size.width, screen_size.width),
629 std::min(new_size.height, screen_size.height));
632 glMatrixMode(GL_PROJECTION);
635 glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1);
637 glMatrixMode(GL_MODELVIEW);
639 glTranslatef(0, 0, 0);
640 check_gl_error("Setting up view matrices");
644 GLRenderer::apply_video_mode(const Size& size, bool fullscreen)
646 // Only change video mode when its different from the current one
647 if (screen_size != size || fullscreen_active != fullscreen)
649 int flags = SDL_OPENGL;
653 flags |= SDL_FULLSCREEN;
657 flags |= SDL_RESIZABLE;
660 if (SDL_Surface *screen = SDL_SetVideoMode(size.width, size.height, 0, flags))
662 screen_size = Size(screen->w, screen->h);
663 fullscreen_active = fullscreen;
667 std::ostringstream msg;
668 msg << "Couldn't set video mode " << size.width << "x" << size.height << ": " << SDL_GetError();
669 throw std::runtime_error(msg.str());