Add support to save screenshots to file

This commit is contained in:
WarmUpTill 2022-09-03 11:21:43 +02:00 committed by WarmUpTill
parent 0169f6c445
commit 0d29fb2ee1
2 changed files with 34 additions and 3 deletions

View File

@ -3,8 +3,11 @@
static void ScreenshotTick(void *param, float);
ScreenshotHelper::ScreenshotHelper(obs_source_t *source)
: weakSource(OBSGetWeakRef(source))
ScreenshotHelper::ScreenshotHelper(obs_source_t *source, bool saveToFile,
std::string path)
: weakSource(OBSGetWeakRef(source)),
_saveToFile(saveToFile),
_path(path)
{
_initDone = true;
obs_add_tick_callback(ScreenshotTick, this);
@ -20,6 +23,9 @@ ScreenshotHelper::~ScreenshotHelper()
obs_remove_tick_callback(ScreenshotTick, this);
}
if (_saveThread.joinable()) {
_saveThread.join();
}
}
void ScreenshotHelper::Screenshot()
@ -100,6 +106,24 @@ void ScreenshotHelper::MarkDone()
done = true;
}
void ScreenshotHelper::WriteToFile()
{
if (!_saveToFile) {
return;
}
_saveThread = std::thread([this]() {
if (image.save(QString::fromStdString(_path))) {
vblog(LOG_INFO, "Wrote screenshot to \"%s\"",
_path.c_str());
} else {
blog(LOG_WARNING,
"Failed to save screenshot to to \"%s\"!\nMaybe unkown format?",
_path.c_str());
}
});
}
#define STAGE_SCREENSHOT 0
#define STAGE_DOWNLOAD 1
#define STAGE_COPY_AND_SAVE 2
@ -125,6 +149,7 @@ static void ScreenshotTick(void *param, float)
case STAGE_COPY_AND_SAVE:
data->Copy();
data->MarkDone();
data->WriteToFile();
obs_remove_tick_callback(ScreenshotTick, data);
break;

View File

@ -3,11 +3,13 @@
#include <string>
#include <QImage>
#include <chrono>
#include <thread>
class ScreenshotHelper {
public:
ScreenshotHelper() = default;
ScreenshotHelper(obs_source_t *source);
ScreenshotHelper(obs_source_t *source, bool saveToFile = false,
std::string path = "");
ScreenshotHelper &operator=(const ScreenshotHelper &) = delete;
ScreenshotHelper(const ScreenshotHelper &) = delete;
~ScreenshotHelper();
@ -16,6 +18,7 @@ public:
void Download();
void Copy();
void MarkDone();
void WriteToFile();
gs_texrender_t *texrender = nullptr;
gs_stagesurf_t *stagesurf = nullptr;
@ -31,4 +34,7 @@ public:
private:
bool _initDone = false;
std::thread _saveThread;
bool _saveToFile = false;
std::string _path = "";
};