Added Image

Added pixel format support
Added MemoryStream
Added Rect
Added ResourceLoader
Added generic loader (bmp, gif, hdr, jpg, jpeg, pic, png, psd, tga)
Added PCX loader
Added utility module initializer
Fixed Config.hpp include
Prerequesites.hpp now overwrites _WIN32_WINNT when defined
version is less than requiered version
Renderer's initialisation will implicitly initialize utility module
Removed RENDERER_SINGLETON option
Shaders are now resources
This commit is contained in:
Lynix
2012-05-21 21:54:13 +02:00
parent 47cdbbcdb0
commit 9b3f4e794a
51 changed files with 6845 additions and 147 deletions

View File

@@ -35,7 +35,7 @@
// Active les tests de sécurité basés sur le code (Conseillé pour le développement)
#define NAZARA_UTILITY_SAFE 1
// Fait tourner chaque fenêtre dans un thread séparé
// Fait tourner chaque fenêtre dans un thread séparé si le système le supporte
#define NAZARA_UTILITY_THREADED_WINDOW 1
#endif // NAZARA_CONFIG_UTILITY_HPP

View File

@@ -0,0 +1,135 @@
// Copyright (C) 2012 Jérôme Leclercq
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_IMAGE_HPP
#define NAZARA_IMAGE_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/InputStream.hpp>
#include <Nazara/Math/Rect.hpp>
#include <Nazara/Utility/ResourceLoader.hpp>
#include <Nazara/Utility/PixelFormat.hpp>
#include <Nazara/Utility/Resource.hpp>
//#include <Nazara/Utility/ThreadSafety.hpp>
enum nzCubemapFace
{
nzCubemapFace_PositiveX,
nzCubemapFace_NegativeX,
nzCubemapFace_PositiveY,
nzCubemapFace_NegativeY,
nzCubemapFace_PositiveZ,
nzCubemapFace_NegativeZ
};
enum nzImageType
{
nzImageType_1D,
nzImageType_2D,
nzImageType_3D,
nzImageType_Cubemap
};
struct NzImageParams
{
bool IsValid() const
{
return true;
}
};
///TODO: Filtres
///TODO: Mipmaps
class NAZARA_API NzImage : public NzResource, public NzResourceLoader<NzImage, NzImageParams>
{
public:
struct SharedImage;
NzImage();
NzImage(const NzImage& image);
NzImage(NzImage&& image);
NzImage(SharedImage* sharedImage);
~NzImage();
bool Copy(const NzImage& source, const NzRectui& srcRect, const NzVector2ui& dstPos);
bool CopyToFace(nzCubemapFace face, const NzImage& source, const NzRectui& srcRect, const NzVector2ui& dstPos);
bool Create(nzImageType type, nzPixelFormat format, unsigned int width, unsigned int height = 1, unsigned int depth = 1);
void Destroy();
nzUInt8 GetBPP() const;
const nzUInt8* GetConstPixels() const;
unsigned int GetDepth() const;
nzPixelFormat GetFormat() const;
unsigned int GetHeight() const;
nzUInt8* GetPixels();
unsigned int GetSize() const;
nzImageType GetType() const;
unsigned int GetWidth() const;
bool IsCompressed() const;
bool IsCubemap() const;
bool IsValid() const;
bool LoadFromFile(const NzString& filePath, const NzImageParams& params = NzImageParams());
bool LoadFromMemory(const void* data, std::size_t size, const NzImageParams& params = NzImageParams());
bool LoadFromStream(NzInputStream& stream, const NzImageParams& params = NzImageParams());
bool Update(const nzUInt8* pixels);
bool Update(const nzUInt8* pixels, const NzRectui& rect);
bool UpdateFace(nzCubemapFace face, const nzUInt8* pixels);
bool UpdateFace(nzCubemapFace face, const nzUInt8* pixels, const NzRectui& rect);
NzImage& operator=(const NzImage& image);
NzImage& operator=(NzImage&& image);
static void RegisterFileLoader(const NzString& extensions, LoadFileFunction loadFile);
static void RegisterMemoryLoader(IsMemoryLoadingSupportedFunction isLoadingSupported, LoadMemoryFunction loadMemory);
static void RegisterStreamLoader(IsStreamLoadingSupportedFunction isLoadingSupported, LoadStreamFunction loadStream);
static void UnregisterFileLoader(const NzString& extensions, LoadFileFunction loadFile);
static void UnregisterMemoryLoader(IsMemoryLoadingSupportedFunction isLoadingSupported, LoadMemoryFunction loadMemory);
static void UnregisterStreamLoader(IsStreamLoadingSupportedFunction isLoadingSupported, LoadStreamFunction loadStream);
struct SharedImage
{
SharedImage() : // Vivement GCC 4.7 sur Windows
refCount(1)
{
}
SharedImage(unsigned short RefCount, nzImageType Type, nzPixelFormat Format, nzUInt8* Pixels, unsigned int Width, unsigned int Height = 1, unsigned int Depth = 1) :
type(Type),
format(Format),
pixels(Pixels),
depth(Depth),
height(Height),
width(Width),
refCount(RefCount)
{
}
nzImageType type;
nzPixelFormat format;
nzUInt8* pixels;
unsigned int depth;
unsigned int height;
unsigned int width;
unsigned short refCount;
NazaraMutex(mutex)
};
static SharedImage emptyImage;
private:
void EnsureOwnership();
void ReleaseImage();
SharedImage* m_sharedImage;
};
#endif // NAZARA_IMAGE_HPP

View File

@@ -0,0 +1,40 @@
// Copyright (C) 2012 Jérôme Leclercq
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_PIXELFORMAT_HPP
#define NAZARA_PIXELFORMAT_HPP
#include <Nazara/Prerequesites.hpp>
enum nzPixelFormat
{
nzPixelFormat_Undefined,
nzPixelFormat_B8G8R8,
nzPixelFormat_B8G8R8A8,
nzPixelFormat_DXT1,
nzPixelFormat_DXT3,
nzPixelFormat_DXT5,
nzPixelFormat_L8,
nzPixelFormat_L8A8,
nzPixelFormat_R4G4A4A4,
nzPixelFormat_R5G5A5A1,
nzPixelFormat_R8,
nzPixelFormat_R8G8,
nzPixelFormat_R8G8B8,
nzPixelFormat_R8G8B8A8
};
class NzPixelFormat
{
public:
static nzUInt8 GetBPP(nzPixelFormat format);
static bool IsCompressed(nzPixelFormat format);
};
#include <Nazara/Utility/PixelFormat.inl>
#endif // NAZARA_PIXELFORMAT_HPP

View File

@@ -0,0 +1,71 @@
// Copyright (C) 2012 Jérôme Leclercq
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Debug.hpp>
inline nzUInt8 NzPixelFormat::GetBPP(nzPixelFormat format)
{
switch (format)
{
case nzPixelFormat_Undefined:
return 0;
case nzPixelFormat_B8G8R8:
return 3;
case nzPixelFormat_B8G8R8A8:
return 4;
case nzPixelFormat_DXT1:
return 1;
case nzPixelFormat_DXT3:
return 2;
case nzPixelFormat_DXT5:
return 2;
case nzPixelFormat_L8:
return 1;
case nzPixelFormat_L8A8:
return 2;
case nzPixelFormat_R4G4A4A4:
return 2;
case nzPixelFormat_R5G5A5A1:
return 2;
case nzPixelFormat_R8:
return 1;
case nzPixelFormat_R8G8:
return 2;
case nzPixelFormat_R8G8B8:
return 3;
case nzPixelFormat_R8G8B8A8:
return 4;
}
return 0;
}
inline bool NzPixelFormat::IsCompressed(nzPixelFormat format)
{
switch (format)
{
case nzPixelFormat_DXT1:
case nzPixelFormat_DXT3:
case nzPixelFormat_DXT5:
return true;
default:
return false;
}
}
#include <Nazara/Core/DebugOff.hpp>

View File

@@ -8,12 +8,12 @@
#define NAZARA_RESOURCE_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Utility/NonCopyable.hpp>
class NAZARA_API NzResource : NzNonCopyable
class NAZARA_API NzResource
{
public:
NzResource(bool persistent = true);
NzResource(const NzResource& resource);
virtual ~NzResource();
void AddResourceReference() const;

View File

@@ -0,0 +1,49 @@
// Copyright (C) 2012 Jérôme Leclercq
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_RESOURCELOADER_HPP
#define NAZARA_RESOURCELOADER_HPP
#include <Nazara/Core/String.hpp>
#include <list>
#include <map>
#include <utility>
class NzInputStream;
template<typename Type, typename Parameters>
class NzResourceLoader
{
public:
typedef bool (*IsMemoryLoadingSupportedFunction)(const void* data, unsigned int size, const Parameters& parameters);
typedef bool (*IsStreamLoadingSupportedFunction)(NzInputStream& stream, const Parameters& parameters);
typedef bool (*LoadFileFunction)(Type* resource, const NzString& filePath, const Parameters& parameters);
typedef bool (*LoadMemoryFunction)(Type* resource, const void* data, unsigned int size, const Parameters& parameters);
typedef bool (*LoadStreamFunction)(Type* resource, NzInputStream& stream, const Parameters& parameters);
protected:
static bool LoadResourceFromFile(Type* resource, const NzString& filePath, const Parameters& parameters);
static bool LoadResourceFromMemory(Type* resource, const void* data, unsigned int size, const Parameters& parameters);
static bool LoadResourceFromStream(Type* resource, NzInputStream& stream, const Parameters& parameters);
static void RegisterResourceFileLoader(const NzString& extensions, LoadFileFunction loadFile);
static void RegisterResourceMemoryLoader(IsMemoryLoadingSupportedFunction isLoadingSupported, LoadMemoryFunction loadMemory);
static void RegisterResourceStreamLoader(IsStreamLoadingSupportedFunction isLoadingSupported, LoadStreamFunction loadStream);
static void UnregisterResourceFileLoader(const NzString& extensions, LoadFileFunction loadFile);
static void UnregisterResourceMemoryLoader(IsMemoryLoadingSupportedFunction isLoadingSupported, LoadMemoryFunction loadMemory);
static void UnregisterResourceStreamLoader(IsStreamLoadingSupportedFunction isLoadingSupported, LoadStreamFunction loadStream);
private:
typedef std::pair<IsMemoryLoadingSupportedFunction, LoadMemoryFunction> MemoryLoader;
typedef std::pair<IsStreamLoadingSupportedFunction, LoadStreamFunction> StreamLoader;
static std::list<MemoryLoader> s_memoryLoaders;
static std::list<StreamLoader> s_streamLoaders;
static std::multimap<NzString, LoadFileFunction> s_fileLoaders;
};
#include <Nazara/Utility/ResourceLoader.inl>
#endif // NAZARA_RESOURCELOADER_HPP

View File

@@ -0,0 +1,186 @@
// Copyright (C) 2012 Jérôme Leclercq
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/File.hpp>
#include <Nazara/Core/InputStream.hpp>
#include <Nazara/Utility/Config.hpp>
#include <Nazara/Utility/Debug.hpp>
template<typename Type, typename Parameters>
bool NzResourceLoader<Type, Parameters>::LoadResourceFromFile(Type* resource, const NzString& filePath, const Parameters& parameters)
{
#if NAZARA_UTILITY_SAFE
if (!parameters.IsValid())
{
NazaraError("Invalid Parameters");
return false;
}
#endif
NzString path = NzFile::NormalizePath(filePath);
NzString ext = path.SubstrFrom('.', -1, true);
if (ext.IsEmpty())
{
NazaraError("Failed to get file extension");
return false;
}
// Récupération de tous les loaders de cette extension
auto range = s_fileLoaders.equal_range(ext);
if (range.first == range.second)
{
NazaraError("No loader found for extension \"" + ext + '"');
return false;
}
for (auto it = range.first; it != range.second; ++it)
{
// Chargement de la ressource
if (it->second(resource, filePath, parameters))
return true;
NazaraWarning("Loader failed");
}
NazaraError("Failed to load file: all loaders failed");
return false;
}
template<typename Type, typename Parameters>
bool NzResourceLoader<Type, Parameters>::LoadResourceFromMemory(Type* resource, const void* data, unsigned int size, const Parameters& parameters)
{
#if NAZARA_UTILITY_SAFE
if (!parameters.IsValid())
{
NazaraError("Invalid Parameters");
return false;
}
if (!data || size == 0)
{
NazaraError("No data to load");
return false;
}
#endif
for (auto loader = s_memoryLoaders.begin(); loader != s_memoryLoaders.end(); ++loader)
{
// Le loader supporte-t-il les données ?
if (!loader->first(data, size, parameters))
continue;
// Chargement de la ressource
if (loader->second(resource, data, size, parameters))
return true;
NazaraWarning("Loader failed");
}
NazaraError("Failed to load file: all loaders failed");
return false;
}
template<typename Type, typename Parameters>
bool NzResourceLoader<Type, Parameters>::LoadResourceFromStream(Type* resource, NzInputStream& stream, const Parameters& parameters)
{
#if NAZARA_UTILITY_SAFE
if (!parameters.IsValid())
{
NazaraError("Invalid Parameters");
return false;
}
if (stream.GetSize() == 0 || stream.GetCursorPos() >= stream.GetSize())
{
NazaraError("No data to load");
return false;
}
#endif
nzUInt64 streamPos = stream.GetCursorPos();
for (auto loader = s_streamLoaders.begin(); loader != s_streamLoaders.end(); ++loader)
{
stream.SetCursorPos(streamPos);
// Le loader supporte-t-il les données ?
if (!loader->first(stream, parameters))
continue;
stream.SetCursorPos(streamPos);
// Chargement de la ressource
if (loader->second(resource, stream, parameters))
return true;
NazaraWarning("Loader failed");
}
NazaraError("Failed to load file: all loaders failed");
return false;
}
template<typename Type, typename Parameters>
void NzResourceLoader<Type, Parameters>::RegisterResourceFileLoader(const NzString& extensions, LoadFileFunction loadFile)
{
std::vector<NzString> exts;
extensions.SplitAny(exts, " /\\*.,;|-_");
for (const NzString& ext : exts)
s_fileLoaders.insert(std::make_pair(ext, loadFile));
}
template<typename Type, typename Parameters>
void NzResourceLoader<Type, Parameters>::RegisterResourceMemoryLoader(IsMemoryLoadingSupportedFunction isLoadingSupported, LoadMemoryFunction loadMemory)
{
s_memoryLoaders.push_back(std::make_pair(isLoadingSupported, loadMemory));
}
template<typename Type, typename Parameters>
void NzResourceLoader<Type, Parameters>::RegisterResourceStreamLoader(IsStreamLoadingSupportedFunction isLoadingSupported, LoadStreamFunction loadStream)
{
s_streamLoaders.push_back(std::make_pair(isLoadingSupported, loadStream));
}
template<typename Type, typename Parameters>
void NzResourceLoader<Type, Parameters>::UnregisterResourceFileLoader(const NzString& extensions, LoadFileFunction loadFile)
{
std::vector<NzString> exts;
extensions.SplitAny(exts, " /\\*.,;|-_");
for (const NzString& ext : exts)
{
// Récupération de tous les loaders de cette extension
auto range = s_fileLoaders.equal_range(ext);
for (auto it = range.first; it != range.second; ++it)
{
if (it->second == loadFile)
{
s_fileLoaders.erase(it);
break;
}
}
}
//s_fileLoaders.erase(std::make_pair(ext, loadFile));
}
template<typename Type, typename Parameters>
void NzResourceLoader<Type, Parameters>::UnregisterResourceMemoryLoader(IsMemoryLoadingSupportedFunction isLoadingSupported, LoadMemoryFunction loadMemory)
{
s_memoryLoaders.remove(std::make_pair(isLoadingSupported, loadMemory));
}
template<typename Type, typename Parameters>
void NzResourceLoader<Type, Parameters>::UnregisterResourceStreamLoader(IsStreamLoadingSupportedFunction isLoadingSupported, LoadStreamFunction loadStream)
{
s_streamLoaders.remove(std::make_pair(isLoadingSupported, loadStream));
}
template<typename T, typename P> std::list<typename NzResourceLoader<T, P>::MemoryLoader> NzResourceLoader<T, P>::s_memoryLoaders;
template<typename T, typename P> std::list<typename NzResourceLoader<T, P>::StreamLoader> NzResourceLoader<T, P>::s_streamLoaders;
template<typename T, typename P> std::multimap<NzString, typename NzResourceLoader<T, P>::LoadFileFunction> NzResourceLoader<T, P>::s_fileLoaders;
#include <Nazara/Utility/DebugOff.hpp>

View File

@@ -0,0 +1,27 @@
// Copyright (C) 2012 Jérôme Leclercq
// This file is part of the "Nazara Engine".
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_UTILITY_HPP
#define NAZARA_UTILITY_HPP
#include <Nazara/Prerequesites.hpp>
class NAZARA_API NzUtility
{
public:
NzUtility();
~NzUtility();
bool Initialize();
void Uninitialize();
static bool IsInitialized();
private:
static bool s_initialized;
};
#endif // NAZARA_UTILITY_HPP