diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2005-09-24 22:34:06 +0000 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2005-09-24 22:34:06 +0000 |
commit | 1a7f56901301fb8fe08f26526f3314b0974626ca (patch) | |
tree | f8c2189c5b502b5b3f610d60310db78a2043f4cd /src/openglgraphics.cpp | |
parent | 0fc148affc222dd1108b6f1fc8ad2757f1fdc4f1 (diff) | |
download | mana-1a7f56901301fb8fe08f26526f3314b0974626ca.tar.gz mana-1a7f56901301fb8fe08f26526f3314b0974626ca.tar.bz2 mana-1a7f56901301fb8fe08f26526f3314b0974626ca.tar.xz mana-1a7f56901301fb8fe08f26526f3314b0974626ca.zip |
Fix crash on exit after taking screenshot in OpenGL and fix compiler warning.
Diffstat (limited to 'src/openglgraphics.cpp')
-rw-r--r-- | src/openglgraphics.cpp | 42 |
1 files changed, 30 insertions, 12 deletions
diff --git a/src/openglgraphics.cpp b/src/openglgraphics.cpp index 27eefc6e..d7915256 100644 --- a/src/openglgraphics.cpp +++ b/src/openglgraphics.cpp @@ -148,23 +148,41 @@ SDL_Surface* OpenGLGraphics::getScreenshot() { int h = mScreen->h; int w = mScreen->w; - SDL_Surface *surface = SDL_CreateRGBSurface(SDL_SWSURFACE, mScreen->w, - mScreen->h, 24, 0xff0000, 0x00ff00, 0x0000ff, NULL); - glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, surface->pixels); - void *data = malloc(w * h * 3); + SDL_Surface *screenshot = SDL_CreateRGBSurface( + SDL_SWSURFACE, + w, h, 24, + 0xff0000, 0x00ff00, 0x0000ff, 0x000000); - for (int i = 0; i < h; i++) + if (SDL_MUSTLOCK(screenshot)) { + SDL_LockSurface(screenshot); + } + + // Grap the pixel buffer and write it to the SDL surface + glPixelStorei(GL_PACK_ALIGNMENT, 1); + glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, screenshot->pixels); + + // Flip the screenshot, as OpenGL has 0,0 in bottom left + unsigned int lineSize = 3 * w; + GLubyte* buf = (GLubyte*)malloc(lineSize); + + for (int i = 0; i < (h / 2); i++) { - memcpy((GLubyte*)data + 3 * w * i, - (GLubyte*)surface->pixels + 3 * w * (h - i), - 3 * w); + GLubyte *top = (GLubyte*)screenshot->pixels + lineSize * i; + GLubyte *bot = (GLubyte*)screenshot->pixels + lineSize * (h - 1 - i); + + memcpy(buf, top, lineSize); + memcpy(top, bot, lineSize); + memcpy(bot, buf, lineSize); + } + + free(buf); + + if (SDL_MUSTLOCK(screenshot)) { + SDL_UnlockSurface(screenshot); } - memcpy(surface->pixels, data, w * h * 3); - free(data); - - return surface; + return screenshot; } bool OpenGLGraphics::pushClipArea(gcn::Rectangle area) |