]> git.jsancho.org Git - lugaru.git/blob - Source/Animation/Animation.cpp
89aa9f4da02f6a671b878c935f71e6eb239b85aa
[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 numframes;
90     unsigned i;
91
92     LOGFUNC;
93
94     // Changing the filename into something the OS can understand
95     std::string filepath = Folders::getResourcePath("Animations/"+filename);
96
97     LOG(std::string("Loading animation...") + filepath);
98
99     height = aheight;
100     attack = aattack;
101
102     if (visibleloading)
103         Game::LoadingScreen();
104
105     // read file in binary mode
106     tfile = Folders::openMandatoryFile( filepath, "rb" );
107
108     // read numframes, joints to know how much memory to allocate
109     funpackf(tfile, "Bi Bi", &numframes, &numjoints);
110
111     // allocate memory for everything
112
113     frames.resize(numframes);
114
115     // read binary data as animation
116
117     // for each frame...
118     for (i = 0; i < frames.size(); i++) {
119         frames[i].joints.resize(numjoints);
120         frames[i].loadBaseInfo(tfile);
121     }
122     // read twist2 for whole animation
123     for (i = 0; i < frames.size(); i++) {
124         frames[i].loadTwist2(tfile);
125     }
126     // read label for each frame
127     for (i = 0; i < frames.size(); i++) {
128         frames[i].loadLabel(tfile);
129     }
130     // read unused weapontargetnum
131     int 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);
136     }
137
138     fclose(tfile);
139
140     XYZ endoffset;
141     endoffset = 0;
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;
148         }
149     }
150     endoffset /= numjoints;
151     offset = endoffset;
152     offset.y = 0;
153 }