SP3-103 SP3-104 Implemented DDS loading and error reporting

Not tested
This commit is contained in:
Xiao Qi 2022-09-22 12:29:30 +08:00
parent 8ea845e9f7
commit 8cfe58a3ad
6 changed files with 89 additions and 4 deletions

View File

@ -0,0 +1,11 @@
#pragma once
#include "tinyddsloader.h"
namespace SHADE
{
struct SHDDSAsset
{
tinyddsloader::DDSFile image;
};
}

View File

@ -0,0 +1,50 @@
#include "SHpch.h"
#include "SHDDSLoader.h"
namespace SHADE
{
std::string SHDDSLoader::TinyDDSResultToString(tinyddsloader::Result value)
{
switch (value)
{
case tinyddsloader::Result::ErrorFileOpen:
return "File open err";
case tinyddsloader::Result::ErrorRead:
return "File read err";
case tinyddsloader::Result::ErrorMagicWord:
return "File header magicword err";
case tinyddsloader::Result::ErrorSize:
return "File size err";
case tinyddsloader::Result::ErrorVerify:
return "Pixel format err";
case tinyddsloader::Result::ErrorNotSupported:
return "Unsupported format";
case tinyddsloader::Result::ErrorInvalidData:
return "Invalid data";
default:
return "Unknown";
}
}
void SHDDSLoader::LoadImageAsset(std::vector<AssetPath> const& paths, std::vector<SHDDSAsset>& images)
{
std::vector<SHDDSAsset> result;
tinyddsloader::Result loadResult = tinyddsloader::Result::Success;
AssetPath lastPath;
for (auto const& path : paths)
{
if (loadResult == tinyddsloader::Result::Success)
{
result.emplace_back();
}
else
{
SHLOG_ERROR("Unable to load DDS file: {} at {}", TinyDDSResultToString(loadResult), lastPath.string());
}
loadResult = result.back().image.Load(path.string().c_str());
lastPath = path;
}
std::swap(images, result);
}
}

View File

@ -0,0 +1,18 @@
#pragma once
#define TINYDDSLOADER_IMPLEMENTATION
#include "../SHAssetMacros.h"
#include "../Asset Types/SHDDSAsset.h"
#include "tinyddsloader.h"
#include <vector>
namespace SHADE
{
class SHDDSLoader
{
private:
static std::string TinyDDSResultToString(tinyddsloader::Result value);
public:
static void LoadImageAsset(std::vector<AssetPath> const& paths, std::vector<SHDDSAsset>& images);
};
}

View File

@ -1,7 +1,7 @@
#pragma once
#include "../SHAssetMacros.h"
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include "../SHAssetMacros.h"
#include "../Asset Types/SHMeshAsset.h"
#include <vector>

View File

@ -15,6 +15,7 @@
#include "Filesystem/SHFileSystem.h"
#include "Libraries/SHMeshLoader.h"
#include "Libraries/SHDDSLoader.h"
namespace SHADE
{
@ -25,6 +26,7 @@ namespace SHADE
std::unordered_map<AssetID, SHAsset> SHAssetManager::assetRegistry;
std::unordered_map<AssetID, SHMeshAsset> SHAssetManager::meshCollection;
std::unordered_map<AssetID, SHDDSAsset> SHAssetManager::ddsCollection;
/****************************************************************************
* \brief Static function to generate resource ID.
@ -80,7 +82,7 @@ namespace SHADE
****************************************************************************/
std::vector<SHAsset> const& SHAssetManager::GetAllAssets() noexcept
{
assetCollection;
return assetCollection;
}
/****************************************************************************
@ -237,11 +239,13 @@ namespace SHADE
{
std::vector<SHMeshAsset> meshes;
std::vector<AssetPath> imagePaths;
std::vector<SHDDSAsset> images;
SHMeshLoader::LoadMesh(meshes, imagePaths, asset.path);
SHDDSLoader::LoadImageAsset(imagePaths, images);
//TODO Recognise new meshes as asset as well and write mesh into binary
//TODO
}
/****************************************************************************

View File

@ -18,6 +18,7 @@
#include "SHAsset.h"
#include "Asset Types/SHMeshAsset.h"
#include "Asset Types/SHDDSAsset.h"
struct _MonoMethod;
@ -127,6 +128,7 @@ namespace SHADE
static std::unordered_map<AssetID, SHAsset> assetRegistry;
static std::unordered_map<AssetID, SHMeshAsset> meshCollection;
static std::unordered_map<AssetID, SHDDSAsset> ddsCollection;
};
}