Play-/Source/ScreenShotUtils.cpp

53 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>
CScreenShotUtils::Connection CScreenShotUtils::TriggerGetScreenshot(CPS2VM* virtualMachine, Callback completionCallback)
2017-04-09 06:48:27 +01:00
{
return virtualMachine->m_ee->m_gs->OnFlipComplete.ConnectOnce(
2019-06-20 00:40:28 +01:00
[=]() -> void {
2018-04-30 21:01:23 +01:00
try
{
auto buffer = virtualMachine->m_ee->m_gs->GetScreenshot();
auto name = GenerateScreenShotPath(virtualMachine->m_ee->m_os->GetExecutableName());
Framework::CStdStream outputStream(name.string().c_str(), "wb");
Framework::CBMP::WriteBitmap(buffer, outputStream);
2017-04-09 06:48:27 +01:00
2018-04-30 21:01:23 +01:00
auto msgstr = string_format("Screenshot saved as '%s'.", name.filename().string().c_str());
completionCallback(0, msgstr.c_str());
}
catch(const std::exception&)
{
completionCallback(-1, "Error occured while trying to save screenshot.");
}
});
2017-04-09 06:48:27 +01:00
}
2019-10-16 20:51:11 -04:00
fs::path CScreenShotUtils::GetScreenShotDirectoryPath()
2017-04-09 06:48:27 +01:00
{
auto screenshotpath(CAppConfig::GetInstance().GetBasePath() / fs::path("screenshots"));
2017-04-09 06:48:27 +01:00
Framework::PathUtils::EnsurePathExists(screenshotpath);
return screenshotpath;
}
2019-10-16 20:51:11 -04:00
fs::path CScreenShotUtils::GenerateScreenShotPath(const char* gameID)
2017-04-09 06:48:27 +01:00
{
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();
2019-10-16 20:51:11 -04:00
return GetScreenShotDirectoryPath() / fs::path(screenshotFileName);
2017-04-09 06:48:27 +01:00
}