From: Côme Chilliet Date: Fri, 9 Dec 2016 16:53:45 +0000 (+0700) Subject: Got rid of malloc in Muscle X-Git-Url: https://git.jsancho.org/?a=commitdiff_plain;h=18e718d3ff51c67029f2d4e1416f979b9ad3afc0;p=lugaru.git Got rid of malloc in Muscle --- diff --git a/Source/Animation/Muscle.cpp b/Source/Animation/Muscle.cpp index 4a5a047..1e7f0d5 100644 --- a/Source/Animation/Muscle.cpp +++ b/Source/Animation/Muscle.cpp @@ -19,19 +19,13 @@ along with Lugaru. If not, see . */ #include "Animation/Muscle.h" +#include "binio.h" extern float multiplier; extern bool freeze; Muscle::Muscle() { - vertices = 0; - verticeslow = 0; - verticesclothes = 0; - - numvertices = 0; - numverticeslow = 0; - numverticesclothes = 0; length = 0; targetlength = 0; parent1 = 0; @@ -50,9 +44,60 @@ Muscle::Muscle() Muscle::~Muscle() { - free(vertices); - free(verticeslow); - free(verticesclothes); +} + +void Muscle::load(FILE* tfile, int vertexNum, Joint* joints) +{ + int numvertices, vertice, parentID; + + // read info + funpackf(tfile, "Bf Bf Bf Bf Bf Bi Bi", &length, &targetlength, &minlength, &maxlength, &strength, &type, &numvertices); + + // read vertices + for (int j = 0; j < numvertices; j++) { + funpackf(tfile, "Bi", &vertice); + if (vertice < vertexNum) { + vertices.push_back(vertice); + } + } + + // read more info + funpackf(tfile, "Bb Bi", &visible, &parentID); + parent1 = &joints[parentID]; + funpackf(tfile, "Bi", &parentID); + parent2 = &joints[parentID]; +} + +void Muscle::loadVerticesLow(FILE* tfile, int vertexNum) +{ + int numvertices, vertice; + + // read numvertices + funpackf(tfile, "Bi", &numvertices); + + // read vertices + for (int j = 0; j < numvertices; j++) { + funpackf(tfile, "Bi", &vertice); + if (vertice < vertexNum) { + verticeslow.push_back(vertice); + } + } +} + +void Muscle::loadVerticesClothes(FILE* tfile, int vertexNum) +{ + int numvertices, vertice; + + // read numvertices + funpackf(tfile, "Bi", &numvertices); + + // read vertices + for (int j = 0; j < numvertices; j++) { + funpackf(tfile, "Bi", &vertice); + if (vertice < vertexNum) { + verticesclothes.push_back(vertice); + } + } } diff --git a/Source/Animation/Muscle.h b/Source/Animation/Muscle.h index cb85f85..5f51161 100644 --- a/Source/Animation/Muscle.h +++ b/Source/Animation/Muscle.h @@ -21,6 +21,7 @@ along with Lugaru. If not, see . #ifndef _MUSCLE_H_ #define _MUSCLE_H_ +#include #include "Animation/Joint.h" enum muscle_type {boneconnect, constraint, muscle}; @@ -28,12 +29,9 @@ enum muscle_type {boneconnect, constraint, muscle}; class Muscle { public: - int numvertices; - int* vertices; - int numverticeslow; - int* verticeslow; - int numverticesclothes; - int* verticesclothes; + std::vector vertices; + std::vector verticeslow; + std::vector verticesclothes; float length; float targetlength; Joint* parent1; @@ -51,6 +49,9 @@ public: Muscle(); ~Muscle(); + void load(FILE* tfile, int vertexNum, Joint* joints); + void loadVerticesLow(FILE* tfile, int vertexNum); + void loadVerticesClothes(FILE* tfile, int vertexNum); void DoConstraint(bool spinny); }; diff --git a/Source/Animation/Skeleton.cpp b/Source/Animation/Skeleton.cpp index ee19b81..065b28e 100644 --- a/Source/Animation/Skeleton.cpp +++ b/Source/Animation/Skeleton.cpp @@ -40,11 +40,55 @@ extern int whichjointendarray[26]; extern bool visibleloading; -/* EFFECT - */ -void dealloc2(void* param) +Skeleton::Skeleton() +{ + num_joints = 0; + + num_muscles = 0; + + selected = 0; + + memset(forwardjoints, 0, sizeof(forwardjoints)); + + id = 0; + + memset(lowforwardjoints, 0, sizeof(lowforwardjoints)); + + memset(jointlabels, 0, sizeof(jointlabels)); + + num_models = 0; + + clothes = 0; + spinny = 0; + + memset(skinText, 0, sizeof(skinText)); + skinsize = 0; + + checkdelay = 0; + + longdead = 0; + broken = 0; + + free = 0; + oldfree = 0; + freetime = 0; + freefall = 0; + + joints = 0; + muscles = 0; +} + +Skeleton::~Skeleton() { - free(param); + if (muscles) { + delete [] muscles; + } + muscles = 0; + + if (joints) { + delete [] joints; + } + joints = 0; } /* EFFECT @@ -631,10 +675,10 @@ void Skeleton::Load(const std::string& filename, const std::string& lowfil drawmodel.Rotate(180, 0, 0); drawmodel.Scale(.04, .04, .04); drawmodel.FlipTexCoords(); - if (tutoriallevel == 1 && id != 0) + if ((tutoriallevel == 1) && (id != 0)) { drawmodel.UniformTexCoords(); - if (tutoriallevel == 1 && id != 0) drawmodel.ScaleTexCoords(0.1); + } drawmodel.CalculateNormals(0); modellow.loadnotex(modellowfilename); @@ -693,6 +737,7 @@ void Skeleton::Load(const std::string& filename, const std::string& lowfil joints[i].parent = &joints[parentID]; joints[i].velocity = 0; joints[i].oldposition = joints[i].position; + joints[i].startpos = joints[i].position; } // read num_muscles @@ -705,27 +750,7 @@ void Skeleton::Load(const std::string& filename, const std::string& lowfil // for each muscle... for (i = 0; i < num_muscles; i++) { - // read info - funpackf(tfile, "Bf Bf Bf Bf Bf Bi Bi", &muscles[i].length, &muscles[i].targetlength, &muscles[i].minlength, &muscles[i].maxlength, &muscles[i].strength, &muscles[i].type, &muscles[i].numvertices); - - // allocate memory for vertices - muscles[i].vertices = (int*)malloc(sizeof(int) * muscles[i].numvertices); - - // read vertices - edit = 0; - for (j = 0; j < muscles[i].numvertices - edit; j++) { - funpackf(tfile, "Bi", &muscles[i].vertices[j + edit]); - if (muscles[i].vertices[j + edit] >= model[0].vertexNum) { - muscles[i].numvertices--; - edit--; - } - } - - // read more info - funpackf(tfile, "Bb Bi", &muscles[i].visible, &parentID); - muscles[i].parent1 = &joints[parentID]; - funpackf(tfile, "Bi", &parentID); - muscles[i].parent2 = &joints[parentID]; + muscles[i].load(tfile, model[0].vertexNum, joints); } // read forwardjoints (?) @@ -739,9 +764,9 @@ void Skeleton::Load(const std::string& filename, const std::string& lowfil // ??? for (j = 0; j < num_muscles; j++) { - for (i = 0; i < muscles[j].numvertices; i++) { + for (i = 0; i < muscles[j].vertices.size(); i++) { for (int k = 0; k < num_models; k++) { - if (muscles[j].numvertices && muscles[j].vertices[i] < model[k].vertexNum) + if (muscles[j].vertices[i] < model[k].vertexNum) model[k].owner[muscles[j].vertices[i]] = j; } } @@ -749,9 +774,6 @@ void Skeleton::Load(const std::string& filename, const std::string& lowfil // calculate some stuff FindForwards(); - for (i = 0; i < num_joints; i++) { - joints[i].startpos = joints[i].position; - } for (i = 0; i < num_muscles; i++) { FindRotationMuscle(i, -1); } @@ -799,11 +821,6 @@ void Skeleton::Load(const std::string& filename, const std::string& lowfil + 1 //sizeof(bool) + sizeof(int); fseek(tfile, lSize, SEEK_CUR); - - if (joints[i].hasparent) - joints[i].parent = &joints[parentID]; - joints[i].velocity = 0; - joints[i].oldposition = joints[i].position; } // read num_muscles @@ -819,23 +836,7 @@ void Skeleton::Load(const std::string& filename, const std::string& lowfil + sizeof(int); fseek(tfile, lSize, SEEK_CUR); - // read numverticeslow - funpackf(tfile, "Bi", &muscles[i].numverticeslow); - - if (muscles[i].numverticeslow) { - // allocate memory - muscles[i].verticeslow = (int*)malloc(sizeof(int) * muscles[i].numverticeslow); - - // read verticeslow - edit = 0; - for (j = 0; j < muscles[i].numverticeslow - edit; j++) { - funpackf(tfile, "Bi", &muscles[i].verticeslow[j + edit]); - if (muscles[i].verticeslow[j + edit] >= modellow.vertexNum) { - muscles[i].numverticeslow--; - edit--; - } - } - } + muscles[i].loadVerticesLow(tfile, modellow.vertexNum); // skip more stuff lSize = 1; //sizeof(bool); @@ -846,7 +847,7 @@ void Skeleton::Load(const std::string& filename, const std::string& lowfil } for (j = 0; j < num_muscles; j++) { - for (i = 0; i < muscles[j].numverticeslow; i++) { + for (i = 0; i < muscles[j].verticeslow.size(); i++) { if (muscles[j].verticeslow[i] < modellow.vertexNum) modellow.owner[muscles[j].verticeslow[i]] = j; } @@ -895,11 +896,6 @@ void Skeleton::Load(const std::string& filename, const std::string& lowfil + 1 //sizeof(bool) + sizeof(int); fseek(tfile, lSize, SEEK_CUR); - - if (joints[i].hasparent) - joints[i].parent = &joints[parentID]; - joints[i].velocity = 0; - joints[i].oldposition = joints[i].position; } // read num_muscles @@ -915,21 +911,7 @@ void Skeleton::Load(const std::string& filename, const std::string& lowfil + sizeof(int); fseek(tfile, lSize, SEEK_CUR); - // read numverticesclothes - funpackf(tfile, "Bi", &muscles[i].numverticesclothes); - - // read verticesclothes - if (muscles[i].numverticesclothes) { - muscles[i].verticesclothes = (int*)malloc(sizeof(int) * muscles[i].numverticesclothes); - edit = 0; - for (j = 0; j < muscles[i].numverticesclothes - edit; j++) { - funpackf(tfile, "Bi", &muscles[i].verticesclothes[j + edit]); - if (muscles[i].verticesclothes[j + edit] >= modelclothes.vertexNum) { - muscles[i].numverticesclothes--; - edit--; - } - } - } + muscles[i].loadVerticesClothes(tfile, modelclothes.vertexNum); // skip more stuff lSize = 1; //sizeof(bool); @@ -942,8 +924,8 @@ void Skeleton::Load(const std::string& filename, const std::string& lowfil // ??? lSize = sizeof(int); for (j = 0; j < num_muscles; j++) { - for (i = 0; i < muscles[j].numverticesclothes; i++) { - if (muscles[j].numverticesclothes && muscles[j].verticesclothes[i] < modelclothes.vertexNum) + for (i = 0; i < muscles[j].verticesclothes.size(); i++) { + if (muscles[j].verticesclothes.size() && muscles[j].verticesclothes[i] < modelclothes.vertexNum) modelclothes.owner[muscles[j].verticesclothes[i]] = j; } } @@ -979,67 +961,6 @@ void Skeleton::Load(const std::string& filename, const std::string& lowfil free = 0; } -Skeleton::Skeleton() -{ - num_joints = 0; - - num_muscles = 0; - - selected = 0; - - memset(forwardjoints, 0, sizeof(forwardjoints)); - // XYZ forward; - - id = 0; - - memset(lowforwardjoints, 0, sizeof(lowforwardjoints)); - // XYZ lowforward; - - // XYZ specialforward[5]; - memset(jointlabels, 0, sizeof(jointlabels)); - - // Model model[7]; - // Model modellow; - // Model modelclothes; - num_models = 0; - - // Model drawmodel; - // Model drawmodellow; - // Model drawmodelclothes; - - clothes = 0; - spinny = 0; - - memset(skinText, 0, sizeof(skinText)); - skinsize = 0; - - checkdelay = 0; - - longdead = 0; - broken = 0; - - free = 0; - oldfree = 0; - freetime = 0; - freefall = 0; - - joints = 0; - muscles = 0; -} - -Skeleton::~Skeleton() -{ - if (muscles) { - delete [] muscles; - } - muscles = 0; - - if (joints) { - delete [] joints; - } - joints = 0; -} - #if 0 // the following functions are not used anywhere diff --git a/Source/Person.cpp b/Source/Person.cpp index dfc0c56..ca54e70 100644 --- a/Source/Person.cpp +++ b/Source/Person.cpp @@ -6106,7 +6106,7 @@ int Person::DrawSkeleton() const int p1 = skeleton.muscles[i].parent1->label; const int p2 = skeleton.muscles[i].parent2->label; - if ((skeleton.muscles[i].numvertices > 0 && playerdetail) || (skeleton.muscles[i].numverticeslow > 0 && !playerdetail)) { + if ((skeleton.muscles[i].vertices.size() > 0 && playerdetail) || (skeleton.muscles[i].verticeslow.size() > 0 && !playerdetail)) { morphness = 0; start = 0; endthing = 0; @@ -6160,7 +6160,7 @@ int Person::DrawSkeleton() glRotatef(-skeleton.muscles[i].lastrotate3, 0, 1, 0); if (playerdetail || skeleton.free == 3) { - for (j = 0; j < skeleton.muscles[i].numvertices; j++) { + for (j = 0; j < skeleton.muscles[i].vertices.size(); j++) { XYZ &v0 = skeleton.model[start].vertex[skeleton.muscles[i].vertices[j]]; XYZ &v1 = skeleton.model[endthing].vertex[skeleton.muscles[i].vertices[j]]; glMatrixMode(GL_MODELVIEW); @@ -6189,7 +6189,7 @@ int Person::DrawSkeleton() } } if (!playerdetail || skeleton.free == 3) { - for (j = 0; j < skeleton.muscles[i].numverticeslow; j++) { + for (j = 0; j < skeleton.muscles[i].verticeslow.size(); j++) { XYZ &v0 = skeleton.modellow.vertex[skeleton.muscles[i].verticeslow[j]]; glMatrixMode(GL_MODELVIEW); glPushMatrix(); @@ -6219,7 +6219,7 @@ int Person::DrawSkeleton() } glPopMatrix(); } - if (skeleton.clothes && skeleton.muscles[i].numverticesclothes > 0) { + if (skeleton.clothes && skeleton.muscles[i].verticesclothes.size() > 0) { mid = (skeleton.muscles[i].parent1->position + skeleton.muscles[i].parent2->position) / 2; glMatrixMode(GL_MODELVIEW); @@ -6239,7 +6239,7 @@ int Person::DrawSkeleton() skeleton.muscles[i].lastrotate3 = skeleton.muscles[i].rotate3; glRotatef(-skeleton.muscles[i].lastrotate3, 0, 1, 0); - for (j = 0; j < skeleton.muscles[i].numverticesclothes; j++) { + for (j = 0; j < skeleton.muscles[i].verticesclothes.size(); j++) { XYZ &v0 = skeleton.modelclothes.vertex[skeleton.muscles[i].verticesclothes[j]]; glMatrixMode(GL_MODELVIEW); glPushMatrix(); @@ -6447,12 +6447,12 @@ int Person::DrawSkeleton() if (weaponactive == k) { if (weapons[i].getType() != staff) { for (j = 0; j < skeleton.num_muscles; j++) { - if ((skeleton.muscles[j].parent1->label == righthand || skeleton.muscles[j].parent2->label == righthand) && skeleton.muscles[j].numvertices > 0) { + if ((skeleton.muscles[j].parent1->label == righthand || skeleton.muscles[j].parent2->label == righthand) && skeleton.muscles[j].vertices.size() > 0) { weaponattachmuscle = j; } } for (j = 0; j < skeleton.num_muscles; j++) { - if ((skeleton.muscles[j].parent1->label == rightwrist || skeleton.muscles[j].parent2->label == rightwrist) && (skeleton.muscles[j].parent1->label != righthand && skeleton.muscles[j].parent2->label != righthand) && skeleton.muscles[j].numvertices > 0) { + if ((skeleton.muscles[j].parent1->label == rightwrist || skeleton.muscles[j].parent2->label == rightwrist) && (skeleton.muscles[j].parent1->label != righthand && skeleton.muscles[j].parent2->label != righthand) && skeleton.muscles[j].vertices.size() > 0) { weaponrotatemuscle = j; } } @@ -6462,12 +6462,12 @@ int Person::DrawSkeleton() } if (weapons[i].getType() == staff) { for (j = 0; j < skeleton.num_muscles; j++) { - if ((skeleton.muscles[j].parent1->label == righthand || skeleton.muscles[j].parent2->label == righthand) && skeleton.muscles[j].numvertices > 0) { + if ((skeleton.muscles[j].parent1->label == righthand || skeleton.muscles[j].parent2->label == righthand) && skeleton.muscles[j].vertices.size() > 0) { weaponattachmuscle = j; } } for (j = 0; j < skeleton.num_muscles; j++) { - if ((skeleton.muscles[j].parent1->label == rightelbow || skeleton.muscles[j].parent2->label == rightelbow) && (skeleton.muscles[j].parent1->label != rightshoulder && skeleton.muscles[j].parent2->label != rightshoulder) && skeleton.muscles[j].numvertices > 0) { + if ((skeleton.muscles[j].parent1->label == rightelbow || skeleton.muscles[j].parent2->label == rightelbow) && (skeleton.muscles[j].parent1->label != rightshoulder && skeleton.muscles[j].parent2->label != rightshoulder) && skeleton.muscles[j].vertices.size() > 0) { weaponrotatemuscle = j; } } @@ -6491,7 +6491,7 @@ int Person::DrawSkeleton() if (weapons[i].getType() == staff) weaponpoint = jointPos(abdomen) + (jointPos(lefthip) - jointPos(righthip)) * .09 + (jointPos(leftshoulder) - jointPos(rightshoulder)) * .33; for (j = 0; j < skeleton.num_muscles; j++) { - if ((skeleton.muscles[j].parent1->label == abdomen || skeleton.muscles[j].parent2->label == abdomen) && (skeleton.muscles[j].parent1->label == neck || skeleton.muscles[j].parent2->label == neck) && skeleton.muscles[j].numvertices > 0) { + if ((skeleton.muscles[j].parent1->label == abdomen || skeleton.muscles[j].parent2->label == abdomen) && (skeleton.muscles[j].parent1->label == neck || skeleton.muscles[j].parent2->label == neck) && skeleton.muscles[j].vertices.size() > 0) { weaponrotatemuscle = j; } } @@ -6502,7 +6502,7 @@ int Person::DrawSkeleton() else weaponpoint = jointPos(abdomen) * .5 + jointPos(neck) * .5 + skeleton.forward * .8; for (j = 0; j < skeleton.num_muscles; j++) { - if ((skeleton.muscles[j].parent1->label == abdomen || skeleton.muscles[j].parent2->label == abdomen) && (skeleton.muscles[j].parent1->label == neck || skeleton.muscles[j].parent2->label == neck) && skeleton.muscles[j].numvertices > 0) { + if ((skeleton.muscles[j].parent1->label == abdomen || skeleton.muscles[j].parent2->label == abdomen) && (skeleton.muscles[j].parent1->label == neck || skeleton.muscles[j].parent2->label == neck) && skeleton.muscles[j].vertices.size() > 0) { weaponrotatemuscle = j; } }