2 Copyright (C) 2010-2016 - Lugaru contributors (see AUTHORS file)
4 This file is part of Lugaru.
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.
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.
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/>.
20 #include "Animation/Skeleton.h"
21 #include "Animation/Animation.h"
22 #include "Utils/Folders.h"
25 extern bool visibleloading;
27 //~ struct animation_data_elt {
28 //~ const std::string& filename;
33 //~ static animation_data_elt animation_data[animation_count] = {
34 //~ #define DECLARE_ANIM(id, file, height, attack, ...) {file, height, attack},
35 //~ #include "Animation.def"
36 //~ #undef DECLARE_ANIM
38 std::vector<Animation> Animation::animations;
40 void Animation::loadAll()
43 #define DECLARE_ANIM(id, file, height, attack, ...) if (i++ < loadable_anim_end) animations.emplace_back(file, height, attack);
44 #include "Animation.def"
49 Animation::Animation():
68 * load an animation from file
70 Animation::Animation(const std::string& filename, int aheight, int aattack):
79 // Changing the filename into something the OS can understand
80 std::string filepath = Folders::getResourcePath("Animations/"+filename);
82 LOG(std::string("Loading animation...") + filepath);
88 Game::LoadingScreen();
90 // read file in binary mode
91 tfile = Folders::openMandatoryFile( filepath, "rb" );
93 // read numframes, joints to know how much memory to allocate
94 funpackf(tfile, "Bi Bi", &numframes, &joints);
96 // allocate memory for everything
98 position = (XYZ**)malloc(sizeof(XYZ*) * joints);
99 for (i = 0; i < joints; i++)
100 position[i] = (XYZ*)malloc(sizeof(XYZ) * numframes);
102 twist = (float**)malloc(sizeof(float*) * joints);
103 for (i = 0; i < joints; i++)
104 twist[i] = (float*)malloc(sizeof(float) * numframes);
106 twist2 = (float**)malloc(sizeof(float*) * joints);
107 for (i = 0; i < joints; i++)
108 twist2[i] = (float*)malloc(sizeof(float) * numframes);
110 speed = (float*)malloc(sizeof(float) * numframes);
112 onground = (bool**)malloc(sizeof(bool*) * joints);
113 for (i = 0; i < joints; i++)
114 onground[i] = (bool*)malloc(sizeof(bool) * numframes);
116 forward = (XYZ*)malloc(sizeof(XYZ) * numframes);
117 weapontarget = (XYZ*)malloc(sizeof(XYZ) * numframes);
118 label = (int*)malloc(sizeof(int) * numframes);
120 // read binary data as animation
123 for (i = 0; i < numframes; i++) {
124 // for each joint in the skeleton...
125 for (j = 0; j < joints; j++) {
126 // read joint position
127 funpackf(tfile, "Bf Bf Bf", &position[j][i].x, &position[j][i].y, &position[j][i].z);
129 for (j = 0; j < joints; j++) {
131 funpackf(tfile, "Bf", &twist[j][i]);
133 for (j = 0; j < joints; j++) {
134 // read onground (boolean)
136 funpackf(tfile, "Bb", &uch);
137 onground[j][i] = (uch != 0);
139 // read frame speed (?)
140 funpackf(tfile, "Bf", &speed[i]);
142 // read twist2 for whole animation
143 for (i = 0; i < numframes; i++) {
144 for (j = 0; j < joints; j++) {
145 funpackf(tfile, "Bf", &twist2[j][i]);
148 // read label for each frame
149 for (i = 0; i < numframes; i++) {
150 funpackf(tfile, "Bf", &label[i]);
152 // read weapontargetnum
153 funpackf(tfile, "Bi", &weapontargetnum);
154 // read weapontarget positions for each frame
155 for (i = 0; i < numframes; i++) {
156 funpackf(tfile, "Bf Bf Bf", &weapontarget[i].x, &weapontarget[i].y, &weapontarget[i].z);
162 // find average position of certain joints on last frames
163 // and save in endoffset
164 // (not sure what exactly this accomplishes. the y < 1 test confuses me.)
165 for (j = 0; j < joints; j++) {
166 if (position[j][numframes - 1].y < 1)
167 endoffset += position[j][numframes - 1];
174 Animation::~Animation()
179 void Animation::deallocate()
184 for (i = 0; i < joints; i++)
192 for (i = 0; i < joints; i++)
200 for (i = 0; i < joints; i++)
208 for (i = 0; i < joints; i++)
234 Animation & Animation::operator = (const Animation & ani)
238 bool allocate = ((ani.numframes != numframes) || (ani.joints != joints));
243 numframes = ani.numframes;
247 weapontargetnum = ani.weapontargetnum;
251 position = (XYZ**)malloc(sizeof(XYZ*)*ani.joints);
252 for (i = 0; i < ani.joints; i++) {
254 position[i] = (XYZ*)malloc(sizeof(XYZ) * ani.numframes);
255 memcpy(position[i], ani.position[i], sizeof(XYZ)*ani.numframes);
259 twist = (float**)malloc(sizeof(float*)*ani.joints);
260 for (i = 0; i < ani.joints; i++) {
262 twist[i] = (float*)malloc(sizeof(float) * ani.numframes);
263 memcpy(twist[i], ani.twist[i], sizeof(float)*ani.numframes);
267 twist2 = (float**)malloc(sizeof(float*)*ani.joints);
268 for (i = 0; i < ani.joints; i++) {
270 twist2[i] = (float*)malloc(sizeof(float) * ani.numframes);
271 memcpy(twist2[i], ani.twist2[i], sizeof(float)*ani.numframes);
275 speed = (float*)malloc(sizeof(float) * ani.numframes);
276 memcpy(speed, ani.speed, sizeof(float)*ani.numframes);
279 onground = (bool**)malloc(sizeof(bool*)*ani.joints);
280 for (i = 0; i < ani.joints; i++) {
282 onground[i] = (bool*)malloc(sizeof(bool) * ani.numframes);
283 memcpy(onground[i], ani.onground[i], sizeof(bool)*ani.numframes);
287 forward = (XYZ*)malloc(sizeof(XYZ) * ani.numframes);
288 memcpy(forward, ani.forward, sizeof(XYZ)*ani.numframes);
291 weapontarget = (XYZ*)malloc(sizeof(XYZ) * ani.numframes);
292 memcpy(weapontarget, ani.weapontarget, sizeof(XYZ)*ani.numframes);
295 label = (int*)malloc(sizeof(int) * ani.numframes);
296 memcpy(label, ani.label, sizeof(int)*ani.numframes);