From 9290acd379db6168df2c8020045171acc4a50ad2 Mon Sep 17 00:00:00 2001 From: fgenesis Date: Fri, 29 Apr 2022 10:26:25 +0200 Subject: [PATCH] fix taking screenshots --- BBGE/Core.cpp | 29 +++++++++++++++++++---------- BBGE/Core.h | 4 ++-- ExternalLibs/tinylibs.cpp | 1 + 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/BBGE/Core.cpp b/BBGE/Core.cpp index c8fef20..225a5c1 100644 --- a/BBGE/Core.cpp +++ b/BBGE/Core.cpp @@ -2210,10 +2210,11 @@ bool Core::canChangeState() // Take a screenshot of the specified region of the screen and store it // in a 32bpp pixel buffer. delete[] the returned buffer when it's no // longer needed. -unsigned char *Core::grabScreenshot(int x, int y, int w, int h) +unsigned char *Core::grabScreenshot(size_t x, size_t y, size_t w, size_t h) { - unsigned int size = sizeof(unsigned char) * w * h * 4; - unsigned char *imageData = new unsigned char[size]; + const size_t N = w * h; + const size_t size = sizeof(unsigned char) * N * 4; + unsigned char * const imageData = new unsigned char[size]; glPushAttrib(GL_ALL_ATTRIB_BITS); glDisable(GL_BLEND); @@ -2232,12 +2233,20 @@ unsigned char *Core::grabScreenshot(int x, int y, int w, int h) // Force all alpha values to 255. unsigned char *c = imageData; - for (int x = 0; x < w; x++) + for (size_t i = 0; i < N; ++i, c += 4) { - for (int y = 0; y < h; y++, c += 4) - { - c[3] = 255; - } + c[3] = 255; + } + + // OpenGL outputs the image upside down -> flip pixel rows + const ptrdiff_t rowOffs = 4 * w; + unsigned char * row0 = imageData; + unsigned char * row1 = imageData + size - rowOffs; + while(row0 < row1) + { + std::swap_ranges(row0, row0 + rowOffs, row1); + row0 += rowOffs; + row1 -= rowOffs; } return imageData; @@ -2245,7 +2254,7 @@ unsigned char *Core::grabScreenshot(int x, int y, int w, int h) } // Like grabScreenshot(), but grab from the center of the screen. -unsigned char *Core::grabCenteredScreenshot(int w, int h) +unsigned char *Core::grabCenteredScreenshot(size_t w, size_t h) { return grabScreenshot(core->width/2 - w/2, core->height/2 - h/2, w, h); } @@ -2253,7 +2262,7 @@ unsigned char *Core::grabCenteredScreenshot(int w, int h) // takes a screen shot and saves it to a TGA or PNG image bool Core::saveScreenshot(const std::string &filename, bool png) { - int w = getWindowWidth(), h = getWindowHeight(); + size_t w = getWindowWidth(), h = getWindowHeight(); unsigned char *imageData = grabCenteredScreenshot(w, h); bool ok = png ? pngSaveRGBA(filename.c_str(), w, h, imageData) diff --git a/BBGE/Core.h b/BBGE/Core.h index ee567f7..68add58 100644 --- a/BBGE/Core.h +++ b/BBGE/Core.h @@ -354,8 +354,8 @@ public: return virtualHeight; } - unsigned char *grabScreenshot(int x, int y, int w, int h); - unsigned char *grabCenteredScreenshot(int w, int h); + unsigned char *grabScreenshot(size_t x, size_t y, size_t w, size_t h); + unsigned char *grabCenteredScreenshot(size_t w, size_t h); bool saveScreenshot(const std::string &filename, bool png); bool minimized; diff --git a/ExternalLibs/tinylibs.cpp b/ExternalLibs/tinylibs.cpp index 50eb40c..6eebcb7 100644 --- a/ExternalLibs/tinylibs.cpp +++ b/ExternalLibs/tinylibs.cpp @@ -19,6 +19,7 @@ static unsigned char * miniz_stbi_compress(unsigned char *data, int data_len, in return NULL; } } + *out_len = (int)maxsz; return packed; }