]> git.jsancho.org Git - lugaru.git/blob - Source/Animation/Animation.cpp
a8ad6e19cf448dba073e62923b5d527d84af1290
[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, ...) \
31     if (id < loadable_anim_end)                     \
32         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     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 (i = 0; i < frames.back().joints.size(); i++) {
145         if (frames.back().joints[i].position.y < 1) {
146             endoffset += frames.back().joints[i].position;
147         }
148     }
149     endoffset /= numjoints;
150     offset = endoffset;
151     offset.y = 0;
152 }