[8526] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program 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, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: Patrick Boenzli |
---|
| 13 | */ |
---|
| 14 | |
---|
| 15 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER |
---|
| 16 | |
---|
| 17 | #include "md3_animation_cfg.h" |
---|
| 18 | |
---|
[8541] | 19 | #include "md3_animation.h" |
---|
| 20 | |
---|
[9347] | 21 | #include "substring.h" |
---|
[8541] | 22 | #include "helper_functions.h" |
---|
[8526] | 23 | |
---|
[8532] | 24 | #include "debug.h" |
---|
[8526] | 25 | |
---|
[8535] | 26 | |
---|
[8541] | 27 | |
---|
[8532] | 28 | |
---|
[9357] | 29 | |
---|
[8526] | 30 | namespace md3 |
---|
| 31 | { |
---|
| 32 | |
---|
| 33 | /** |
---|
| 34 | * create an empty MD3AnimationCfg object |
---|
| 35 | */ |
---|
| 36 | MD3AnimationCfg::MD3AnimationCfg() |
---|
| 37 | { |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | /** |
---|
| 42 | * create a new MD3 Anumation object and initialize it with the data on the given line |
---|
| 43 | * @param line: the line to read from |
---|
| 44 | */ |
---|
[8532] | 45 | MD3AnimationCfg::MD3AnimationCfg(std::string filename) |
---|
[8526] | 46 | { |
---|
[8532] | 47 | |
---|
[8533] | 48 | |
---|
| 49 | this->loadConfig(filename); |
---|
[8526] | 50 | } |
---|
| 51 | |
---|
| 52 | |
---|
| 53 | /** |
---|
| 54 | * deconstructor |
---|
| 55 | */ |
---|
| 56 | MD3AnimationCfg::~MD3AnimationCfg() |
---|
| 57 | {} |
---|
| 58 | |
---|
| 59 | |
---|
[8532] | 60 | /** |
---|
| 61 | * loads the configuration file |
---|
| 62 | */ |
---|
[8533] | 63 | void MD3AnimationCfg::loadConfig(std::string filename) |
---|
[8532] | 64 | { |
---|
| 65 | // BufferedReader bin=new BufferedReader(new InputStreamReader(in)); |
---|
[8526] | 66 | |
---|
[8532] | 67 | int i = 0; //!< last read animation, animation order is hard coded! |
---|
| 68 | int torsoOffset = -1; //!< torso offset |
---|
| 69 | int firstTorsoFrame = -1; //!< first of the TORSO animation frames |
---|
| 70 | bool inHeader = true; //!< are we still in the header of the .cfg file? |
---|
| 71 | |
---|
| 72 | |
---|
[8535] | 73 | FILE* pFile; |
---|
| 74 | char* cLine; |
---|
| 75 | size_t len = 0; |
---|
| 76 | ssize_t read = 0; |
---|
[8532] | 77 | |
---|
[8535] | 78 | PRINTF(0)("opening file: %s\n", filename.c_str()); |
---|
| 79 | pFile = fopen(filename.c_str(), "r"); |
---|
| 80 | if( !pFile) { |
---|
| 81 | PRINTF(1)("Error: file could not be opened: %s\n", filename.c_str()); |
---|
| 82 | return; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | |
---|
[8532] | 86 | // parse file |
---|
[8535] | 87 | while( (read = getline(&cLine, &len, pFile)) != -1) { /*( !this->dataStream.eof()) {*/ |
---|
| 88 | std::string line(cLine); |
---|
[8532] | 89 | |
---|
[8546] | 90 | // PRINTF(0)("size = %i, line = |%s|\n", read, line.c_str()); |
---|
[8536] | 91 | |
---|
[8532] | 92 | //ignore empty lines and comments |
---|
[8546] | 93 | if( line.length() > 2 && line.find("//") != 0) { |
---|
| 94 | |
---|
| 95 | // find the gender section |
---|
[8532] | 96 | if( inHeader && line.find("sex") == 0) { |
---|
| 97 | //parse line: sex [m | f | ...] |
---|
[9347] | 98 | SubString tokens(line, " \t\n\r\f/"); |
---|
[8532] | 99 | |
---|
[8536] | 100 | std::string sexStr = tokens.back(); |
---|
| 101 | // probably error in the file format |
---|
| 102 | if( sexStr.length() != 1) { |
---|
| 103 | this->sex = '?'; |
---|
| 104 | PRINTF(2)("no section \"sex\" in the config file %s found\n", filename.c_str()); |
---|
[8532] | 105 | } |
---|
[8536] | 106 | else { |
---|
| 107 | this->sex = sexStr[0]; |
---|
| 108 | } |
---|
[8533] | 109 | |
---|
[8536] | 110 | PRINTF(0)("sex of the md3 model: %c\n", this->sex); |
---|
[8533] | 111 | } |
---|
[8546] | 112 | else if( inHeader && line.find("headoffset") == 0) { |
---|
[8536] | 113 | // parse line: headoffset X Y Z |
---|
[9347] | 114 | SubString tokens(line, " \t\n\r\f/"); |
---|
[8536] | 115 | |
---|
[8537] | 116 | float z = atof(tokens.back().c_str()); tokens.pop_back(); |
---|
| 117 | float y = atof(tokens.back().c_str()); tokens.pop_back(); |
---|
| 118 | float x = atof(tokens.back().c_str()); tokens.pop_back(); |
---|
[8536] | 119 | |
---|
[8537] | 120 | this->headOffset = Vector(x, y, z); |
---|
[8532] | 121 | |
---|
[8537] | 122 | PRINTF(0)("got head offset: %f, %f, %f\n", x, y, z); |
---|
| 123 | } |
---|
[8546] | 124 | else if( inHeader && line.find("footsteps") == 0) { |
---|
[8537] | 125 | //parse line: footsteps [normal | mech | ...] |
---|
[9347] | 126 | SubString tokens (line, " \t\n\r\f/"); |
---|
[8532] | 127 | |
---|
[8541] | 128 | this->footsteps = tokens.back(); |
---|
[8537] | 129 | |
---|
[8541] | 130 | //check if value is ok |
---|
| 131 | if (!(nocaseCmp(this->footsteps, "default") || |
---|
| 132 | nocaseCmp(this->footsteps, "normal") || |
---|
| 133 | nocaseCmp(this->footsteps, "boot") || |
---|
| 134 | nocaseCmp(this->footsteps, "flesh") || |
---|
| 135 | nocaseCmp(this->footsteps, "mech") || |
---|
| 136 | nocaseCmp(this->footsteps, "energy")) ) { |
---|
| 137 | this->footsteps="illegal footsteps value (" + this->footsteps + ")"; |
---|
| 138 | } |
---|
| 139 | PRINTF(0)("got footsteps: %s\n", this->footsteps.c_str()); |
---|
[8532] | 140 | } |
---|
[8655] | 141 | else |
---|
| 142 | { |
---|
[8541] | 143 | // assume it's an animation line |
---|
| 144 | inHeader = false; |
---|
[8532] | 145 | |
---|
[8545] | 146 | MD3Animation* animation = new MD3Animation(); |
---|
| 147 | this->loadAnimation(animation, line); |
---|
[8532] | 148 | |
---|
[8546] | 149 | if( i < 25) |
---|
| 150 | { |
---|
| 151 | animation->name = MD3Animation::animationList[i].animationName; |
---|
| 152 | animation->type = MD3Animation::animationList[i++].animationType; |
---|
| 153 | } |
---|
[8532] | 154 | |
---|
[8541] | 155 | // workaround for strange numbering in animation.cfg: skip TORSO frames |
---|
| 156 | // for LEGS animation lines... |
---|
[8655] | 157 | if( animation->type == LEGS) |
---|
| 158 | { |
---|
| 159 | //when first LEGS animation is found, calc # of TORSO frames |
---|
[8541] | 160 | if( torsoOffset == -1) |
---|
| 161 | torsoOffset = animation->first - firstTorsoFrame; |
---|
[8532] | 162 | |
---|
[8541] | 163 | animation->first -= torsoOffset; |
---|
| 164 | animation->offset = torsoOffset; |
---|
[8532] | 165 | } |
---|
[8655] | 166 | else if( animation->type == TORSO) |
---|
[8541] | 167 | if( firstTorsoFrame == -1) |
---|
| 168 | firstTorsoFrame = animation->first; |
---|
[8532] | 169 | |
---|
[8544] | 170 | this->putAnimation(animation); |
---|
[8532] | 171 | } |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | |
---|
[8545] | 177 | /** |
---|
| 178 | * loading the animation data itself |
---|
| 179 | */ |
---|
| 180 | void MD3AnimationCfg::loadAnimation(MD3Animation* anim, std::string line) |
---|
| 181 | { |
---|
| 182 | // parse the line: |
---|
| 183 | // first frame, num frames, looping frames, frames per second (fps) |
---|
[9347] | 184 | SubString tokens(line, " \t\n\r\f/"); |
---|
[8532] | 185 | |
---|
[8546] | 186 | anim->fps = atoi(tokens.back().c_str()); tokens.pop_back(); |
---|
| 187 | anim->numLoopFrames = atoi(tokens.back().c_str()); tokens.pop_back(); |
---|
| 188 | anim->numFrames = atoi(tokens.back().c_str()); tokens.pop_back(); |
---|
| 189 | anim->first = atoi(tokens.back().c_str()); tokens.pop_back(); |
---|
| 190 | |
---|
[8656] | 191 | //some adjustements: |
---|
| 192 | if( anim->fps == 0) |
---|
[8659] | 193 | anim->fps = 33; |
---|
[8656] | 194 | |
---|
[8546] | 195 | PRINTF(0)("Animation: first frame: %i, num frames: %i, num loops: %i, fps: %i\n", anim->first, anim->numFrames, anim->numLoopFrames, anim->fps); |
---|
[8545] | 196 | } |
---|
| 197 | |
---|
| 198 | |
---|
[8526] | 199 | /** |
---|
[8541] | 200 | * Look up the animation data for the animation with the specified name. |
---|
[8526] | 201 | * |
---|
| 202 | * @param name Name of the animation. |
---|
| 203 | * @return The animation data associated with the name or null if no data was found. |
---|
| 204 | */ |
---|
[8541] | 205 | MD3Animation* MD3AnimationCfg::getAnimation(std::string name) |
---|
[8526] | 206 | { |
---|
[8544] | 207 | return this->animations[name]; |
---|
[8526] | 208 | } |
---|
| 209 | |
---|
[8541] | 210 | |
---|
| 211 | /** |
---|
| 212 | * Add the specified animation to the list of known animations. |
---|
| 213 | */ |
---|
| 214 | void MD3AnimationCfg::putAnimation(MD3Animation* anim) |
---|
| 215 | { |
---|
[8544] | 216 | this->animations[anim->name] = anim; |
---|
[8541] | 217 | } |
---|
| 218 | |
---|
| 219 | |
---|
[8526] | 220 | } |
---|
| 221 | |
---|
| 222 | |
---|
| 223 | |
---|