Play-/Source/ScreenShotUtils.cpp

56 lines
1.7 KiB
C++
Raw Normal View History

2017-04-09 06:48:27 +01:00
#include "AppConfig.h"
#include "bitmap/BMP.h"
#include "ScreenShotUtils.h"
#include "StdStream.h"
#include "PathUtils.h"
2017-05-17 09:29:44 -04:00
#include "string_format.h"
2017-04-09 06:48:27 +01:00
#include <chrono>
#include <ctime>
#include <iomanip>
#include <sstream>
void CScreenShotUtils::TriggerGetScreenshot(CPS2VM* virtualMachine, Callback completionCallback)
{
virtualMachine->m_ee->m_gs->OnFlipComplete.connect_extended(
[=](const boost::signals2::connection &c)->void
{
c.disconnect();
try
{
2017-05-17 09:29:44 -04:00
auto buffer = virtualMachine->m_ee->m_gs->GetScreenshot();
2017-04-09 06:48:27 +01:00
auto name = GenerateScreenShotPath(virtualMachine->m_ee->m_os->GetExecutableName());
Framework::CStdStream outputStream(name.string().c_str(), "wb");
2017-05-17 09:29:44 -04:00
Framework::CBMP::WriteBitmap(buffer, outputStream);
2017-04-09 06:48:27 +01:00
2017-05-17 09:29:44 -04:00
auto msgstr = string_format("Screenshot saved as '%s'.", name.filename().string().c_str());
2017-04-09 06:48:27 +01:00
completionCallback(0, msgstr.c_str());
}
2017-05-17 09:29:44 -04:00
catch(const std::exception&)
2017-04-09 06:48:27 +01:00
{
completionCallback(-1, "Error occured while trying to save screenshot.");
}
}
);
}
Framework::CConfig::PathType CScreenShotUtils::GetScreenShotDirectoryPath()
{
auto screenshotpath(CAppConfig::GetBasePath() / boost::filesystem::path("screenshots"));
Framework::PathUtils::EnsurePathExists(screenshotpath);
return screenshotpath;
}
Framework::CConfig::PathType CScreenShotUtils::GenerateScreenShotPath(const char* gameID)
{
auto t = std::time(nullptr);
auto tm = *std::localtime(&t);
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now().time_since_epoch()).count() % 1000;
std::ostringstream oss;
oss << gameID << std::put_time(&tm, "_%d-%m-%Y_%H.%M.%S.") << ms << ".bmp";
auto screenshotFileName = oss.str();
return GetScreenShotDirectoryPath() / boost::filesystem::path(screenshotFileName);
}