#include "core/rendering/screenshot.hpp" #include #include #include #include #include #include #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 copy(buffer, buffer + (static_cast(width) * height)); std::string filename; const std::string FILEPATH = generateFilePath(filename); return writePng(copy.data(), width, height, FILEPATH) ? filename : ""; } } // namespace Screenshot