Restore screenshot key functionality

c++11
scrawl 9 years ago
parent 2a85a22dba
commit e642f20a65

@ -7,6 +7,7 @@
#include <osgViewer/ViewerEventHandlers>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <SDL.h>
@ -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)

@ -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<std::string> mArchives;
boost::filesystem::path mResDir;
osg::ref_ptr<osgViewer::Viewer> mViewer;
osg::ref_ptr<osgViewer::ScreenCaptureHandler> mScreenCaptureHandler;
std::string mCellName;
std::vector<std::string> mContentFiles;
bool mVerboseScripts;

@ -41,6 +41,8 @@ InputWrapper::InputWrapper(SDL_Window* window, osg::ref_ptr<osgViewer::Viewer> v
void InputWrapper::capture(bool windowEventsOnly)
{
mViewer->getEventQueue()->frame(0.f);
SDL_PumpEvents();
SDL_Event evt;

Loading…
Cancel
Save