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);
127 if(texture_manager != 0)
128 texture_manager->save_textures();
130 if(config->try_vsync) {
131 /* we want vsync for smooth scrolling */
132 SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
135 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
136 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
137 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
138 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
140 int flags = SDL_OPENGL;
141 if(config->use_fullscreen)
142 flags |= SDL_FULLSCREEN;
143 int width = config->screenwidth;
144 int height = config->screenheight;
147 SDL_Surface *screen = SDL_SetVideoMode(width, height, bpp, flags);
149 std::stringstream msg;
150 msg << "Couldn't set video mode (" << width << "x" << height
151 << "-" << bpp << "bpp): " << SDL_GetError();
152 throw std::runtime_error(msg.str());
155 // setup opengl state and transform
156 glDisable(GL_DEPTH_TEST);
157 glDisable(GL_CULL_FACE);
158 glEnable(GL_TEXTURE_2D);
160 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
162 glViewport(0, 0, screen->w, screen->h);
163 glMatrixMode(GL_PROJECTION);
165 // logical resolution here not real monitor resolution
166 glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
167 glMatrixMode(GL_MODELVIEW);
169 glTranslatef(0, 0, 0);
171 check_gl_error("Setting up view matrices");
174 if(texture_manager == 0)
175 texture_manager = new TextureManager();
177 texture_manager->reload_textures();
180 Renderer::~Renderer()
185 Renderer::draw_surface(const DrawingRequest& request)
187 const Surface* surface = (const Surface*) request.request_data;
188 GL::Texture *gltexture = dynamic_cast<GL::Texture *>(surface->get_texture());
189 GL::SurfaceData *surface_data = reinterpret_cast<GL::SurfaceData *>(surface->get_surface_data());
191 glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
192 intern_draw(request.pos.x, request.pos.y,
193 request.pos.x + surface->get_width(),
194 request.pos.y + surface->get_height(),
195 surface_data->get_uv_left(),
196 surface_data->get_uv_top(),
197 surface_data->get_uv_right(),
198 surface_data->get_uv_bottom(),
203 request.drawing_effect);
207 Renderer::draw_surface_part(const DrawingRequest& request)
209 const SurfacePartRequest* surfacepartrequest
210 = (SurfacePartRequest*) request.request_data;
211 const Surface *surface = surfacepartrequest->surface;
212 GL::Texture *gltexture = dynamic_cast<GL::Texture *>(surface->get_texture());
213 GL::SurfaceData *surface_data = reinterpret_cast<GL::SurfaceData *>(surface->get_surface_data());
215 float uv_width = surface_data->get_uv_right() - surface_data->get_uv_left();
216 float uv_height = surface_data->get_uv_bottom() - surface_data->get_uv_top();
218 float uv_left = surface_data->get_uv_left() + (uv_width * surfacepartrequest->source.x) / surface->get_width();
219 float uv_top = surface_data->get_uv_top() + (uv_height * surfacepartrequest->source.y) / surface->get_height();
220 float uv_right = surface_data->get_uv_left() + (uv_width * (surfacepartrequest->source.x + surfacepartrequest->size.x)) / surface->get_width();
221 float uv_bottom = surface_data->get_uv_top() + (uv_height * (surfacepartrequest->source.y + surfacepartrequest->size.y)) / surface->get_height();
223 glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
224 intern_draw(request.pos.x, request.pos.y,
225 request.pos.x + surfacepartrequest->size.x,
226 request.pos.y + surfacepartrequest->size.y,
233 Color(1.0, 1.0, 1.0),
235 request.drawing_effect);
239 Renderer::draw_gradient(const DrawingRequest& request)
241 const GradientRequest* gradientrequest
242 = (GradientRequest*) request.request_data;
243 const Color& top = gradientrequest->top;
244 const Color& bottom = gradientrequest->bottom;
246 glDisable(GL_TEXTURE_2D);
248 glColor4f(top.red, top.green, top.blue, top.alpha);
250 glVertex2f(SCREEN_WIDTH, 0);
251 glColor4f(bottom.red, bottom.green, bottom.blue, bottom.alpha);
252 glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
253 glVertex2f(0, SCREEN_HEIGHT);
255 glEnable(GL_TEXTURE_2D);
256 glColor4f(1, 1, 1, 1);
260 Renderer::draw_filled_rect(const DrawingRequest& request)
262 const FillRectRequest* fillrectrequest
263 = (FillRectRequest*) request.request_data;
265 if (fillrectrequest->radius != 0.0f)
268 // Keep radius in the limits, so that we get a circle instead of
270 float radius = std::min(fillrectrequest->radius,
271 std::min(fillrectrequest->size.x/2,
272 fillrectrequest->size.y/2));
275 Rect irect(request.pos.x + radius,
276 request.pos.y + radius,
277 request.pos.x + fillrectrequest->size.x - radius,
278 request.pos.y + fillrectrequest->size.y - radius);
280 glDisable(GL_TEXTURE_2D);
281 glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
282 fillrectrequest->color.blue, fillrectrequest->color.alpha);
286 glBegin(GL_QUAD_STRIP);
287 for(int i = 0; i <= n; ++i)
289 float x = sinf(i * (M_PI/2) / n) * radius;
290 float y = cosf(i * (M_PI/2) / n) * radius;
292 glVertex2f(irect.get_left() - x, irect.get_top() - y);
293 glVertex2f(irect.get_right() + x, irect.get_top() - y);
295 for(int i = 0; i <= n; ++i)
297 float x = cosf(i * (M_PI/2) / n) * radius;
298 float y = sinf(i * (M_PI/2) / n) * radius;
300 glVertex2f(irect.get_left() - x, irect.get_bottom() + y);
301 glVertex2f(irect.get_right() + x, irect.get_bottom() + y);
304 glEnable(GL_TEXTURE_2D);
305 glColor4f(1, 1, 1, 1);
309 float x = request.pos.x;
310 float y = request.pos.y;
311 float w = fillrectrequest->size.x;
312 float h = fillrectrequest->size.y;
314 glDisable(GL_TEXTURE_2D);
315 glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
316 fillrectrequest->color.blue, fillrectrequest->color.alpha);
321 glVertex2f(x+w, y+h);
324 glEnable(GL_TEXTURE_2D);
325 glColor4f(1, 1, 1, 1);
330 Renderer::draw_inverse_ellipse(const DrawingRequest& request)
332 const InverseEllipseRequest* ellipse = (InverseEllipseRequest*)request.request_data;
334 glDisable(GL_TEXTURE_2D);
335 glColor4f(ellipse->color.red, ellipse->color.green,
336 ellipse->color.blue, ellipse->color.alpha);
338 float x = request.pos.x;
339 float y = request.pos.y;
340 float w = ellipse->size.x/2.0f;
341 float h = ellipse->size.y/2.0f;
343 glBegin(GL_TRIANGLES);
346 glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
347 glVertex2f(0, SCREEN_HEIGHT);
351 glVertex2f(SCREEN_WIDTH, 0);
356 glVertex2f(SCREEN_WIDTH, 0);
357 glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
362 glVertex2f(0, SCREEN_HEIGHT);
368 for(int i = 0; i < slices; ++i)
370 float ex1 = sinf(M_PI/2 / slices * i) * w;
371 float ey1 = cosf(M_PI/2 / slices * i) * h;
373 float ex2 = sinf(M_PI/2 / slices * (i+1)) * w;
374 float ey2 = cosf(M_PI/2 / slices * (i+1)) * h;
376 glBegin(GL_TRIANGLES);
379 glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
380 glVertex2f(x + ex1, y + ey1);
381 glVertex2f(x + ex2, y + ey2);
385 glVertex2f(x - ex1, y - ey1);
386 glVertex2f(x - ex2, y - ey2);
389 glVertex2f(SCREEN_WIDTH, 0);
390 glVertex2f(x + ex1, y - ey1);
391 glVertex2f(x + ex2, y - ey2);
394 glVertex2f(0, SCREEN_HEIGHT);
395 glVertex2f(x - ex1, y + ey1);
396 glVertex2f(x - ex2, y + ey2);
400 glEnable(GL_TEXTURE_2D);
401 glColor4f(1, 1, 1, 1);
405 Renderer::do_take_screenshot()
407 // [Christoph] TODO: Yes, this method also takes care of the actual disk I/O. Split it?
409 SDL_Surface *shot_surf;
410 // create surface to hold screenshot
411 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
412 shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x00FF0000, 0x0000FF00, 0x000000FF, 0);
414 shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0);
417 log_warning << "Could not create RGB Surface to contain screenshot" << std::endl;
421 // read pixels into array
422 char* pixels = new char[3 * SCREEN_WIDTH * SCREEN_HEIGHT];
424 log_warning << "Could not allocate memory to store screenshot" << std::endl;
425 SDL_FreeSurface(shot_surf);
428 glReadPixels(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, pixels);
430 // copy array line-by-line
431 for (int i = 0; i < SCREEN_HEIGHT; i++) {
432 char* src = pixels + (3 * SCREEN_WIDTH * (SCREEN_HEIGHT - i - 1));
433 if(SDL_MUSTLOCK(shot_surf))
435 SDL_LockSurface(shot_surf);
437 char* dst = ((char*)shot_surf->pixels) + i * shot_surf->pitch;
438 memcpy(dst, src, 3 * SCREEN_WIDTH);
439 if(SDL_MUSTLOCK(shot_surf))
441 SDL_UnlockSurface(shot_surf);
449 static const std::string writeDir = PHYSFS_getWriteDir();
450 static const std::string dirSep = PHYSFS_getDirSeparator();
451 static const std::string baseName = "screenshot";
452 static const std::string fileExt = ".bmp";
453 std::string fullFilename;
454 for (int num = 0; num < 1000; num++) {
455 std::ostringstream oss;
457 oss << std::setw(3) << std::setfill('0') << num;
459 std::string fileName = oss.str();
460 fullFilename = writeDir + dirSep + fileName;
461 if (!PHYSFS_exists(fileName.c_str())) {
462 SDL_SaveBMP(shot_surf, fullFilename.c_str());
463 log_debug << "Wrote screenshot to \"" << fullFilename << "\"" << std::endl;
464 SDL_FreeSurface(shot_surf);
468 log_warning << "Did not save screenshot, because all files up to \"" << fullFilename << "\" already existed" << std::endl;
469 SDL_FreeSurface(shot_surf);
475 assert_gl("drawing");
476 SDL_GL_SwapBuffers();