diff --git a/CMakeLists.txt b/CMakeLists.txt index 48ac10c..f26a3e1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -194,7 +194,6 @@ set( HEADERS src/lightmap/worker.h src/lightmap/collision.h src/lightmap/halffloat.h - src/lightmap/kexlib/array.h src/lightmap/kexlib/binfile.h src/lightmap/kexlib/kstring.h src/lightmap/kexlib/memheap.h diff --git a/src/lightmap/common.h b/src/lightmap/common.h index e80031b..d4ca17b 100644 --- a/src/lightmap/common.h +++ b/src/lightmap/common.h @@ -34,6 +34,7 @@ #include #include #include +#include #ifdef _MSC_VER #pragma warning(disable: 4267) // warning C4267: 'argument': conversion from 'size_t' to 'int', possible loss of data diff --git a/src/lightmap/kexlib/array.h b/src/lightmap/kexlib/array.h deleted file mode 100644 index 87fbfb4..0000000 --- a/src/lightmap/kexlib/array.h +++ /dev/null @@ -1,263 +0,0 @@ -//----------------------------------------------------------------------------- -// Note: this is a modified version of dlight. It is not the original software. -//----------------------------------------------------------------------------- -// -// Copyright (c) 2013-2014 Samuel Villarreal -// svkaiser@gmail.com -// -// This software is provided 'as-is', without any express or implied -// warranty. In no event will the authors be held liable for any damages -// arising from the use of this software. -// -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it -// freely, subject to the following restrictions: -// -// 1. The origin of this software must not be misrepresented; you must not -// claim that you wrote the original software. If you use this software -// in a product, an acknowledgment in the product documentation would be -// appreciated but is not required. -// -// 2. Altered source versions must be plainly marked as such, and must not be -// misrepresented as being the original software. -// -// 3. This notice may not be removed or altered from any source -// distribution. -// - -#pragma once - -#include - -template -class kexArray -{ -public: - kexArray(); - ~kexArray(); - - typedef int compare_t(const type*, const type*); - - void Push(type o); - void Pop(); - void Empty(); - void Init(); - void Resize(unsigned int size); - type IndexOf(unsigned int index) const; - bool Contains(const type check) const; - void Splice(const unsigned int start, unsigned int len); - void Sort(compare_t *function); - void Sort(compare_t *function, unsigned int count); - - const unsigned int Length() const { return length; } - type GetData(const int index) { return data[index]; } - - type &operator[](unsigned int index); - kexArray &operator=(const kexArray &arr); - -protected: - type *data; - unsigned int length; - unsigned int aidx; -}; - -template -kexArray::kexArray() -{ - Init(); -} - -template -kexArray::~kexArray() -{ - Empty(); -} - -template -void kexArray::Init() -{ - data = NULL; - length = 0; - aidx = 0; -} - -template -void kexArray::Resize(unsigned int size) -{ - type *tmp; - - if (size == length) - { - return; - } - - if (size <= 0 && length != 0) - { - delete[] data; - data = NULL; - length = 0; - return; - } - - if (length == 0) - { - data = new type[size]; - length = size; - return; - } - - tmp = data; - data = new type[size]; - - for (unsigned int i = 0; i < length; i++) - { - data[i] = tmp[i]; - } - - length = size; - delete[] tmp; -} - -template -void kexArray::Push(type o) -{ - Resize(length + 1); - data[aidx++] = o; -} - -template -void kexArray::Pop() -{ - if (length == 0) - { - return; - } - - Resize(length - 1); - aidx--; -} - -template -void kexArray::Empty() -{ - if (data && length > 0) - { - delete[] data; - data = NULL; - length = 0; - aidx = 0; - } -} - -template -type kexArray::IndexOf(unsigned int index) const -{ - if (index >= length) - { - index = length - 1; - } - - return data[index]; -} - -template -bool kexArray::Contains(const type check) const -{ - for (unsigned int i = 0; i < length; ++i) - { - if (data[i] == check) - { - return true; - } - } - - return false; -} - -template -void kexArray::Splice(const unsigned int start, unsigned int len) -{ - if (length == 0 || len == 0) - { - return; - } - - if (len >= length) - { - len = length; - } - - type *tmp = new type[len]; - - for (unsigned int i = 0; i < len; i++) - { - tmp[i] = data[start + i]; - } - - delete[] data; - data = tmp; - length = length - len; - aidx = length - 1; -} - -// Note that data will be shuffled around, so this could invalidate any pointers that relies on the array/data -template -void kexArray::Sort(compare_t *function) -{ - if (data == NULL) - { - return; - } - - typedef int compareCast(const void*, const void*); - compareCast *cmpFunc = (compareCast*)function; - - qsort((void*)data, length, sizeof(type), cmpFunc); -} - -// Note that data will be shuffled around, so this could invalidate any pointers that relies on the array/data -template -void kexArray::Sort(compare_t *function, unsigned int count) -{ - if (data == NULL) - { - return; - } - - typedef int compareCast(const void*, const void*); - compareCast *cmpFunc = (compareCast*)function; - - qsort((void*)data, count, sizeof(type), cmpFunc); -} - -template -type &kexArray::operator[](unsigned int index) -{ - assert(index < length); - return data[index]; -} - -template -kexArray &kexArray::operator=(const kexArray &arr) -{ - if (data) - { - delete[] data; - } - - data = NULL; - length = arr.length; - aidx = arr.aidx; - - if (arr.length > 0) - { - data = new type[arr.length]; - - for (unsigned int i = 0; i < arr.length; i++) - { - data[i] = arr.data[i]; - } - } - - return *this; -} diff --git a/src/lightmap/kexlib/kstring.cpp b/src/lightmap/kexlib/kstring.cpp index 5d1bad0..eaea3f4 100644 --- a/src/lightmap/kexlib/kstring.cpp +++ b/src/lightmap/kexlib/kstring.cpp @@ -481,7 +481,7 @@ kexStr kexStr::Substr(int start, int len) const return str.Concat((const char*)&charPtr[start], len); } - +/* void kexStr::Split(kexStrList &list, const char seperator) { int splitLen = 0; @@ -509,7 +509,7 @@ void kexStr::Split(kexStrList &list, const char seperator) list.Push(kexStr(&charPtr[startOffs], splitLen)); } } - +*/ int kexStr::Atoi() { return atoi(charPtr); diff --git a/src/lightmap/kexlib/kstring.h b/src/lightmap/kexlib/kstring.h index b767853..d7f3e97 100644 --- a/src/lightmap/kexlib/kstring.h +++ b/src/lightmap/kexlib/kstring.h @@ -29,11 +29,10 @@ #include #include "lightmap/common.h" -#include "array.h" class kexStr; -typedef kexArray kexStrList; +//typedef kexArray kexStrList; class kexStr { @@ -60,7 +59,7 @@ public: kexStr &ToLower(); int Hash(); kexStr Substr(int start, int len) const; - void Split(kexStrList &list, const char seperator); + //void Split(kexStrList &list, const char seperator); int Atoi(); float Atof(); void WriteToFile(const char *file); diff --git a/src/lightmap/lightmap.cpp b/src/lightmap/lightmap.cpp index c123901..0596e5c 100644 --- a/src/lightmap/lightmap.cpp +++ b/src/lightmap/lightmap.cpp @@ -73,7 +73,7 @@ void kexLightmapBuilder::NewTexture() memset(allocBlocks[numTextures - 1], 0, sizeof(int) * textureWidth); uint16_t *texture = (uint16_t*)Mem_Calloc((textureWidth * textureHeight) * 3 * 2, hb_static); - textures.Push(texture); + textures.push_back(texture); } // Determines where to map a new block on to the lightmap texture @@ -547,10 +547,10 @@ void kexLightmapBuilder::TraceSurface(surface_t *surface) void kexLightmapBuilder::LightSurface(const int surfid) { float remaining; - int numsurfs = surfaces.Length(); + int numsurfs = surfaces.size(); // TODO: this should NOT happen, but apparently, it can randomly occur - if (surfaces.Length() == 0) + if (surfaces.size() == 0) { return; } @@ -577,7 +577,7 @@ void kexLightmapBuilder::CreateLightmaps(FLevel &doomMap) printf("------------- Tracing surfaces -------------\n"); processed = 0; - kexWorker::RunJob(surfaces.Length(), [=](int id) { + kexWorker::RunJob(surfaces.size(), [=](int id) { LightSurface(id); }); @@ -589,7 +589,7 @@ void kexLightmapBuilder::AddLightmapLump(FWadWriter &wadFile) // Calculate size of lump int numTexCoords = 0; int numSurfaces = 0; - for (unsigned int i = 0; i < surfaces.Length(); i++) + for (size_t i = 0; i < surfaces.size(); i++) { if (surfaces[i]->lightmapNum != -1) { @@ -599,9 +599,9 @@ void kexLightmapBuilder::AddLightmapLump(FWadWriter &wadFile) } int version = 0; int headerSize = 3 * sizeof(uint32_t) + 2 * sizeof(uint16_t); - int surfacesSize = surfaces.Length() * 5 * sizeof(uint32_t); + int surfacesSize = surfaces.size() * 5 * sizeof(uint32_t); int texCoordsSize = numTexCoords * 2 * sizeof(float); - int texDataSize = textures.Length() * textureWidth * textureHeight * 3 * 2; + int texDataSize = textures.size() * textureWidth * textureHeight * 3 * 2; int lumpSize = headerSize + surfacesSize + texCoordsSize + texDataSize; // Setup buffer @@ -612,13 +612,13 @@ void kexLightmapBuilder::AddLightmapLump(FWadWriter &wadFile) // Write header lumpFile.Write32(version); lumpFile.Write16(textureWidth); - lumpFile.Write16(textures.Length()); + lumpFile.Write16(textures.size()); lumpFile.Write32(numSurfaces); lumpFile.Write32(numTexCoords); // Write surfaces int coordOffsets = 0; - for (unsigned int i = 0; i < surfaces.Length(); i++) + for (size_t i = 0; i < surfaces.size(); i++) { if (surfaces[i]->lightmapNum == -1) continue; @@ -632,7 +632,7 @@ void kexLightmapBuilder::AddLightmapLump(FWadWriter &wadFile) } // Write texture coordinates - for (unsigned int i = 0; i < surfaces.Length(); i++) + for (size_t i = 0; i < surfaces.size(); i++) { if (surfaces[i]->lightmapNum == -1) continue; @@ -673,7 +673,7 @@ void kexLightmapBuilder::AddLightmapLump(FWadWriter &wadFile) } // Write lightmap textures - for (unsigned int i = 0; i < textures.Length(); i++) + for (size_t i = 0; i < textures.size(); i++) { unsigned int count = (textureWidth * textureHeight) * 3; for (unsigned int j = 0; j < count; j++) @@ -701,7 +701,7 @@ void kexLightmapBuilder::WriteTexturesToTGA() { kexBinFile file; - for (unsigned int i = 0; i < textures.Length(); i++) + for (unsigned int i = 0; i < textures.size(); i++) { file.Create(Va("lightmap_%02d.tga", i)); file.Write16(0); @@ -730,7 +730,7 @@ void kexLightmapBuilder::WriteMeshToOBJ() std::map> sortedSurfs; - for (unsigned int i = 0; i < surfaces.Length(); i++) + for (unsigned int i = 0; i < surfaces.size(); i++) sortedSurfs[surfaces[i]->lightmapNum].push_back(surfaces[i]); for (const auto &it : sortedSurfs) diff --git a/src/lightmap/lightmap.h b/src/lightmap/lightmap.h index 36a8550..fb437ba 100644 --- a/src/lightmap/lightmap.h +++ b/src/lightmap/lightmap.h @@ -64,7 +64,7 @@ private: bool EmitFromCeiling(kexTrace &trace, const surface_t *surface, const kexVec3 &origin, const kexVec3 &normal, kexVec3 &color); FLevel *map; - kexArray textures; + std::vector textures; int **allocBlocks; int numTextures; int extraSamples; diff --git a/src/lightmap/lightsurface.cpp b/src/lightmap/lightsurface.cpp index 870dfc0..13656d7 100644 --- a/src/lightmap/lightsurface.cpp +++ b/src/lightmap/lightsurface.cpp @@ -66,11 +66,11 @@ void kexLightSurface::CreateCenterOrigin() center += surface->verts[i]; } - origins.Push(center / (float)surface->numVerts); + origins.push_back(center / (float)surface->numVerts); } else { - origins.Push(kexVec3((surface->verts[1].x + surface->verts[0].x) * 0.5f, + origins.push_back(kexVec3((surface->verts[1].x + surface->verts[0].x) * 0.5f, (surface->verts[1].y + surface->verts[0].y) * 0.5f, (surface->verts[2].z + surface->verts[0].z) * 0.5f)); } @@ -79,32 +79,32 @@ void kexLightSurface::CreateCenterOrigin() // Splits surface vertices into two groups while adding new ones caused by the split void kexLightSurface::Clip(vertexBatch_t &points, const kexVec3 &normal, float dist, vertexBatch_t *frontPoints, vertexBatch_t *backPoints) { - kexArray dists; - kexArray sides; + std::vector dists; + std::vector sides; // determines what sides the vertices lies on - for (unsigned int i = 0; i < points.Length(); ++i) + for (size_t i = 0; i < points.size(); ++i) { float d = points[i].Dot(normal) - dist; - dists.Push(d); + dists.push_back(d); if (d > 0.1f) { - sides.Push(1); // front + sides.push_back(1); // front } else if (d < -0.1f) { - sides.Push(-1); // directly on the split plane + sides.push_back(-1); // directly on the split plane } else { - sides.Push(0); // back + sides.push_back(0); // back } } // add points - for (unsigned int i = 0; i < points.Length(); ++i) + for (unsigned int i = 0; i < points.size(); ++i) { int next; float frac; @@ -113,16 +113,16 @@ void kexLightSurface::Clip(vertexBatch_t &points, const kexVec3 &normal, float d switch (sides[i]) { case -1: - backPoints->Push(points[i]); + backPoints->push_back(points[i]); break; case 1: - frontPoints->Push(points[i]); + frontPoints->push_back(points[i]); break; default: - frontPoints->Push(points[i]); - backPoints->Push(points[i]); + frontPoints->push_back(points[i]); + backPoints->push_back(points[i]); // point is on the split plane so no new split vertex is needed continue; @@ -130,7 +130,7 @@ void kexLightSurface::Clip(vertexBatch_t &points, const kexVec3 &normal, float d } // check if the edge crosses the split plane - next = (i + 1) % points.Length(); + next = (i + 1) % points.size(); if (sides[next] == 0 || sides[next] == sides[i]) { @@ -145,13 +145,13 @@ void kexLightSurface::Clip(vertexBatch_t &points, const kexVec3 &normal, float d frac = dists[i] / (dists[i] - dists[next]); pt3 = pt1.Lerp(pt2, frac); - frontPoints->Push(pt3); - backPoints->Push(pt3); + frontPoints->push_back(pt3); + backPoints->push_back(pt3); } } // Recursively divides the surface -bool kexLightSurface::SubdivideRecursion(vertexBatch_t &surfPoints, float divide, kexArray &points) +bool kexLightSurface::SubdivideRecursion(vertexBatch_t &surfPoints, float divide, std::vector &points) { kexBBox bounds; kexVec3 splitNormal; @@ -160,7 +160,7 @@ bool kexLightSurface::SubdivideRecursion(vertexBatch_t &surfPoints, float divide vertexBatch_t *backPoints; // get bounds from current set of points - for (unsigned int i = 0; i < surfPoints.Length(); ++i) + for (unsigned int i = 0; i < surfPoints.size(); ++i) { bounds.AddPoint(surfPoints[i]); } @@ -183,7 +183,7 @@ bool kexLightSurface::SubdivideRecursion(vertexBatch_t &surfPoints, float divide if (!SubdivideRecursion(*frontPoints, divide, points)) { - points.Push(frontPoints); + points.push_back(frontPoints); } else { @@ -192,7 +192,7 @@ bool kexLightSurface::SubdivideRecursion(vertexBatch_t &surfPoints, float divide if (!SubdivideRecursion(*backPoints, divide, points)) { - points.Push(backPoints); + points.push_back(backPoints); } else { @@ -208,36 +208,35 @@ bool kexLightSurface::SubdivideRecursion(vertexBatch_t &surfPoints, float divide void kexLightSurface::Subdivide(const float divide) { - kexArray points; + std::vector points; vertexBatch_t surfPoints; for (int i = 0; i < surface->numVerts; ++i) { - surfPoints.Push(surface->verts[i]); + surfPoints.push_back(surface->verts[i]); } SubdivideRecursion(surfPoints, divide, points); // from each group of vertices caused by the split, begin // creating a origin point based on the center of that group - for (unsigned int i = 0; i < points.Length(); ++i) + for (size_t i = 0; i < points.size(); ++i) { vertexBatch_t *vb = points[i]; kexVec3 center; - for (unsigned int j = 0; j < vb->Length(); ++j) + for (unsigned int j = 0; j < vb->size(); ++j) { center += (*vb)[j]; } - origins.Push(center / (float)vb->Length()); + origins.push_back(center / (float)vb->size()); } - for (unsigned int i = 0; i < points.Length(); ++i) + for (size_t i = 0; i < points.size(); ++i) { vertexBatch_t *vb = points[i]; - vb->Empty(); delete vb; } } @@ -272,7 +271,7 @@ float kexLightSurface::TraceSurface(FLevel *doomMap, kexTrace &trace, const surf float total = 0.0f; float closestDistance = distance * gzdoomRadiusScale; - for (unsigned int i = 0; i < origins.Length(); ++i) + for (size_t i = 0; i < origins.size(); ++i) { kexVec3 center = origins[i]; @@ -318,5 +317,5 @@ float kexLightSurface::TraceSurface(FLevel *doomMap, kexTrace &trace, const surf } float attenuation = 1.0f - closestDistance / (distance * gzdoomRadiusScale); - return attenuation * total / origins.Length(); + return attenuation * total / origins.size(); } diff --git a/src/lightmap/lightsurface.h b/src/lightmap/lightsurface.h index ff3afb4..dd30f01 100644 --- a/src/lightmap/lightsurface.h +++ b/src/lightmap/lightsurface.h @@ -52,7 +52,7 @@ public: const vertexBatch_t Origins() const { return origins; } private: - bool SubdivideRecursion(vertexBatch_t &surfPoints, float divide, kexArray &points); + bool SubdivideRecursion(vertexBatch_t &surfPoints, float divide, std::vector &points); void Clip(vertexBatch_t &points, const kexVec3 &normal, float dist, vertexBatch_t *frontPoints, vertexBatch_t *backPoints); float distance; diff --git a/src/lightmap/mapdata.cpp b/src/lightmap/mapdata.cpp index 4289c5a..ac4e099 100644 --- a/src/lightmap/mapdata.cpp +++ b/src/lightmap/mapdata.cpp @@ -165,7 +165,7 @@ FloatVertex FLevel::GetSegVertex(int index) void FLevel::CreateLights() { thingLight_t *thingLight; - unsigned int j; + size_t j; int numSurfLights; kexVec2 pt; @@ -239,7 +239,7 @@ void FLevel::CreateLights() // // add surface lights // - for (j = 0; j < surfaces.Length(); ++j) + for (j = 0; j < surfaces.size(); ++j) { surface_t *surface = surfaces[j]; diff --git a/src/lightmap/surfaces.cpp b/src/lightmap/surfaces.cpp index 48d92bf..359cbe8 100644 --- a/src/lightmap/surfaces.cpp +++ b/src/lightmap/surfaces.cpp @@ -34,7 +34,7 @@ #include "mapdata.h" #include "surfaces.h" -kexArray surfaces; +std::vector surfaces; static void CreateSideSurfaces(FLevel &doomMap, IntSideDef *side) { @@ -96,7 +96,7 @@ static void CreateSideSurfaces(FLevel &doomMap, IntSideDef *side) surf->type = ST_LOWERSIDE; surf->typeIndex = side - &doomMap.Sides[0]; - surfaces.Push(surf); + surfaces.push_back(surf); } v1Bottom = v1BottomBack; @@ -139,7 +139,7 @@ static void CreateSideSurfaces(FLevel &doomMap, IntSideDef *side) surf->typeIndex = side - &doomMap.Sides[0]; surf->bSky = bSky; - surfaces.Push(surf); + surfaces.push_back(surf); } v1Top = v1TopBack; @@ -168,7 +168,7 @@ static void CreateSideSurfaces(FLevel &doomMap, IntSideDef *side) surf->type = ST_MIDDLESIDE; surf->typeIndex = side - &doomMap.Sides[0]; - surfaces.Push(surf); + surfaces.push_back(surf); } } @@ -224,7 +224,7 @@ static void CreateSubsectorSurfaces(FLevel &doomMap) surf->type = ST_FLOOR; surf->typeIndex = i; - surfaces.Push(surf); + surfaces.push_back(surf); surf = (surface_t*)Mem_Calloc(sizeof(surface_t), hb_static); surf->numVerts = sub->numlines; @@ -250,10 +250,10 @@ static void CreateSubsectorSurfaces(FLevel &doomMap) surf->type = ST_CEILING; surf->typeIndex = i; - surfaces.Push(surf); + surfaces.push_back(surf); } - printf("\nLeaf surfaces: %i\n", surfaces.Length() - doomMap.NumGLSubsectors); + printf("\nLeaf surfaces: %i\n", (int)surfaces.size() - doomMap.NumGLSubsectors); } static bool IsDegenerate(const kexVec3 &v0, const kexVec3 &v1, const kexVec3 &v2) @@ -274,7 +274,7 @@ static bool IsDegenerate(const kexVec3 &v0, const kexVec3 &v1, const kexVec3 &v2 void CreateSurfaces(FLevel &doomMap) { - for (unsigned int i = 0; i < surfaces.Length(); i++) + for (size_t i = 0; i < surfaces.size(); i++) Mem_Free(surfaces[i]); surfaces = {}; @@ -321,15 +321,15 @@ void CreateSurfaces(FLevel &doomMap) printf("sides: %i / %i\r", i + 1, doomMap.Sides.Size()); } - printf("\nSide surfaces: %i\n", surfaces.Length()); + printf("\nSide surfaces: %i\n", (int)surfaces.size()); CreateSubsectorSurfaces(doomMap); - printf("Surfaces total: %i\n\n", surfaces.Length()); + printf("Surfaces total: %i\n\n", (int)surfaces.size()); printf("Building collision mesh..\n\n"); - for (unsigned int i = 0; i < surfaces.Length(); i++) + for (size_t i = 0; i < surfaces.size(); i++) { const auto &s = surfaces[i]; int numVerts = s->numVerts; diff --git a/src/lightmap/surfaces.h b/src/lightmap/surfaces.h index af753c0..96e7605 100644 --- a/src/lightmap/surfaces.h +++ b/src/lightmap/surfaces.h @@ -39,7 +39,7 @@ enum surfaceType_t ST_FLOOR }; -typedef kexArray vertexBatch_t; +typedef std::vector vertexBatch_t; // convert from fixed point(FRACUNIT) to floating point #define F(x) (((float)(x))/65536.0f) @@ -62,7 +62,7 @@ struct surface_t bool bSky; }; -extern kexArray surfaces; +extern std::vector surfaces; struct FLevel;