2 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
4 This file is part of Lugaru.
6 Lugaru is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 Lugaru is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Lugaru. If not, see <http://www.gnu.org/licenses/>.
20 #include "Animation/Animation.hpp"
22 #include "Animation/Skeleton.hpp"
24 #include "Utils/Folders.hpp"
26 std::vector<Animation> Animation::animations;
28 void Animation::loadAll()
30 #define DECLARE_ANIM(id, file, height, attack, ...) if (id < loadable_anim_end) animations.emplace_back(file, height, attack);
31 #include "Animation.def"
35 void AnimationFrame::loadBaseInfo(FILE* tfile)
37 // for each joint in the skeleton...
38 for (unsigned j = 0; j < joints.size(); j++) {
39 // read joint position
40 funpackf(tfile, "Bf Bf Bf", &joints[j].position.x, &joints[j].position.y, &joints[j].position.z);
42 for (unsigned j = 0; j < joints.size(); j++) {
44 funpackf(tfile, "Bf", &joints[j].twist);
46 for (unsigned j = 0; j < joints.size(); j++) {
47 // read onground (boolean)
49 funpackf(tfile, "Bb", &uch);
50 joints[j].onground = (uch != 0);
52 // read frame speed (?)
53 funpackf(tfile, "Bf", &speed);
56 void AnimationFrame::loadTwist2(FILE* tfile)
58 for (unsigned j = 0; j < joints.size(); j++) {
59 funpackf(tfile, "Bf", &joints[j].twist2);
63 void AnimationFrame::loadLabel(FILE* tfile)
65 funpackf(tfile, "Bf", &label);
68 void AnimationFrame::loadWeaponTarget(FILE* tfile)
70 funpackf(tfile, "Bf Bf Bf", &weapontarget.x, &weapontarget.y, &weapontarget.z);
73 Animation::Animation():
81 * load an animation from file
83 Animation::Animation(const std::string& filename, anim_height_type aheight, anim_attack_type aattack):
92 // Changing the filename into something the OS can understand
93 std::string filepath = Folders::getResourcePath("Animations/"+filename);
95 LOG(std::string("Loading animation...") + filepath);
100 Game::LoadingScreen();
102 // read file in binary mode
103 tfile = Folders::openMandatoryFile( filepath, "rb" );
105 // read numframes, joints to know how much memory to allocate
106 funpackf(tfile, "Bi Bi", &numframes, &numjoints);
108 // allocate memory for everything
110 frames.resize(numframes);
112 // read binary data as animation
115 for (i = 0; i < frames.size(); i++) {
116 frames[i].joints.resize(numjoints);
117 frames[i].loadBaseInfo(tfile);
119 // read twist2 for whole animation
120 for (i = 0; i < frames.size(); i++) {
121 frames[i].loadTwist2(tfile);
123 // read label for each frame
124 for (i = 0; i < frames.size(); i++) {
125 frames[i].loadLabel(tfile);
127 // read unused weapontargetnum
129 funpackf(tfile, "Bi", &weapontargetnum);
130 // read weapontarget positions for each frame
131 for (i = 0; i < frames.size(); i++) {
132 frames[i].loadWeaponTarget(tfile);
139 // find average position of certain joints on last frames
140 // and save in endoffset
141 // (not sure what exactly this accomplishes. the y < 1 test confuses me.)
142 for (i = 0; i < frames.back().joints.size(); i++) {
143 if (frames.back().joints[i].position.y < 1) {
144 endoffset += frames.back().joints[i].position;
147 endoffset /= numjoints;