]> git.jsancho.org Git - lugaru.git/blob - Source/Animation/Animation.cpp
Using the right type for animation height and type
[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/Skeleton.h"
21 #include "Animation/Animation.h"
22 #include "Utils/Folders.h"
23 #include "Game.h"
24
25 extern bool visibleloading;
26
27 std::vector<Animation> Animation::animations;
28
29 void Animation::loadAll()
30 {
31 #define DECLARE_ANIM(id, file, height, attack, ...) if (id < loadable_anim_end) animations.emplace_back(file, height, attack);
32 #include "Animation.def"
33 #undef DECLARE_ANIM
34 }
35
36 void AnimationFrame::loadBaseInfo(FILE* tfile)
37 {
38     // for each joint in the skeleton...
39     for (unsigned j = 0; j < joints.size(); j++) {
40         // read joint position
41         funpackf(tfile, "Bf Bf Bf", &joints[j].position.x, &joints[j].position.y, &joints[j].position.z);
42     }
43     for (unsigned j = 0; j < joints.size(); j++) {
44         // read twist
45         funpackf(tfile, "Bf", &joints[j].twist);
46     }
47     for (unsigned j = 0; j < joints.size(); j++) {
48         // read onground (boolean)
49         unsigned char uch;
50         funpackf(tfile, "Bb", &uch);
51         joints[j].onground = (uch != 0);
52     }
53     // read frame speed (?)
54     funpackf(tfile, "Bf", &speed);
55 }
56
57 void AnimationFrame::loadTwist2(FILE* tfile)
58 {
59     for (unsigned j = 0; j < joints.size(); j++) {
60         funpackf(tfile, "Bf", &joints[j].twist2);
61     }
62 }
63
64 void AnimationFrame::loadLabel(FILE* tfile)
65 {
66     funpackf(tfile, "Bf", &label);
67 }
68
69 void AnimationFrame::loadWeaponTarget(FILE* tfile)
70 {
71     funpackf(tfile, "Bf Bf Bf", &weapontarget.x, &weapontarget.y, &weapontarget.z);
72 }
73
74 Animation::Animation():
75     height(lowheight),
76     attack(neutral),
77     numjoints(0)
78 {
79 }
80
81 /* EFFECT
82  * load an animation from file
83  */
84 Animation::Animation(const std::string& filename, anim_height_type aheight, anim_attack_type aattack):
85     Animation()
86 {
87     FILE *tfile;
88     int i, j, numframes;
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     if (visibleloading)
101         Game::LoadingScreen();
102
103     // read file in binary mode
104     tfile = Folders::openMandatoryFile( filepath, "rb" );
105
106     // read numframes, joints to know how much memory to allocate
107     funpackf(tfile, "Bi Bi", &numframes, &numjoints);
108
109     // allocate memory for everything
110
111     frames.resize(numframes);
112
113     // read binary data as animation
114
115     // for each frame...
116     for (i = 0; i < frames.size(); i++) {
117         frames[i].joints.resize(numjoints);
118         frames[i].loadBaseInfo(tfile);
119     }
120     // read twist2 for whole animation
121     for (i = 0; i < frames.size(); i++) {
122         frames[i].loadTwist2(tfile);
123     }
124     // read label for each frame
125     for (i = 0; i < frames.size(); i++) {
126         frames[i].loadLabel(tfile);
127     }
128     // read unused weapontargetnum
129     int weapontargetnum;
130     funpackf(tfile, "Bi", &weapontargetnum);
131     // read weapontarget positions for each frame
132     for (i = 0; i < frames.size(); i++) {
133         frames[i].loadWeaponTarget(tfile);
134     }
135
136     fclose(tfile);
137
138     XYZ endoffset;
139     endoffset = 0;
140     // find average position of certain joints on last frames
141     // and save in endoffset
142     // (not sure what exactly this accomplishes. the y < 1 test confuses me.)
143     for (j = 0; j < numjoints; j++) {
144         if (frames.back().joints[j].position.y < 1) {
145             endoffset += frames.back().joints[j].position;
146         }
147     }
148     endoffset /= numjoints;
149     offset = endoffset;
150     offset.y = 0;
151 }
152
153 Animation::~Animation()
154 {
155 }