/* Crafter®.Asset Copyright (C) 2026 Catcrafts® catcrafts.net This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 3.0 as published by the Free Software Foundation; This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ module; #define STB_IMAGE_IMPLEMENTATION #include "../lib/stb_image.h" export module Crafter.Asset:Texture; import std; import Crafter.Math; namespace fs = std::filesystem; export namespace Crafter { template struct TextureAsset { std::uint32_t sizeX; std::uint32_t sizeY; std::uint32_t sizeZ; std::vector pixels; void Save(fs::path path) { std::ofstream file(path, std::ios::binary); file.write(reinterpret_cast(&sizeX), sizeof(sizeX)); file.write(reinterpret_cast(&sizeY), sizeof(sizeY)); file.write(reinterpret_cast(&sizeZ), sizeof(sizeZ)); file.write(reinterpret_cast(pixels.data()), pixels.size() * sizeof(T)); } static TextureAsset Load(fs::path path) { TextureAsset tex; std::ifstream file(path, std::ios::binary); file.read(reinterpret_cast(&tex.sizeX), sizeof(tex.sizeX)); file.read(reinterpret_cast(&tex.sizeY), sizeof(tex.sizeY)); file.read(reinterpret_cast(&tex.sizeZ), sizeof(tex.sizeZ)); tex.pixels.resize(tex.sizeX * tex.sizeY * tex.sizeZ); file.read(reinterpret_cast(tex.pixels.data()), tex.sizeX * tex.sizeY * tex.sizeZ * sizeof(T)); return tex; } static TextureAsset LoadPNG(fs::path path) requires (std::same_as>) { TextureAsset tex; std::filesystem::path abs = std::filesystem::absolute(path); int sizeX; int sizeY; unsigned char* data = stbi_load(abs.string().c_str(), &sizeX, &sizeY, nullptr, 4); tex.sizeX = sizeX; tex.sizeY = sizeY; tex.sizeZ = 1; tex.pixels.resize(tex.sizeX*tex.sizeY); std::memcpy(tex.pixels.data(), data, tex.sizeX * tex.sizeY * 4); return tex; } }; }