diff --git a/src/base.cpp b/src/base.cpp index 818ac5d..ca8bcd6 100644 --- a/src/base.cpp +++ b/src/base.cpp @@ -874,6 +874,7 @@ StreamMemory::getLength(void) StreamFile* StreamFile::open(const char *path, const char *mode) { + assert(this->file == nil); this->file = fopen(path, mode); if(this->file == nil){ RWERROR((ERR_FILE, path)); @@ -885,7 +886,9 @@ StreamFile::open(const char *path, const char *mode) void StreamFile::close(void) { + assert(this->file); fclose(this->file); + this->file = nil; } uint32 diff --git a/src/matfx.cpp b/src/matfx.cpp index 3861db7..777c065 100644 --- a/src/matfx.cpp +++ b/src/matfx.cpp @@ -95,28 +95,37 @@ clearMatFX(MatFX *matfx) } void -MatFX::setEffects(uint32 type) +MatFX::setEffects(Material *mat, uint32 type) { - if(this->type != 0 && this->type != type) - clearMatFX(this); - this->type = type; + MatFX *matfx; + + matfx = MatFX::get(mat); + if(matfx == nil){ + matfx = new MatFX; + memset(matfx, 0, sizeof(MatFX)); + *PLUGINOFFSET(MatFX*, mat, matFXGlobals.materialOffset) = matfx; + } + + if(matfx->type != 0 && matfx->type != type) + clearMatFX(matfx); + matfx->type = type; switch(type){ case BUMPMAP: case ENVMAP: case DUAL: case UVTRANSFORM: - this->fx[0].type = type; - this->fx[1].type = NOTHING; + matfx->fx[0].type = type; + matfx->fx[1].type = NOTHING; break; case BUMPENVMAP: - this->fx[0].type = BUMPMAP; - this->fx[1].type = ENVMAP; + matfx->fx[0].type = BUMPMAP; + matfx->fx[1].type = ENVMAP; break; case DUALUVTRANSFORM: - this->fx[0].type = UVTRANSFORM; - this->fx[1].type = DUAL; + matfx->fx[0].type = UVTRANSFORM; + matfx->fx[1].type = DUAL; break; } } @@ -261,16 +270,17 @@ copyMaterialMatFX(void *dst, void *src, int32 offset, int32) static Stream* readMaterialMatFX(Stream *stream, int32, void *object, int32 offset, int32) { + Material *mat; + MatFX *matfx; Texture *tex, *bumpedTex; float coefficient; int32 fbAlpha; int32 srcBlend, dstBlend; int32 idx; - MatFX *matfx = new MatFX; - memset(matfx, 0, sizeof(MatFX)); - *PLUGINOFFSET(MatFX*, object, offset) = matfx; - matfx->setEffects(stream->readU32()); + mat = (Material*)object; + MatFX::setEffects(mat, stream->readU32()); + matfx = MatFX::get(mat); for(int i = 0; i < 2; i++){ uint32 type = stream->readU32(); @@ -390,7 +400,7 @@ getSizeMaterialMatFX(void *object, int32 offset, int32) return -1; int32 size = 4 + 4 + 4; - for(int i = 0; i < 2; i++) + for(int i = 0; i < 2; i++){ switch(matfx->fx[i].type){ case MatFX::BUMPMAP: size += 4 + 4 + 4; @@ -416,6 +426,7 @@ getSizeMaterialMatFX(void *object, int32 offset, int32) matfx->fx[i].dual.tex->streamGetSize(); break; } + } return size; } diff --git a/src/rwbase.h b/src/rwbase.h index 85b64a0..f313b1c 100644 --- a/src/rwbase.h +++ b/src/rwbase.h @@ -300,6 +300,7 @@ class StreamFile : public Stream { FILE *file; public: + StreamFile(void) { file = nil; } void close(void); uint32 write(const void *data, uint32 length); uint32 read(void *data, uint32 length); diff --git a/src/rwplugins.h b/src/rwplugins.h index 8793d7e..111c19d 100644 --- a/src/rwplugins.h +++ b/src/rwplugins.h @@ -116,6 +116,7 @@ struct MatFX float *dualTransform; }; struct { +// uint32 foo[32]; uint32 type; union { Bump bump; @@ -126,7 +127,7 @@ struct MatFX } fx[2]; uint32 type; - void setEffects(uint32 flags); + static void setEffects(Material *m, uint32 flags); static uint32 getEffects(Material *m); static MatFX *get(Material *m); uint32 getEffectIndex(uint32 type);