screenshot.cpp ara només inclou les declaracions de stb_image_write.h; la macro STB_IMAGE_WRITE_IMPLEMENTATION viu a un .cpp nou dins de source/external/, que ja està exclòs del pre-commit hook (clang-format i clang-tidy) i cobert pel seu .clang-tidy amb Checks: '-*'. Així podem retirar la desactivació global de clang-analyzer-optin.portability .UnixAPI i el check queda actiu arreu del nostre propi codi.
61 lines
2.3 KiB
C++
61 lines
2.3 KiB
C++
#include "core/rendering/screenshot.hpp"
|
|
|
|
#include <chrono>
|
|
#include <cstdint>
|
|
#include <ctime>
|
|
#include <filesystem>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include "external/stb_image_write.h"
|
|
|
|
namespace Screenshot {
|
|
|
|
namespace {
|
|
|
|
// Crea la carpeta `screenshots/` si no existeix i torna la ruta
|
|
// completa al fitxer. El paràmetre `filename` rep el nom nu (sense
|
|
// directori) per poder mostrar-lo a la notificació.
|
|
auto generateFilePath(std::string& filename) -> std::string {
|
|
std::filesystem::create_directories("screenshots");
|
|
|
|
const auto NOW = std::chrono::system_clock::now();
|
|
const std::time_t TIME = std::chrono::system_clock::to_time_t(NOW);
|
|
const std::tm* tm = std::localtime(&TIME);
|
|
|
|
char timestamp[20];
|
|
std::strftime(timestamp, sizeof(timestamp), "%Y%m%d_%H%M%S", tm);
|
|
|
|
filename = std::string("scr_") + timestamp + ".png";
|
|
return "screenshots/" + filename;
|
|
}
|
|
|
|
// Converteix ARGB8888 → RGBA8888 in-place i escriu el PNG. stb_image_write
|
|
// espera RGBA en ordre byte (little-endian: R al byte baix, A al byte alt).
|
|
auto writePng(std::uint32_t* buffer, int width, int height, const std::string& filepath) -> bool {
|
|
const int TOTAL = width * height;
|
|
for (int i = 0; i < TOTAL; ++i) {
|
|
const std::uint32_t P = buffer[i];
|
|
const std::uint32_t A = (P >> 24) & 0xFF;
|
|
const std::uint32_t R = (P >> 16) & 0xFF;
|
|
const std::uint32_t G = (P >> 8) & 0xFF;
|
|
const std::uint32_t B = P & 0xFF;
|
|
buffer[i] = R | (G << 8) | (B << 16) | (A << 24);
|
|
}
|
|
return stbi_write_png(filepath.c_str(), width, height, 4, buffer, width * 4) != 0;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
auto save(const std::uint32_t* buffer, int width, int height) -> std::string {
|
|
// Còpia local per no tocar el canvas original durant la conversió.
|
|
std::vector<std::uint32_t> copy(buffer, buffer + (static_cast<size_t>(width) * height));
|
|
|
|
std::string filename;
|
|
const std::string FILEPATH = generateFilePath(filename);
|
|
|
|
return writePng(copy.data(), width, height, FILEPATH) ? filename : "";
|
|
}
|
|
|
|
} // namespace Screenshot
|