]> git.jsancho.org Git - lugaru.git/blob - Source/Animation/Animation.cpp
Rename all C++ headers with .hpp extension
[lugaru.git] / Source / Animation / Animation.cpp
1 /*
2 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
3
4 This file is part of Lugaru.
5
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.
10
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.
15
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/>.
18 */
19
20 #include "Animation/Animation.hpp"
21
22 #include "Animation/Skeleton.hpp"
23 #include "Game.hpp"
24 #include "Utils/Folders.hpp"
25
26 extern bool visibleloading;
27
28 std::vector<Animation> Animation::animations;
29
30 void Animation::loadAll()
31 {
32 #define DECLARE_ANIM(id, file, height, attack, ...) if (id < loadable_anim_end) animations.emplace_back(file, height, attack);
33 #include "Animation.def"
34 #undef DECLARE_ANIM
35 }
36
37 void AnimationFrame::loadBaseInfo(FILE* tfile)
38 {
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);
43     }
44     for (unsigned j = 0; j < joints.size(); j++) {
45         // read twist
46         funpackf(tfile, "Bf", &joints[j].twist);
47     }
48     for (unsigned j = 0; j < joints.size(); j++) {
49         // read onground (boolean)
50         unsigned char uch;
51         funpackf(tfile, "Bb", &uch);
52         joints[j].onground = (uch != 0);
53     }
54     // read frame speed (?)
55     funpackf(tfile, "Bf", &speed);
56 }
57
58 void AnimationFrame::loadTwist2(FILE* tfile)
59 {
60     for (unsigned j = 0; j < joints.size(); j++) {
61         funpackf(tfile, "Bf", &joints[j].twist2);
62     }
63 }
64
65 void AnimationFrame::loadLabel(FILE* tfile)
66 {
67     funpackf(tfile, "Bf", &label);
68 }
69
70 void AnimationFrame::loadWeaponTarget(FILE* tfile)
71 {
72     funpackf(tfile, "Bf Bf Bf", &weapontarget.x, &weapontarget.y, &weapontarget.z);
73 }
74
75 Animation::Animation():
76     height(lowheight),
77     attack(neutral),
78     numjoints(0)
79 {
80 }
81
82 /* EFFECT
83  * load an animation from file
84  */
85 Animation::Animation(const std::string& filename, anim_height_type aheight, anim_attack_type aattack):
86     Animation()
87 {
88     FILE *tfile;
89     int i, j, numframes;
90
91     LOGFUNC;
92
93     // Changing the filename into something the OS can understand
94     std::string filepath = Folders::getResourcePath("Animations/"+filename);
95
96     LOG(std::string("Loading animation...") + filepath);
97
98     height = aheight;
99     attack = aattack;
100
101     if (visibleloading)
102         Game::LoadingScreen();
103
104     // read file in binary mode
105     tfile = Folders::openMandatoryFile( filepath, "rb" );
106
107     // read numframes, joints to know how much memory to allocate
108     funpackf(tfile, "Bi Bi", &numframes, &numjoints);
109
110     // allocate memory for everything
111
112     frames.resize(numframes);
113
114     // read binary data as animation
115
116     // for each frame...
117     for (i = 0; i < frames.size(); i++) {
118         frames[i].joints.resize(numjoints);
119         frames[i].loadBaseInfo(tfile);
120     }
121     // read twist2 for whole animation
122     for (i = 0; i < frames.size(); i++) {
123         frames[i].loadTwist2(tfile);
124     }
125     // read label for each frame
126     for (i = 0; i < frames.size(); i++) {
127         frames[i].loadLabel(tfile);
128     }
129     // read unused weapontargetnum
130     int 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);
135     }
136
137     fclose(tfile);
138
139     XYZ endoffset;
140     endoffset = 0;
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 (j = 0; j < numjoints; j++) {
145         if (frames.back().joints[j].position.y < 1) {
146             endoffset += frames.back().joints[j].position;
147         }
148     }
149     endoffset /= numjoints;
150     offset = endoffset;
151     offset.y = 0;
152 }