]> git.jsancho.org Git - lugaru.git/blobdiff - Source/Animation/Animation.cpp
Applied clang-format on all files
[lugaru.git] / Source / Animation / Animation.cpp
index 3ccd17cf5a5db262777eda841bd243cf1a299561..a8ad6e19cf448dba073e62923b5d527d84af1290 100644 (file)
@@ -17,283 +17,136 @@ You should have received a copy of the GNU General Public License
 along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#include "Animation/Skeleton.h"
-#include "Animation/Animation.h"
-#include "Utils/Folders.h"
-#include "Game.h"
+#include "Animation/Animation.hpp"
 
-extern bool visibleloading;
+#include "Animation/Skeleton.hpp"
+#include "Game.hpp"
+#include "Utils/Folders.hpp"
 
-//~ struct animation_data_elt {
-    //~ const std::string& filename;
-    //~ int height;
-    //~ int attack;
-//~ };
-
-//~ static animation_data_elt animation_data[animation_count] = {
-//~ #define DECLARE_ANIM(id, file, height, attack, ...) {file, height, attack},
-//~ #include "Animation.def"
-//~ #undef DECLARE_ANIM
-//~ };
 std::vector<Animation> Animation::animations;
 
 void Animation::loadAll()
 {
-    int i = 0;
-#define DECLARE_ANIM(id, file, height, attack, ...) if (i++ < loadable_anim_end) animations.emplace_back(file, height, attack);
+#define DECLARE_ANIM(id, file, height, attack, ...) \
+    if (id < loadable_anim_end)                     \
+        animations.emplace_back(file, height, attack);
 #include "Animation.def"
 #undef DECLARE_ANIM
 }
 
+void AnimationFrame::loadBaseInfo(FILE* tfile)
+{
+    // for each joint in the skeleton...
+    for (unsigned j = 0; j < joints.size(); j++) {
+        // read joint position
+        funpackf(tfile, "Bf Bf Bf", &joints[j].position.x, &joints[j].position.y, &joints[j].position.z);
+    }
+    for (unsigned j = 0; j < joints.size(); j++) {
+        // read twist
+        funpackf(tfile, "Bf", &joints[j].twist);
+    }
+    for (unsigned j = 0; j < joints.size(); j++) {
+        // read onground (boolean)
+        unsigned char uch;
+        funpackf(tfile, "Bb", &uch);
+        joints[j].onground = (uch != 0);
+    }
+    // read frame speed (?)
+    funpackf(tfile, "Bf", &speed);
+}
+
+void AnimationFrame::loadTwist2(FILE* tfile)
+{
+    for (unsigned j = 0; j < joints.size(); j++) {
+        funpackf(tfile, "Bf", &joints[j].twist2);
+    }
+}
+
+void AnimationFrame::loadLabel(FILE* tfile)
+{
+    funpackf(tfile, "Bf", &label);
+}
 
-Animation::Animation():
-    numframes(0),
-    height(0),
-    attack(0),
-    joints(0),
-    weapontargetnum(0),
+void AnimationFrame::loadWeaponTarget(FILE* tfile)
+{
+    funpackf(tfile, "Bf Bf Bf", &weapontarget.x, &weapontarget.y, &weapontarget.z);
+}
 
-    position(0),
-    twist(0),
-    twist2(0),
-    speed(0),
-    onground(0),
-    forward(0),
-    label(0),
-    weapontarget(0)
+Animation::Animation()
+    : height(lowheight)
+    , attack(neutral)
+    , numjoints(0)
 {
 }
 
 /* EFFECT
  * load an animation from file
  */
-Animation::Animation(const std::string& filename, int aheight, int aattack):
-    Animation()
+Animation::Animation(const std::string& filename, anim_height_type aheight, anim_attack_type aattack)
+    Animation()
 {
-    FILE *tfile;
-    int i, j;
-    XYZ endoffset;
+    FILEtfile;
+    int numframes;
+    unsigned i;
 
     LOGFUNC;
 
     // Changing the filename into something the OS can understand
-    std::string filepath = Folders::getResourcePath("Animations/"+filename);
+    std::string filepath = Folders::getResourcePath("Animations/" + filename);
 
     LOG(std::string("Loading animation...") + filepath);
 
     height = aheight;
     attack = aattack;
 
-    if (visibleloading)
-        Game::LoadingScreen();
+    Game::LoadingScreen();
 
     // read file in binary mode
-    tfile = Folders::openMandatoryFile( filepath, "rb" );
+    tfile = Folders::openMandatoryFile(filepath, "rb");
 
     // read numframes, joints to know how much memory to allocate
-    funpackf(tfile, "Bi Bi", &numframes, &joints);
+    funpackf(tfile, "Bi Bi", &numframes, &numjoints);
 
     // allocate memory for everything
 
-    position = (XYZ**)malloc(sizeof(XYZ*) * joints);
-    for (i = 0; i < joints; i++)
-        position[i] = (XYZ*)malloc(sizeof(XYZ) * numframes);
-
-    twist = (float**)malloc(sizeof(float*) * joints);
-    for (i = 0; i < joints; i++)
-        twist[i] = (float*)malloc(sizeof(float) * numframes);
-
-    twist2 = (float**)malloc(sizeof(float*) * joints);
-    for (i = 0; i < joints; i++)
-        twist2[i] = (float*)malloc(sizeof(float) * numframes);
-
-    speed = (float*)malloc(sizeof(float) * numframes);
-
-    onground = (bool**)malloc(sizeof(bool*) * joints);
-    for (i = 0; i < joints; i++)
-        onground[i] = (bool*)malloc(sizeof(bool) * numframes);
-
-    forward = (XYZ*)malloc(sizeof(XYZ) * numframes);
-    weapontarget = (XYZ*)malloc(sizeof(XYZ) * numframes);
-    label = (int*)malloc(sizeof(int) * numframes);
+    frames.resize(numframes);
 
     // read binary data as animation
 
     // for each frame...
-    for (i = 0; i < numframes; i++) {
-        // for each joint in the skeleton...
-        for (j = 0; j < joints; j++) {
-            // read joint position
-            funpackf(tfile, "Bf Bf Bf", &position[j][i].x, &position[j][i].y, &position[j][i].z);
-        }
-        for (j = 0; j < joints; j++) {
-            // read twist
-            funpackf(tfile, "Bf", &twist[j][i]);
-        }
-        for (j = 0; j < joints; j++) {
-            // read onground (boolean)
-            unsigned char uch;
-            funpackf(tfile, "Bb", &uch);
-            onground[j][i] = (uch != 0);
-        }
-        // read frame speed (?)
-        funpackf(tfile, "Bf", &speed[i]);
+    for (i = 0; i < frames.size(); i++) {
+        frames[i].joints.resize(numjoints);
+        frames[i].loadBaseInfo(tfile);
     }
     // read twist2 for whole animation
-    for (i = 0; i < numframes; i++) {
-        for (j = 0; j < joints; j++) {
-            funpackf(tfile, "Bf", &twist2[j][i]);
-        }
+    for (i = 0; i < frames.size(); i++) {
+        frames[i].loadTwist2(tfile);
     }
     // read label for each frame
-    for (i = 0; i < numframes; i++) {
-        funpackf(tfile, "Bf", &label[i]);
+    for (i = 0; i < frames.size(); i++) {
+        frames[i].loadLabel(tfile);
     }
-    // read weapontargetnum
+    // read unused weapontargetnum
+    int weapontargetnum;
     funpackf(tfile, "Bi", &weapontargetnum);
     // read weapontarget positions for each frame
-    for (i = 0; i < numframes; i++) {
-        funpackf(tfile, "Bf Bf Bf", &weapontarget[i].x, &weapontarget[i].y, &weapontarget[i].z);
+    for (i = 0; i < frames.size(); i++) {
+        frames[i].loadWeaponTarget(tfile);
     }
 
     fclose(tfile);
 
+    XYZ endoffset;
     endoffset = 0;
     // find average position of certain joints on last frames
     // and save in endoffset
     // (not sure what exactly this accomplishes. the y < 1 test confuses me.)
-    for (j = 0; j < joints; j++) {
-        if (position[j][numframes - 1].y < 1)
-            endoffset += position[j][numframes - 1];
+    for (i = 0; i < frames.back().joints.size(); i++) {
+        if (frames.back().joints[i].position.y < 1) {
+            endoffset += frames.back().joints[i].position;
+        }
     }
-    endoffset /= joints;
+    endoffset /= numjoints;
     offset = endoffset;
     offset.y = 0;
 }
-
-Animation::~Animation()
-{
-    //~ deallocate();
-}
-
-void Animation::deallocate()
-{
-    int i = 0;
-
-    if (position) {
-        for (i = 0; i < joints; i++)
-            free(position[i]);
-
-        free(position);
-    }
-    position = 0;
-
-    if (twist) {
-        for (i = 0; i < joints; i++)
-            free(twist[i]);
-
-        free(twist);
-    }
-    twist = 0;
-
-    if (twist2) {
-        for (i = 0; i < joints; i++)
-            free(twist2[i]);
-
-        free(twist2);
-    }
-    twist2 = 0;
-
-    if (onground) {
-        for (i = 0; i < joints; i++)
-            free(onground[i]);
-
-        free(onground);
-    }
-    onground = 0;
-
-    if (speed)
-        free(speed);
-    speed = 0;
-
-    if (forward)
-        free(forward);
-    forward = 0;
-
-    if (weapontarget)
-        free(weapontarget);
-    weapontarget = 0;
-
-    if (label)
-        free(label);
-    label = 0;
-
-    joints = 0;
-}
-
-Animation & Animation::operator = (const Animation & ani)
-{
-    int i = 0;
-
-    bool allocate = ((ani.numframes != numframes) || (ani.joints != joints));
-
-    if (allocate)
-        deallocate();
-
-    numframes = ani.numframes;
-    height = ani.height;
-    attack = ani.attack;
-    joints = ani.joints;
-    weapontargetnum = ani.weapontargetnum;
-    offset = ani.offset;
-
-    if (allocate)
-        position = (XYZ**)malloc(sizeof(XYZ*)*ani.joints);
-    for (i = 0; i < ani.joints; i++) {
-        if (allocate)
-            position[i] = (XYZ*)malloc(sizeof(XYZ) * ani.numframes);
-        memcpy(position[i], ani.position[i], sizeof(XYZ)*ani.numframes);
-    }
-
-    if (allocate)
-        twist = (float**)malloc(sizeof(float*)*ani.joints);
-    for (i = 0; i < ani.joints; i++) {
-        if (allocate)
-            twist[i] = (float*)malloc(sizeof(float) * ani.numframes);
-        memcpy(twist[i], ani.twist[i], sizeof(float)*ani.numframes);
-    }
-
-    if (allocate)
-        twist2 = (float**)malloc(sizeof(float*)*ani.joints);
-    for (i = 0; i < ani.joints; i++) {
-        if (allocate)
-            twist2[i] = (float*)malloc(sizeof(float) * ani.numframes);
-        memcpy(twist2[i], ani.twist2[i], sizeof(float)*ani.numframes);
-    }
-
-    if (allocate)
-        speed = (float*)malloc(sizeof(float) * ani.numframes);
-    memcpy(speed, ani.speed, sizeof(float)*ani.numframes);
-
-    if (allocate)
-        onground = (bool**)malloc(sizeof(bool*)*ani.joints);
-    for (i = 0; i < ani.joints; i++) {
-        if (allocate)
-            onground[i] = (bool*)malloc(sizeof(bool) * ani.numframes);
-        memcpy(onground[i], ani.onground[i], sizeof(bool)*ani.numframes);
-    }
-
-    if (allocate)
-        forward = (XYZ*)malloc(sizeof(XYZ) * ani.numframes);
-    memcpy(forward, ani.forward, sizeof(XYZ)*ani.numframes);
-
-    if (allocate)
-        weapontarget = (XYZ*)malloc(sizeof(XYZ) * ani.numframes);
-    memcpy(weapontarget, ani.weapontarget, sizeof(XYZ)*ani.numframes);
-
-    if (allocate)
-        label = (int*)malloc(sizeof(int) * ani.numframes);
-    memcpy(label, ani.label, sizeof(int)*ani.numframes);
-
-    return (*this);
-}