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, ...) \
31 if (id < loadable_anim_end) \
32 animations.emplace_back(file, height, attack);
33 #include "Animation.def"
37 void AnimationFrame::loadBaseInfo(FILE* tfile)
39 // for each joint in the skeleton...
40 for (unsigned j = 0; j < joints.size(); j++) {
41 // read joint position
42 funpackf(tfile, "Bf Bf Bf", &joints[j].position.x, &joints[j].position.y, &joints[j].position.z);
44 for (unsigned j = 0; j < joints.size(); j++) {
46 funpackf(tfile, "Bf", &joints[j].twist);
48 for (unsigned j = 0; j < joints.size(); j++) {
49 // read onground (boolean)
51 funpackf(tfile, "Bb", &uch);
52 joints[j].onground = (uch != 0);
54 // read frame speed (?)
55 funpackf(tfile, "Bf", &speed);
58 void AnimationFrame::loadTwist2(FILE* tfile)
60 for (unsigned j = 0; j < joints.size(); j++) {
61 funpackf(tfile, "Bf", &joints[j].twist2);
65 void AnimationFrame::loadLabel(FILE* tfile)
67 funpackf(tfile, "Bf", &label);
70 void AnimationFrame::loadWeaponTarget(FILE* tfile)
72 funpackf(tfile, "Bf Bf Bf", &weapontarget.x, &weapontarget.y, &weapontarget.z);
75 Animation::Animation()
83 * load an animation from file
85 Animation::Animation(const std::string& filename, anim_height_type aheight, anim_attack_type aattack)
94 // Changing the filename into something the OS can understand
95 std::string filepath = Folders::getResourcePath("Animations/" + filename);
97 LOG(std::string("Loading animation...") + filepath);
102 Game::LoadingScreen();
104 // read file in binary mode
105 tfile = Folders::openMandatoryFile(filepath, "rb");
107 // read numframes, joints to know how much memory to allocate
108 funpackf(tfile, "Bi Bi", &numframes, &numjoints);
110 // allocate memory for everything
112 frames.resize(numframes);
114 // read binary data as animation
117 for (i = 0; i < frames.size(); i++) {
118 frames[i].joints.resize(numjoints);
119 frames[i].loadBaseInfo(tfile);
121 // read twist2 for whole animation
122 for (i = 0; i < frames.size(); i++) {
123 frames[i].loadTwist2(tfile);
125 // read label for each frame
126 for (i = 0; i < frames.size(); i++) {
127 frames[i].loadLabel(tfile);
129 // read unused weapontargetnum
131 funpackf(tfile, "Bi", &weapontargetnum);
132 // read weapontarget positions for each frame
133 for (i = 0; i < frames.size(); i++) {
134 frames[i].loadWeaponTarget(tfile);
141 // find average position of certain joints on last frames
142 // and save in endoffset
143 // (not sure what exactly this accomplishes. the y < 1 test confuses me.)
144 for (i = 0; i < frames.back().joints.size(); i++) {
145 if (frames.back().joints[i].position.y < 1) {
146 endoffset += frames.back().joints[i].position;
149 endoffset /= numjoints;