Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/heathaze/src/lib/graphics/importer/md3/md3_animation_cfg.cc

Last change on this file was 9406, checked in by bensch, 18 years ago

orxonox/trunk: merged the proxy back

merged with commandsvn merge -r9346:HEAD https://svn.orxonox.net/orxonox/branches/proxy .

no conflicts

File size: 6.2 KB
Line 
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
19#include "md3_animation.h"
20
21#include "substring.h"
22#include "helper_functions.h"
23
24#include "debug.h"
25
26
27
28
29
30namespace 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   */
45  MD3AnimationCfg::MD3AnimationCfg(std::string filename)
46  {
47
48
49    this->loadConfig(filename);
50  }
51
52
53  /**
54   * deconstructor
55   */
56  MD3AnimationCfg::~MD3AnimationCfg()
57  {}
58
59
60  /**
61   * loads the configuration file
62   */
63  void MD3AnimationCfg::loadConfig(std::string filename)
64  {
65//     BufferedReader bin=new BufferedReader(new InputStreamReader(in));
66
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
73    FILE* pFile;
74    char* cLine;
75    size_t len = 0;
76    ssize_t read = 0;
77
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
86    // parse file
87    /// @FIXME FIXME bensch took this out, cause: not portable
88      //    while( (read = getline(&cLine, &len, pFile)) != -1) { /*( !this->dataStream.eof()) {*/
89      /*
90      std::string line(cLine);
91
92//       PRINTF(0)("size = %i, line = |%s|\n", read, line.c_str());
93
94      //ignore empty lines and comments
95      if( line.length() > 2 && line.find("//") != 0) {
96
97        // find the gender section
98        if( inHeader && line.find("sex") == 0) {
99          //parse line: sex [m | f | ...]
100          SubString tokens(line, " \t\n\r\f/");
101
102          std::string sexStr = tokens.back();
103          // probably error in the file format
104          if( sexStr.length() != 1) {
105            this->sex = '?';
106            PRINTF(2)("no section \"sex\" in the config file %s found\n", filename.c_str());
107          }
108          else {
109            this->sex = sexStr[0];
110          }
111
112          PRINTF(0)("sex of the md3 model: %c\n", this->sex);
113        }
114        else if( inHeader && line.find("headoffset") == 0) {
115          // parse line: headoffset X Y Z
116          SubString tokens(line, " \t\n\r\f/");
117
118          float z = atof(tokens.back().c_str()); tokens.pop_back();
119          float y = atof(tokens.back().c_str()); tokens.pop_back();
120          float x = atof(tokens.back().c_str()); tokens.pop_back();
121
122          this->headOffset = Vector(x, y, z);
123
124          PRINTF(0)("got head offset: %f, %f, %f\n", x, y, z);
125        }
126        else if( inHeader && line.find("footsteps") == 0) {
127          //parse line: footsteps [normal | mech | ...]
128          SubString tokens (line, " \t\n\r\f/");
129
130          this->footsteps = tokens.back();
131
132          //check if value is ok
133          if (!(nocaseCmp(this->footsteps, "default")   ||
134                nocaseCmp(this->footsteps, "normal")    ||
135                nocaseCmp(this->footsteps, "boot")      ||
136                nocaseCmp(this->footsteps, "flesh")     ||
137                nocaseCmp(this->footsteps, "mech")      ||
138                nocaseCmp(this->footsteps, "energy")) ) {
139            this->footsteps="illegal footsteps value (" + this->footsteps + ")";
140          }
141          PRINTF(0)("got footsteps: %s\n", this->footsteps.c_str());
142        }
143        else
144        {
145          // assume it's an animation line
146          inHeader = false;
147
148          MD3Animation* animation = new MD3Animation();
149          this->loadAnimation(animation, line);
150
151          if( i < 25)
152          {
153            animation->name = MD3Animation::animationList[i].animationName;
154            animation->type = MD3Animation::animationList[i++].animationType;
155          }
156
157          // workaround for strange numbering in animation.cfg: skip TORSO frames
158          // for LEGS animation lines...
159          if( animation->type == LEGS)
160          {
161            //when first LEGS animation is found, calc # of TORSO frames
162            if( torsoOffset == -1)
163              torsoOffset = animation->first - firstTorsoFrame;
164
165            animation->first -= torsoOffset;
166            animation->offset = torsoOffset;
167          }
168          else if( animation->type == TORSO)
169            if( firstTorsoFrame == -1)
170              firstTorsoFrame = animation->first;
171
172          this->putAnimation(animation);
173        }
174        }
175    }
176      */
177  }
178
179
180  /**
181   * loading the animation data itself
182   */
183  void MD3AnimationCfg::loadAnimation(MD3Animation* anim, std::string line)
184  {
185    // parse the line:
186    // first frame, num frames, looping frames, frames per second (fps)
187    SubString tokens(line, " \t\n\r\f/");
188
189    anim->fps = atoi(tokens.back().c_str()); tokens.pop_back();
190    anim->numLoopFrames = atoi(tokens.back().c_str()); tokens.pop_back();
191    anim->numFrames = atoi(tokens.back().c_str()); tokens.pop_back();
192    anim->first = atoi(tokens.back().c_str()); tokens.pop_back();
193
194    //some adjustements:
195    if( anim->fps == 0)
196      anim->fps = 33;
197
198    PRINTF(0)("Animation: first frame: %i, num frames: %i, num loops: %i, fps: %i\n", anim->first, anim->numFrames, anim->numLoopFrames, anim->fps);
199  }
200
201
202  /**
203  * Look up the animation data for the animation with the specified name.
204  *
205  * @param name Name of the animation.
206  * @return The animation data associated with the name or null if no data was found.
207  */
208  MD3Animation* MD3AnimationCfg::getAnimation(std::string name)
209  {
210    return this->animations[name];
211  }
212
213
214  /**
215   * Add the specified animation to the list of known animations.
216   */
217   void MD3AnimationCfg::putAnimation(MD3Animation* anim)
218   {
219      this->animations[anim->name] = anim;
220   }
221
222
223}
224
225
226
Note: See TracBrowser for help on using the repository browser.