From f22625d0e79cd943acf117c1e5bd32e4bbef5a2a Mon Sep 17 00:00:00 2001 From: Björn Steinbrink Date: Sun, 18 Sep 2005 11:22:27 +0000 Subject: Move the writing logic out of the Graphics class once again... Some cleanups in the screenshot filename selection code. --- src/game.cpp | 38 ++++++++++++++++++++++++++++++++++++-- src/graphics.cpp | 45 +-------------------------------------------- src/graphics.h | 7 ------- 3 files changed, 37 insertions(+), 53 deletions(-) (limited to 'src') diff --git a/src/game.cpp b/src/game.cpp index 5506947d..98dbb318 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -23,7 +23,10 @@ #include "game.h" +#include +#include #include +#include #include @@ -69,6 +72,8 @@ #include "net/network.h" #include "net/protocol.h" +#include "resources/imagewriter.h" + extern Graphics *graphics; std::string map_path; @@ -326,6 +331,30 @@ void do_init() } } +bool saveScreenshot(SDL_Surface *screenshot) +{ + static unsigned int screenshotCount = 0; + + // Search for an unused screenshot name + std::stringstream filename; + std::fstream testExists; + bool found = false; + + do { + screenshotCount++; + filename.str(""); + #ifdef __USE_UNIX98 + filename << PHYSFS_getUserDir() << "/"; + #endif + filename << "TMW_Screenshot_" << screenshotCount << ".png"; + testExists.open(filename.str().c_str(), std::ios::in); + found = !testExists.is_open(); + testExists.close(); + } while (!found); + + return ImageWriter::writePNG(screenshot, filename.str()); +} + void game() { // Needs to be initialised _before_ the engine is created... @@ -578,11 +607,16 @@ void do_input() */ // screenshot (picture, hence the p) case SDLK_p: - if (!graphics->saveScreenshot()) { - logger->log("Error: could not save Screenshot."); + SDL_Surface *screenshot = graphics->getScreenshot(); + if (!saveScreenshot(screenshot)) + { + logger->log("Error: could not save Screenshot."); + } + SDL_FreeSurface(screenshot); } break; + // Skill window case SDLK_k: skillDialog->setVisible(!skillDialog->isVisible()); diff --git a/src/graphics.cpp b/src/graphics.cpp index f179eff2..33f25468 100644 --- a/src/graphics.cpp +++ b/src/graphics.cpp @@ -28,17 +28,11 @@ #include "graphic/imagerect.h" #include "resources/image.h" -#include -#include -#include -#include -#include "resources/imagewriter.h" extern volatile int framesToDraw; Graphics::Graphics(): - mScreen(0), - screenshotsCount(1) + mScreen(0) { } @@ -246,43 +240,6 @@ void Graphics::updateScreen() } } -bool Graphics::saveScreenshot() -{ - std::stringstream pictureFilename; - // Write it under user home dir on *nices. - #ifdef __USE_UNIX98 - pictureFilename << PHYSFS_getUserDir() << "/"; - #endif - pictureFilename << "TMW_Screenshot_" << screenshotsCount << ".png"; - - // While these screenshots files already exists, increment the - // screenshots count. - std::fstream testExists; - testExists.open(std::string(pictureFilename.str()).c_str(), std::ios::in); - while ( testExists.is_open() ) - { - testExists.close(); - screenshotsCount++; - pictureFilename.str(""); - #ifdef __USE_UNIX98 - pictureFilename << PHYSFS_getUserDir() << "/"; - #endif - pictureFilename << "TMW_Screenshot_" << screenshotsCount << ".png"; - testExists.open(std::string(pictureFilename.str()).c_str(), std::ios::in); - } - testExists.close(); - - if ( ImageWriter::writePNG(getScreenshot(), pictureFilename.str()) ) - { - screenshotsCount++; - return true; - } - else - { - return false; - } -} - SDL_Surface* Graphics::getScreenshot() { #if SDL_BYTEORDER == SDL_BIG_ENDIAN diff --git a/src/graphics.h b/src/graphics.h index 18a3be51..a0749795 100644 --- a/src/graphics.h +++ b/src/graphics.h @@ -119,11 +119,6 @@ class Graphics : public gcn::SDLGraphics { */ int getHeight(); - /** - * takes a screenshot, and saves it as a png - */ - bool saveScreenshot(); - /** * takes a screenshot and returns it as SDL surface */ @@ -132,8 +127,6 @@ class Graphics : public gcn::SDLGraphics { protected: SDL_Surface *mScreen; bool mFullscreen, mHWAccel; - private: - int screenshotsCount; }; #endif -- cgit v1.2.3-60-g2f50