From 92a3db015c8a6eb195d96116da9f72824fc5a82d Mon Sep 17 00:00:00 2001 From: Angelo Papenhoff Date: Sat, 20 Dec 2014 11:38:27 +0100 Subject: [PATCH] Added some reference counting. --- TODO | 6 ++--- geometry.cpp | 70 +++++++++++++++++++++++++++++++++++++++------------- rw.h | 24 +++++++++++++++++- 3 files changed, 78 insertions(+), 22 deletions(-) diff --git a/TODO b/TODO index a362d58..f980e86 100644 --- a/TODO +++ b/TODO @@ -13,7 +13,6 @@ Clump & related: Clump Collision Frame - Frame HAnim Atomic Right To Render @@ -21,10 +20,9 @@ Clump & related: Mat FX Pipeline Set Geometry - Mesh Native Data - Mesh Extension - Night Vertex Colors + Breakable + Extra Vertex Colors (Morph) Skin (ADC) diff --git a/geometry.cpp b/geometry.cpp index 8460b74..d2872c9 100644 --- a/geometry.cpp +++ b/geometry.cpp @@ -47,8 +47,9 @@ Geometry::Geometry(int32 numVerts, int32 numTris, uint32 flags) m->normals = new float32[3*this->numVertices]; } this->numMaterials = 0; - materialList = NULL; - meshHeader = NULL; + this->materialList = NULL; + this->meshHeader = NULL; + this->refCount = 1; this->constructPlugins(); } @@ -60,12 +61,32 @@ Geometry::~Geometry(void) for(int32 i = 0; i < this->numTexCoordSets; i++) delete[] this->texCoords[i]; delete[] this->triangles; + for(int32 i = 0; i < this->numMorphTargets; i++){ MorphTarget *m = &this->morphTargets[i]; delete[] m->vertices; delete[] m->normals; } delete[] this->morphTargets; + + if(this->meshHeader){ + for(uint32 i = 0; i < this->meshHeader->numMeshes; i++) + delete[] this->meshHeader->mesh[i].indices; + delete[] this->meshHeader->mesh; + delete this->meshHeader; + } + + for(int32 i = 0; i < this->numMaterials; i++) + this->materialList[i]->decRef(); + delete[] this->materialList; +} + +void +Geometry::decRef(void) +{ + this->refCount--; + if(this->refCount) + delete this; } struct GeoStreamData @@ -248,9 +269,10 @@ Geometry::addMorphTargets(int32 n) Material::Material(void) { this->texture = NULL; - color[0] = color[1] = color[2] = color[3] = 0xFF; + memset(this->color, 0xFF, 4); surfaceProps[0] = surfaceProps[1] = surfaceProps[2] = 1.0f; - constructPlugins(); + this->refCount = 1; + this->constructPlugins(); } Material::Material(Material *m) @@ -262,12 +284,25 @@ Material::Material(Material *m) m->surfaceProps[0] = this->surfaceProps[0]; m->surfaceProps[1] = this->surfaceProps[1]; m->surfaceProps[2] = this->surfaceProps[2]; - copyPlugins(m); + m->texture = this->texture; + if(m->texture) + m->texture->refCount++; + this->copyPlugins(m); } Material::~Material(void) { - destructPlugins(); + this->destructPlugins(); + if(this->texture) + this->texture->decRef(); +} + +void +Material::decRef(void) +{ + this->refCount--; + if(this->refCount) + delete this; } struct MatStreamData @@ -349,21 +384,22 @@ Texture::Texture(void) { memset(this->name, 0, 32); memset(this->mask, 0, 32); - this->filterAddressing = 0; - constructPlugins(); -} - -Texture::Texture(Texture *t) -{ - memcpy(this->name, t->name, 32); - memcpy(this->mask, t->name, 32); - this->filterAddressing = t->filterAddressing; - copyPlugins(t); + this->filterAddressing = (WRAP << 12) | (WRAP << 8) | NEAREST; + this->refCount = 1; + this->constructPlugins(); } Texture::~Texture(void) { - destructPlugins(); + this->destructPlugins(); +} + +void +Texture::decRef(void) +{ + this->refCount--; + if(this->refCount) + delete this; } Texture* diff --git a/rw.h b/rw.h index 3f5762d..7569956 100644 --- a/rw.h +++ b/rw.h @@ -8,18 +8,35 @@ struct Object void *parent; }; +// TODO: raster, link into texdict struct Texture : PluginBase { char name[32]; char mask[32]; uint32 filterAddressing; + int32 refCount; Texture(void); - Texture(Texture *t); ~Texture(void); + void decRef(void); static Texture *streamRead(std::istream &stream); bool streamWrite(std::ostream &stream); uint32 streamGetSize(void); + + enum FilterMode { + NEAREST = 1, + LINEAR, + MIPNEAREST, + MIPLINEAR, + LINEARMIPNEAREST, + LINEARMIPLINEAR + }; + enum Addressing { + WRAP = 1, + MIRROR, + CLAMP, + BORDER + }; }; struct Material : PluginBase @@ -27,9 +44,11 @@ struct Material : PluginBase Texture *texture; uint8 color[4]; float32 surfaceProps[3]; + int32 refCount; Material(void); Material(Material *m); + void decRef(void); ~Material(void); static Material *streamRead(std::istream &stream); bool streamWrite(std::ostream &stream); @@ -78,7 +97,10 @@ struct Geometry : PluginBase, Object MeshHeader *meshHeader; + int32 refCount; + Geometry(int32 numVerts, int32 numTris, uint32 flags); + void decRef(void); ~Geometry(void); static Geometry *streamRead(std::istream &stream); bool streamWrite(std::ostream &stream);