Refactored mathematics module

Added AABBs
Added code examples
Added experimental support for texture arrays (1D/2D)
Added initialisers (new way of initialising modules)
Added global headers (Plus a global header generator script)
Added pattern support for directory
Added support for spinlocks critical section on Windows
Added NzRenderWindow::SetFramerateLimit
Core project now includes Mathematics files
Fixed color implementation using double
Fixed declaration needing renderer include
Fixed MLT not clearing nextFree(File/Line) after Free
Fixed move operators not being noexcept
Fixed thread-safety (Now working correctly - If I'm lucky)
Moved Resource to core
New interface for modules
New interface for the renderer
Put some global functions to anonymous namespace
Removed empty modules
Renamed ThreadCondition to ConditionVariable
Replaced redirect to cerr log option by duplicate to cout
Setting mouse position relative to a window will make this window ignore
the event
Shaders sending methods no longer takes the uniform variable name (it's
using ID instead)
Using new OpenGL 4.3 header
This commit is contained in:
Lynix
2012-08-08 04:44:17 +02:00
parent 06eda4eba9
commit b442ab0bd2
142 changed files with 6861 additions and 2326 deletions

View File

@@ -0,0 +1,162 @@
// Copyright (C) 2011 Jérôme Leclercq
// This file is part of the "Ungine".
// For conditions of distribution and use, see copyright notice in Core.h
#include <Nazara/Utility/AxisAlignedBox.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Utility/Debug.hpp>
NzAxisAlignedBox::NzAxisAlignedBox() :
m_extend(nzExtend_Null)
{
}
NzAxisAlignedBox::NzAxisAlignedBox(const NzVector3f& vec1, const NzVector3f& vec2) :
m_extend(nzExtend_Finite),
m_cube(vec1, vec2)
{
}
NzAxisAlignedBox::NzAxisAlignedBox(nzExtend extend) :
m_extend(extend)
{
}
bool NzAxisAlignedBox::Contains(const NzAxisAlignedBox& box)
{
if (m_extend == nzExtend_Null || box.m_extend == nzExtend_Null)
return false;
else if (m_extend == nzExtend_Infinite || box.m_extend == nzExtend_Infinite)
return true;
return m_cube.Contains(box.m_cube);
}
void NzAxisAlignedBox::ExtendTo(const NzAxisAlignedBox& box)
{
switch (m_extend)
{
case nzExtend_Finite:
{
switch (box.m_extend)
{
case nzExtend_Finite:
m_cube.ExtendTo(box.m_cube);
break;
case nzExtend_Infinite:
SetInfinite();
break;
case nzExtend_Null:
break;
break;
}
case nzExtend_Infinite:
// Rien à faire
break;
case nzExtend_Null:
operator=(box);
break;
}
}
}
void NzAxisAlignedBox::ExtendTo(const NzVector3f& vector)
{
switch (m_extend)
{
case nzExtend_Finite:
m_cube.ExtendTo(vector);
break;
case nzExtend_Infinite:
// Rien à faire
break;
case nzExtend_Null:
// Nous étendons l'AABB en la construisant de l'origine jusqu'au vecteur
m_cube.x = 0.f;
m_cube.y = 0.f;
m_cube.z = 0.f;
m_cube.width = std::fabs(vector.x);
m_cube.height = std::fabs(vector.y);
m_cube.depth = std::fabs(vector.z);
break;
}
}
nzExtend NzAxisAlignedBox::GetExtend() const
{
return m_extend;
}
NzVector3f NzAxisAlignedBox::GetMaximum() const
{
return NzVector3f(m_cube.x+m_cube.width, m_cube.y+m_cube.height, m_cube.z+m_cube.depth);
}
NzVector3f NzAxisAlignedBox::GetMinimum() const
{
return NzVector3f(m_cube.x, m_cube.y, m_cube.z);
}
bool NzAxisAlignedBox::IsFinite() const
{
return m_extend == nzExtend_Finite;
}
bool NzAxisAlignedBox::IsInfinite() const
{
return m_extend == nzExtend_Infinite;
}
bool NzAxisAlignedBox::IsNull() const
{
return m_extend == nzExtend_Null;
}
void NzAxisAlignedBox::SetInfinite()
{
m_extend = nzExtend_Infinite;
}
void NzAxisAlignedBox::SetExtends(const NzVector3f& vec1, const NzVector3f& vec2)
{
m_extend = nzExtend_Finite;
m_cube.Set(vec1, vec2);
}
void NzAxisAlignedBox::SetNull()
{
m_extend = nzExtend_Null;
}
NzString NzAxisAlignedBox::ToString() const
{
switch (m_extend)
{
case nzExtend_Finite:
return "NzAxisAlignedBox(min=" + GetMinimum().ToString() + ", max=" + GetMaximum().ToString() + ')';
case nzExtend_Infinite:
return "NzAxisAlignedBox(Infinite)";
case nzExtend_Null:
return "NzAxisAlignedBox(Null)";
}
return "NzAxisAlignedBox(ERROR)";
}
const NzAxisAlignedBox NzAxisAlignedBox::Infinite(nzExtend_Infinite);
const NzAxisAlignedBox NzAxisAlignedBox::Null(nzExtend_Null);
std::ostream& operator<<(std::ostream& out, const NzAxisAlignedBox& aabb)
{
out << aabb.ToString();
return out;
}

View File

@@ -290,9 +290,10 @@ bool NzBuffer::SetStorage(nzBufferStorage storage)
m_impl->Unmap();
m_impl->Destroy();
delete m_impl;
delete m_impl;
m_impl = impl;
m_storage = storage;
return true;

View File

@@ -17,7 +17,7 @@ namespace
inline nzUInt8* GetPixelPtr(nzUInt8* base, nzUInt8 bpp, unsigned int x, unsigned int y, unsigned int z, unsigned int width, unsigned int height)
{
return &base[(width*(height*z+y) + x) * bpp];
return &base[(width*(height*z + y) + x)*bpp];
}
}
@@ -43,7 +43,7 @@ m_sharedImage(image.m_sharedImage)
}
}
NzImage::NzImage(NzImage&& image) :
NzImage::NzImage(NzImage&& image) noexcept :
m_sharedImage(image.m_sharedImage)
{
image.m_sharedImage = &emptyImage;
@@ -95,14 +95,15 @@ bool NzImage::Convert(nzPixelFormat format)
unsigned int srcStride = pixelsPerFace * NzPixelFormat::GetBPP(m_sharedImage->format);
unsigned int dstStride = pixelsPerFace * NzPixelFormat::GetBPP(format);
levels[i] = ptr;
for (unsigned int d = 0; d < depth; ++d)
{
if (!NzPixelFormat::Convert(m_sharedImage->format, format, pixels, &pixels[srcStride], ptr))
{
NazaraError("Failed to convert image");
for (unsigned int j = 0; j <= i; ++j)
// Nettoyage de la mémoire
delete[] ptr; // Permet une optimisation de boucle (GCC)
for (unsigned int j = 0; j < i; ++j)
delete[] levels[j];
delete[] levels;
@@ -114,6 +115,8 @@ bool NzImage::Convert(nzPixelFormat format)
ptr += dstStride;
}
levels[i] = ptr;
if (width > 1)
width >>= 1;
@@ -223,6 +226,7 @@ bool NzImage::Create(nzImageType type, nzPixelFormat format, unsigned int width,
}
break;
case nzImageType_1D_Array:
case nzImageType_2D:
if (depth > 1)
{
@@ -231,6 +235,7 @@ bool NzImage::Create(nzImageType type, nzPixelFormat format, unsigned int width,
}
break;
case nzImageType_2D_Array:
case nzImageType_3D:
break;
@@ -281,7 +286,10 @@ bool NzImage::Create(nzImageType type, nzPixelFormat format, unsigned int width,
catch (const std::exception& e)
{
NazaraError("Failed to allocate image's level " + NzString::Number(i) + " (" + NzString(e.what()) + ')');
for (unsigned int j = 0; j <= i; ++j)
// Nettoyage
delete[] levels[i]; // Permet une optimisation de boucle (GCC)
for (unsigned int j = 0; j < i; ++j)
delete[] levels[j];
delete[] levels;
@@ -302,7 +310,7 @@ void NzImage::Destroy()
bool NzImage::Fill(const NzColor& color)
{
#if NAZARA_RENDERER_SAFE
#if NAZARA_UTILITY_SAFE
if (!IsValid())
{
NazaraError("Image must be valid");
@@ -399,6 +407,7 @@ bool NzImage::Fill(const NzColor& color, const NzRectui& rect, unsigned int z)
return false;
}
///FIXME: L'algorithme a du mal avec un bpp non multiple de 2
nzUInt8* dstPixels = GetPixelPtr(m_sharedImage->pixels[0], bpp, rect.x, rect.y, z, m_sharedImage->width, m_sharedImage->height);
unsigned int srcStride = rect.width * bpp;
unsigned int dstStride = m_sharedImage->width * bpp;
@@ -415,6 +424,8 @@ bool NzImage::Fill(const NzColor& color, const NzRectui& rect, unsigned int z)
dstPixels += dstStride;
}
delete[] pixels;
return true;
}
@@ -452,6 +463,7 @@ bool NzImage::Fill(const NzColor& color, const NzCubeui& cube)
return false;
}
///FIXME: L'algorithme a du mal avec un bpp non multiple de 2
nzUInt8* dstPixels = GetPixelPtr(m_sharedImage->pixels[0], bpp, cube.x, cube.y, cube.z, m_sharedImage->width, m_sharedImage->height);
unsigned int srcStride = cube.width * bpp;
unsigned int dstStride = m_sharedImage->width * bpp;
@@ -562,7 +574,7 @@ nzUInt8 NzImage::GetBPP() const
return NzPixelFormat::GetBPP(m_sharedImage->format);
}
const nzUInt8* NzImage::GetConstPixels(nzUInt8 level, unsigned int x, unsigned int y, unsigned int z) const
const nzUInt8* NzImage::GetConstPixels(unsigned int x, unsigned int y, unsigned int z, nzUInt8 level) const
{
#if NAZARA_UTILITY_SAFE
if (!IsValid())
@@ -571,12 +583,6 @@ const nzUInt8* NzImage::GetConstPixels(nzUInt8 level, unsigned int x, unsigned i
return nullptr;
}
if (level >= m_sharedImage->levelCount)
{
NazaraError("Level out of bounds (" + NzString::Number(level) + " >= " + NzString::Number(m_sharedImage->levelCount) + ')');
return nullptr;
}
if (x >= m_sharedImage->width)
{
NazaraError("X value exceeds width (" + NzString::Number(x) + " >= (" + NzString::Number(m_sharedImage->width) + ')');
@@ -595,6 +601,12 @@ const nzUInt8* NzImage::GetConstPixels(nzUInt8 level, unsigned int x, unsigned i
NazaraError("Z value exceeds depth (" + NzString::Number(z) + " >= (" + NzString::Number(depth) + ')');
return nullptr;
}
if (level >= m_sharedImage->levelCount)
{
NazaraError("Level out of bounds (" + NzString::Number(level) + " >= " + NzString::Number(m_sharedImage->levelCount) + ')');
return nullptr;
}
#endif
return GetPixelPtr(m_sharedImage->pixels[level], NzPixelFormat::GetBPP(m_sharedImage->format), x, y, z, m_sharedImage->width, m_sharedImage->height);
@@ -685,7 +697,7 @@ NzColor NzImage::GetPixelColor(unsigned int x, unsigned int y, unsigned int z) c
return color;
}
nzUInt8* NzImage::GetPixels(nzUInt8 level, unsigned int x, unsigned int y, unsigned int z)
nzUInt8* NzImage::GetPixels(unsigned int x, unsigned int y, unsigned int z, nzUInt8 level)
{
#if NAZARA_UTILITY_SAFE
if (!IsValid())
@@ -694,12 +706,6 @@ nzUInt8* NzImage::GetPixels(nzUInt8 level, unsigned int x, unsigned int y, unsig
return nullptr;
}
if (level >= m_sharedImage->levelCount)
{
NazaraError("Level out of bounds (" + NzString::Number(level) + " >= " + NzString::Number(m_sharedImage->levelCount) + ')');
return nullptr;
}
if (x >= m_sharedImage->width)
{
NazaraError("X value exceeds width (" + NzString::Number(x) + " >= (" + NzString::Number(m_sharedImage->width) + ')');
@@ -718,6 +724,13 @@ nzUInt8* NzImage::GetPixels(nzUInt8 level, unsigned int x, unsigned int y, unsig
NazaraError("Z value exceeds depth (" + NzString::Number(z) + " >= (" + NzString::Number(depth) + ')');
return nullptr;
}
if (level >= m_sharedImage->levelCount)
{
NazaraError("Level out of bounds (" + NzString::Number(level) + " >= " + NzString::Number(m_sharedImage->levelCount) + ')');
return nullptr;
}
#endif
EnsureOwnership();
@@ -1077,7 +1090,7 @@ NzImage& NzImage::operator=(const NzImage& image)
return *this;
}
NzImage& NzImage::operator=(NzImage&& image)
NzImage& NzImage::operator=(NzImage&& image) noexcept
{
std::swap(m_sharedImage, image.m_sharedImage);
@@ -1086,11 +1099,11 @@ NzImage& NzImage::operator=(NzImage&& image)
nzUInt8 NzImage::GetMaxLevel(unsigned int width, unsigned int height, unsigned int depth)
{
static const float l2 = std::log(2);
static const float l2 = std::log(2.f);
unsigned int widthLevel = std::log(width)/l2;
unsigned int heightLevel = std::log(height)/l2;
unsigned int depthLevel = std::log(depth)/l2;
unsigned int widthLevel = std::log(static_cast<float>(width))/l2;
unsigned int heightLevel = std::log(static_cast<float>(height))/l2;
unsigned int depthLevel = std::log(static_cast<float>(depth))/l2;
return std::max(std::max(std::max(widthLevel, heightLevel), depthLevel), 1U);
}

View File

@@ -48,7 +48,7 @@ namespace
}
// Les fichiers MD2 sont en little endian
#if NAZARA_BIG_ENDIAN
#if defined(NAZARA_BIG_ENDIAN)
NzByteSwap(&header.ident, sizeof(nzUInt32));
#endif
@@ -58,7 +58,7 @@ namespace
return false;
}
#if NAZARA_BIG_ENDIAN
#if defined(NAZARA_BIG_ENDIAN)
NzByteSwap(&header.version, sizeof(nzUInt32));
#endif
@@ -68,7 +68,7 @@ namespace
return false;
}
#if NAZARA_BIG_ENDIAN
#if defined(NAZARA_BIG_ENDIAN)
NzByteSwap(&header.skinwidth, sizeof(nzUInt32));
NzByteSwap(&header.skinheight, sizeof(nzUInt32));
NzByteSwap(&header.framesize, sizeof(nzUInt32));
@@ -129,6 +129,7 @@ namespace
NzAnimation* animation = new NzAnimation;
if (animation->Create(nzAnimationType_Keyframe, endFrame-startFrame+1))
{
// Décodage des séquences
NzString frameName;
NzSequence sequence;
@@ -154,7 +155,7 @@ namespace
stream.SetCursorPos(header.offset_frames + i*header.framesize + offsetof(md2_frame, name));
stream.Read(name, 16*sizeof(char));
int pos = std::strlen(name)-1;
pos = std::strlen(name)-1;
for (unsigned int j = 0; j < 2; ++j)
{
if (!std::isdigit(name[pos]))
@@ -217,7 +218,7 @@ namespace
const md2_header* header = reinterpret_cast<const md2_header*>(data);
#if NAZARA_BIG_ENDIAN
#if defined(NAZARA_BIG_ENDIAN)
nzUInt32 ident = header->ident;
nzUInt32 version = header->version;
@@ -238,7 +239,7 @@ namespace
if (stream.Read(&magic[0], 2*sizeof(nzUInt32)) != 2*sizeof(nzUInt32))
return false;
#if NAZARA_BIG_ENDIAN
#if defined(NAZARA_BIG_ENDIAN)
NzByteSwap(&magic[0], sizeof(nzUInt32));
NzByteSwap(&magic[1], sizeof(nzUInt32));
#endif

View File

@@ -41,7 +41,7 @@ bool NzMD2Mesh::Create(const md2_header& header, NzInputStream& stream, const Nz
stream.SetCursorPos(header.offset_st);
stream.Read(&texCoords[0], header.num_st*sizeof(md2_texCoord));
#if NAZARA_BIG_ENDIAN
#if defined(NAZARA_BIG_ENDIAN)
for (unsigned int i = 0; i < header.num_st; ++i)
{
NzByteSwap(&texCoords[i].u, sizeof(nzInt16));
@@ -52,7 +52,7 @@ bool NzMD2Mesh::Create(const md2_header& header, NzInputStream& stream, const Nz
stream.SetCursorPos(header.offset_tris);
stream.Read(&triangles[0], header.num_tris*sizeof(md2_triangle));
#if NAZARA_BIG_ENDIAN
#if defined(NAZARA_BIG_ENDIAN)
for (unsigned int i = 0; i < header.num_tris; ++i)
{
NzByteSwap(&triangles[i].vertices[0], sizeof(nzUInt16));
@@ -72,9 +72,7 @@ bool NzMD2Mesh::Create(const md2_header& header, NzInputStream& stream, const Nz
frame.vertices.resize(header.num_vertices);
// Pour que le modèle soit correctement aligné, on génère une matrice de rotation que nous appliquerons à chacune des vertices
NzMatrix4f rotationMatrix = NzMatrix4f::Rotate(NzEulerAnglesf(-90.f, 0.f, -90.f));
//NzMatrix4f rotationMatrix;
//rotationMatrix.SetIdentity();
NzMatrix4f rotationMatrix = NzMatrix4f::Rotate(NzEulerAnglesf(90.f, -90.f, 0.f));
unsigned int stride = s_declaration.GetStride(nzElementStream_VertexData);
@@ -86,7 +84,7 @@ bool NzMD2Mesh::Create(const md2_header& header, NzInputStream& stream, const Nz
stream.Read(&frame.name, 16*sizeof(char));
stream.Read(&frame.vertices[0], header.num_vertices*sizeof(md2_vertex));
#if NAZARA_BIG_ENDIAN
#if defined(NAZARA_BIG_ENDIAN)
NzByteSwap(&frame.scale.x, sizeof(float));
NzByteSwap(&frame.scale.y, sizeof(float));
NzByteSwap(&frame.scale.z, sizeof(float));
@@ -98,6 +96,8 @@ bool NzMD2Mesh::Create(const md2_header& header, NzInputStream& stream, const Nz
m_frames[i].normal = new nzUInt8[m_vertexCount]; // Nous stockons l'indice de la normale plutôt que la normale (gain d'espace)
m_frames[i].vertices = new NzVector3f[m_vertexCount];
NzVector3f max, min;
for (unsigned int t = 0; t < header.num_tris; ++t)
{
for (unsigned int v = 0; v < 3; ++v)
@@ -105,17 +105,19 @@ bool NzMD2Mesh::Create(const md2_header& header, NzInputStream& stream, const Nz
const md2_vertex& vert = frame.vertices[triangles[t].vertices[v]];
NzVector3f vertex = rotationMatrix * NzVector3f(vert.x * frame.scale.x + frame.translate.x, vert.y * frame.scale.y + frame.translate.y, vert.z * frame.scale.z + frame.translate.z);
max.MakeCeil(vertex);
min.MakeFloor(vertex);
m_frames[i].normal[t*3+v] = vert.n;
m_frames[i].vertices[t*3+v] = vertex;
}
}
m_frames[i].aabb.SetExtends(min, max);
}
nzBufferStorage storage = (NzBuffer::IsSupported(nzBufferStorage_Hardware) && !parameters.forceSoftware) ? nzBufferStorage_Hardware : nzBufferStorage_Software;
m_indexBuffer = nullptr; // Pas d'indexbuffer pour l'instant
m_vertexBuffer = new NzVertexBuffer(m_vertexCount, (3+3+2)*sizeof(float), storage, nzBufferUsage_Dynamic);
m_vertexBuffer = new NzVertexBuffer(m_vertexCount, (3+3+2)*sizeof(float), parameters.storage, nzBufferUsage_Dynamic);
nzUInt8* ptr = reinterpret_cast<nzUInt8*>(m_vertexBuffer->Map(nzBufferAccess_WriteOnly));
if (!ptr)
@@ -185,6 +187,11 @@ void NzMD2Mesh::Destroy()
}
}
const NzAxisAlignedBox& NzMD2Mesh::GetAABB() const
{
return m_aabb;
}
nzAnimationType NzMD2Mesh::GetAnimationType() const
{
if (m_frameCount > 1)
@@ -262,14 +269,19 @@ void NzMD2Mesh::AnimateImpl(unsigned int frameA, unsigned int frameB, float inte
NzVector3f* position = reinterpret_cast<NzVector3f*>(ptr + positionOffset);
NzVector3f* normal = reinterpret_cast<NzVector3f*>(ptr + normalOffset);
*position = fA->vertices[i] + interpolation * (fB->vertices[i] - fA->vertices[i]);
*normal = md2Normals[fA->normal[i]] + interpolation * (md2Normals[fB->normal[i]] - md2Normals[fA->normal[i]]);
*position = fA->vertices[i] + interpolation * (fB->vertices[i] - fA->vertices[i]);
*normal = md2Normals[fA->normal[i]] + interpolation * (md2Normals[fB->normal[i]] - md2Normals[fA->normal[i]]);
ptr += stride;
}
if (!m_vertexBuffer->Unmap())
NazaraWarning("Failed to unmap vertex buffer, expect mesh corruption");
// Interpolation de l'AABB
NzVector3f max1 = fA->aabb.GetMaximum();
NzVector3f min1 = fA->aabb.GetMinimum();
m_aabb.SetExtends(min1 + interpolation * (fB->aabb.GetMinimum() - min1), max1 + interpolation * (fB->aabb.GetMaximum() - max1));
}
NzVertexDeclaration NzMD2Mesh::s_declaration;

View File

@@ -27,6 +27,7 @@ class NAZARA_API NzMD2Mesh : public NzKeyframeMesh
bool Create(const md2_header& header, NzInputStream& stream, const NzMeshParams& parameters);
void Destroy();
const NzAxisAlignedBox& GetAABB() const;
nzAnimationType GetAnimationType() const;
unsigned int GetFrameCount() const;
const NzIndexBuffer* GetIndexBuffer() const;
@@ -44,14 +45,14 @@ class NAZARA_API NzMD2Mesh : public NzKeyframeMesh
struct Frame
{
//AxisAlignedBox aabb;
NzAxisAlignedBox aabb;
nzUInt8* normal;
NzVector3f* tangents;
NzVector3f* vertices;
char name[16];
};
//AxisAlignedBox m_aabb;
NzAxisAlignedBox m_aabb;
Frame* m_frames;
NzIndexBuffer* m_indexBuffer;
NzVertexBuffer* m_vertexBuffer;

View File

@@ -74,7 +74,7 @@ namespace
return false;
}
#if NAZARA_BIG_ENDIAN
#if defined(NAZARA_BIG_ENDIAN)
// Les fichiers PCX sont en little endian
NzByteSwap(&header.xmin, sizeof(nzUInt16));
NzByteSwap(&header.ymin, sizeof(nzUInt16));

View File

@@ -4,6 +4,7 @@
#include <Nazara/Utility/Mesh.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Utility/Buffer.hpp>
#include <Nazara/Utility/Config.hpp>
#include <Nazara/Utility/SubMesh.hpp>
#include <cstring>
@@ -18,6 +19,12 @@ bool NzMeshParams::IsValid() const
return false;
}
if (!NzBuffer::IsSupported(storage))
{
NazaraError("Storage not supported");
return false;
}
return true;
}
@@ -27,6 +34,7 @@ struct NzMeshImpl
std::deque<NzString> skins;
std::vector<NzSubMesh*> subMeshes;
nzAnimationType animationType;
NzAxisAlignedBox aabb;
const NzAnimation* animation = nullptr;
};
@@ -76,6 +84,8 @@ nzUInt8 NzMesh::AddSubMesh(NzSubMesh* subMesh)
#endif
subMesh->AddResourceReference();
m_impl->aabb.SetNull(); // On invalide l'AABB
m_impl->subMeshes.push_back(subMesh);
return m_impl->subMeshes.size()-1;
@@ -114,6 +124,7 @@ nzUInt8 NzMesh::AddSubMesh(const NzString& identifier, NzSubMesh* subMesh)
subMesh->AddResourceReference();
m_impl->aabb.SetNull(); // On invalide l'AABB
m_impl->subMeshes.push_back(subMesh);
m_impl->subMeshMap[identifier] = index;
@@ -157,6 +168,8 @@ void NzMesh::Animate(unsigned int frameA, unsigned int frameB, float interpolati
for (NzSubMesh* subMesh : m_impl->subMeshes)
subMesh->AnimateImpl(frameA, frameB, interpolation);
m_impl->aabb.SetNull(); // On invalide l'AABB
}
bool NzMesh::Create(nzAnimationType type)
@@ -184,6 +197,25 @@ void NzMesh::Destroy()
}
}
const NzAxisAlignedBox& NzMesh::GetAABB() const
{
#if NAZARA_UTILITY_SAFE
if (!m_impl)
{
NazaraError("Mesh not created");
return NzAxisAlignedBox::Null;
}
#endif
if (m_impl->aabb.IsNull())
{
for (NzSubMesh* subMesh : m_impl->subMeshes)
m_impl->aabb.ExtendTo(subMesh->GetAABB());
}
return m_impl->aabb;
}
const NzAnimation* NzMesh::GetAnimation() const
{
#if NAZARA_UTILITY_SAFE
@@ -373,6 +405,19 @@ unsigned int NzMesh::GetVertexCount() const
return vertexCount;
}
void NzMesh::InvalidateAABB() const
{
#if NAZARA_UTILITY_SAFE
if (!m_impl)
{
NazaraError("Mesh not created");
return;
}
#endif
m_impl->aabb.SetNull();
}
bool NzMesh::HasAnimation() const
{
#if NAZARA_UTILITY_SAFE
@@ -505,6 +550,8 @@ void NzMesh::RemoveSubMesh(const NzString& identifier)
std::advance(it2, index);
m_impl->subMeshes.erase(it2);
m_impl->aabb.SetNull(); // On invalide l'AABB
}
void NzMesh::RemoveSubMesh(nzUInt8 index)
@@ -527,6 +574,8 @@ void NzMesh::RemoveSubMesh(nzUInt8 index)
std::advance(it, index);
m_impl->subMeshes.erase(it);
m_impl->aabb.SetNull(); // On invalide l'AABB
}
bool NzMesh::SetAnimation(const NzAnimation* animation)

View File

@@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Utility/Mouse.hpp>
#include <Nazara/Utility/Window.hpp>
#if defined(NAZARA_PLATFORM_WINDOWS)
#include <Nazara/Utility/Win32/InputImpl.hpp>
@@ -36,6 +37,9 @@ void NzMouse::SetPosition(const NzVector2i& position)
void NzMouse::SetPosition(const NzVector2i& position, const NzWindow& relativeTo)
{
if (position.x > 0 && position.y > 0)
relativeTo.IgnoreNextMouseEvent(position.x, position.y);
NzEventImpl::SetMousePosition(position.x, position.y, relativeTo);
}
@@ -46,5 +50,8 @@ void NzMouse::SetPosition(int x, int y)
void NzMouse::SetPosition(int x, int y, const NzWindow& relativeTo)
{
if (x > 0 && y > 0)
relativeTo.IgnoreNextMouseEvent(x, y);
NzEventImpl::SetMousePosition(x, y, relativeTo);
}

View File

@@ -1320,5 +1320,5 @@ void NzPixelFormat::Uninitialize()
s_flipFunctions[i].clear();
}
NzPixelFormat::ConvertFunction NzPixelFormat::s_convertFunctions[nzPixelFormat_Max+1][nzPixelFormat_Max+1] = {{0}}; ///FIXME: Fonctionne correctement ?
NzPixelFormat::ConvertFunction NzPixelFormat::s_convertFunctions[nzPixelFormat_Max+1][nzPixelFormat_Max+1] = {{nullptr}}; ///FIXME: Fonctionne correctement ?
std::map<nzPixelFormat, NzPixelFormat::FlipFunction> NzPixelFormat::s_flipFunctions[nzPixelFlipping_Max+1];

View File

@@ -1,54 +0,0 @@
// 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/Utility/Resource.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Utility/Config.hpp>
#include <Nazara/Utility/Debug.hpp>
NzResource::NzResource(bool persistent) :
m_resourcePersistent(persistent),
m_resourceReferenceCount(0)
{
}
NzResource::NzResource(const NzResource& resource) :
m_resourcePersistent(resource.m_resourcePersistent),
m_resourceReferenceCount(0)
{
}
NzResource::~NzResource() = default;
void NzResource::AddResourceReference() const
{
m_resourceReferenceCount++;
}
bool NzResource::IsPersistent() const
{
return m_resourcePersistent;
}
void NzResource::RemoveResourceReference() const
{
#if NAZARA_UTILITY_SAFE
if (m_resourceReferenceCount == 0)
{
NazaraError("Impossible to remove reference (Ref. counter is already 0)");
return;
}
#endif
if (--m_resourceReferenceCount == 0 && !m_resourcePersistent)
delete this;
}
void NzResource::SetPersistent(bool persistent)
{
m_resourcePersistent = persistent;
if (!persistent && m_resourceReferenceCount == 0)
delete this;
}

View File

@@ -12,17 +12,17 @@ NzSubMesh(parent)
{
}
NzStaticMesh::NzStaticMesh(const NzMesh* parent, const NzVertexBuffer* vertexBuffer, const NzVertexDeclaration* vertexDeclaration, const NzIndexBuffer* indexBuffer) :
NzStaticMesh::NzStaticMesh(const NzMesh* parent, const NzVertexDeclaration* vertexDeclaration, NzVertexBuffer* vertexBuffer, NzIndexBuffer* indexBuffer) :
NzSubMesh(parent)
{
#ifdef NAZARA_DEBUG
if (!Create(vertexBuffer, vertexDeclaration, indexBuffer))
if (!Create(vertexDeclaration, vertexBuffer, indexBuffer))
{
NazaraError("Failed to create mesh");
throw std::runtime_error("Constructor failed");
}
#else
Create(vertexBuffer, vertexDeclaration, indexBuffer);
Create(vertexDeclaration, vertexBuffer, indexBuffer);
#endif
}
@@ -31,22 +31,22 @@ NzStaticMesh::~NzStaticMesh()
Destroy();
}
bool NzStaticMesh::Create(const NzVertexBuffer* vertexBuffer, const NzVertexDeclaration* vertexDeclaration, const NzIndexBuffer* indexBuffer)
bool NzStaticMesh::Create(const NzVertexDeclaration* vertexDeclaration, NzVertexBuffer* vertexBuffer, NzIndexBuffer* indexBuffer)
{
Destroy();
#if NAZARA_UTILITY_SAFE
if (!vertexBuffer)
{
NazaraError("Vertex buffer is null");
return false;
}
if (!vertexDeclaration)
{
NazaraError("Vertex declaration is null");
return false;
}
if (!vertexBuffer)
{
NazaraError("Vertex buffer is null");
return false;
}
#endif
if (indexBuffer)
@@ -66,6 +66,8 @@ bool NzStaticMesh::Create(const NzVertexBuffer* vertexBuffer, const NzVertexDecl
void NzStaticMesh::Destroy()
{
m_aabb.SetNull();
if (m_indexBuffer)
{
m_indexBuffer->RemoveResourceReference();
@@ -85,6 +87,44 @@ void NzStaticMesh::Destroy()
}
}
bool NzStaticMesh::GenerateAABB()
{
if (!m_aabb.IsNull())
return true;
const NzVertexElement* position = m_vertexDeclaration->GetElement(nzElementStream_VertexData, nzElementUsage_Position);
if (position && position->type == nzElementType_Float3) // Si nous avons des positions du type Vec3
{
// On lock le buffer pour itérer sur toutes les positions et composer notre AABB
nzUInt8* buffer = reinterpret_cast<nzUInt8*>(m_vertexBuffer->Map(nzBufferAccess_ReadOnly));
if (!buffer)
{
NazaraWarning("Failed to lock vertex buffer");
return false;
}
buffer += position->offset;
unsigned int stride = m_vertexDeclaration->GetStride(nzElementStream_VertexData);
unsigned int vertexCount = m_vertexBuffer->GetVertexCount();
for (unsigned int i = 0; i < vertexCount; ++i)
{
m_aabb.ExtendTo(*reinterpret_cast<NzVector3f*>(buffer));
buffer += stride;
}
if (!m_vertexBuffer->Unmap())
NazaraWarning("Failed to unmap vertex buffer");
}
return true;
}
const NzAxisAlignedBox& NzStaticMesh::GetAABB() const
{
return m_aabb;
}
nzAnimationType NzStaticMesh::GetAnimationType() const
{
return nzAnimationType_Static;
@@ -125,6 +165,11 @@ bool NzStaticMesh::IsValid() const
return m_vertexBuffer != nullptr && m_vertexDeclaration != nullptr;
}
void NzStaticMesh::SetAABB(const NzAxisAlignedBox& aabb)
{
m_aabb = aabb;
}
void NzStaticMesh::SetPrimitiveType(nzPrimitiveType primitiveType)
{
m_primitiveType = primitiveType;

View File

@@ -14,7 +14,7 @@ NzSubMesh::NzSubMesh(const NzMesh* parent) :
NzResource(false), // Un SubMesh n'est pas persistant par défaut
m_parent(parent)
{
#if NAZARA_DEBUG
#ifdef NAZARA_DEBUG
if (!m_parent)
{
NazaraError("Parent mesh is null");
@@ -55,6 +55,8 @@ void NzSubMesh::Animate(unsigned int frameA, unsigned int frameB, float interpol
#endif
AnimateImpl(frameA, frameB, interpolation);
m_parent->InvalidateAABB();
}
const NzMesh* NzSubMesh::GetParent() const

View File

@@ -3,7 +3,9 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Utility/Utility.hpp>
#include <Nazara/Core/Core.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/Log.hpp>
#include <Nazara/Utility/Buffer.hpp>
#include <Nazara/Utility/Config.hpp>
#include <Nazara/Utility/Loaders/MD2.hpp>
@@ -13,26 +15,19 @@
#include <Nazara/Utility/Window.hpp>
#include <Nazara/Utility/Debug.hpp>
NzUtility::NzUtility()
{
}
NzUtility::~NzUtility()
{
if (s_initialized)
Uninitialize();
}
bool NzUtility::Initialize()
{
#if NAZARA_UTILITY_SAFE
if (s_initialized)
{
NazaraError("Renderer already initialized");
return true;
}
#endif
if (s_moduleReferenceCouter++ != 0)
return true; // Déjà initialisé
// Initialisation des dépendances
if (!NzCore::Initialize())
{
NazaraError("Failed to initialize core module");
return false;
}
// Initialisation du module
if (!NzBuffer::Initialize())
{
NazaraError("Failed to initialize buffers");
@@ -64,21 +59,22 @@ bool NzUtility::Initialize()
// Image
NzLoaders_STB_Register(); // Loader générique (STB)
s_initialized = true;
NazaraNotice("Initialized: Utility module");
return true;
}
bool NzUtility::IsInitialized()
{
return s_moduleReferenceCouter != 0;
}
void NzUtility::Uninitialize()
{
#if NAZARA_UTILITY_SAFE
if (!s_initialized)
{
NazaraError("Utility not initialized");
return;
}
#endif
if (--s_moduleReferenceCouter != 0)
return; // Encore utili
// Libération du module
NzLoaders_MD2_Unregister();
NzLoaders_PCX_Unregister();
NzLoaders_STB_Unregister();
@@ -87,12 +83,11 @@ void NzUtility::Uninitialize()
NzPixelFormat::Uninitialize();
NzBuffer::Uninitialize();
s_initialized = false;
NazaraNotice("Uninitialized: Utility module");
// Libération des dépendances
NzCore::Uninitialize();
}
bool NzUtility::IsInitialized()
{
return s_initialized;
}
unsigned int NzUtility::s_moduleReferenceCouter = 0;
bool NzUtility::s_initialized = false;

View File

@@ -10,7 +10,7 @@
#include <stdexcept>
#include <vector>
#if NAZARA_THREADSAFETY_VERTEXDECLARATION
#if NAZARA_UTILITY_THREADSAFE && NAZARA_THREADSAFETY_VERTEXDECLARATION
#include <Nazara/Core/ThreadSafety.hpp>
#else
#include <Nazara/Core/ThreadSafetyOff.hpp>
@@ -22,15 +22,15 @@ namespace
{
const unsigned int size[] =
{
4, // nzElementType_Color
8, // nzElementType_Double1
16, // nzElementType_Double2
24, // nzElementType_Double3
32, // nzElementType_Double4
4, // nzElementType_Float1
8, // nzElementType_Float2
12, // nzElementType_Float3
16 // nzElementType_Float4
sizeof(nzUInt32), // nzElementType_Color
1*sizeof(double), // nzElementType_Double1
2*sizeof(double), // nzElementType_Double2
3*sizeof(double), // nzElementType_Double3
4*sizeof(double), // nzElementType_Double4
1*sizeof(float), // nzElementType_Float1
2*sizeof(float), // nzElementType_Float2
3*sizeof(float), // nzElementType_Float3
4*sizeof(float) // nzElementType_Float4
};
bool VertexElementCompare(const NzVertexElement& elementA, const NzVertexElement& elementB)
@@ -57,7 +57,7 @@ struct NzVertexDeclarationImpl
int streamPos[nzElementStream_Max+1];
unsigned int stride[nzElementStream_Max+1] = {0};
unsigned short refCount;
unsigned short refCount = 1;
NazaraMutex(mutex)
};
@@ -86,7 +86,7 @@ m_sharedImpl(declaration.m_sharedImpl)
}
}
NzVertexDeclaration::NzVertexDeclaration(NzVertexDeclaration&& declaration) :
NzVertexDeclaration::NzVertexDeclaration(NzVertexDeclaration&& declaration) noexcept :
m_sharedImpl(declaration.m_sharedImpl)
{
declaration.m_sharedImpl = nullptr;
@@ -146,7 +146,7 @@ bool NzVertexDeclaration::Create(const NzVertexElement* elements, unsigned int e
impl->stride[current.stream] += size[current.type];
}
#if NAZARA_RENDERER_FORCE_DECLARATION_STRIDE_MULTIPLE_OF_32
#if NAZARA_UTILITY_FORCE_DECLARATION_STRIDE_MULTIPLE_OF_32
for (unsigned int& stride : impl->stride)
stride = ((static_cast<int>(stride)-1)/32+1)*32;
#endif
@@ -335,7 +335,7 @@ NzVertexDeclaration& NzVertexDeclaration::operator=(const NzVertexDeclaration& d
return *this;
}
NzVertexDeclaration& NzVertexDeclaration::operator=(NzVertexDeclaration&& declaration)
NzVertexDeclaration& NzVertexDeclaration::operator=(NzVertexDeclaration&& declaration) noexcept
{
Destroy();

View File

@@ -192,7 +192,7 @@ bool NzEventImpl::IsKeyPressed(NzKeyboard::Key key)
bool NzEventImpl::IsMouseButtonPressed(NzMouse::Button button)
{
static int vButtons[NzMouse::Count] = {
static int vButtons[NzMouse::Max+1] = {
VK_LBUTTON, // Button::Left
VK_MBUTTON, // Button::Middle
VK_RBUTTON, // Button::Right

View File

@@ -7,10 +7,10 @@
#define OEMRESOURCE
#include <Nazara/Utility/Win32/WindowImpl.hpp>
#include <Nazara/Core/ConditionVariable.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/Mutex.hpp>
#include <Nazara/Core/Thread.hpp>
#include <Nazara/Core/ThreadCondition.hpp>
#include <Nazara/Utility/Config.hpp>
#include <Nazara/Utility/Cursor.hpp>
#include <Nazara/Utility/Image.hpp>
@@ -53,28 +53,6 @@ m_scrolling(0)
{
}
void NzWindowImpl::Close()
{
if (m_ownsWindow)
{
#if NAZARA_UTILITY_THREADED_WINDOW
if (m_thread)
{
m_threadActive = false;
PostMessageW(m_handle, WM_NULL, 0, 0); // Pour réveiller le thread
m_thread->Join();
delete m_thread;
}
#else
if (m_handle)
DestroyWindow(m_handle);
#endif
}
else
SetEventListener(false);
}
bool NzWindowImpl::Create(NzVideoMode mode, const NzString& title, nzUInt32 style)
{
bool fullscreen = (style & nzWindowStyle_Fullscreen) != 0;
@@ -99,6 +77,7 @@ bool NzWindowImpl::Create(NzVideoMode mode, const NzString& title, nzUInt32 styl
}
}
// Testé une seconde fois car sa valeur peut changer
if (fullscreen)
{
x = 0;
@@ -143,7 +122,7 @@ bool NzWindowImpl::Create(NzVideoMode mode, const NzString& title, nzUInt32 styl
#if NAZARA_UTILITY_THREADED_WINDOW
NzMutex mutex;
NzThreadCondition condition;
NzConditionVariable condition;
m_thread = new NzThread(WindowThread, &m_handle, win32StyleEx, wtitle, win32Style, x, y, width, height, this, &mutex, &condition);
m_threadActive = true;
@@ -166,6 +145,9 @@ bool NzWindowImpl::Create(NzVideoMode mode, const NzString& title, nzUInt32 styl
m_eventListener = true;
m_ownsWindow = true;
#if !NAZARA_UTILITY_THREADED_WINDOW
m_sizemove = false;
#endif
return m_handle != nullptr;
}
@@ -181,10 +163,35 @@ bool NzWindowImpl::Create(NzWindowHandle handle)
m_handle = reinterpret_cast<HWND>(handle);
m_eventListener = false;
m_ownsWindow = false;
#if !NAZARA_UTILITY_THREADED_WINDOW
m_sizemove = false;
#endif
return true;
}
void NzWindowImpl::Destroy()
{
if (m_ownsWindow)
{
#if NAZARA_UTILITY_THREADED_WINDOW
if (m_thread)
{
m_threadActive = false;
PostMessageW(m_handle, WM_NULL, 0, 0); // Pour réveiller le thread
m_thread->Join();
delete m_thread;
}
#else
if (m_handle)
DestroyWindow(m_handle);
#endif
}
else
SetEventListener(false);
}
void NzWindowImpl::EnableKeyRepeat(bool enable)
{
m_keyRepeat = enable;
@@ -267,6 +274,13 @@ void NzWindowImpl::ProcessEvents(bool block)
}
}
void NzWindowImpl::IgnoreNextMouseEvent(int mouseX, int mouseY)
{
// Petite astuce ...
m_mousePos.x = mouseX;
m_mousePos.y = mouseY;
}
bool NzWindowImpl::IsMinimized() const
{
return IsIconic(m_handle);
@@ -317,12 +331,12 @@ void NzWindowImpl::SetCursor(nzWindowCursor cursor)
case nzWindowCursor_ResizeNW:
case nzWindowCursor_ResizeSE:
m_cursor = reinterpret_cast<HCURSOR>(LoadImage(nullptr, IDC_SIZENESW, IMAGE_CURSOR, 0, 0, LR_SHARED));
m_cursor = reinterpret_cast<HCURSOR>(LoadImage(nullptr, IDC_SIZENWSE, IMAGE_CURSOR, 0, 0, LR_SHARED));
break;
case nzWindowCursor_ResizeNE:
case nzWindowCursor_ResizeSW:
m_cursor = reinterpret_cast<HCURSOR>(LoadImage(nullptr, IDC_SIZENWSE, IMAGE_CURSOR, 0, 0, LR_SHARED));
m_cursor = reinterpret_cast<HCURSOR>(LoadImage(nullptr, IDC_SIZENESW, IMAGE_CURSOR, 0, 0, LR_SHARED));
break;
case nzWindowCursor_ResizeE:
@@ -418,7 +432,7 @@ void NzWindowImpl::SetMinimumSize(int width, int height)
void NzWindowImpl::SetPosition(int x, int y)
{
SetWindowPos(m_handle, 0, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
SetWindowPos(m_handle, nullptr, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
}
void NzWindowImpl::SetSize(unsigned int width, unsigned int height)
@@ -427,7 +441,15 @@ void NzWindowImpl::SetSize(unsigned int width, unsigned int height)
RECT rect = {0, 0, static_cast<LONG>(width), static_cast<LONG>(height)};
AdjustWindowRect(&rect, GetWindowLongPtr(m_handle, GWL_STYLE), false);
SetWindowPos(m_handle, 0, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOMOVE | SWP_NOZORDER);
SetWindowPos(m_handle, nullptr, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOMOVE | SWP_NOZORDER);
}
void NzWindowImpl::SetStayOnTop(bool stayOnTop)
{
if (stayOnTop)
SetWindowPos(m_handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
else
SetWindowPos(m_handle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
void NzWindowImpl::SetTitle(const NzString& title)
@@ -442,11 +464,6 @@ void NzWindowImpl::SetVisible(bool visible)
ShowWindow(m_handle, (visible) ? SW_SHOW : SW_HIDE);
}
void NzWindowImpl::StayOnTop(bool stayOnTop)
{
SetWindowPos(m_handle, (stayOnTop) ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
}
bool NzWindowImpl::HandleMessage(HWND window, UINT message, WPARAM wParam, LPARAM lParam)
{
// Inutile de récupérer des évènements ne venant pas de notre fenêtre
@@ -495,11 +512,13 @@ bool NzWindowImpl::HandleMessage(HWND window, UINT message, WPARAM wParam, LPARA
case WM_CHAR:
{
// http://msdn.microsoft.com/en-us/library/ms646267(VS.85).aspx
if (m_keyRepeat || (HIWORD(lParam) & KF_REPEAT) == 0)
bool repeated = ((HIWORD(lParam) & KF_REPEAT) != 0);
if (m_keyRepeat || !repeated)
{
NzEvent event;
event.type = NzEvent::TextEntered;
event.text.character = static_cast<nzUInt32>(wParam);
event.type = nzEventType_TextEntered;
event.text.character = static_cast<char32_t>(wParam);
event.text.repeated = repeated;
m_parent->PushEvent(event);
}
@@ -509,65 +528,64 @@ bool NzWindowImpl::HandleMessage(HWND window, UINT message, WPARAM wParam, LPARA
case WM_CLOSE:
{
NzEvent event;
event.type = NzEvent::Quit;
event.type = nzEventType_Quit;
m_parent->PushEvent(event);
return true; // Afin que Windows ne ferme pas la fenêtre automatiquement
}
case WM_LBUTTONDBLCLK:
#if !NAZARA_UTILITY_THREADED_WINDOW
case WM_ENTERSIZEMOVE:
{
// Cet évènement est généré à la place d'un WM_LBUTTONDOWN lors d'un double-clic.
// Comme nous désirons quand même notifier chaque clic, nous envoyons les deux évènements.
NzEvent event;
event.mouseButton.button = NzMouse::Left;
event.mouseButton.x = GET_X_LPARAM(lParam);
event.mouseButton.y = GET_Y_LPARAM(lParam);
event.type = NzEvent::MouseButtonDoubleClicked;
m_parent->PushEvent(event);
event.type = NzEvent::MouseButtonPressed;
m_parent->PushEvent(event);
m_sizemove = true;
break;
}
case WM_LBUTTONDOWN:
case WM_EXITSIZEMOVE:
{
NzEvent event;
event.type = NzEvent::MouseButtonPressed;
event.mouseButton.button = NzMouse::Left;
event.mouseButton.x = GET_X_LPARAM(lParam);
event.mouseButton.y = GET_Y_LPARAM(lParam);
m_parent->PushEvent(event);
m_sizemove = false;
break;
// On vérifie ce qui a changé
NzVector2i position = GetPosition();
if (m_position != position)
{
m_position = position;
NzEvent event;
event.type = nzEventType_Moved;
event.position.x = position.x;
event.position.y = position.y;
m_parent->PushEvent(event);
}
NzVector2ui size = GetSize();
if (m_size != size)
{
m_size = size;
NzEvent event;
event.type = nzEventType_Resized;
event.size.width = size.x;
event.size.height = size.y;
m_parent->PushEvent(event);
}
}
case WM_LBUTTONUP:
{
NzEvent event;
event.type = NzEvent::MouseButtonReleased;
event.mouseButton.button = NzMouse::Left;
event.mouseButton.x = GET_X_LPARAM(lParam);
event.mouseButton.y = GET_Y_LPARAM(lParam);
m_parent->PushEvent(event);
break;
}
#endif
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
{
// http://msdn.microsoft.com/en-us/library/ms646267(VS.85).aspx
if (m_keyRepeat || (HIWORD(lParam) & KF_REPEAT) == 0)
bool repeated = ((HIWORD(lParam) & KF_REPEAT) != 0);
if (m_keyRepeat || !repeated)
{
NzEvent event;
event.type = NzEvent::KeyPressed;
event.type = nzEventType_KeyPressed;
event.key.code = ConvertVirtualKey(wParam, lParam);
event.key.alt = ((GetAsyncKeyState(VK_MENU) & 0x8000) != 0);
event.key.control = ((GetAsyncKeyState(VK_CONTROL) & 0x8000) != 0);
event.key.repeated = repeated;
event.key.shift = ((GetAsyncKeyState(VK_SHIFT) & 0x8000) != 0);
event.key.system = (((GetAsyncKeyState(VK_LWIN) & 0x8000) != 0) || ((GetAsyncKeyState(VK_RWIN) & 0x8000) != 0));
m_parent->PushEvent(event);
@@ -581,7 +599,7 @@ bool NzWindowImpl::HandleMessage(HWND window, UINT message, WPARAM wParam, LPARA
{
// http://msdn.microsoft.com/en-us/library/ms646267(VS.85).aspx
NzEvent event;
event.type = NzEvent::KeyReleased;
event.type = nzEventType_KeyReleased;
event.key.code = ConvertVirtualKey(wParam, lParam);
event.key.alt = ((GetAsyncKeyState(VK_MENU) & 0x8000) != 0);
event.key.control = ((GetAsyncKeyState(VK_CONTROL) & 0x8000) != 0);
@@ -595,7 +613,49 @@ bool NzWindowImpl::HandleMessage(HWND window, UINT message, WPARAM wParam, LPARA
case WM_KILLFOCUS:
{
NzEvent event;
event.type = NzEvent::LostFocus;
event.type = nzEventType_LostFocus;
m_parent->PushEvent(event);
break;
}
case WM_LBUTTONDBLCLK:
{
// Cet évènement est généré à la place d'un WM_LBUTTONDOWN lors d'un double-clic.
// Comme nous désirons quand même notifier chaque clic, nous envoyons les deux évènements.
NzEvent event;
event.mouseButton.button = NzMouse::Left;
event.mouseButton.x = GET_X_LPARAM(lParam);
event.mouseButton.y = GET_Y_LPARAM(lParam);
event.type = nzEventType_MouseButtonDoubleClicked;
m_parent->PushEvent(event);
event.type = nzEventType_MouseButtonPressed;
m_parent->PushEvent(event);
break;
}
case WM_LBUTTONDOWN:
{
NzEvent event;
event.type = nzEventType_MouseButtonPressed;
event.mouseButton.button = NzMouse::Left;
event.mouseButton.x = GET_X_LPARAM(lParam);
event.mouseButton.y = GET_Y_LPARAM(lParam);
m_parent->PushEvent(event);
break;
}
case WM_LBUTTONUP:
{
NzEvent event;
event.type = nzEventType_MouseButtonReleased;
event.mouseButton.button = NzMouse::Left;
event.mouseButton.x = GET_X_LPARAM(lParam);
event.mouseButton.y = GET_Y_LPARAM(lParam);
m_parent->PushEvent(event);
break;
@@ -608,10 +668,10 @@ bool NzWindowImpl::HandleMessage(HWND window, UINT message, WPARAM wParam, LPARA
event.mouseButton.x = GET_X_LPARAM(lParam);
event.mouseButton.y = GET_Y_LPARAM(lParam);
event.type = NzEvent::MouseButtonDoubleClicked;
event.type = nzEventType_MouseButtonDoubleClicked;
m_parent->PushEvent(event);
event.type = NzEvent::MouseButtonPressed;
event.type = nzEventType_MouseButtonPressed;
m_parent->PushEvent(event);
break;
@@ -620,7 +680,7 @@ bool NzWindowImpl::HandleMessage(HWND window, UINT message, WPARAM wParam, LPARA
case WM_MBUTTONDOWN:
{
NzEvent event;
event.type = NzEvent::MouseButtonPressed;
event.type = nzEventType_MouseButtonPressed;
event.mouseButton.button = NzMouse::Middle;
event.mouseButton.x = GET_X_LPARAM(lParam);
event.mouseButton.y = GET_Y_LPARAM(lParam);
@@ -632,7 +692,7 @@ bool NzWindowImpl::HandleMessage(HWND window, UINT message, WPARAM wParam, LPARA
case WM_MBUTTONUP:
{
NzEvent event;
event.type = NzEvent::MouseButtonReleased;
event.type = nzEventType_MouseButtonReleased;
event.mouseButton.button = NzMouse::Middle;
event.mouseButton.x = GET_X_LPARAM(lParam);
event.mouseButton.y = GET_Y_LPARAM(lParam);
@@ -648,13 +708,16 @@ bool NzWindowImpl::HandleMessage(HWND window, UINT message, WPARAM wParam, LPARA
m_mouseInside = false;
NzEvent event;
event.type = NzEvent::MouseLeft;
event.type = nzEventType_MouseLeft;
m_parent->PushEvent(event);
break;
}
case WM_MOUSEMOVE:
{
int currentX = GET_X_LPARAM(lParam);
int currentY = GET_Y_LPARAM(lParam);
if (!m_mouseInside)
{
m_mouseInside = true;
@@ -667,14 +730,37 @@ bool NzWindowImpl::HandleMessage(HWND window, UINT message, WPARAM wParam, LPARA
TrackMouseEvent(&mouseEvent);
NzEvent event;
event.type = NzEvent::MouseEntered;
event.type = nzEventType_MouseEntered;
m_parent->PushEvent(event);
event.type = nzEventType_MouseMoved;
// Le delta sera 0
event.mouseMove.deltaX = 0;
event.mouseMove.deltaY = 0;
event.mouseMove.x = currentX;
event.mouseMove.y = currentY;
m_mousePos.x = currentX;
m_mousePos.y = currentY;
m_parent->PushEvent(event);
break;
}
else if (m_mousePos.x == currentX && m_mousePos.y == currentY)
break;
NzEvent event;
event.type = NzEvent::MouseMoved;
event.mouseMove.x = GET_X_LPARAM(lParam);
event.mouseMove.y = GET_Y_LPARAM(lParam);
event.type = nzEventType_MouseMoved;
event.mouseMove.deltaX = currentX - m_mousePos.x;
event.mouseMove.deltaY = currentY - m_mousePos.y;
event.mouseMove.x = currentX;
event.mouseMove.y = currentY;
m_mousePos.x = currentX;
m_mousePos.y = currentY;
m_parent->PushEvent(event);
break;
}
@@ -684,7 +770,7 @@ bool NzWindowImpl::HandleMessage(HWND window, UINT message, WPARAM wParam, LPARA
if (m_smoothScrolling)
{
NzEvent event;
event.type = NzEvent::MouseWheelMoved;
event.type = nzEventType_MouseWheelMoved;
event.mouseWheel.delta = static_cast<float>(GET_WHEEL_DELTA_WPARAM(wParam))/WHEEL_DELTA;
m_parent->PushEvent(event);
}
@@ -694,7 +780,7 @@ bool NzWindowImpl::HandleMessage(HWND window, UINT message, WPARAM wParam, LPARA
if (std::abs(m_scrolling) >= WHEEL_DELTA)
{
NzEvent event;
event.type = NzEvent::MouseWheelMoved;
event.type = nzEventType_MouseWheelMoved;
event.mouseWheel.delta = m_scrolling/WHEEL_DELTA;
m_parent->PushEvent(event);
@@ -709,7 +795,7 @@ bool NzWindowImpl::HandleMessage(HWND window, UINT message, WPARAM wParam, LPARA
NzVector2i position = GetPosition();
NzEvent event;
event.type = NzEvent::Moved;
event.type = nzEventType_Moved;
event.position.x = position.x;
event.position.y = position.y;
m_parent->PushEvent(event);
@@ -724,10 +810,10 @@ bool NzWindowImpl::HandleMessage(HWND window, UINT message, WPARAM wParam, LPARA
event.mouseButton.x = GET_X_LPARAM(lParam);
event.mouseButton.y = GET_Y_LPARAM(lParam);
event.type = NzEvent::MouseButtonDoubleClicked;
event.type = nzEventType_MouseButtonDoubleClicked;
m_parent->PushEvent(event);
event.type = NzEvent::MouseButtonPressed;
event.type = nzEventType_MouseButtonPressed;
m_parent->PushEvent(event);
break;
@@ -736,7 +822,7 @@ bool NzWindowImpl::HandleMessage(HWND window, UINT message, WPARAM wParam, LPARA
case WM_RBUTTONDOWN:
{
NzEvent event;
event.type = NzEvent::MouseButtonPressed;
event.type = nzEventType_MouseButtonPressed;
event.mouseButton.button = NzMouse::Right;
event.mouseButton.x = GET_X_LPARAM(lParam);
event.mouseButton.y = GET_Y_LPARAM(lParam);
@@ -748,7 +834,7 @@ bool NzWindowImpl::HandleMessage(HWND window, UINT message, WPARAM wParam, LPARA
case WM_RBUTTONUP:
{
NzEvent event;
event.type = NzEvent::MouseButtonReleased;
event.type = nzEventType_MouseButtonReleased;
event.mouseButton.button = NzMouse::Right;
event.mouseButton.x = GET_X_LPARAM(lParam);
event.mouseButton.y = GET_Y_LPARAM(lParam);
@@ -760,7 +846,7 @@ bool NzWindowImpl::HandleMessage(HWND window, UINT message, WPARAM wParam, LPARA
case WM_SETFOCUS:
{
NzEvent event;
event.type = NzEvent::GainedFocus;
event.type = nzEventType_GainedFocus;
m_parent->PushEvent(event);
break;
@@ -768,12 +854,22 @@ bool NzWindowImpl::HandleMessage(HWND window, UINT message, WPARAM wParam, LPARA
case WM_SIZE:
{
#if NAZARA_UTILITY_THREADED_WINDOW
if (wParam != SIZE_MINIMIZED)
#else
if (!m_sizemove && wParam != SIZE_MINIMIZED)
#endif
{
NzVector2ui size = GetSize(); // On récupère uniquement la taille de la zone client
#if !NAZARA_UTILITY_THREADED_WINDOW
if (m_size == size)
break;
m_size = size;
#endif
NzEvent event;
event.type = NzEvent::Resized;
event.type = nzEventType_Resized;
event.size.width = size.x;
event.size.height = size.y;
m_parent->PushEvent(event);
@@ -792,10 +888,10 @@ bool NzWindowImpl::HandleMessage(HWND window, UINT message, WPARAM wParam, LPARA
event.mouseButton.x = GET_X_LPARAM(lParam);
event.mouseButton.y = GET_Y_LPARAM(lParam);
event.type = NzEvent::MouseButtonDoubleClicked;
event.type = nzEventType_MouseButtonDoubleClicked;
m_parent->PushEvent(event);
event.type = NzEvent::MouseButtonPressed;
event.type = nzEventType_MouseButtonPressed;
m_parent->PushEvent(event);
break;
@@ -804,6 +900,8 @@ bool NzWindowImpl::HandleMessage(HWND window, UINT message, WPARAM wParam, LPARA
case WM_XBUTTONDOWN:
{
NzEvent event;
event.type = nzEventType_MouseButtonPressed;
if (HIWORD(wParam) == XBUTTON1)
event.mouseButton.button = NzMouse::XButton1;
else
@@ -819,6 +917,8 @@ bool NzWindowImpl::HandleMessage(HWND window, UINT message, WPARAM wParam, LPARA
case WM_XBUTTONUP:
{
NzEvent event;
event.type = nzEventType_MouseButtonReleased;
if (HIWORD(wParam) == XBUTTON1)
event.mouseButton.button = NzMouse::XButton1;
else
@@ -1018,7 +1118,7 @@ NzKeyboard::Key NzWindowImpl::ConvertVirtualKey(WPARAM key, LPARAM flags)
}
#if NAZARA_UTILITY_THREADED_WINDOW
void NzWindowImpl::WindowThread(HWND* handle, DWORD styleEx, const wchar_t* title, DWORD style, unsigned int x, unsigned int y, unsigned int width, unsigned int height, NzWindowImpl* window, NzMutex* mutex, NzThreadCondition* condition)
void NzWindowImpl::WindowThread(HWND* handle, DWORD styleEx, const wchar_t* title, DWORD style, unsigned int x, unsigned int y, unsigned int width, unsigned int height, NzWindowImpl* window, NzMutex* mutex, NzConditionVariable* condition)
{
*handle = CreateWindowExW(styleEx, className, title, style, x, y, width, height, nullptr, nullptr, GetModuleHandle(nullptr), window);

View File

@@ -20,9 +20,9 @@
#include <windows.h>
#if NAZARA_UTILITY_THREADED_WINDOW
class NzConditionVariable;
class NzMutex;
class NzThread;
class NzThreadCondition;
#endif
class NzWindow;
@@ -34,11 +34,11 @@ class NzWindowImpl : NzNonCopyable
NzWindowImpl(NzWindow* parent);
~NzWindowImpl() = default;
void Close();
bool Create(NzVideoMode mode, const NzString& title, nzUInt32 style);
bool Create(NzWindowHandle handle);
void Destroy();
void EnableKeyRepeat(bool enable);
void EnableSmoothScrolling(bool enable);
@@ -53,6 +53,8 @@ class NzWindowImpl : NzNonCopyable
void ProcessEvents(bool block);
void IgnoreNextMouseEvent(int mouseX, int mouseY);
bool IsMinimized() const;
bool IsVisible() const;
@@ -65,11 +67,10 @@ class NzWindowImpl : NzNonCopyable
void SetMinimumSize(int width, int height);
void SetPosition(int x, int y);
void SetSize(unsigned int width, unsigned int height);
void SetStayOnTop(bool stayOnTop);
void SetTitle(const NzString& title);
void SetVisible(bool visible);
void StayOnTop(bool stayOnTop);
static bool Initialize();
static void Uninitialize();
@@ -79,7 +80,7 @@ class NzWindowImpl : NzNonCopyable
static LRESULT CALLBACK MessageHandler(HWND window, UINT message, WPARAM wParam, LPARAM lParam);
static NzKeyboard::Key ConvertVirtualKey(WPARAM key, LPARAM flags);
#if NAZARA_UTILITY_THREADED_WINDOW
static void WindowThread(HWND* handle, DWORD styleEx, const wchar_t* title, DWORD style, unsigned int x, unsigned int y, unsigned int width, unsigned int height, NzWindowImpl* window, NzMutex* mutex, NzThreadCondition* condition);
static void WindowThread(HWND* handle, DWORD styleEx, const wchar_t* title, DWORD style, unsigned int x, unsigned int y, unsigned int width, unsigned int height, NzWindowImpl* window, NzMutex* mutex, NzConditionVariable* condition);
#endif
HCURSOR m_cursor;
@@ -87,7 +88,11 @@ class NzWindowImpl : NzNonCopyable
LONG_PTR m_callback;
NzVector2i m_maxSize;
NzVector2i m_minSize;
#if NAZARA_UTILITY_THREADED_WINDOW
NzVector2i m_mousePos;
#if !NAZARA_UTILITY_THREADED_WINDOW
NzVector2i m_position;
NzVector2ui m_size;
#else
NzThread* m_thread;
#endif
NzWindow* m_parent;
@@ -95,6 +100,9 @@ class NzWindowImpl : NzNonCopyable
bool m_keyRepeat;
bool m_mouseInside;
bool m_ownsWindow;
#if !NAZARA_UTILITY_THREADED_WINDOW
bool m_sizemove;
#endif
bool m_smoothScrolling;
#if NAZARA_UTILITY_THREADED_WINDOW
bool m_threadActive;

View File

@@ -82,32 +82,18 @@ m_impl(nullptr)
NzWindow::~NzWindow()
{
Close();
}
void NzWindow::Close()
{
if (m_impl)
{
OnClose();
m_impl->Close();
delete m_impl;
m_impl = nullptr;
if (fullscreenWindow == this)
fullscreenWindow = nullptr;
}
Destroy();
}
bool NzWindow::Create(NzVideoMode mode, const NzString& title, nzUInt32 style)
{
// Si la fenêtre est déjà ouverte, nous conservons sa position
bool opened = IsOpen();
NzVector2i position;
if (opened)
position = m_impl->GetPosition();
Close();
Destroy();
// Inspiré du code de la SFML par Laurent Gomila
if (style & nzWindowStyle_Fullscreen)
@@ -143,7 +129,7 @@ bool NzWindow::Create(NzVideoMode mode, const NzString& title, nzUInt32 style)
m_ownsWindow = true;
if (!OnCreate())
if (!OnWindowCreated())
{
NazaraError("Failed to initialize window extension");
delete m_impl;
@@ -152,6 +138,7 @@ bool NzWindow::Create(NzVideoMode mode, const NzString& title, nzUInt32 style)
return false;
}
// Paramètres par défaut
m_impl->EnableKeyRepeat(true);
m_impl->EnableSmoothScrolling(false);
m_impl->SetCursor(nzWindowCursor_Default);
@@ -167,7 +154,7 @@ bool NzWindow::Create(NzVideoMode mode, const NzString& title, nzUInt32 style)
bool NzWindow::Create(NzWindowHandle handle)
{
Close();
Destroy();
m_impl = new NzWindowImpl(this);
if (!m_impl->Create(handle))
@@ -181,7 +168,7 @@ bool NzWindow::Create(NzWindowHandle handle)
m_ownsWindow = false;
if (!OnCreate())
if (!OnWindowCreated())
{
NazaraError("Failed to initialize window's derivate");
delete m_impl;
@@ -193,6 +180,21 @@ bool NzWindow::Create(NzWindowHandle handle)
return true;
}
void NzWindow::Destroy()
{
if (m_impl)
{
OnWindowDestroying();
m_impl->Destroy();
delete m_impl;
m_impl = nullptr;
if (fullscreenWindow == this)
fullscreenWindow = nullptr;
}
}
void NzWindow::EnableKeyRepeat(bool enable)
{
if (m_impl)
@@ -210,7 +212,7 @@ NzWindowHandle NzWindow::GetHandle() const
if (m_impl)
return m_impl->GetHandle();
else
return 0;
return reinterpret_cast<NzWindowHandle>(0);
}
unsigned int NzWindow::GetHeight() const
@@ -420,6 +422,12 @@ void NzWindow::SetSize(unsigned int width, unsigned int height)
m_impl->SetSize(width, height);
}
void NzWindow::SetStayOnTop(bool stayOnTop)
{
if (m_impl)
m_impl->SetStayOnTop(stayOnTop);
}
void NzWindow::SetTitle(const NzString& title)
{
if (m_impl)
@@ -432,12 +440,6 @@ void NzWindow::SetVisible(bool visible)
m_impl->SetVisible(visible);
}
void NzWindow::StayOnTop(bool stayOnTop)
{
if (m_impl)
m_impl->StayOnTop(stayOnTop);
}
bool NzWindow::WaitEvent(NzEvent* event)
{
if (!m_impl)
@@ -481,15 +483,21 @@ bool NzWindow::WaitEvent(NzEvent* event)
#endif
}
void NzWindow::OnClose()
void NzWindow::OnWindowDestroying()
{
}
bool NzWindow::OnCreate()
bool NzWindow::OnWindowCreated()
{
return true;
}
void NzWindow::IgnoreNextMouseEvent(int mouseX, int mouseY) const
{
if (m_impl)
m_impl->IgnoreNextMouseEvent(mouseX, mouseY);
}
void NzWindow::PushEvent(const NzEvent& event)
{
#if NAZARA_UTILITY_THREADED_WINDOW