summaryrefslogtreecommitdiff
path: root/src/openglgraphics.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/openglgraphics.cpp')
-rw-r--r--src/openglgraphics.cpp42
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)