X-Git-Url: https://git.ucc.asn.au/?p=ipdf%2Fcode.git;a=blobdiff_plain;f=src%2Fscreen.cpp;h=e97995c5af67c79a0da4b2b4b00cb72e811eb7c5;hp=6293fab19b9934597bda23680b4b977ff1a85949;hb=4e0ddbcec6e182430a31e0f1c661f2c93c9e0308;hpb=d1f4d742e6634fda7c8a1e018b4f9c8ec0f4e51c diff --git a/src/screen.cpp b/src/screen.cpp index 6293fab..e97995c 100644 --- a/src/screen.cpp +++ b/src/screen.cpp @@ -122,47 +122,23 @@ void Screen::Present() void Screen::ScreenShot(const char * filename) const { Debug("Attempting to save BMP to file %s", filename); - SDL_Surface * info = SDL_GetWindowSurface(m_window); - if (info == NULL) - { - Fatal("Failed to create info surface from m_window - %s", SDL_GetError()); - } - - unsigned num_pix = info->w * info->h * info->format->BytesPerPixel; - unsigned char * pixels = new unsigned char[num_pix]; + + int w = ViewportWidth(); + int h = ViewportHeight(); + unsigned char * pixels = new unsigned char[w*h*4]; if (pixels == NULL) - { - Fatal("Failed to allocate %u pixel array - %s", num_pix, strerror(errno)); - } + Fatal("Failed to allocate %d x %d x 4 = %d pixel array", w, h, w*h*4); - SDL_Renderer * renderer = SDL_GetRenderer(m_window); - if (renderer == NULL) - { - Fatal("Couldn't get renderer from m_window - %s", SDL_GetError()); - } - if (SDL_RenderReadPixels(renderer, &(info->clip_rect), info->format->format, pixels, info->w * info->format->BytesPerPixel) != 0) - { - Fatal("SDL_RenderReadPixels failed - %s", SDL_GetError()); - } + glReadPixels(0,0,w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels); - // This line is disgusting - SDL_Surface * save = SDL_CreateRGBSurfaceFrom(pixels, info->w, info->h, info->format->BitsPerPixel, info->w * info->format->BytesPerPixel, - info->format->Rmask, info->format->Gmask, info->format->Bmask, info->format->Amask); - if (save == NULL) - { - Fatal("Couldn't create SDL_Surface from renderer pixel data - %s", SDL_GetError()); - } - if (SDL_SaveBMP(save, filename) != 0) - { - Fatal("SDL_SaveBMP to %s failed - %s", filename, SDL_GetError()); - } + SDL_Surface * surf = SDL_CreateRGBSurfaceFrom(pixels, w, h, 8*4, w*4, 0,0,0,0); + if (surf == NULL) + Fatal("Failed to create SDL_Surface from pixel data - %s", SDL_GetError()); - //SDL_DestroyRenderer(renderer); - SDL_FreeSurface(save); - SDL_FreeSurface(info); + if (SDL_SaveBMP(surf, filename) != 0) + Fatal("SDL_SaveBMP failed - %s", SDL_GetError()); + + SDL_FreeSurface(surf); delete [] pixels; - Debug("Succeeded!"); - - }