diff options
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/game.cpp | 11 | ||||
-rw-r--r-- | src/graphics.cpp | 104 | ||||
-rw-r--r-- | src/graphics.h | 4 | ||||
-rw-r--r-- | src/openglgraphics.cpp | 55 | ||||
-rw-r--r-- | src/openglgraphics.h | 5 |
7 files changed, 91 insertions, 94 deletions
@@ -1,5 +1,9 @@ 2005-09-18 Björn Steinbrink <B.Steinbrink@gmx.de> + * src/Makefile.am, src/game.cpp, src/graphics.cpp, src/graphics.h, + src/openglgraphics.cpp, src/openglgraphics.h: Changed saveScreenshot + function in Graphics to getScreenshot. We now use the ImageWriter to + save that screenshot. * src/resources/imagewriter.cpp, src/resources/imagewriter.h: Added ImageWriter class that provides a function to save a SDL surface as png. diff --git a/src/Makefile.am b/src/Makefile.am index 5e866f71..ca96c88d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -119,6 +119,8 @@ tmw_SOURCES = graphic/spriteset.cpp \ net/win2linux.h \ resources/image.cpp \ resources/image.h \ + resources/imagewriter.cpp \ + resources/imagewriter.h \ resources/iteminfo.cpp \ resources/iteminfo.h \ resources/itemmanager.cpp \ diff --git a/src/game.cpp b/src/game.cpp index 7ef4967e..4778dae7 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -23,6 +23,8 @@ #include "game.h" +#include <sstream> + #include <guichan/sdl/sdlinput.hpp> #include "being.h" @@ -67,6 +69,7 @@ #include "net/network.h" #include "net/protocol.h" +#include "resources/imagewriter.h" extern Graphics *graphics; @@ -577,10 +580,12 @@ void do_input() */ // screenshot (picture, hence the p) case SDLK_p: - static int picCount = 1; - if (!graphics->saveScreenshot("Screenshot%d.png", picCount)) { - logger->log("Error: could not save Screenshot%d.png", picCount); + // TODO Fix the counting to start at a sane value. + static int picCount = 1; + std::stringstream name; + name << "Screenshot-" << picCount << ".png"; + ImageWriter::writePNG(graphics->getScreenshot(), name.str()); picCount++; } break; diff --git a/src/graphics.cpp b/src/graphics.cpp index f12d3898..33f25468 100644 --- a/src/graphics.cpp +++ b/src/graphics.cpp @@ -23,9 +23,6 @@ #include "graphics.h" -#include <cstdarg> -#include <png.h> - #include "log.h" #include "graphic/imagerect.h" @@ -243,100 +240,29 @@ void Graphics::updateScreen() } } -bool Graphics::saveScreenshot(char *filename, ...) +SDL_Surface* Graphics::getScreenshot() { - va_list ap; - char *newname = (char *)malloc(32); - va_start(ap, filename); - vsprintf(newname, filename, ap); - va_end(ap); - - FILE *fp = fopen(newname, "wb"); - if (!fp) - { - logger->log("could not open file &s for writing", newname); - return false; - } - - png_structp png_ptr; - png_infop info_ptr; - png_bytep *row_pointers; - int colortype; - - #if SDL_BYTEORDER == SDL_BIG_ENDIAN +#if SDL_BYTEORDER == SDL_BIG_ENDIAN int rmask = 0xff000000; int gmask = 0x00ff0000; int bmask = 0x0000ff00; - int amask = 0x000000ff; - #else +#else int rmask = 0x000000ff; int gmask = 0x0000ff00; int bmask = 0x00ff0000; - int amask = 0xff000000; - #endif - - SDL_Surface *surface = SDL_CreateRGBSurface(SDL_SWSURFACE, mScreen->w, - mScreen->h, 32, rmask, gmask, bmask, amask); - //SDL_LockSurface(mScreen); - SDL_BlitSurface(mScreen, NULL, surface, NULL); - //SDL_UnlockSurface(mScreen); - - SDL_LockSurface(surface); - - png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0); - if (!png_ptr) - { - logger->log("Had trouble creating png_structp"); - return false; - } - - info_ptr = png_create_info_struct(png_ptr); - if (!info_ptr) - { - logger->log("Could not create png_info"); - return false; +#endif + int amask = 0x00000000; + + SDL_Surface *screenshot = SDL_CreateRGBSurface(SDL_SWSURFACE, mScreen->w, + mScreen->h, 24, rmask, gmask, bmask, amask); + + if (SDL_MUSTLOCK(mScreen)) { + SDL_LockSurface(mScreen); } - - - if (setjmp(png_ptr->jmpbuf)) - { - logger->log("problem writing to %s", newname); - return false; - } - - png_init_io(png_ptr, fp); - - if (mScreen->format->BitsPerPixel == 24) colortype = PNG_COLOR_TYPE_RGB; - else colortype = PNG_COLOR_TYPE_RGB_ALPHA; - - png_set_IHDR(png_ptr, info_ptr, surface->w, surface->h, 8,colortype, - PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); - - png_write_info(png_ptr, info_ptr); - - png_set_packing(png_ptr); - - row_pointers = (png_bytep*) malloc(sizeof(png_bytep)*surface->h); - if (!row_pointers) - { - logger->log("Had trouble converting surface to row pointers"); - return false; + SDL_BlitSurface(mScreen, NULL, screenshot, NULL); + if (SDL_MUSTLOCK(mScreen)) { + SDL_UnlockSurface(mScreen); } - - for (int i = 0; i < surface->h; i++) - row_pointers[i] = (png_bytep)(Uint8 *)surface->pixels + i * surface->pitch; - - png_write_image(png_ptr, row_pointers); - png_write_end(png_ptr, info_ptr); - fclose(fp); - if (row_pointers) free(row_pointers); - - if (info_ptr->palette) free(info_ptr->palette); - - png_destroy_write_struct(&png_ptr, (png_infopp)NULL); - - SDL_UnlockSurface(surface); - SDL_FreeSurface(surface); - return true; + return screenshot; } diff --git a/src/graphics.h b/src/graphics.h index b45bc15c..a0749795 100644 --- a/src/graphics.h +++ b/src/graphics.h @@ -120,9 +120,9 @@ class Graphics : public gcn::SDLGraphics { int getHeight(); /** - * takes a screenshot, and saves it as a png + * takes a screenshot and returns it as SDL surface */ - bool saveScreenshot(char *filename, ...); + virtual SDL_Surface* getScreenshot(); protected: SDL_Surface *mScreen; diff --git a/src/openglgraphics.cpp b/src/openglgraphics.cpp index 86bb734a..b9591591 100644 --- a/src/openglgraphics.cpp +++ b/src/openglgraphics.cpp @@ -25,6 +25,7 @@ #include "openglgraphics.h" +#include <iostream> #include <SDL.h> #include <guichan/image.hpp> @@ -134,6 +135,60 @@ void OpenGLGraphics::_endDraw() { } +SDL_Surface* OpenGLGraphics::getScreenshot() +{ + // TODO I expect this to be unneeded for OpenGL, someone with a PPC or + // sth. else that is big endian should check this. +#if SDL_BYTEORDER == SDL_BIG_ENDIAN + int rmask = 0xff000000; + int gmask = 0x00ff0000; + int bmask = 0x0000ff00; +#else + int rmask = 0x000000ff; + int gmask = 0x0000ff00; + int bmask = 0x00ff0000; +#endif + int amask = 0x00000000; + + SDL_Surface *screenshot = SDL_CreateRGBSurface(SDL_SWSURFACE, mScreen->w, + mScreen->h, 24, rmask, gmask, bmask, amask); + + if (SDL_MUSTLOCK(screenshot)) { + SDL_LockSurface(screenshot); + } + glPixelStorei(GL_PACK_ALIGNMENT, 1); + glReadPixels(0, 0, mScreen->w, mScreen->h, GL_RGB, GL_UNSIGNED_BYTE, screenshot->pixels); + + unsigned char *data = (unsigned char*)screenshot->pixels; + for (int x = 0; x < mScreen->w; x++) { + for (int y=0; y < mScreen->h / 2; y++) { + int i1 = (y * mScreen->w + x) * 3; + int i2 = ((mScreen->h - y - 1) * mScreen->w + x) * 3; + + unsigned char temp = data[i1]; + data[i1] = data[i2]; + data[i2] = temp; + + i1++; i2++; + temp = data[i1]; + data[i1] = data[i2]; + data[i2] = temp; + + i1++; i2++; + temp = data[i1]; + data[i1] = data[i2]; + data[i2] = temp; + + } + } + + if (SDL_MUSTLOCK(screenshot)) { + SDL_UnlockSurface(screenshot); + } + + return screenshot; +} + bool OpenGLGraphics::pushClipArea(gcn::Rectangle area) { int transX = 0; diff --git a/src/openglgraphics.h b/src/openglgraphics.h index b082d0d3..cd99e6a6 100644 --- a/src/openglgraphics.h +++ b/src/openglgraphics.h @@ -62,6 +62,11 @@ class OpenGLGraphics : public Graphics void setTargetPlane(int width, int height); + /** + * takes a screenshot and returns it as SDL surface + */ + SDL_Surface* getScreenshot(); + protected: void setTexturingAndBlending(bool enable); void drawTexedQuad(int x, int y, int w, int h, |