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 extern bool visibleloading;
28 std::vector<Animation> Animation::animations;
30 void Animation::loadAll()
32 #define DECLARE_ANIM(id, file, height, attack, ...) if (id < loadable_anim_end) 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);
103 Game::LoadingScreen();
105 // read file in binary mode
106 tfile = Folders::openMandatoryFile( filepath, "rb" );
108 // read numframes, joints to know how much memory to allocate
109 funpackf(tfile, "Bi Bi", &numframes, &numjoints);
111 // allocate memory for everything
113 frames.resize(numframes);
115 // read binary data as animation
118 for (i = 0; i < frames.size(); i++) {
119 frames[i].joints.resize(numjoints);
120 frames[i].loadBaseInfo(tfile);
122 // read twist2 for whole animation
123 for (i = 0; i < frames.size(); i++) {
124 frames[i].loadTwist2(tfile);
126 // read label for each frame
127 for (i = 0; i < frames.size(); i++) {
128 frames[i].loadLabel(tfile);
130 // read unused weapontargetnum
132 funpackf(tfile, "Bi", &weapontargetnum);
133 // read weapontarget positions for each frame
134 for (i = 0; i < frames.size(); i++) {
135 frames[i].loadWeaponTarget(tfile);
142 // find average position of certain joints on last frames
143 // and save in endoffset
144 // (not sure what exactly this accomplishes. the y < 1 test confuses me.)
145 for (i = 0; i < frames.back().joints.size(); i++) {
146 if (frames.back().joints[i].position.y < 1) {
147 endoffset += frames.back().joints[i].position;
150 endoffset /= numjoints;