diff --git a/src/common/Platform.cpp b/src/common/Platform.cpp index 9b8127c7e..6860d9ec7 100644 --- a/src/common/Platform.cpp +++ b/src/common/Platform.cpp @@ -373,7 +373,7 @@ String DoMigration(ByteString fromDir, ByteString toDir) auto scripts = DirectorySearch(fromDir + "scripts", "", { ".lua", ".txt" }); auto downloadedScripts = DirectorySearch(fromDir + "scripts/downloaded", "", { ".lua" }); bool hasScriptinfo = FileExists(toDir + "scripts/downloaded/scriptinfo"); - auto screenshots = DirectorySearch(fromDir, "powdertoy-", { ".png" }); + auto screenshots = DirectorySearch(fromDir, "screenshot", { ".png" }); bool hasAutorun = FileExists(fromDir + "autorun.lua"); bool hasPref = FileExists(fromDir + "powder.pref"); diff --git a/src/gui/game/GameController.cpp b/src/gui/game/GameController.cpp index e6bbbb9e9..73d26f7a6 100644 --- a/src/gui/game/GameController.cpp +++ b/src/gui/game/GameController.cpp @@ -1543,6 +1543,11 @@ String GameController::WallName(int type) return String(); } +ByteString GameController::TakeScreenshot(int captureUI, int fileType) +{ + return gameView->TakeScreenshot(captureUI, fileType); +} + int GameController::Record(bool record) { return gameView->Record(record); diff --git a/src/gui/game/GameController.h b/src/gui/game/GameController.h index e510f8797..d9ff1a2ad 100644 --- a/src/gui/game/GameController.h +++ b/src/gui/game/GameController.h @@ -160,6 +160,7 @@ public: String BasicParticleInfo(Particle const &sample_part); bool IsValidElement(int type); String WallName(int type); + ByteString TakeScreenshot(int captureUI, int fileType); int Record(bool record); void ResetAir(); diff --git a/src/gui/game/GameView.cpp b/src/gui/game/GameView.cpp index 716332edd..9f707f33e 100644 --- a/src/gui/game/GameView.cpp +++ b/src/gui/game/GameView.cpp @@ -193,7 +193,9 @@ GameView::GameView(): introTextMessage(ByteString(introTextData).FromUtf8()), doScreenshot(false), - screenshotIndex(0), + screenshotIndex(1), + lastScreenshotTime(0), + recordingIndex(0), recording(false), recordingFolder(0), currentPoint(ui::Point(0, 0)), @@ -889,9 +891,53 @@ void GameView::NotifyBrushChanged(GameModel * sender) activeBrush = sender->GetBrush(); } -void GameView::screenshot() +ByteString GameView::TakeScreenshot(int captureUI, int fileType) { - doScreenshot = true; + VideoBuffer *screenshot; + std::vector data; + time_t screenshotTime = time(nullptr); + std::string extension; + + if (captureUI) + screenshot = new VideoBuffer(ren->DumpFrame()); + else + screenshot = new VideoBuffer(ui::Engine::Ref().g->DumpFrame()); + + if (fileType == 1) + { + data = format::VideoBufferToBMP(screenshot); + extension = ".bmp"; + } + else if (fileType == 2) + { + data = format::VideoBufferToPPM(screenshot); + extension = ".ppm"; + } + else + { + data = format::VideoBufferToPNG(screenshot); + extension = ".png"; + } + + // Optional suffix to distinguish screenshots taken at the exact same time + ByteString suffix = ""; + if (screenshotTime == lastScreenshotTime) + { + screenshotIndex++; + suffix = ByteString::Build(" (", screenshotIndex, ")"); + } + else + { + screenshotIndex = 1; + } + std::string date = format::UnixtimeToDate(screenshotTime, "%Y-%m-%d %H.%M.%S"); + ByteString filename = ByteString::Build("screenshot ", date, suffix, extension); + + Client::Ref().WriteFile(data, filename); + doScreenshot = false; + lastScreenshotTime = screenshotTime; + + return filename; } int GameView::Record(bool record) @@ -912,6 +958,7 @@ int GameView::Record(bool record) Platform::MakeDirectory("recordings"); Platform::MakeDirectory(ByteString::Build("recordings", PATH_SEP, recordingFolder).c_str()); recording = true; + recordingIndex = 0; } } return recordingFolder; @@ -1328,7 +1375,7 @@ void GameView::OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, c->SetActiveTool(0, "DEFAULT_UI_PROPERTY"); } else - screenshot(); + doScreenshot = true; break; case SDL_SCANCODE_F3: SetDebugHUD(!GetDebugHUD()); @@ -2088,15 +2135,9 @@ void GameView::OnDraw() ren->RenderEnd(); - if(doScreenshot) + if (doScreenshot) { - VideoBuffer screenshot(ren->DumpFrame()); - std::vector data = format::VideoBufferToPNG(screenshot); - - ByteString filename = ByteString::Build("screenshot_", Format::Width(screenshotIndex++, 6), ".png"); - - Client::Ref().WriteFile(data, filename); - doScreenshot = false; + TakeScreenshot(0, 0); } if(recording) @@ -2104,7 +2145,7 @@ void GameView::OnDraw() VideoBuffer screenshot(ren->DumpFrame()); std::vector data = format::VideoBufferToPPM(screenshot); - ByteString filename = ByteString::Build("recordings", PATH_SEP, recordingFolder, PATH_SEP, "frame_", Format::Width(screenshotIndex++, 6), ".ppm"); + ByteString filename = ByteString::Build("recordings", PATH_SEP, recordingFolder, PATH_SEP, "frame_", Format::Width(recordingIndex++, 6), ".ppm"); Client::Ref().WriteFile(data, filename); } diff --git a/src/gui/game/GameView.h b/src/gui/game/GameView.h index c82215d21..8d4092fb2 100644 --- a/src/gui/game/GameView.h +++ b/src/gui/game/GameView.h @@ -1,6 +1,7 @@ #ifndef GAMEVIEW_H #define GAMEVIEW_H +#include #include #include #include "common/String.h" @@ -71,6 +72,8 @@ private: bool doScreenshot; int screenshotIndex; + time_t lastScreenshotTime; + int recordingIndex; bool recording; int recordingFolder; @@ -124,8 +127,6 @@ private: void SetSaveButtonTooltips(); - void screenshot(); - void enableShiftBehaviour(); void disableShiftBehaviour(); void enableCtrlBehaviour(); @@ -157,6 +158,7 @@ public: void BeginStampSelection(); ui::Point GetPlaceSaveOffset() { return placeSaveOffset; } void SetPlaceSaveOffset(ui::Point offset) { placeSaveOffset = offset; } + ByteString TakeScreenshot(int captureUI, int fileType); int Record(bool record); //all of these are only here for one debug lines diff --git a/src/lua/LegacyLuaAPI.cpp b/src/lua/LegacyLuaAPI.cpp index 1554fa170..fea17b895 100644 --- a/src/lua/LegacyLuaAPI.cpp +++ b/src/lua/LegacyLuaAPI.cpp @@ -1414,38 +1414,8 @@ int luatpt_screenshot(lua_State* l) { int captureUI = luaL_optint(l, 1, 0); int fileType = luaL_optint(l, 2, 0); - std::vector data; - if(captureUI) - { - VideoBuffer screenshot(ui::Engine::Ref().g->DumpFrame()); - if(fileType == 1) { - data = format::VideoBufferToBMP(screenshot); - } else if(fileType == 2) { - data = format::VideoBufferToPPM(screenshot); - } else { - data = format::VideoBufferToPNG(screenshot); - } - } - else - { - VideoBuffer screenshot(luacon_ren->DumpFrame()); - if(fileType == 1) { - data = format::VideoBufferToBMP(screenshot); - } else if(fileType == 2) { - data = format::VideoBufferToPPM(screenshot); - } else { - data = format::VideoBufferToPNG(screenshot); - } - } - ByteString filename = ByteString::Build("screenshot_", Format::Width(screenshotIndex++, 6)); - if(fileType == 1) { - filename += ".bmp"; - } else if(fileType == 2) { - filename += ".ppm"; - } else { - filename += ".png"; - } - Client::Ref().WriteFile(data, filename); + + ByteString filename = luacon_controller->TakeScreenshot(captureUI, fileType); lua_pushstring(l, filename.c_str()); return 1; }