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