82 lines
No EOL
2.7 KiB
C++
82 lines
No EOL
2.7 KiB
C++
/*
|
|
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 <typename T>
|
|
struct TextureAsset {
|
|
std::uint32_t sizeX;
|
|
std::uint32_t sizeY;
|
|
std::uint32_t sizeZ;
|
|
std::vector<T> pixels;
|
|
|
|
void Save(fs::path path) {
|
|
std::ofstream file(path, std::ios::binary);
|
|
|
|
file.write(reinterpret_cast<char*>(&sizeX), sizeof(sizeX));
|
|
file.write(reinterpret_cast<char*>(&sizeY), sizeof(sizeY));
|
|
file.write(reinterpret_cast<char*>(&sizeZ), sizeof(sizeZ));
|
|
|
|
file.write(reinterpret_cast<char*>(pixels.data()), pixels.size() * sizeof(T));
|
|
}
|
|
|
|
static TextureAsset<T> Load(fs::path path) {
|
|
TextureAsset<T> tex;
|
|
|
|
std::ifstream file(path, std::ios::binary);
|
|
|
|
file.read(reinterpret_cast<char*>(&tex.sizeX), sizeof(tex.sizeX));
|
|
file.read(reinterpret_cast<char*>(&tex.sizeY), sizeof(tex.sizeY));
|
|
file.read(reinterpret_cast<char*>(&tex.sizeZ), sizeof(tex.sizeZ));
|
|
|
|
tex.pixels.resize(tex.sizeX * tex.sizeY * tex.sizeZ);
|
|
|
|
file.read(reinterpret_cast<char*>(tex.pixels.data()), tex.sizeX * tex.sizeY * tex.sizeZ * sizeof(T));
|
|
return tex;
|
|
}
|
|
|
|
static TextureAsset<T> LoadPNG(fs::path path) requires (std::same_as<T, Vector<std::uint8_t, 4, 0>>) {
|
|
TextureAsset<T> 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;
|
|
}
|
|
};
|
|
} |