Added some reference counting.

This commit is contained in:
Angelo Papenhoff 2014-12-20 11:38:27 +01:00
parent c0c8271b03
commit 92a3db015c
3 changed files with 78 additions and 22 deletions

6
TODO
View File

@ -13,7 +13,6 @@ Clump & related:
Clump Clump
Collision Collision
Frame Frame
Frame
HAnim HAnim
Atomic Atomic
Right To Render Right To Render
@ -21,10 +20,9 @@ Clump & related:
Mat FX Mat FX
Pipeline Set Pipeline Set
Geometry Geometry
Mesh
Native Data Native Data
Mesh Extension Breakable
Night Vertex Colors Extra Vertex Colors
(Morph) (Morph)
Skin Skin
(ADC) (ADC)

View File

@ -47,8 +47,9 @@ Geometry::Geometry(int32 numVerts, int32 numTris, uint32 flags)
m->normals = new float32[3*this->numVertices]; m->normals = new float32[3*this->numVertices];
} }
this->numMaterials = 0; this->numMaterials = 0;
materialList = NULL; this->materialList = NULL;
meshHeader = NULL; this->meshHeader = NULL;
this->refCount = 1;
this->constructPlugins(); this->constructPlugins();
} }
@ -60,12 +61,32 @@ Geometry::~Geometry(void)
for(int32 i = 0; i < this->numTexCoordSets; i++) for(int32 i = 0; i < this->numTexCoordSets; i++)
delete[] this->texCoords[i]; delete[] this->texCoords[i];
delete[] this->triangles; delete[] this->triangles;
for(int32 i = 0; i < this->numMorphTargets; i++){ for(int32 i = 0; i < this->numMorphTargets; i++){
MorphTarget *m = &this->morphTargets[i]; MorphTarget *m = &this->morphTargets[i];
delete[] m->vertices; delete[] m->vertices;
delete[] m->normals; delete[] m->normals;
} }
delete[] this->morphTargets; 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 struct GeoStreamData
@ -248,9 +269,10 @@ Geometry::addMorphTargets(int32 n)
Material::Material(void) Material::Material(void)
{ {
this->texture = NULL; 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; surfaceProps[0] = surfaceProps[1] = surfaceProps[2] = 1.0f;
constructPlugins(); this->refCount = 1;
this->constructPlugins();
} }
Material::Material(Material *m) Material::Material(Material *m)
@ -262,12 +284,25 @@ Material::Material(Material *m)
m->surfaceProps[0] = this->surfaceProps[0]; m->surfaceProps[0] = this->surfaceProps[0];
m->surfaceProps[1] = this->surfaceProps[1]; m->surfaceProps[1] = this->surfaceProps[1];
m->surfaceProps[2] = this->surfaceProps[2]; m->surfaceProps[2] = this->surfaceProps[2];
copyPlugins(m); m->texture = this->texture;
if(m->texture)
m->texture->refCount++;
this->copyPlugins(m);
} }
Material::~Material(void) 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 struct MatStreamData
@ -349,21 +384,22 @@ Texture::Texture(void)
{ {
memset(this->name, 0, 32); memset(this->name, 0, 32);
memset(this->mask, 0, 32); memset(this->mask, 0, 32);
this->filterAddressing = 0; this->filterAddressing = (WRAP << 12) | (WRAP << 8) | NEAREST;
constructPlugins(); this->refCount = 1;
} this->constructPlugins();
Texture::Texture(Texture *t)
{
memcpy(this->name, t->name, 32);
memcpy(this->mask, t->name, 32);
this->filterAddressing = t->filterAddressing;
copyPlugins(t);
} }
Texture::~Texture(void) Texture::~Texture(void)
{ {
destructPlugins(); this->destructPlugins();
}
void
Texture::decRef(void)
{
this->refCount--;
if(this->refCount)
delete this;
} }
Texture* Texture*

24
rw.h
View File

@ -8,18 +8,35 @@ struct Object
void *parent; void *parent;
}; };
// TODO: raster, link into texdict
struct Texture : PluginBase<Texture> struct Texture : PluginBase<Texture>
{ {
char name[32]; char name[32];
char mask[32]; char mask[32];
uint32 filterAddressing; uint32 filterAddressing;
int32 refCount;
Texture(void); Texture(void);
Texture(Texture *t);
~Texture(void); ~Texture(void);
void decRef(void);
static Texture *streamRead(std::istream &stream); static Texture *streamRead(std::istream &stream);
bool streamWrite(std::ostream &stream); bool streamWrite(std::ostream &stream);
uint32 streamGetSize(void); uint32 streamGetSize(void);
enum FilterMode {
NEAREST = 1,
LINEAR,
MIPNEAREST,
MIPLINEAR,
LINEARMIPNEAREST,
LINEARMIPLINEAR
};
enum Addressing {
WRAP = 1,
MIRROR,
CLAMP,
BORDER
};
}; };
struct Material : PluginBase<Material> struct Material : PluginBase<Material>
@ -27,9 +44,11 @@ struct Material : PluginBase<Material>
Texture *texture; Texture *texture;
uint8 color[4]; uint8 color[4];
float32 surfaceProps[3]; float32 surfaceProps[3];
int32 refCount;
Material(void); Material(void);
Material(Material *m); Material(Material *m);
void decRef(void);
~Material(void); ~Material(void);
static Material *streamRead(std::istream &stream); static Material *streamRead(std::istream &stream);
bool streamWrite(std::ostream &stream); bool streamWrite(std::ostream &stream);
@ -78,7 +97,10 @@ struct Geometry : PluginBase<Geometry>, Object
MeshHeader *meshHeader; MeshHeader *meshHeader;
int32 refCount;
Geometry(int32 numVerts, int32 numTris, uint32 flags); Geometry(int32 numVerts, int32 numTris, uint32 flags);
void decRef(void);
~Geometry(void); ~Geometry(void);
static Geometry *streamRead(std::istream &stream); static Geometry *streamRead(std::istream &stream);
bool streamWrite(std::ostream &stream); bool streamWrite(std::ostream &stream);