- get rid of the naked new and deletes

This commit is contained in:
Magnus Norddahl 2018-11-03 19:33:59 +01:00
parent e89aa8a5d4
commit cf61394714
10 changed files with 67 additions and 124 deletions

View file

@ -363,12 +363,11 @@ struct FLevel
std::vector<bool> bSkySectors;
TArray<thingLight_t*> thingLights;
TArray<kexLightSurface*> lightSurfaces;
std::vector<std::unique_ptr<thingLight_t>> thingLights;
std::vector<std::unique_ptr<kexLightSurface>> lightSurfaces;
void SetupDlight();
void CreateLights();
void CleanupThingLights();
LevelTraceHit Trace(const kexVec3 &startVec, const kexVec3 &endVec);

View file

@ -19,6 +19,7 @@
*/
#include "level/level.h"
#include "lightmap/lightsurface.h"
//#include "rejectbuilder.h"
#include <memory>
@ -53,7 +54,6 @@ FLevel::FLevel ()
FLevel::~FLevel ()
{
CleanupThingLights();
if (Vertices) delete[] Vertices;
if (Subsectors) delete[] Subsectors;
if (Segs) delete[] Segs;

View file

@ -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_t>();
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<kexLightSurface>();
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<kexLightSurface>();
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;
}

View file

@ -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<int>(textureWidth));
textures.push_back(std::vector<uint16_t>(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<std::mutex> 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<std::mutex> lock(mutex);
@ -731,7 +721,7 @@ void kexLightmapBuilder::WriteMeshToOBJ()
std::map<int, std::vector<surface_t*>> 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)
{

View file

@ -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<uint16_t*> textures;
std::vector<int*> allocBlocks;
int numTextures;
int extraSamples;
int tracedTexels;
std::vector<std::vector<uint16_t>> textures;
std::vector<std::vector<int>> allocBlocks;
int numTextures = 0;
int extraSamples = 2;
int tracedTexels = 0;
std::mutex mutex;
int processed = 0;

View file

@ -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<vertexBatch_t*> &points)
bool kexLightSurface::SubdivideRecursion(vertexBatch_t &surfPoints, float divide, std::vector<std::unique_ptr<vertexBatch_t>> &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<vertexBatch_t>();
auto backPoints = std::make_unique<vertexBatch_t>();
// 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<vertexBatch_t*> points;
std::vector<std::unique_ptr<vertexBatch_t>> 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)

View file

@ -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<vertexBatch_t*> &points);
typedef std::vector<kexVec3> vertexBatch_t;
bool SubdivideRecursion(vertexBatch_t &surfPoints, float divide, std::vector<std::unique_ptr<vertexBatch_t>> &points);
void Clip(vertexBatch_t &points, const kexVec3 &normal, float dist, vertexBatch_t *frontPoints, vertexBatch_t *backPoints);
float distance;

View file

@ -39,11 +39,10 @@
#pragma warning(disable: 4244) // warning C4244: '=': conversion from '__int64' to 'int', possible loss of data
#endif
std::vector<surface_t*> surfaces;
std::vector<std::unique_ptr<surface_t>> 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<surface_t>();
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<surface_t>();
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<surface_t>();
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<surface_t>();
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<surface_t>();
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<TriangleMeshShape>(&doomMap.MeshVertices[0], doomMap.MeshVertices.Size(), &doomMap.MeshElements[0], doomMap.MeshElements.Size(), &doomMap.MeshSurfaces[0]);
}

View file

@ -28,6 +28,7 @@
#pragma once
#include <vector>
#include <memory>
struct MapSubsectorEx;
@ -41,8 +42,6 @@ enum surfaceType_t
ST_FLOOR
};
typedef std::vector<kexVec3> 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<kexVec3> verts;
float *lightmapCoords;
std::vector<float> lightmapCoords;
surfaceType_t type;
int typeIndex;
bool bSky;
};
extern std::vector<surface_t*> surfaces;
extern std::vector<std::unique_ptr<surface_t>> surfaces;
struct FLevel;

View file

@ -4,8 +4,6 @@
#include <vector>
#include <thread>
#include <algorithm>
#undef min
#undef max
extern int NumThreads;
@ -26,7 +24,8 @@ void kexWorker::RunJob(int count, std::function<void(int)> callback)
{
threads.push_back(std::thread([=]() {
colorSamples = new kexVec3[1024 * 1024];
std::vector<kexVec3> 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<void(int)> callback)
callback(i);
}
delete[] colorSamples;
}));
}