Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/importer/md3/md3_animation_cfg.cc @ 9391

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

orxonox/trunk: compiles perfectly on windows from here
had to: switch from AC_PREFQ(2.59 → 2.56)
reinvent the lua-lib-stuff, as including liblua in liblualib is mandatory in f windows
remove some unportable line in the md3_animation
@patrick: please can you have a look at md3-animation, the part i have commented out is most possibly the whole readin process

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