Refactor the way resources are loaded (#191)

* WIP

* WIP

* Font works

* WIP: Only Music remains

* Looks like it's working

* Fix oopsie

* Core/ObjectRef: Add cast functions

* Update ChangeLog.md

* Audio/SoundStream: Make sound stream thread-safe
This commit is contained in:
Jérôme Leclercq
2018-10-28 01:53:11 +02:00
committed by GitHub
parent fa7cbc21e5
commit ed46c87781
64 changed files with 1058 additions and 1071 deletions

View File

@@ -261,21 +261,6 @@ namespace Nz
return m_impl != nullptr;
}
bool Animation::LoadFromFile(const String& filePath, const AnimationParams& params)
{
return AnimationLoader::LoadFromFile(this, filePath, params);
}
bool Animation::LoadFromMemory(const void* data, std::size_t size, const AnimationParams& params)
{
return AnimationLoader::LoadFromMemory(this, data, size, params);
}
bool Animation::LoadFromStream(Stream& stream, const AnimationParams& params)
{
return AnimationLoader::LoadFromStream(this, stream, params);
}
void Animation::RemoveSequence(const String& identifier)
{
NazaraAssert(m_impl, "Animation not created");
@@ -304,6 +289,21 @@ namespace Nz
m_impl->sequences.erase(it);
}
AnimationRef Animation::LoadFromFile(const String& filePath, const AnimationParams& params)
{
return AnimationLoader::LoadFromFile(filePath, params);
}
AnimationRef Animation::LoadFromMemory(const void* data, std::size_t size, const AnimationParams& params)
{
return AnimationLoader::LoadFromMemory(data, size, params);
}
AnimationRef Animation::LoadFromStream(Stream& stream, const AnimationParams& params)
{
return AnimationLoader::LoadFromStream(stream, params);
}
bool Animation::Initialize()
{
if (!AnimationLibrary::Initialize())

View File

@@ -280,21 +280,6 @@ namespace Nz
return true;
}
bool Font::OpenFromFile(const String& filePath, const FontParams& params)
{
return FontLoader::LoadFromFile(this, filePath, params);
}
bool Font::OpenFromMemory(const void* data, std::size_t size, const FontParams& params)
{
return FontLoader::LoadFromMemory(this, data, size, params);
}
bool Font::OpenFromStream(Stream& stream, const FontParams& params)
{
return FontLoader::LoadFromStream(this, stream, params);
}
void Font::SetAtlas(const std::shared_ptr<AbstractAtlas>& atlas)
{
if (m_atlas != atlas)
@@ -358,10 +343,8 @@ namespace Nz
if (!s_defaultFont)
{
FontRef cabin = Font::New();
if (cabin->OpenFromMemory(r_cabinRegular, sizeof(r_cabinRegular)))
s_defaultFont = cabin;
else
s_defaultFont = Font::OpenFromMemory(r_cabinRegular, sizeof(r_cabinRegular));
if (!s_defaultFont)
NazaraError("Failed to open default font");
}
@@ -378,6 +361,21 @@ namespace Nz
return s_defaultMinimumStepSize;
}
FontRef Font::OpenFromFile(const String& filePath, const FontParams& params)
{
return FontLoader::LoadFromFile(filePath, params);
}
FontRef Font::OpenFromMemory(const void* data, std::size_t size, const FontParams& params)
{
return FontLoader::LoadFromMemory(data, size, params);
}
FontRef Font::OpenFromStream(Stream& stream, const FontParams& params)
{
return FontLoader::LoadFromStream(stream, params);
}
void Font::SetDefaultAtlas(const std::shared_ptr<AbstractAtlas>& atlas)
{
s_defaultAtlas = atlas;

View File

@@ -38,7 +38,7 @@ namespace Nz
return (magic == DDS_Magic) ? Ternary_True : Ternary_False;
}
static bool Load(Image* image, Stream& stream, const ImageParams& parameters)
static ImageRef Load(Stream& stream, const ImageParams& parameters)
{
NazaraUnused(parameters);
@@ -81,14 +81,14 @@ namespace Nz
// First, identify the type
ImageType type;
if (!IdentifyImageType(header, headerDX10, &type))
return false;
return nullptr;
// Then the format
PixelFormatType format;
if (!IdentifyPixelFormat(header, headerDX10, &format))
return false;
return nullptr;
image->Create(type, format, width, height, depth, levelCount);
ImageRef image = Image::New(type, format, width, height, depth, levelCount);
// Read all mipmap levels
for (unsigned int i = 0; i < image->GetLevelCount(); i++)
@@ -100,7 +100,7 @@ namespace Nz
if (byteStream.Read(ptr, byteCount) != byteCount)
{
NazaraError("Failed to read level #" + String::Number(i));
return false;
return nullptr;
}
if (width > 1)
@@ -117,7 +117,7 @@ namespace Nz
if (parameters.loadFormat != PixelFormatType_Undefined)
image->Convert(parameters.loadFormat);
return true;
return image;
}
private:

View File

@@ -361,7 +361,7 @@ namespace Nz
return Ternary_False;
}
bool LoadFile(Font* font, const String& filePath, const FontParams& parameters)
FontRef LoadFile(const String& filePath, const FontParams& parameters)
{
NazaraUnused(parameters);
@@ -370,25 +370,26 @@ namespace Nz
if (!face->SetFile(filePath))
{
NazaraError("Failed to open file");
return false;
return nullptr;
}
if (!face->Open())
{
NazaraError("Failed to open face");
return false;
return nullptr;
}
FontRef font = Font::New();
if (font->Create(face.get()))
{
face.release();
return true;
return font;
}
else
return false;
return nullptr;
}
bool LoadMemory(Font* font, const void* data, std::size_t size, const FontParams& parameters)
FontRef LoadMemory(const void* data, std::size_t size, const FontParams& parameters)
{
NazaraUnused(parameters);
@@ -398,19 +399,20 @@ namespace Nz
if (!face->Open())
{
NazaraError("Failed to open face");
return false;
return nullptr;
}
FontRef font = Font::New();
if (font->Create(face.get()))
{
face.release();
return true;
return font;
}
else
return false;
return nullptr;
}
bool LoadStream(Font* font, Stream& stream, const FontParams& parameters)
FontRef LoadStream(Stream& stream, const FontParams& parameters)
{
NazaraUnused(parameters);
@@ -420,16 +422,17 @@ namespace Nz
if (!face->Open())
{
NazaraError("Failed to open face");
return false;
return nullptr;
}
FontRef font = Font::New();
if (font->Create(face.get()))
{
face.release();
return true;
return font;
}
else
return false;
return nullptr;
}
}
@@ -439,7 +442,7 @@ namespace Nz
{
if (FT_Init_FreeType(&s_library) == 0)
{
s_libraryOwner.reset(new FreeTypeLibrary);
s_libraryOwner = std::make_shared<FreeTypeLibrary>();
FontLoader::RegisterLoader(IsSupported, Check, LoadStream, LoadFile, LoadMemory);
}
else

View File

@@ -46,13 +46,13 @@ namespace Nz
return Ternary_False;
}
bool Load(Mesh* mesh, Stream& stream, const MeshParams& parameters)
MeshRef Load(Stream& stream, const MeshParams& parameters)
{
MD2_Header header;
if (stream.Read(&header, sizeof(MD2_Header)) != sizeof(MD2_Header))
{
NazaraError("Failed to read header");
return false;
return nullptr;
}
#ifdef NAZARA_BIG_ENDIAN
@@ -76,14 +76,15 @@ namespace Nz
if (stream.GetSize() < header.offset_end)
{
NazaraError("Incomplete MD2 file");
return false;
return nullptr;
}
// Since the engine no longer supports keyframe animations, let's make a static mesh
MeshRef mesh = Nz::Mesh::New();
if (!mesh->CreateStatic())
{
NazaraInternalError("Failed to create mesh");
return false;
return nullptr;
}
// Extract skins (texture name)
@@ -253,7 +254,7 @@ namespace Nz
if (parameters.center)
mesh->Recenter();
return true;
return mesh;
}
}

View File

@@ -28,7 +28,7 @@ namespace Nz
return parser.Check();
}
bool Load(Animation* animation, Stream& stream, const AnimationParams& /*parameters*/)
AnimationRef Load(Stream& stream, const AnimationParams& /*parameters*/)
{
///TODO: Utiliser les paramètres
MD5AnimParser parser(stream);
@@ -36,7 +36,7 @@ namespace Nz
if (!parser.Parse())
{
NazaraError("MD5Anim parser failed");
return false;
return nullptr;
}
const MD5AnimParser::Frame* frames = parser.GetFrames();
@@ -46,6 +46,7 @@ namespace Nz
UInt32 jointCount = parser.GetJointCount();
// À ce stade, nous sommes censés avoir assez d'informations pour créer l'animation
AnimationRef animation = Animation::New();
animation->CreateSkeletal(frameCount, jointCount);
Sequence sequence;
@@ -84,7 +85,7 @@ namespace Nz
}
}
return true;
return animation;
}
}

View File

@@ -35,13 +35,13 @@ namespace Nz
return parser.Check();
}
bool Load(Mesh* mesh, Stream& stream, const MeshParams& parameters)
MeshRef Load(Stream& stream, const MeshParams& parameters)
{
MD5MeshParser parser(stream);
if (!parser.Parse())
{
NazaraError("MD5Mesh parser failed");
return false;
return nullptr;
}
// Pour que le squelette soit correctement aligné, il faut appliquer un quaternion "de correction" aux joints à la base du squelette
@@ -62,6 +62,7 @@ namespace Nz
if (parameters.animated)
{
MeshRef mesh = Mesh::New();
mesh->CreateSkeletal(jointCount);
Skeleton* skeleton = mesh->GetSkeleton();
@@ -218,13 +219,16 @@ namespace Nz
mesh->SetAnimation(path);
}
}
return mesh;
}
else
{
MeshRef mesh = Mesh::New();
if (!mesh->CreateStatic()) // Ne devrait jamais échouer
{
NazaraInternalError("Failed to create mesh");
return false;
return nullptr;
}
mesh->SetMaterialCount(meshCount);
@@ -309,9 +313,9 @@ namespace Nz
if (parameters.center)
mesh->Recenter();
}
return true;
return mesh;
}
}
}

View File

@@ -153,7 +153,7 @@ namespace Nz
return true;
}
bool Load(Mesh* mesh, Stream& stream, const MeshParams& parameters)
MeshRef Load(Stream& stream, const MeshParams& parameters)
{
long long reservedVertexCount;
if (!parameters.custom.GetIntegerParameter("NativeOBJLoader_VertexCount", &reservedVertexCount))
@@ -163,9 +163,10 @@ namespace Nz
if (!parser.Parse(stream, reservedVertexCount))
{
NazaraError("OBJ parser failed");
return false;
return nullptr;
}
MeshRef mesh = Mesh::New();
mesh->CreateStatic();
const String* materials = parser.GetMaterials();
@@ -341,7 +342,7 @@ namespace Nz
ParseMTL(mesh, stream.GetDirectory() + mtlLib, materials, meshes, meshCount);
}
return true;
return mesh;
}
}

View File

@@ -61,7 +61,7 @@ namespace Nz
return Ternary_False;
}
bool Load(Image* image, Stream& stream, const ImageParams& parameters)
ImageRef Load(Stream& stream, const ImageParams& parameters)
{
NazaraUnused(parameters);
@@ -69,7 +69,7 @@ namespace Nz
if (stream.Read(&header, sizeof(pcx_header)) != sizeof(pcx_header))
{
NazaraError("Failed to read header");
return false;
return nullptr;
}
#ifdef NAZARA_BIG_ENDIAN
@@ -91,10 +91,11 @@ namespace Nz
unsigned int width = header.xmax - header.xmin+1;
unsigned int height = header.ymax - header.ymin+1;
ImageRef image = Image::New();
if (!image->Create(ImageType_2D, PixelFormatType_RGB8, width, height, 1, (parameters.levelCount > 0) ? parameters.levelCount : 1))
{
NazaraError("Failed to create image");
return false;
return nullptr;
}
UInt8* pixels = image->GetPixels();
@@ -119,7 +120,7 @@ namespace Nz
if (!stream.Read(&rle_value, 1))
{
NazaraError("Failed to read stream (byte " + String::Number(stream.GetCursorPos()) + ')');
return false;
return nullptr;
}
if (rle_value < 0xc0)
@@ -130,7 +131,7 @@ namespace Nz
if (!stream.Read(&rle_value, 1))
{
NazaraError("Failed to read stream (byte " + String::Number(stream.GetCursorPos()) + ')');
return false;
return nullptr;
}
}
}
@@ -174,7 +175,7 @@ namespace Nz
if (!stream.Read(&rle_value, 1))
{
NazaraError("Failed to read stream (byte " + String::Number(stream.GetCursorPos()) + ')');
return false;
return nullptr;
}
if (rle_value < 0xc0)
@@ -185,7 +186,7 @@ namespace Nz
if (!stream.Read(&rle_value, 1))
{
NazaraError("Failed to read stream (byte " + String::Number(stream.GetCursorPos()) + ')');
return false;
return nullptr;
}
}
}
@@ -225,21 +226,21 @@ namespace Nz
if (!stream.Read(&magic, 1))
{
NazaraError("Failed to read stream (byte " + String::Number(stream.GetCursorPos()) + ')');
return false;
return nullptr;
}
/* first byte must be equal to 0x0c (12) */
if (magic != 0x0c)
{
NazaraError("Colormap's first byte must be 0x0c (0x" + String::Number(magic, 16) + ')');
return false;
return nullptr;
}
/* read palette */
if (stream.Read(palette, 768) != 768)
{
NazaraError("Failed to read palette");
return false;
return nullptr;
}
stream.SetCursorPos(curPos);
@@ -258,7 +259,7 @@ namespace Nz
if (!stream.Read(&rle_value, 1))
{
NazaraError("Failed to read stream (byte " + String::Number(stream.GetCursorPos()) + ')');
return false;
return nullptr;
}
if (rle_value < 0xc0)
@@ -269,7 +270,7 @@ namespace Nz
if (!stream.Read(&rle_value, 1))
{
NazaraError("Failed to read stream (byte " + String::Number(stream.GetCursorPos()) + ')');
return false;
return nullptr;
}
}
}
@@ -302,7 +303,7 @@ namespace Nz
if (!stream.Read(&rle_value, 1))
{
NazaraError("Failed to read stream (byte " + String::Number(stream.GetCursorPos()) + ')');
return false;
return nullptr;
}
if (rle_value < 0xc0)
@@ -313,7 +314,7 @@ namespace Nz
if (!stream.Read(&rle_value, 1))
{
NazaraError("Failed to read stream (byte " + String::Number(stream.GetCursorPos()) + ')');
return false;
return nullptr;
}
}
}
@@ -329,13 +330,13 @@ namespace Nz
default:
NazaraError("Unsupported " + String::Number(bitCount) + " bitcount for pcx files");
return false;
return nullptr;
}
if (parameters.loadFormat != PixelFormatType_Undefined)
image->Convert(parameters.loadFormat);
return true;
return image;
}
}

View File

@@ -4,6 +4,7 @@
#include <Nazara/Utility/Formats/STBLoader.hpp>
#include <stb/stb_image.h>
#include <Nazara/Core/CallOnExit.hpp>
#include <Nazara/Core/Endianness.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/Stream.hpp>
@@ -54,7 +55,7 @@ namespace Nz
return Ternary_False;
}
bool Load(Image* image, Stream& stream, const ImageParams& parameters)
ImageRef Load(Stream& stream, const ImageParams& parameters)
{
// Je charge tout en RGBA8 et je converti ensuite via la méthode Convert
// Ceci à cause d'un bug de STB lorsqu'il s'agit de charger certaines images (ex: JPG) en "default"
@@ -64,24 +65,29 @@ namespace Nz
if (!ptr)
{
NazaraError("Failed to load image: " + String(stbi_failure_reason()));
return false;
return nullptr;
}
CallOnExit freeStbiImage([ptr]()
{
stbi_image_free(ptr);
});
ImageRef image = Image::New();
if (!image->Create(ImageType_2D, PixelFormatType_RGBA8, width, height, 1, (parameters.levelCount > 0) ? parameters.levelCount : 1))
{
NazaraError("Failed to create image");
stbi_image_free(ptr);
return false;
return nullptr;
}
image->Update(ptr);
stbi_image_free(ptr);
freeStbiImage.CallAndReset();
if (parameters.loadFormat != PixelFormatType_Undefined)
image->Convert(parameters.loadFormat);
return true;
return image;
}
}

View File

@@ -166,8 +166,7 @@ namespace Nz
std::unique_ptr<Image> newImage(new Image(ImageType_2D, PixelFormatType_A8, size.x, size.y));
if (oldImage)
{
Image& image = *static_cast<Image*>(oldImage);
newImage->Copy(image, Rectui(size), Vector2ui(0, 0)); // Copie des anciennes données
newImage->Copy(static_cast<Image*>(oldImage), Rectui(size), Vector2ui(0, 0)); // Copie des anciennes données
}
return newImage.release();

View File

@@ -106,7 +106,7 @@ namespace Nz
for (unsigned int i = 0; i < levels.size(); ++i)
{
unsigned int pixelsPerFace = width * height;
levels[i].reset(new UInt8[pixelsPerFace * depth * PixelFormat::GetBytesPerPixel(newFormat)]);
levels[i] = std::make_unique<UInt8[]>(pixelsPerFace * depth * PixelFormat::GetBytesPerPixel(newFormat));
UInt8* dst = levels[i].get();
UInt8* src = m_sharedImage->levels[i].get();
@@ -143,29 +143,13 @@ namespace Nz
return true;
}
void Image::Copy(const Image& source, const Boxui& srcBox, const Vector3ui& dstPos)
void Image::Copy(const Image* source, const Boxui& srcBox, const Vector3ui& dstPos)
{
#if NAZARA_UTILITY_SAFE
if (m_sharedImage == &emptyImage)
{
NazaraError("Image must be valid");
return;
}
NazaraAssert(IsValid(), "Invalid image");
NazaraAssert(source && source->IsValid(), "Invalid source image");
NazaraAssert(source->GetFormat() == m_sharedImage->format, "Image formats don't match");
if (!source.IsValid())
{
NazaraError("Source image must be valid");
return;
}
if (source.GetFormat() != m_sharedImage->format)
{
NazaraError("Source image format does not match destination image format");
return;
}
#endif
const UInt8* srcPtr = source.GetConstPixels(srcBox.x, srcBox.y, srcBox.z);
const UInt8* srcPtr = source->GetConstPixels(srcBox.x, srcBox.y, srcBox.z);
#if NAZARA_UTILITY_SAFE
if (!srcPtr)
{
@@ -177,7 +161,7 @@ namespace Nz
UInt8 bpp = PixelFormat::GetBytesPerPixel(m_sharedImage->format);
UInt8* dstPtr = GetPixelPtr(m_sharedImage->levels[0].get(), bpp, dstPos.x, dstPos.y, dstPos.z, m_sharedImage->width, m_sharedImage->height);
Copy(dstPtr, srcPtr, m_sharedImage->format, srcBox.width, srcBox.height, srcBox.depth, m_sharedImage->width, m_sharedImage->height, source.GetWidth(), source.GetHeight());
Copy(dstPtr, srcPtr, m_sharedImage->format, srcBox.width, srcBox.height, srcBox.depth, m_sharedImage->width, m_sharedImage->height, source->GetWidth(), source->GetHeight());
}
bool Image::Create(ImageType type, PixelFormatType format, unsigned int width, unsigned int height, unsigned int depth, UInt8 levelCount)
@@ -271,7 +255,7 @@ namespace Nz
// Cette allocation est protégée car sa taille dépend directement de paramètres utilisateurs
try
{
levels[i].reset(new UInt8[PixelFormat::ComputeSize(format, w, h, d)]);
levels[i] = std::make_unique<UInt8[]>(PixelFormat::ComputeSize(format, w, h, d));
if (w > 1)
w >>= 1;
@@ -337,12 +321,12 @@ namespace Nz
// Les images 3D et cubemaps sont stockés de la même façon
unsigned int depth = (m_sharedImage->type == ImageType_Cubemap) ? 6 : m_sharedImage->depth;
for (unsigned int i = 0; i < levels.size(); ++i)
for (auto & level : levels)
{
std::size_t size = PixelFormat::ComputeSize(m_sharedImage->format, width, height, depth);
levels[i].reset(new UInt8[size]);
level = std::make_unique<UInt8[]>(size);
UInt8* ptr = levels[i].get();
UInt8* ptr = level.get();
UInt8* end = &ptr[size];
while (ptr < end)
@@ -514,9 +498,9 @@ namespace Nz
unsigned int width = m_sharedImage->width;
unsigned int height = m_sharedImage->height;
unsigned int depth = (m_sharedImage->type == ImageType_Cubemap) ? 6 : m_sharedImage->depth;
for (unsigned int level = 0; level < m_sharedImage->levels.size(); ++level)
for (auto& level : m_sharedImage->levels)
{
UInt8* ptr = m_sharedImage->levels[level].get();
UInt8* ptr = level.get();
if (!PixelFormat::Flip(PixelFlipping_Horizontally, m_sharedImage->format, width, height, depth, ptr, ptr))
{
NazaraError("Failed to flip image");
@@ -557,9 +541,9 @@ namespace Nz
unsigned int width = m_sharedImage->width;
unsigned int height = m_sharedImage->height;
unsigned int depth = (m_sharedImage->type == ImageType_Cubemap) ? 6 : m_sharedImage->depth;
for (unsigned int level = 0; level < m_sharedImage->levels.size(); ++level)
for (auto& level : m_sharedImage->levels)
{
UInt8* ptr = m_sharedImage->levels[level].get();
UInt8* ptr = level.get();
if (!PixelFormat::Flip(PixelFlipping_Vertically, m_sharedImage->format, width, height, depth, ptr, ptr))
{
NazaraError("Failed to flip image");
@@ -805,7 +789,7 @@ namespace Nz
return Vector3ui(GetLevelSize(m_sharedImage->width, level), GetLevelSize(m_sharedImage->height, level), GetLevelSize(m_sharedImage->depth, level));
}
ImageType Image::GetType() const
ImageType Image::GetType() const
{
return m_sharedImage->type;
}
@@ -865,67 +849,48 @@ namespace Nz
return m_sharedImage != &emptyImage;
}
bool Image::LoadFromFile(const String& filePath, const ImageParams& params)
{
return ImageLoader::LoadFromFile(this, filePath, params);
}
bool Image::LoadFromMemory(const void* data, std::size_t size, const ImageParams& params)
{
return ImageLoader::LoadFromMemory(this, data, size, params);
}
bool Image::LoadFromStream(Stream& stream, const ImageParams& params)
{
return ImageLoader::LoadFromStream(this, stream, params);
}
// LoadArray
bool Image::LoadArrayFromFile(const String& filePath, const ImageParams& imageParams, const Vector2ui& atlasSize)
ImageRef Image::LoadArrayFromFile(const String& filePath, const ImageParams& imageParams, const Vector2ui& atlasSize)
{
Image image;
if (!image.LoadFromFile(filePath, imageParams))
ImageRef image = Image::LoadFromFile(filePath, imageParams);
if (!image)
{
NazaraError("Failed to load image");
return false;
return nullptr;
}
return LoadArrayFromImage(image, atlasSize);
}
bool Image::LoadArrayFromImage(const Image& image, const Vector2ui& atlasSize)
ImageRef Image::LoadArrayFromImage(const Image* image, const Vector2ui& atlasSize)
{
#if NAZARA_UTILITY_SAFE
if (!image.IsValid())
{
NazaraError("Image must be valid");
return false;
}
NazaraAssert(image && image->IsValid(), "Invalid image");
#if NAZARA_UTILITY_SAFE
if (atlasSize.x == 0)
{
NazaraError("Atlas width must be over zero");
return false;
return nullptr;
}
if (atlasSize.y == 0)
{
NazaraError("Atlas height must be over zero");
return false;
return nullptr;
}
#endif
ImageType type = image.GetType();
ImageType type = image->GetType();
#if NAZARA_UTILITY_SAFE
if (type != ImageType_1D && type != ImageType_2D)
{
NazaraError("Image type not handled (0x" + String::Number(type, 16) + ')');
return false;
return nullptr;
}
#endif
Vector2ui imageSize(image.GetWidth(), image.GetHeight());
Vector2ui imageSize(image->GetWidth(), image->GetHeight());
if (imageSize.x % atlasSize.x != 0)
{
@@ -941,82 +906,85 @@ namespace Nz
unsigned int layerCount = atlasSize.x*atlasSize.y;
ImageRef arrayImage = New();
// Selon le type de l'image de base, on va créer un array d'images 2D ou 1D
if (type == ImageType_2D)
Create(ImageType_2D_Array, image.GetFormat(), faceSize.x, faceSize.y, layerCount);
arrayImage->Create(ImageType_2D_Array, image->GetFormat(), faceSize.x, faceSize.y, layerCount);
else
Create(ImageType_1D_Array, image.GetFormat(), faceSize.x, layerCount);
arrayImage->Create(ImageType_1D_Array, image->GetFormat(), faceSize.x, layerCount);
if (!arrayImage->IsValid())
{
NazaraError("Failed to create image");
return nullptr;
}
unsigned int layer = 0;
for (unsigned int j = 0; j < atlasSize.y; ++j)
for (unsigned int i = 0; i < atlasSize.x; ++i)
Copy(image, Rectui(i*faceSize.x, j*faceSize.y, faceSize.x, faceSize.y), Vector3ui(0, 0, layer++));
arrayImage->Copy(image, Rectui(i*faceSize.x, j*faceSize.y, faceSize.x, faceSize.y), Vector3ui(0, 0, layer++));
return true;
return arrayImage;
}
bool Image::LoadArrayFromMemory(const void* data, std::size_t size, const ImageParams& imageParams, const Vector2ui& atlasSize)
ImageRef Image::LoadArrayFromMemory(const void* data, std::size_t size, const ImageParams& imageParams, const Vector2ui& atlasSize)
{
Image image;
if (!image.LoadFromMemory(data, size, imageParams))
ImageRef image = Image::LoadFromMemory(data, size, imageParams);
if (!image)
{
NazaraError("Failed to load image");
return false;
return nullptr;
}
return LoadArrayFromImage(image, atlasSize);
}
bool Image::LoadArrayFromStream(Stream& stream, const ImageParams& imageParams, const Vector2ui& atlasSize)
ImageRef Image::LoadArrayFromStream(Stream& stream, const ImageParams& imageParams, const Vector2ui& atlasSize)
{
Image image;
if (!image.LoadFromStream(stream, imageParams))
ImageRef image = Image::LoadFromStream(stream, imageParams);
if (!image)
{
NazaraError("Failed to load image");
return false;
return nullptr;
}
return LoadArrayFromImage(image, atlasSize);
}
bool Image::LoadCubemapFromFile(const String& filePath, const ImageParams& imageParams, const CubemapParams& cubemapParams)
ImageRef Image::LoadCubemapFromFile(const String& filePath, const ImageParams& imageParams, const CubemapParams& cubemapParams)
{
Image image;
if (!image.LoadFromFile(filePath, imageParams))
ImageRef image = Image::LoadFromFile(filePath, imageParams);
if (!image)
{
NazaraError("Failed to load image");
return false;
return nullptr;
}
return LoadCubemapFromImage(image, cubemapParams);
}
bool Image::LoadCubemapFromImage(const Image& image, const CubemapParams& params)
ImageRef Image::LoadCubemapFromImage(const Image* image, const CubemapParams& params)
{
#if NAZARA_UTILITY_SAFE
if (!image.IsValid())
{
NazaraError("Image must be valid");
return false;
}
NazaraAssert(image && image->IsValid(), "Invalid image");
ImageType type = image.GetType();
#if NAZARA_UTILITY_SAFE
ImageType type = image->GetType();
if (type != ImageType_2D)
{
NazaraError("Image type not handled (0x" + String::Number(type, 16) + ')');
return false;
return nullptr;
}
#endif
unsigned int width = image.GetWidth();
unsigned int height = image.GetHeight();
unsigned int width = image->GetWidth();
unsigned int height = image->GetHeight();
unsigned int faceSize = (params.faceSize == 0) ? std::max(width, height)/4 : params.faceSize;
// Sans cette vérification, celles des rectangles pourrait réussir via un overflow
if (width < faceSize || height < faceSize)
{
NazaraError("Image is too small for this face size");
return false;
return nullptr;
}
// Calcul et vérification des surfaces
@@ -1027,84 +995,80 @@ namespace Nz
if (backPos.x > limitX || backPos.y > limitY)
{
NazaraError("Back rectangle is out of image");
return false;
return nullptr;
}
Vector2ui downPos = params.downPosition * faceSize;
if (downPos.x > limitX || downPos.y > limitY)
{
NazaraError("Down rectangle is out of image");
return false;
return nullptr;
}
Vector2ui forwardPos = params.forwardPosition * faceSize;
if (forwardPos.x > limitX || forwardPos.y > limitY)
{
NazaraError("Forward rectangle is out of image");
return false;
return nullptr;
}
Vector2ui leftPos = params.leftPosition * faceSize;
if (leftPos.x > limitX || leftPos.y > limitY)
{
NazaraError("Left rectangle is out of image");
return false;
return nullptr;
}
Vector2ui rightPos = params.rightPosition * faceSize;
if (rightPos.x > limitX || rightPos.y > limitY)
{
NazaraError("Right rectangle is out of image");
return false;
return nullptr;
}
Vector2ui upPos = params.upPosition * faceSize;
if (upPos.x > limitX || upPos.y > limitY)
{
NazaraError("Up rectangle is out of image");
return false;
return nullptr;
}
Create(ImageType_Cubemap, image.GetFormat(), faceSize, faceSize);
#ifdef NAZARA_DEBUG
// Les paramètres sont valides, que Create ne fonctionne pas relèverait d'un bug
if (m_sharedImage == &emptyImage)
ImageRef cubemap = New();
if (!cubemap->Create(ImageType_Cubemap, image->GetFormat(), faceSize, faceSize))
{
NazaraInternalError("Failed to create cubemap");
return false;
NazaraError("Failed to create cubemap");
return nullptr;
}
#endif
Copy(image, Rectui(backPos.x, backPos.y, faceSize, faceSize), Vector3ui(0, 0, CubemapFace_NegativeZ));
Copy(image, Rectui(downPos.x, downPos.y, faceSize, faceSize), Vector3ui(0, 0, CubemapFace_NegativeY));
Copy(image, Rectui(forwardPos.x, forwardPos.y, faceSize, faceSize), Vector3ui(0, 0, CubemapFace_PositiveZ));
Copy(image, Rectui(leftPos.x, leftPos.y, faceSize, faceSize), Vector3ui(0, 0, CubemapFace_NegativeX));
Copy(image, Rectui(rightPos.x, rightPos.y, faceSize, faceSize), Vector3ui(0, 0, CubemapFace_PositiveX));
Copy(image, Rectui(upPos.x, upPos.y, faceSize, faceSize), Vector3ui(0, 0, CubemapFace_PositiveY));
cubemap->Copy(image, Rectui(backPos.x, backPos.y, faceSize, faceSize), Vector3ui(0, 0, CubemapFace_NegativeZ));
cubemap->Copy(image, Rectui(downPos.x, downPos.y, faceSize, faceSize), Vector3ui(0, 0, CubemapFace_NegativeY));
cubemap->Copy(image, Rectui(forwardPos.x, forwardPos.y, faceSize, faceSize), Vector3ui(0, 0, CubemapFace_PositiveZ));
cubemap->Copy(image, Rectui(leftPos.x, leftPos.y, faceSize, faceSize), Vector3ui(0, 0, CubemapFace_NegativeX));
cubemap->Copy(image, Rectui(rightPos.x, rightPos.y, faceSize, faceSize), Vector3ui(0, 0, CubemapFace_PositiveX));
cubemap->Copy(image, Rectui(upPos.x, upPos.y, faceSize, faceSize), Vector3ui(0, 0, CubemapFace_PositiveY));
return true;
return cubemap;
}
bool Image::LoadCubemapFromMemory(const void* data, std::size_t size, const ImageParams& imageParams, const CubemapParams& cubemapParams)
ImageRef Image::LoadCubemapFromMemory(const void* data, std::size_t size, const ImageParams& imageParams, const CubemapParams& cubemapParams)
{
Image image;
if (!image.LoadFromMemory(data, size, imageParams))
ImageRef image = Image::LoadFromMemory(data, size, imageParams);
if (!image)
{
NazaraError("Failed to load image");
return false;
return nullptr;
}
return LoadCubemapFromImage(image, cubemapParams);
}
bool Image::LoadCubemapFromStream(Stream& stream, const ImageParams& imageParams, const CubemapParams& cubemapParams)
ImageRef Image::LoadCubemapFromStream(Stream& stream, const ImageParams& imageParams, const CubemapParams& cubemapParams)
{
Image image;
if (!image.LoadFromStream(stream, imageParams))
ImageRef image = Image::LoadFromStream(stream, imageParams);
if (!image)
{
NazaraError("Failed to load image");
return false;
return nullptr;
}
return LoadCubemapFromImage(image, cubemapParams);
@@ -1114,21 +1078,21 @@ namespace Nz
{
NazaraAssert(IsValid() && IsCubemap(), "Texture must be a valid cubemap");
Image image;
if (!image.LoadFromFile(filePath, params))
ImageRef image = Image::LoadFromFile(filePath, params);
if (!image)
{
NazaraError("Failed to load image");
return false;
}
if (!image.Convert(GetFormat()))
if (!image->Convert(GetFormat()))
{
NazaraError("Failed to convert image to texture format");
return false;
}
unsigned int faceSize = GetWidth();
if (image.GetWidth() != faceSize || image.GetHeight() != faceSize)
if (image->GetWidth() != faceSize || image->GetHeight() != faceSize)
{
NazaraError("Image size must match texture face size");
return false;
@@ -1142,21 +1106,21 @@ namespace Nz
{
NazaraAssert(IsValid() && IsCubemap(), "Texture must be a valid cubemap");
Image image;
if (!image.LoadFromMemory(data, size, params))
ImageRef image = Image::LoadFromMemory(data, size, params);
if (!image)
{
NazaraError("Failed to load image");
return false;
}
if (!image.Convert(GetFormat()))
if (!image->Convert(GetFormat()))
{
NazaraError("Failed to convert image to texture format");
return false;
}
unsigned int faceSize = GetWidth();
if (image.GetWidth() != faceSize || image.GetHeight() != faceSize)
if (image->GetWidth() != faceSize || image->GetHeight() != faceSize)
{
NazaraError("Image size must match texture face size");
return false;
@@ -1170,21 +1134,21 @@ namespace Nz
{
NazaraAssert(IsValid() && IsCubemap(), "Texture must be a valid cubemap");
Image image;
if (!image.LoadFromStream(stream, params))
ImageRef image = Image::LoadFromStream(stream, params);
if (!image)
{
NazaraError("Failed to load image");
return false;
}
if (!image.Convert(GetFormat()))
if (!image->Convert(GetFormat()))
{
NazaraError("Failed to convert image to texture format");
return false;
}
unsigned int faceSize = GetWidth();
if (image.GetWidth() != faceSize || image.GetHeight() != faceSize)
if (image->GetWidth() != faceSize || image->GetHeight() != faceSize)
{
NazaraError("Image size must match texture face size");
return false;
@@ -1232,7 +1196,7 @@ namespace Nz
m_sharedImage->levels.resize(levelCount);
for (UInt8 i = oldLevelCount; i < maxLevelCount; ++i)
m_sharedImage->levels[i].reset(new UInt8[GetMemoryUsage(i)]);
m_sharedImage->levels[i] = std::make_unique<UInt8[]>(GetMemoryUsage(i));
}
bool Image::SetPixelColor(const Color& color, unsigned int x, unsigned int y, unsigned int z)
@@ -1463,7 +1427,21 @@ namespace Nz
NazaraError("Image type not handled (0x" + String::Number(type, 16) + ')');
return 0;
}
ImageRef Image::LoadFromFile(const String& filePath, const ImageParams& params)
{
return ImageLoader::LoadFromFile(filePath, params);
}
ImageRef Image::LoadFromMemory(const void* data, std::size_t size, const ImageParams& params)
{
return ImageLoader::LoadFromMemory(data, size, params);
}
ImageRef Image::LoadFromStream(Stream& stream, const ImageParams& params)
{
return ImageLoader::LoadFromStream(stream, params);
}
void Image::EnsureOwnership()
@@ -1477,7 +1455,7 @@ namespace Nz
for (unsigned int i = 0; i < levels.size(); ++i)
{
std::size_t size = GetMemoryUsage(i);
levels[i].reset(new UInt8[size]);
levels[i] = std::make_unique<UInt8[]>(size);
std::memcpy(levels[i].get(), m_sharedImage->levels[i].get(), size);
}

View File

@@ -521,21 +521,6 @@ namespace Nz
return m_isValid;
}
bool Mesh::LoadFromFile(const String& filePath, const MeshParams& params)
{
return MeshLoader::LoadFromFile(this, filePath, params);
}
bool Mesh::LoadFromMemory(const void* data, std::size_t size, const MeshParams& params)
{
return MeshLoader::LoadFromMemory(this, data, size, params);
}
bool Mesh::LoadFromStream(Stream& stream, const MeshParams& params)
{
return MeshLoader::LoadFromStream(this, stream, params);
}
void Mesh::Recenter()
{
NazaraAssert(m_isValid, "Mesh should be created first");
@@ -663,6 +648,21 @@ namespace Nz
}
}
MeshRef Mesh::LoadFromFile(const String& filePath, const MeshParams& params)
{
return MeshLoader::LoadFromFile(filePath, params);
}
MeshRef Mesh::LoadFromMemory(const void* data, std::size_t size, const MeshParams& params)
{
return MeshLoader::LoadFromMemory(data, size, params);
}
MeshRef Mesh::LoadFromStream(Stream& stream, const MeshParams& params)
{
return MeshLoader::LoadFromStream(stream, params);
}
bool Mesh::Initialize()
{
if (!MeshLibrary::Initialize())