File: Animation.cpp

package info (click to toggle)
lugaru 1.2-7.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 39,748 kB
  • sloc: cpp: 29,640; ansic: 537; sh: 34; xml: 28; makefile: 14
file content (152 lines) | stat: -rw-r--r-- 4,209 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
Copyright (C) 2010-2017 - Lugaru contributors (see AUTHORS file)

This file is part of Lugaru.

Lugaru is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

Lugaru is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Lugaru.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "Animation/Animation.hpp"

#include "Animation/Skeleton.hpp"
#include "Game.hpp"
#include "Utils/Folders.hpp"

std::vector<Animation> Animation::animations;

void Animation::loadAll()
{
#define DECLARE_ANIM(id, file, height, attack, ...) \
    if (id < loadable_anim_end)                     \
        animations.emplace_back(file, height, attack);
#include "Animation.def"
#undef DECLARE_ANIM
}

void AnimationFrame::loadBaseInfo(FILE* tfile)
{
    // for each joint in the skeleton...
    for (unsigned j = 0; j < joints.size(); j++) {
        // read joint position
        funpackf(tfile, "Bf Bf Bf", &joints[j].position.x, &joints[j].position.y, &joints[j].position.z);
    }
    for (unsigned j = 0; j < joints.size(); j++) {
        // read twist
        funpackf(tfile, "Bf", &joints[j].twist);
    }
    for (unsigned j = 0; j < joints.size(); j++) {
        // read onground (boolean)
        unsigned char uch;
        funpackf(tfile, "Bb", &uch);
        joints[j].onground = (uch != 0);
    }
    // read frame speed (?)
    funpackf(tfile, "Bf", &speed);
}

void AnimationFrame::loadTwist2(FILE* tfile)
{
    for (unsigned j = 0; j < joints.size(); j++) {
        funpackf(tfile, "Bf", &joints[j].twist2);
    }
}

void AnimationFrame::loadLabel(FILE* tfile)
{
    funpackf(tfile, "Bf", &label);
}

void AnimationFrame::loadWeaponTarget(FILE* tfile)
{
    funpackf(tfile, "Bf Bf Bf", &weapontarget.x, &weapontarget.y, &weapontarget.z);
}

Animation::Animation()
    : height(lowheight)
    , attack(neutral)
    , numjoints(0)
{
}

/* EFFECT
 * load an animation from file
 */
Animation::Animation(const std::string& filename, anim_height_type aheight, anim_attack_type aattack)
    : Animation()
{
    FILE* tfile;
    int numframes;
    unsigned i;

    LOGFUNC;

    // Changing the filename into something the OS can understand
    std::string filepath = Folders::getResourcePath("Animations/" + filename);

    LOG(std::string("Loading animation...") + filepath);

    height = aheight;
    attack = aattack;

    Game::LoadingScreen();

    // read file in binary mode
    tfile = Folders::openMandatoryFile(filepath, "rb");

    // read numframes, joints to know how much memory to allocate
    funpackf(tfile, "Bi Bi", &numframes, &numjoints);

    // allocate memory for everything

    frames.resize(numframes);

    // read binary data as animation

    // for each frame...
    for (i = 0; i < frames.size(); i++) {
        frames[i].joints.resize(numjoints);
        frames[i].loadBaseInfo(tfile);
    }
    // read twist2 for whole animation
    for (i = 0; i < frames.size(); i++) {
        frames[i].loadTwist2(tfile);
    }
    // read label for each frame
    for (i = 0; i < frames.size(); i++) {
        frames[i].loadLabel(tfile);
    }
    // read unused weapontargetnum
    int weapontargetnum;
    funpackf(tfile, "Bi", &weapontargetnum);
    // read weapontarget positions for each frame
    for (i = 0; i < frames.size(); i++) {
        frames[i].loadWeaponTarget(tfile);
    }

    fclose(tfile);

    XYZ endoffset;
    endoffset = 0;
    // find average position of certain joints on last frames
    // and save in endoffset
    // (not sure what exactly this accomplishes. the y < 1 test confuses me.)
    for (i = 0; i < frames.back().joints.size(); i++) {
        if (frames.back().joints[i].position.y < 1) {
            endoffset += frames.back().joints[i].position;
        }
    }
    endoffset /= numjoints;
    offset = endoffset;
    offset.y = 0;
}