133 lines
3.2 KiB
C++
133 lines
3.2 KiB
C++
|
|
#include <asset_packer/asset.hpp>
|
|
|
|
#include <asset/asset.hpp>
|
|
#include <asset/raylib_asset.hpp>
|
|
|
|
#include <raylib.h>
|
|
|
|
#include <string>
|
|
#include <iostream>
|
|
#include <filesystem>
|
|
#include <variant>
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
AssetLoader::AssetLoader(std::string asset_data) : asset_reader(asset_data){
|
|
if (!asset_reader.load_index()){
|
|
std::cerr << "Failed to load assets!!!" << std::endl;
|
|
}
|
|
}
|
|
|
|
AssetReader AssetLoader::get_reader(){
|
|
return asset_reader;
|
|
}
|
|
|
|
RaylibAssetLoader::RaylibAssetLoader(std::string asset_data) : AssetLoader(asset_data) {
|
|
|
|
}
|
|
|
|
RaylibAssetLoader::~RaylibAssetLoader(){
|
|
unload_all_assets();
|
|
}
|
|
|
|
|
|
void RaylibAssetLoader::load_asset(std::string path){
|
|
fs::path file_path(path);
|
|
Asset new_asset;
|
|
new_asset.name = file_path.filename().string() ;
|
|
|
|
std::cout << file_path.extension() << std::endl;
|
|
|
|
if (file_path.extension() == ".png"){
|
|
auto texture_data = get_reader().extract_asset(path);
|
|
Image img = LoadImageFromMemory(".png", texture_data.data(), texture_data.size());
|
|
auto texture = LoadTextureFromImage(img);
|
|
UnloadImage(img);
|
|
new_asset.data = ImageData { new Texture(texture)};
|
|
|
|
|
|
}else if(file_path.extension() == ".ogg"){
|
|
|
|
}else if(file_path.extension() == ".wav"){
|
|
|
|
}else if(file_path.extension() == ".json"){
|
|
|
|
}
|
|
|
|
|
|
assets[path] = new_asset;
|
|
|
|
}
|
|
|
|
void RaylibAssetLoader::unload_asset(std::string path){
|
|
Asset asset = assets[path];
|
|
|
|
std::visit([](auto&& arg) {
|
|
using T = std::decay_t<decltype(arg)>;
|
|
|
|
if constexpr (std::is_same_v<T, ImageData>) {
|
|
|
|
UnloadTexture(*((Texture*) arg.texture));
|
|
delete (Texture*) arg.texture;
|
|
}
|
|
else if constexpr (std::is_same_v<T, SoundData>) {
|
|
UnloadSound(*((Sound*) arg.sound));
|
|
delete (Sound*) arg.sound;
|
|
}
|
|
else if constexpr (std::is_same_v<T, MusicData>) {
|
|
UnloadMusicStream(*((Music*) arg.music));
|
|
delete (Music*) arg.music;
|
|
}
|
|
// JsonData requires no manual step because std::string cleans itself up!
|
|
}, asset.data);
|
|
|
|
}
|
|
|
|
|
|
void RaylibAssetLoader::unload_all_assets(){
|
|
for (auto [path,_] : assets){
|
|
unload_asset(path);
|
|
}
|
|
}
|
|
|
|
Asset RaylibAssetLoader::get_asset(std::string path){
|
|
if (assets.contains(path)){
|
|
return assets[path];
|
|
}else{
|
|
return {};
|
|
}
|
|
}
|
|
|
|
|
|
Texture* RaylibAssetLoader::get_texture(std::string path){
|
|
Asset asset = get_asset(path);
|
|
if (auto* ptr = std::get_if<ImageData>(&asset.data)) {
|
|
return (Texture*)ptr->texture;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
Sound* RaylibAssetLoader::get_sound(std::string path){
|
|
Asset asset = get_asset(path);
|
|
if (auto* ptr = std::get_if<SoundData>(&asset.data)) {
|
|
return (Sound*)ptr->sound;
|
|
}
|
|
return nullptr;
|
|
}
|
|
Music* RaylibAssetLoader::get_music(std::string path){
|
|
Asset asset = get_asset(path);
|
|
if (auto* ptr = std::get_if<MusicData>(&asset.data)) {
|
|
return (Music*)ptr->music;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
std::string RaylibAssetLoader::get_json(std::string path){
|
|
Asset asset = get_asset(path);
|
|
if (auto* ptr = std::get_if<JsonData>(&asset.data)) {
|
|
return ptr->json;
|
|
}
|
|
return "{}";
|
|
}
|