From e642f20a65972f7e6bb9900816f42e54a41390e3 Mon Sep 17 00:00:00 2001 From: scrawl Date: Wed, 3 Jun 2015 17:25:18 +0200 Subject: [PATCH] Restore screenshot key functionality --- apps/openmw/engine.cpp | 73 +++++++++++++++++++------- apps/openmw/engine.hpp | 6 +++ components/sdlutil/sdlinputwrapper.cpp | 2 + 3 files changed, 63 insertions(+), 18 deletions(-) diff --git a/apps/openmw/engine.cpp b/apps/openmw/engine.cpp index fed1f134b..a37d84621 100644 --- a/apps/openmw/engine.cpp +++ b/apps/openmw/engine.cpp @@ -7,6 +7,7 @@ #include #include +#include #include @@ -532,6 +533,54 @@ void OMW::Engine::prepareEngine (Settings::Manager & settings) } } +class WriteScreenshotToFileOperation : public osgViewer::ScreenCaptureHandler::CaptureOperation +{ +public: + WriteScreenshotToFileOperation(const std::string& screenshotPath, const std::string& screenshotFormat) + : mScreenshotPath(screenshotPath) + , mScreenshotFormat(screenshotFormat) + { + } + + virtual void operator()(const osg::Image& image, const unsigned int context_id) + { + // Count screenshots. + int shotCount = 0; + + // Find the first unused filename with a do-while + std::ostringstream stream; + do + { + // Reset the stream + stream.str(""); + stream.clear(); + + stream << mScreenshotPath << "/screenshot" << std::setw(3) << std::setfill('0') << shotCount++ << "." << mScreenshotFormat; + + } while (boost::filesystem::exists(stream.str())); + + boost::filesystem::ofstream outStream; + outStream.open(boost::filesystem::path(stream.str()), std::ios::binary); + + osgDB::ReaderWriter* readerwriter = osgDB::Registry::instance()->getReaderWriterForExtension(mScreenshotFormat); + if (!readerwriter) + { + std::cerr << "Can't write screenshot, no '" << mScreenshotFormat << "' readerwriter found" << std::endl; + return; + } + + osgDB::ReaderWriter::WriteResult result = readerwriter->writeImage(image, outStream); + if (!result.success()) + { + std::cerr << "Can't write screenshot: " << result.message() << std::endl; + } + } + +private: + std::string mScreenshotPath; + std::string mScreenshotFormat; +}; + // Initialise and enter main loop. void OMW::Engine::go() @@ -557,6 +606,10 @@ void OMW::Engine::go() settingspath = loadSettings (settings); + mScreenCaptureHandler = new osgViewer::ScreenCaptureHandler(new WriteScreenshotToFileOperation(mCfgMgr.getUserDataPath().string(), + Settings::Manager::getString("screenshot format", "General"))); + mViewer->addEventHandler(mScreenCaptureHandler); + // Create encoder ToUTF8::Utf8Encoder encoder (mEncoding); mEncoder = &encoder; @@ -639,24 +692,8 @@ void OMW::Engine::activate() void OMW::Engine::screenshot() { - // Count screenshots. - int shotCount = 0; - - const std::string& screenshotPath = mCfgMgr.getUserDataPath().string(); - std::string format = Settings::Manager::getString("screenshot format", "General"); - // Find the first unused filename with a do-while - std::ostringstream stream; - do - { - // Reset the stream - stream.str(""); - stream.clear(); - - stream << screenshotPath << "screenshot" << std::setw(3) << std::setfill('0') << shotCount++ << "." << format; - - } while (boost::filesystem::exists(stream.str())); - - //mOgre->screenshot(stream.str(), format); + mScreenCaptureHandler->setFramesToCapture(1); + mScreenCaptureHandler->captureNextFrame(*mViewer); } void OMW::Engine::setCompileAll (bool all) diff --git a/apps/openmw/engine.hpp b/apps/openmw/engine.hpp index 8b792c5a8..8ac6098b8 100644 --- a/apps/openmw/engine.hpp +++ b/apps/openmw/engine.hpp @@ -53,6 +53,11 @@ namespace Files struct ConfigurationManager; } +namespace osgViewer +{ + class ScreenCaptureHandler; +} + struct SDL_Window; namespace OMW @@ -70,6 +75,7 @@ namespace OMW std::vector mArchives; boost::filesystem::path mResDir; osg::ref_ptr mViewer; + osg::ref_ptr mScreenCaptureHandler; std::string mCellName; std::vector mContentFiles; bool mVerboseScripts; diff --git a/components/sdlutil/sdlinputwrapper.cpp b/components/sdlutil/sdlinputwrapper.cpp index a9209bf58..f607b7046 100644 --- a/components/sdlutil/sdlinputwrapper.cpp +++ b/components/sdlutil/sdlinputwrapper.cpp @@ -41,6 +41,8 @@ InputWrapper::InputWrapper(SDL_Window* window, osg::ref_ptr v void InputWrapper::capture(bool windowEventsOnly) { + mViewer->getEventQueue()->frame(0.f); + SDL_PumpEvents(); SDL_Event evt;