]> git.jsancho.org Git - lugaru.git/blobdiff - Source/Animation/Skeleton.cpp
Got rid of malloc in Muscle
[lugaru.git] / Source / Animation / Skeleton.cpp
index aac06eee16c9f91c7ab66f898582f9340f68c4b8..065b28ef14cc6c983a2f8ae4761ecff5cb8326b5 100644 (file)
@@ -40,89 +40,55 @@ extern int whichjointendarray[26];
 
 extern bool visibleloading;
 
-/* EFFECT
- */
-void dealloc2(void* param)
+Skeleton::Skeleton()
 {
-    free(param);
-}
-
-enum {boneconnect, constraint, muscle};
-
+    num_joints = 0;
 
-/* EFFECT
- * sets strength, length,
- *      parent1->position, parent2->position,
- *      parent1->velocity, parent2->velocity
- * used for ragdolls?
- *
- * USES:
- * Skeleton::DoConstraints
- */
-void Muscle::DoConstraint(bool spinny)
-{
-    // FIXME: relaxlength shouldn't be static, but may not always be set
-    // so I don't want to change the existing behavior even though it's probably a bug
-    static float relaxlength;
+    num_muscles = 0;
 
-    float oldlength = length;
+    selected = 0;
 
-    if (type != boneconnect)
-        relaxlength = findDistance(&parent1->position, &parent2->position);
+    memset(forwardjoints, 0, sizeof(forwardjoints));
 
-    if (type == boneconnect)
-        strength = 1;
-    if (type == constraint)
-        strength = 0;
+    id = 0;
 
-    // clamp strength
-    if (strength < 0)
-        strength = 0;
-    if (strength > 1)
-        strength = 1;
+    memset(lowforwardjoints, 0, sizeof(lowforwardjoints));
 
-    length -= (length - relaxlength) * (1 - strength) * multiplier * 10000;
-    length -= (length - targetlength) * (strength) * multiplier * 10000;
-    if (strength == 0)
-        length = relaxlength;
+    memset(jointlabels, 0, sizeof(jointlabels));
 
-    if ((relaxlength - length > 0 && relaxlength - oldlength < 0) || (relaxlength - length < 0 && relaxlength - oldlength > 0))
-        length = relaxlength;
+    num_models = 0;
 
-    // clamp length
-    if (length < minlength)
-        length = minlength;
-    if (length > maxlength)
-        length = maxlength;
+    clothes = 0;
+    spinny = 0;
 
-    if (length == relaxlength)
-        return;
+    memset(skinText, 0, sizeof(skinText));
+    skinsize = 0;
 
-    // relax muscle?
+    checkdelay = 0;
 
-    //Find midpoint
-    XYZ midp = (parent1->position * parent1->mass + parent2->position * parent2->mass) / (parent1->mass + parent2->mass);
+    longdead = 0;
+    broken = 0;
 
-    //Find vector from midpoint to second vector
-    XYZ vel = parent2->position - midp;
+    free = 0;
+    oldfree = 0;
+    freetime = 0;
+    freefall = 0;
 
-    //Change to unit vector
-    Normalise(&vel);
+    joints = 0;
+    muscles = 0;
+}
 
-    //Apply velocity change
-    XYZ newpoint1 = midp - vel * length * (parent2->mass / (parent1->mass + parent2->mass));
-    XYZ newpoint2 = midp + vel * length * (parent1->mass / (parent1->mass + parent2->mass));
-    if (!freeze && spinny) {
-        parent1->velocity = parent1->velocity + (newpoint1 - parent1->position) / multiplier / 4;
-        parent2->velocity = parent2->velocity + (newpoint2 - parent2->position) / multiplier / 4;
-    } else {
-        parent1->velocity = parent1->velocity + (newpoint1 - parent1->position);
-        parent2->velocity = parent2->velocity + (newpoint2 - parent2->position);
+Skeleton::~Skeleton()
+{
+    if (muscles) {
+        delete [] muscles;
     }
+    muscles = 0;
 
-    //Move child point to within certain distance of parent point
-    parent1->position = newpoint1;
-    parent2->position = newpoint2;
+    if (joints) {
+        delete [] joints;
+    }
+    joints = 0;
 }
 
 /* EFFECT
@@ -709,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);
@@ -771,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
@@ -783,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 (?)
@@ -817,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;
             }
         }
@@ -827,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);
     }
@@ -877,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
@@ -897,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);
@@ -924,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;
         }
@@ -973,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
@@ -993,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);
@@ -1020,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;
             }
         }
@@ -1057,99 +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;
-}
-
-Muscle::Muscle()
-{
-    vertices = 0;
-    verticeslow = 0;
-    verticesclothes = 0;
-
-    numvertices = 0;
-    numverticeslow = 0;
-    numverticesclothes = 0;
-    length = 0;
-    targetlength = 0;
-    parent1 = 0;
-    parent2 = 0;
-    maxlength = 0;
-    minlength = 0;
-    type = 0;
-    visible = 0;
-    rotate1 = 0, rotate2 = 0, rotate3 = 0;
-    lastrotate1 = 0, lastrotate2 = 0, lastrotate3 = 0;
-    oldrotate1 = 0, oldrotate2 = 0, oldrotate3 = 0;
-    newrotate1 = 0, newrotate2 = 0, newrotate3 = 0;
-
-    strength = 0;
-}
-
-Muscle::~Muscle()
-{
-    dealloc2(vertices);
-    dealloc2(verticeslow);
-    dealloc2(verticesclothes);
-}
-
 #if 0
 
 // the following functions are not used anywhere
@@ -1187,19 +998,18 @@ void Skeleton::Draw(int muscleview)
 {
     static float jointcolor[4];
 
-    if (muscleview != 2) {
-        jointcolor[0] = 0;
-        jointcolor[1] = 0;
-        jointcolor[2] = .5;
-        jointcolor[3] = 1;
-    }
-
     if (muscleview == 2) {
         jointcolor[0] = 0;
         jointcolor[1] = 0;
         jointcolor[2] = 0;
         jointcolor[3] = .5;
+    } else {
+        jointcolor[0] = 0;
+        jointcolor[1] = 0;
+        jointcolor[2] = .5;
+        jointcolor[3] = 1;
     }
+
     //Calc motionblur-ness
     for (int i = 0; i < num_joints; i++) {
         joints[i].oldposition = joints[i].position;