From cf613947140279937840a0a095b6d1bd6ce64c24 Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Sat, 3 Nov 2018 19:33:59 +0100 Subject: [PATCH] - get rid of the naked new and deletes --- src/level/doomdata.h | 5 ++--- src/level/level.cpp | 2 +- src/level/level_light.cpp | 40 +++++++++------------------------ src/lightmap/lightmap.cpp | 32 +++++++++----------------- src/lightmap/lightmap.h | 18 +++++++-------- src/lightmap/lightsurface.cpp | 33 +++++++-------------------- src/lightmap/lightsurface.h | 5 +++-- src/lightmap/surfaces.cpp | 42 ++++++++++++++--------------------- src/lightmap/surfaces.h | 7 +++--- src/lightmap/worker.cpp | 7 ++---- 10 files changed, 67 insertions(+), 124 deletions(-) diff --git a/src/level/doomdata.h b/src/level/doomdata.h index 5428324..7e15e0e 100644 --- a/src/level/doomdata.h +++ b/src/level/doomdata.h @@ -363,12 +363,11 @@ struct FLevel std::vector bSkySectors; - TArray thingLights; - TArray lightSurfaces; + std::vector> thingLights; + std::vector> lightSurfaces; void SetupDlight(); void CreateLights(); - void CleanupThingLights(); LevelTraceHit Trace(const kexVec3 &startVec, const kexVec3 &endVec); diff --git a/src/level/level.cpp b/src/level/level.cpp index ff55767..e897817 100644 --- a/src/level/level.cpp +++ b/src/level/level.cpp @@ -19,6 +19,7 @@ */ #include "level/level.h" +#include "lightmap/lightsurface.h" //#include "rejectbuilder.h" #include @@ -53,7 +54,6 @@ FLevel::FLevel () FLevel::~FLevel () { - CleanupThingLights(); if (Vertices) delete[] Vertices; if (Subsectors) delete[] Subsectors; if (Segs) delete[] Segs; diff --git a/src/level/level_light.cpp b/src/level/level_light.cpp index ed3977c..cdd4caf 100644 --- a/src/level/level_light.cpp +++ b/src/level/level_light.cpp @@ -170,14 +170,7 @@ FloatVertex FLevel::GetSegVertex(int index) void FLevel::CreateLights() { - thingLight_t *thingLight; - size_t j; - int numSurfLights; - kexVec2 pt; - - // // add lights from thing sources - // for (int i = 0; i < (int)Things.Size(); ++i) { IntThing *thing = &Things[i]; @@ -218,7 +211,7 @@ void FLevel::CreateLights() int x = thing->x >> FRACBITS; int y = thing->y >> FRACBITS; - thingLight = new thingLight_t(); + auto thingLight = std::make_unique(); thingLight->mapThing = thing; thingLight->rgb.x = ((lightcolor >> 16) & 0xff) / 255.0f; @@ -234,20 +227,17 @@ void FLevel::CreateLights() thingLight->sector = GetSectorFromSubSector(thingLight->ssect); thingLight->origin.Set(x, y); - thingLights.Push(thingLight); + thingLights.push_back(std::move(thingLight)); } } - printf("Thing lights: %i\n", thingLights.Size()); + printf("Thing lights: %i\n", (int)thingLights.size()); - numSurfLights = 0; - - // // add surface lights - // - for (j = 0; j < surfaces.size(); ++j) + int numSurfLights = 0; + for (size_t j = 0; j < surfaces.size(); ++j) { - surface_t *surface = surfaces[j]; + surface_t *surface = surfaces[j].get(); if (surface->type >= ST_MIDDLESIDE && surface->type <= ST_LOWERSIDE) { @@ -284,11 +274,11 @@ void FLevel::CreateLights() desc.rgb.y = ((lightcolor >> 8) & 0xff) / 255.0f; desc.rgb.z = (lightcolor & 0xff) / 255.0f; - kexLightSurface *lightSurface = new kexLightSurface(); + auto lightSurface = std::make_unique(); lightSurface->Init(desc, surface, true); lightSurface->Subdivide(16); //lightSurface->CreateCenterOrigin(); - lightSurfaces.Push(lightSurface); + lightSurfaces.push_back(std::move(lightSurface)); numSurfLights++; } } @@ -348,10 +338,10 @@ void FLevel::CreateLights() desc.rgb.y = ((lightcolor >> 8) & 0xff) / 255.0f; desc.rgb.z = (lightcolor & 0xff) / 255.0f; - kexLightSurface *lightSurface = new kexLightSurface(); + auto lightSurface = std::make_unique(); lightSurface->Init(desc, surface, false); lightSurface->Subdivide(16); - lightSurfaces.Push(lightSurface); + lightSurfaces.push_back(std::move(lightSurface)); numSurfLights++; } } @@ -361,14 +351,6 @@ void FLevel::CreateLights() printf("Surface lights: %i\n", numSurfLights); } -void FLevel::CleanupThingLights() -{ - for (unsigned int i = 0; i < thingLights.Size(); i++) - { - delete thingLights[i]; - } -} - LevelTraceHit FLevel::Trace(const kexVec3 &startVec, const kexVec3 &endVec) { TraceHit hit = TriangleMeshShape::find_first_hit(CollisionMesh.get(), startVec, endVec); @@ -377,6 +359,6 @@ LevelTraceHit FLevel::Trace(const kexVec3 &startVec, const kexVec3 &endVec) trace.start = startVec; trace.end = endVec; trace.fraction = hit.fraction; - trace.hitSurface = (trace.fraction < 1.0f) ? surfaces[hit.surface] : nullptr; + trace.hitSurface = (trace.fraction < 1.0f) ? surfaces[hit.surface].get() : nullptr; return trace; } diff --git a/src/lightmap/lightmap.cpp b/src/lightmap/lightmap.cpp index 1762046..ed44dbd 100644 --- a/src/lightmap/lightmap.cpp +++ b/src/lightmap/lightmap.cpp @@ -55,13 +55,6 @@ const kexVec3 kexLightmapBuilder::gridSize(64, 64, 128); kexLightmapBuilder::kexLightmapBuilder() { - textureWidth = 128; - textureHeight = 128; - numTextures = 0; - samples = 16; - extraSamples = 2; - ambience = 0.0f; - tracedTexels = 0; } kexLightmapBuilder::~kexLightmapBuilder() @@ -72,11 +65,8 @@ void kexLightmapBuilder::NewTexture() { numTextures++; - allocBlocks.push_back(new int[textureWidth]); - memset(allocBlocks.back(), 0, sizeof(int) * textureWidth); - - uint16_t *texture = new uint16_t[textureWidth * textureHeight * 3]; - textures.push_back(texture); + allocBlocks.push_back(std::vector(textureWidth)); + textures.push_back(std::vector(textureWidth * textureHeight * 3)); } // Determines where to map a new block on to the lightmap texture @@ -225,9 +215,9 @@ kexVec3 kexLightmapBuilder::LightTexelSample(const kexVec3 &origin, surface_t *s kexVec3 color(0.0f, 0.0f, 0.0f); // check all thing lights - for (unsigned int i = 0; i < map->thingLights.Size(); i++) + for (size_t i = 0; i < map->thingLights.size(); i++) { - thingLight_t *tl = map->thingLights[i]; + thingLight_t *tl = map->thingLights[i].get(); float originZ; if (!tl->bCeiling) @@ -301,9 +291,9 @@ kexVec3 kexLightmapBuilder::LightTexelSample(const kexVec3 &origin, surface_t *s } // trace against surface lights - for (unsigned int i = 0; i < map->lightSurfaces.Size(); ++i) + for (size_t i = 0; i < map->lightSurfaces.size(); ++i) { - kexLightSurface *surfaceLight = map->lightSurfaces[i]; + kexLightSurface *surfaceLight = map->lightSurfaces[i].get(); float attenuation = surfaceLight->TraceSurface(map, surface, origin); if (attenuation > 0.0f) @@ -386,7 +376,7 @@ void kexLightmapBuilder::BuildSurfaceParams(surface_t *surface) height = textureHeight; } - surface->lightmapCoords = new float[surface->numVerts * 2]; + surface->lightmapCoords.resize(surface->numVerts * 2); surface->textureCoords[0] = tCoords[0]; surface->textureCoords[1] = tCoords[1]; @@ -523,7 +513,7 @@ void kexLightmapBuilder::TraceSurface(surface_t *surface) } std::unique_lock lock(mutex); - currentTexture = textures[surface->lightmapNum]; + currentTexture = textures[surface->lightmapNum].data(); lock.unlock(); // store results to lightmap texture @@ -553,8 +543,8 @@ void kexLightmapBuilder::LightSurface(const int surfid) return; } - BuildSurfaceParams(surfaces[surfid]); - TraceSurface(surfaces[surfid]); + BuildSurfaceParams(surfaces[surfid].get()); + TraceSurface(surfaces[surfid].get()); std::unique_lock lock(mutex); @@ -731,7 +721,7 @@ void kexLightmapBuilder::WriteMeshToOBJ() std::map> sortedSurfs; for (unsigned int i = 0; i < surfaces.size(); i++) - sortedSurfs[surfaces[i]->lightmapNum].push_back(surfaces[i]); + sortedSurfs[surfaces[i]->lightmapNum].push_back(surfaces[i].get()); for (const auto &it : sortedSurfs) { diff --git a/src/lightmap/lightmap.h b/src/lightmap/lightmap.h index 3d4dce9..ece75be 100644 --- a/src/lightmap/lightmap.h +++ b/src/lightmap/lightmap.h @@ -48,10 +48,10 @@ public: void WriteMeshToOBJ(); void AddLightmapLump(FWadWriter &wadFile); - int samples; - float ambience; - int textureWidth; - int textureHeight; + int samples = 16; + float ambience = 0.0f; + int textureWidth = 128; + int textureHeight = 128; static const kexVec3 gridSize; @@ -63,11 +63,11 @@ private: bool EmitFromCeiling(const surface_t *surface, const kexVec3 &origin, const kexVec3 &normal, kexVec3 &color); FLevel *map; - std::vector textures; - std::vector allocBlocks; - int numTextures; - int extraSamples; - int tracedTexels; + std::vector> textures; + std::vector> allocBlocks; + int numTextures = 0; + int extraSamples = 2; + int tracedTexels = 0; std::mutex mutex; int processed = 0; diff --git a/src/lightmap/lightsurface.cpp b/src/lightmap/lightsurface.cpp index d322417..b13860e 100644 --- a/src/lightmap/lightsurface.cpp +++ b/src/lightmap/lightsurface.cpp @@ -150,13 +150,11 @@ void kexLightSurface::Clip(vertexBatch_t &points, const kexVec3 &normal, float d } // Recursively divides the surface -bool kexLightSurface::SubdivideRecursion(vertexBatch_t &surfPoints, float divide, std::vector &points) +bool kexLightSurface::SubdivideRecursion(vertexBatch_t &surfPoints, float divide, std::vector> &points) { kexBBox bounds; kexVec3 splitNormal; float dist; - vertexBatch_t *frontPoints; - vertexBatch_t *backPoints; // get bounds from current set of points for (unsigned int i = 0; i < surfPoints.size(); ++i) @@ -174,28 +172,20 @@ bool kexLightSurface::SubdivideRecursion(vertexBatch_t &surfPoints, float divide dist = (bounds.max[i] + bounds.min[i]) * 0.5f; - frontPoints = new vertexBatch_t; - backPoints = new vertexBatch_t; + auto frontPoints = std::make_unique(); + auto backPoints = std::make_unique(); // start clipping - Clip(surfPoints, splitNormal, dist, frontPoints, backPoints); + Clip(surfPoints, splitNormal, dist, frontPoints.get(), backPoints.get()); if (!SubdivideRecursion(*frontPoints, divide, points)) { - points.push_back(frontPoints); - } - else - { - delete frontPoints; + points.push_back(std::move(frontPoints)); } if (!SubdivideRecursion(*backPoints, divide, points)) { - points.push_back(backPoints); - } - else - { - delete backPoints; + points.push_back(std::move(backPoints)); } return true; @@ -207,7 +197,7 @@ bool kexLightSurface::SubdivideRecursion(vertexBatch_t &surfPoints, float divide void kexLightSurface::Subdivide(const float divide) { - std::vector points; + std::vector> points; vertexBatch_t surfPoints; for (int i = 0; i < surface->numVerts; ++i) @@ -221,7 +211,7 @@ void kexLightSurface::Subdivide(const float divide) // creating a origin point based on the center of that group for (size_t i = 0; i < points.size(); ++i) { - vertexBatch_t *vb = points[i]; + vertexBatch_t *vb = points[i].get(); kexVec3 center; for (unsigned int j = 0; j < vb->size(); ++j) @@ -231,13 +221,6 @@ void kexLightSurface::Subdivide(const float divide) origins.push_back(center / (float)vb->size()); } - - for (size_t i = 0; i < points.size(); ++i) - { - vertexBatch_t *vb = points[i]; - - delete vb; - } } float kexLightSurface::TraceSurface(FLevel *map, const surface_t *surf, const kexVec3 &origin) diff --git a/src/lightmap/lightsurface.h b/src/lightmap/lightsurface.h index 93b3a19..3422766 100644 --- a/src/lightmap/lightsurface.h +++ b/src/lightmap/lightsurface.h @@ -48,10 +48,11 @@ public: const kexVec3 GetRGB() const { return rgb; } const bool IsAWall() const { return bWall; } const surface_t *Surface() const { return surface; } - const vertexBatch_t Origins() const { return origins; } private: - bool SubdivideRecursion(vertexBatch_t &surfPoints, float divide, std::vector &points); + typedef std::vector vertexBatch_t; + + 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/surfaces.cpp b/src/lightmap/surfaces.cpp index 4ef0a69..fb4fd50 100644 --- a/src/lightmap/surfaces.cpp +++ b/src/lightmap/surfaces.cpp @@ -39,11 +39,10 @@ #pragma warning(disable: 4244) // warning C4244: '=': conversion from '__int64' to 'int', possible loss of data #endif -std::vector surfaces; +std::vector> surfaces; static void CreateSideSurfaces(FLevel &doomMap, IntSideDef *side) { - surface_t *surf; IntSector *front; IntSector *back; @@ -83,7 +82,7 @@ static void CreateSideSurfaces(FLevel &doomMap, IntSideDef *side) { if (side->bottomtexture[0] != '-') { - surf = new surface_t(); + auto surf = std::make_unique(); surf->numVerts = 4; surf->verts.resize(4); @@ -101,7 +100,7 @@ static void CreateSideSurfaces(FLevel &doomMap, IntSideDef *side) surf->type = ST_LOWERSIDE; surf->typeIndex = side - &doomMap.Sides[0]; - surfaces.push_back(surf); + surfaces.push_back(std::move(surf)); } v1Bottom = v1BottomBack; @@ -125,7 +124,7 @@ static void CreateSideSurfaces(FLevel &doomMap, IntSideDef *side) if (side->toptexture[0] != '-' || bSky) { - surf = new surface_t(); + auto surf = std::make_unique(); surf->numVerts = 4; surf->verts.resize(4); @@ -144,7 +143,7 @@ static void CreateSideSurfaces(FLevel &doomMap, IntSideDef *side) surf->typeIndex = side - &doomMap.Sides[0]; surf->bSky = bSky; - surfaces.push_back(surf); + surfaces.push_back(std::move(surf)); } v1Top = v1TopBack; @@ -155,7 +154,7 @@ static void CreateSideSurfaces(FLevel &doomMap, IntSideDef *side) // middle seg if (back == nullptr) { - surf = new surface_t(); + auto surf = std::make_unique(); surf->numVerts = 4; surf->verts.resize(4); @@ -173,20 +172,15 @@ static void CreateSideSurfaces(FLevel &doomMap, IntSideDef *side) surf->type = ST_MIDDLESIDE; surf->typeIndex = side - &doomMap.Sides[0]; - surfaces.push_back(surf); + surfaces.push_back(std::move(surf)); } } static void CreateSubsectorSurfaces(FLevel &doomMap) { - surface_t *surf; - IntSector *sector = nullptr; - int i; - int j; - printf("------------- Building subsector surfaces -------------\n"); - for (i = 0; i < doomMap.NumGLSubsectors; i++) + for (int i = 0; i < doomMap.NumGLSubsectors; i++) { printf("subsectors: %i / %i\r", i + 1, doomMap.NumGLSubsectors); @@ -197,7 +191,7 @@ static void CreateSubsectorSurfaces(FLevel &doomMap) continue; } - sector = doomMap.GetSectorFromSubSector(sub); + IntSector *sector = doomMap.GetSectorFromSubSector(sub); if (!sector) continue; @@ -205,12 +199,12 @@ static void CreateSubsectorSurfaces(FLevel &doomMap) if (sector->controlsector) continue; - surf = new surface_t(); + auto surf = std::make_unique(); surf->numVerts = sub->numlines; surf->verts.resize(surf->numVerts); // floor verts - for (j = 0; j < surf->numVerts; j++) + for (int j = 0; j < surf->numVerts; j++) { MapSegGLEx *seg = &doomMap.GLSegs[sub->firstline + (surf->numVerts - 1) - j]; FloatVertex v1 = doomMap.GetSegVertex(seg->v1); @@ -224,9 +218,9 @@ static void CreateSubsectorSurfaces(FLevel &doomMap) surf->type = ST_FLOOR; surf->typeIndex = i; - surfaces.push_back(surf); + surfaces.push_back(std::move(surf)); - surf = new surface_t(); + surf = std::make_unique(); surf->numVerts = sub->numlines; surf->verts.resize(surf->numVerts); @@ -236,7 +230,7 @@ static void CreateSubsectorSurfaces(FLevel &doomMap) } // ceiling verts - for (j = 0; j < surf->numVerts; j++) + for (int j = 0; j < surf->numVerts; j++) { MapSegGLEx *seg = &doomMap.GLSegs[sub->firstline + j]; FloatVertex v1 = doomMap.GetSegVertex(seg->v1); @@ -250,7 +244,7 @@ static void CreateSubsectorSurfaces(FLevel &doomMap) surf->type = ST_CEILING; surf->typeIndex = i; - surfaces.push_back(surf); + surfaces.push_back(std::move(surf)); } printf("\nLeaf surfaces: %i\n", (int)surfaces.size() - doomMap.NumGLSubsectors); @@ -274,9 +268,7 @@ static bool IsDegenerate(const kexVec3 &v0, const kexVec3 &v1, const kexVec3 &v2 void CreateSurfaces(FLevel &doomMap) { - for (size_t i = 0; i < surfaces.size(); i++) - delete surfaces[i]; - surfaces = {}; + surfaces.clear(); for (unsigned int i = 0; i < doomMap.Sectors.Size(); i++) doomMap.Sectors[i].controlsector = false; @@ -370,5 +362,5 @@ void CreateSurfaces(FLevel &doomMap) } } - doomMap.CollisionMesh.reset(new TriangleMeshShape(&doomMap.MeshVertices[0], doomMap.MeshVertices.Size(), &doomMap.MeshElements[0], doomMap.MeshElements.Size(), &doomMap.MeshSurfaces[0])); + doomMap.CollisionMesh = std::make_unique(&doomMap.MeshVertices[0], doomMap.MeshVertices.Size(), &doomMap.MeshElements[0], doomMap.MeshElements.Size(), &doomMap.MeshSurfaces[0]); } diff --git a/src/lightmap/surfaces.h b/src/lightmap/surfaces.h index 8e5087f..15b766a 100644 --- a/src/lightmap/surfaces.h +++ b/src/lightmap/surfaces.h @@ -28,6 +28,7 @@ #pragma once #include +#include struct MapSubsectorEx; @@ -41,8 +42,6 @@ enum surfaceType_t ST_FLOOR }; -typedef std::vector vertexBatch_t; - // convert from fixed point(FRACUNIT) to floating point #define F(x) (((float)(x))/65536.0f) @@ -58,13 +57,13 @@ struct surface_t kexBBox bounds; int numVerts; std::vector verts; - float *lightmapCoords; + std::vector lightmapCoords; surfaceType_t type; int typeIndex; bool bSky; }; -extern std::vector surfaces; +extern std::vector> surfaces; struct FLevel; diff --git a/src/lightmap/worker.cpp b/src/lightmap/worker.cpp index fbb576c..a4a86df 100644 --- a/src/lightmap/worker.cpp +++ b/src/lightmap/worker.cpp @@ -4,8 +4,6 @@ #include #include #include -#undef min -#undef max extern int NumThreads; @@ -26,7 +24,8 @@ void kexWorker::RunJob(int count, std::function callback) { threads.push_back(std::thread([=]() { - colorSamples = new kexVec3[1024 * 1024]; + std::vector samples(1024 * 1024); + colorSamples = samples.data(); int start = threadIndex * count / numThreads; int end = std::min((threadIndex + 1) * count / numThreads, count); @@ -35,8 +34,6 @@ void kexWorker::RunJob(int count, std::function callback) callback(i); } - delete[] colorSamples; - })); }