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 "drawing_context.hpp"
37 #include "drawing_request.hpp"
38 #include "surface.hpp"
41 #include "gameconfig.hpp"
42 #include "texture.hpp"
43 #include "texture_manager.hpp"
44 #include "obstack/obstackpp.hpp"
45 #define LIGHTMAP_DIV 5
49 inline void intern_draw(float left, float top, float right, float bottom,
50 float uv_left, float uv_top,
51 float uv_right, float uv_bottom,
52 float angle, float alpha,
57 if(effect & HORIZONTAL_FLIP)
58 std::swap(uv_left, uv_right);
59 if(effect & VERTICAL_FLIP) {
60 std::swap(uv_top, uv_bottom);
63 float center_x = (left + right) / 2;
64 float center_y = (top + bottom) / 2;
66 float sa = sinf(angle/180.0f*M_PI);
67 float ca = cosf(angle/180.0f*M_PI);
75 glBlendFunc(blend.sfactor, blend.dfactor);
76 glColor4f(color.red, color.green, color.blue, color.alpha * alpha);
78 glTexCoord2f(uv_left, uv_top);
79 glVertex2f(left*ca - top*sa + center_x,
80 left*sa + top*ca + center_y);
82 glTexCoord2f(uv_right, uv_top);
83 glVertex2f(right*ca - top*sa + center_x,
84 right*sa + top*ca + center_y);
86 glTexCoord2f(uv_right, uv_bottom);
87 glVertex2f(right*ca - bottom*sa + center_x,
88 right*sa + bottom*ca + center_y);
90 glTexCoord2f(uv_left, uv_bottom);
91 glVertex2f(left*ca - bottom*sa + center_x,
92 left*sa + bottom*ca + center_y);
95 // FIXME: find a better way to restore the blend mode
96 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
97 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
105 if(texture_manager != 0)
106 texture_manager->save_textures();
108 if(config->try_vsync) {
109 /* we want vsync for smooth scrolling */
110 SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
113 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
114 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
115 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
116 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
118 int flags = SDL_OPENGL;
119 if(config->use_fullscreen)
120 flags |= SDL_FULLSCREEN;
121 int width = config->screenwidth;
122 int height = config->screenheight;
125 SDL_Surface *screen = SDL_SetVideoMode(width, height, bpp, flags);
127 std::stringstream msg;
128 msg << "Couldn't set video mode (" << width << "x" << height
129 << "-" << bpp << "bpp): " << SDL_GetError();
130 throw std::runtime_error(msg.str());
133 // setup opengl state and transform
134 glDisable(GL_DEPTH_TEST);
135 glDisable(GL_CULL_FACE);
136 glEnable(GL_TEXTURE_2D);
138 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
140 glViewport(0, 0, screen->w, screen->h);
141 glMatrixMode(GL_PROJECTION);
143 // logical resolution here not real monitor resolution
144 glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
145 glMatrixMode(GL_MODELVIEW);
147 glTranslatef(0, 0, 0);
149 check_gl_error("Setting up view matrices");
152 if(texture_manager == 0)
153 texture_manager = new TextureManager();
155 texture_manager->reload_textures();
158 Renderer::~Renderer()
163 Renderer::draw_surface(const DrawingRequest& request)
165 const Surface* surface = (const Surface*) request.request_data;
166 GL::Texture *gltexture = dynamic_cast<GL::Texture *>(surface->get_texture());
167 glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
168 intern_draw(request.pos.x, request.pos.y,
169 request.pos.x + surface->get_width(),
170 request.pos.y + surface->get_height(),
171 surface->get_uv_left(),
172 surface->get_uv_top(),
173 surface->get_uv_right(),
174 surface->get_uv_bottom(),
179 request.drawing_effect);
183 Renderer::draw_surface_part(const DrawingRequest& request)
185 const SurfacePartRequest* surfacepartrequest
186 = (SurfacePartRequest*) request.request_data;
187 const Surface *surface = surfacepartrequest->surface;
188 GL::Texture *gltexture = dynamic_cast<GL::Texture *>(surface->get_texture());
190 float uv_width = surface->get_uv_right() - surface->get_uv_left();
191 float uv_height = surface->get_uv_bottom() - surface->get_uv_top();
193 float uv_left = surface->get_uv_left() + (uv_width * surfacepartrequest->source.x) / surface->get_width();
194 float uv_top = surface->get_uv_top() + (uv_height * surfacepartrequest->source.y) / surface->get_height();
195 float uv_right = surface->get_uv_left() + (uv_width * (surfacepartrequest->source.x + surfacepartrequest->size.x)) / surface->get_width();
196 float uv_bottom = surface->get_uv_top() + (uv_height * (surfacepartrequest->source.y + surfacepartrequest->size.y)) / surface->get_height();
198 glBindTexture(GL_TEXTURE_2D, gltexture->get_handle());
199 intern_draw(request.pos.x, request.pos.y,
200 request.pos.x + surfacepartrequest->size.x,
201 request.pos.y + surfacepartrequest->size.y,
208 Color(1.0, 1.0, 1.0),
210 request.drawing_effect);
214 Renderer::draw_gradient(const DrawingRequest& request)
216 const GradientRequest* gradientrequest
217 = (GradientRequest*) request.request_data;
218 const Color& top = gradientrequest->top;
219 const Color& bottom = gradientrequest->bottom;
221 glDisable(GL_TEXTURE_2D);
223 glColor4f(top.red, top.green, top.blue, top.alpha);
225 glVertex2f(SCREEN_WIDTH, 0);
226 glColor4f(bottom.red, bottom.green, bottom.blue, bottom.alpha);
227 glVertex2f(SCREEN_WIDTH, SCREEN_HEIGHT);
228 glVertex2f(0, SCREEN_HEIGHT);
230 glEnable(GL_TEXTURE_2D);
231 glColor4f(1, 1, 1, 1);
235 Renderer::draw_text(const DrawingRequest& request)
237 const TextRequest* textrequest = (TextRequest*) request.request_data;
239 textrequest->font->draw(this, textrequest->text, request.pos,
240 textrequest->alignment, request.drawing_effect, request.alpha);
244 Renderer::draw_filled_rect(const DrawingRequest& request)
246 const FillRectRequest* fillrectrequest
247 = (FillRectRequest*) request.request_data;
249 float x = request.pos.x;
250 float y = request.pos.y;
251 float w = fillrectrequest->size.x;
252 float h = fillrectrequest->size.y;
254 glDisable(GL_TEXTURE_2D);
255 glColor4f(fillrectrequest->color.red, fillrectrequest->color.green,
256 fillrectrequest->color.blue, fillrectrequest->color.alpha);
261 glVertex2f(x+w, y+h);
264 glEnable(GL_TEXTURE_2D);
265 glColor4f(1, 1, 1, 1);
269 Renderer::do_take_screenshot()
271 // [Christoph] TODO: Yes, this method also takes care of the actual disk I/O. Split it?
273 SDL_Surface *shot_surf;
274 // create surface to hold screenshot
275 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
276 shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x00FF0000, 0x0000FF00, 0x000000FF, 0);
278 shot_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SCREEN_WIDTH, SCREEN_HEIGHT, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0);
281 log_warning << "Could not create RGB Surface to contain screenshot" << std::endl;
285 // read pixels into array
286 char* pixels = new char[3 * SCREEN_WIDTH * SCREEN_HEIGHT];
288 log_warning << "Could not allocate memory to store screenshot" << std::endl;
289 SDL_FreeSurface(shot_surf);
292 glReadPixels(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, pixels);
294 // copy array line-by-line
295 for (int i = 0; i < SCREEN_HEIGHT; i++) {
296 char* src = pixels + (3 * SCREEN_WIDTH * (SCREEN_HEIGHT - i - 1));
297 char* dst = ((char*)shot_surf->pixels) + i * shot_surf->pitch;
298 memcpy(dst, src, 3 * SCREEN_WIDTH);
305 static const std::string writeDir = PHYSFS_getWriteDir();
306 static const std::string dirSep = PHYSFS_getDirSeparator();
307 static const std::string baseName = "screenshot";
308 static const std::string fileExt = ".bmp";
309 std::string fullFilename;
310 for (int num = 0; num < 1000; num++) {
311 std::ostringstream oss;
313 oss << std::setw(3) << std::setfill('0') << num;
315 std::string fileName = oss.str();
316 fullFilename = writeDir + dirSep + fileName;
317 if (!PHYSFS_exists(fileName.c_str())) {
318 SDL_SaveBMP(shot_surf, fullFilename.c_str());
319 log_debug << "Wrote screenshot to \"" << fullFilename << "\"" << std::endl;
320 SDL_FreeSurface(shot_surf);
324 log_warning << "Did not save screenshot, because all files up to \"" << fullFilename << "\" already existed" << std::endl;
325 SDL_FreeSurface(shot_surf);
331 assert_gl("drawing");
332 SDL_GL_SwapBuffers();