Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MeshAnimation Class Reference

#include <unit_generic.h>

Public Member Functions

 MeshAnimation (Unit *_unitDst)
 
bool Init (const char *filename, int faction, Flightgroup *flightgrp=NULL, const char *animationExt=NULL)
 
void AnimationStep ()
 
void clear ()
 
 ~MeshAnimation ()
 
string getAnimationName (unsigned int animationNumber) const
 
unsigned int getAnimationNumber (const char *name) const
 
void ChangeAnimation (const char *name)
 
void ChangeAnimation (unsigned int AnimNumber)
 
void StartAnimation (unsigned int how_many_times=0, int numAnimation=0)
 
void StopAnimation ()
 
bool isAnimatedMesh () const
 
bool animationRuns () const
 
unsigned int numAnimations ()
 
bool isContinuousLoop () const
 
void addAnimation (std::vector< Mesh * > *meshes, const char *name)
 
double framesPerSecond () const
 
double timePerFrame () const
 
void ToggleAnimatedMesh (bool on)
 
void SetAniSpeed (float speed)
 

Static Public Member Functions

static void UpdateFrames ()
 

Public Attributes

double curtime
 

Static Public Attributes

static unsigned int unitCount = 0
 
static std::map< string, Unit * > Units
 

Protected Attributes

std::vector< std::vector< Mesh * > * > vecAnimations
 
std::vector< string > vecAnimationNames
 
bool animatedMesh
 
unsigned int activeAnimation
 
double timeperframe
 
bool done
 
unsigned int activeMesh
 
unsigned int nextactiveMesh
 
bool infiniteLoop
 
unsigned int loopCount
 
string uniqueUnitName
 
UnitunitDst
 

Detailed Description

Definition at line 1627 of file unit_generic.h.

Constructor & Destructor Documentation

MeshAnimation::MeshAnimation ( Unit _unitDst)

Definition at line 8973 of file unit_generic.cpp.

8973  :
8974  animatedMesh(true),
8975  activeAnimation(0),
8976  timeperframe(3.0),
8977  done(true),
8978  activeMesh(0),
8979  nextactiveMesh(1),
8980  infiniteLoop(true),
8981  loopCount(0),
8982  unitDst(_unitDst),
8983  curtime(0.0)
8984 
8985 {}
MeshAnimation::~MeshAnimation ( )
inline

Definition at line 1662 of file unit_generic.h.

1662 { clear(); }

Member Function Documentation

void MeshAnimation::addAnimation ( std::vector< Mesh * > *  meshes,
const char *  name 
)

Definition at line 9147 of file unit_generic.cpp.

References animatedMesh, vecAnimationNames, and vecAnimations.

Referenced by Init().

9148 {
9149  if( (meshes->size() > 0) && animatedMesh ) {
9150  vecAnimations.push_back( meshes );
9151  vecAnimationNames.push_back(string(name));
9152  }
9153 }
bool MeshAnimation::animationRuns ( ) const

Definition at line 9254 of file unit_generic.cpp.

References done.

Referenced by StartAnimation().

9255 {
9256  return !done;
9257 }
void MeshAnimation::AnimationStep ( )

Definition at line 9099 of file unit_generic.cpp.

References activeAnimation, activeMesh, Unit::Draw(), isContinuousLoop(), loopCount, Unit::meshdata, nextactiveMesh, uniqueUnitName, unitDst, and vecAnimations.

9100 {
9101 #ifdef DEBUG_MESH_ANI
9102  std::cerr << "Starting animation step of Unit: " << uniqueUnitName << std::endl;
9103 #endif
9104  if((!this->isContinuousLoop())&&(loopCount==0))
9105  return;
9106 
9107  int numvold = 0;
9108  int numvertices = 0;
9109  //copy reference to data
9111 
9112 #ifdef DEBUG_MESH_ANI
9113  std::cerr << "vertices changed from: " << numvold << " to: " << numvertices << std::endl;
9114 #endif
9115 
9116  unitDst->Draw();
9117 
9118 #ifdef DEBUG_MESH_ANI
9119  std::cerr << "Drawed mesh: " << uniqueUnitName << std::endl;
9120 #endif
9121 
9123  nextactiveMesh++;
9124  if(nextactiveMesh >= vecAnimations.at(activeAnimation)->size())
9125  nextactiveMesh = 0;
9126 
9127  if(loopCount > 0)
9128  loopCount--;
9129 #ifdef DEBUG_MESH_ANI
9130  std::cerr << "Ending animation step of Unit: " << uniqueUnitName << std::endl;
9131 #endif
9132 }
void MeshAnimation::ChangeAnimation ( const char *  name)

Definition at line 9189 of file unit_generic.cpp.

References activeAnimation, getAnimationNumber(), isAnimatedMesh(), and numAnimations().

9190 {
9191  unsigned int AnimNumber = getAnimationNumber(name);
9192  if( (AnimNumber < numAnimations()) && isAnimatedMesh() )
9193  activeAnimation = AnimNumber;
9194 }
void MeshAnimation::ChangeAnimation ( unsigned int  AnimNumber)

Definition at line 9196 of file unit_generic.cpp.

References activeAnimation, isAnimatedMesh(), and numAnimations().

9197 {
9198  if( (AnimNumber < numAnimations()) && isAnimatedMesh() )
9199  activeAnimation = AnimNumber;
9200 }
void MeshAnimation::clear ( )

Definition at line 9237 of file unit_generic.cpp.

References j, StopAnimation(), uniqueUnitName, Units, vecAnimationNames, and vecAnimations.

9238 {
9239  StopAnimation();
9240 
9241  for (unsigned int i = 0; i < vecAnimations.size(); i++) {
9242  for(unsigned int j = 0; j < vecAnimations[i]->size(); j++) {
9243  delete vecAnimations[i]->at(j);
9244  }
9245  delete vecAnimations[i];
9246  vecAnimations[i]->clear();
9247  }
9248  vecAnimations.clear();
9249  vecAnimationNames.clear();
9250 
9251  Units.erase(uniqueUnitName);
9252 }
double MeshAnimation::framesPerSecond ( ) const

Definition at line 9207 of file unit_generic.cpp.

References timeperframe.

9208 {
9209  return 1/timeperframe;
9210 }
string MeshAnimation::getAnimationName ( unsigned int  animationNumber) const

Definition at line 9174 of file unit_generic.cpp.

References vecAnimationNames.

9175 {
9176  return vecAnimationNames.at(animationNumber);
9177 }
unsigned int MeshAnimation::getAnimationNumber ( const char *  name) const

Definition at line 9179 of file unit_generic.cpp.

References vecAnimationNames.

Referenced by ChangeAnimation().

9180 {
9181  string strname(name);
9182  for(unsigned int i=0;i<vecAnimationNames.size();i++)
9183  if(strname == vecAnimationNames[i])
9184  return i;
9185 
9186  return 0; //NOT FOUND!
9187 }
bool MeshAnimation::Init ( const char *  filename,
int  faction,
Flightgroup flightgrp = NULL,
const char *  animationExt = NULL 
)

Definition at line 9030 of file unit_generic.cpp.

References addAnimation(), StarSystemGent::faction, VSFileSystem::FileExistsData(), GFXVertexList::GetNumVertices(), Mesh::getVertexList(), Mesh::LoadMesh(), VSFileSystem::MeshFile, Unit::name, toLowerCase(), uniqueUnitName, unitCount, unitDst, Units, and Vector.

9032 {
9033  string fnam(filename);
9034  string::size_type pos = fnam.find('.');
9035  string anifilename = fnam.substr(0, pos);
9036 
9037  if(animationExt)
9038  anifilename += string("_") + string(animationExt);
9039 
9040  std::vector< Mesh* > *meshes = new vector<Mesh *>();
9041  int i = 1;
9042  char count[30] = "1";
9043  string dir = anifilename;
9044  while(true)
9045  {
9046  sprintf( count, "%d", i );
9047  string unit_name = toLowerCase(anifilename) + "_";
9048  if(i < 10)
9049  unit_name += "0";
9050  if(i < 100)
9051  unit_name += "0";
9052  if(i < 1000)
9053  unit_name += "0";
9054  if(i < 10000)
9055  unit_name += "0";
9056  if(i < 100000)
9057  unit_name += "0";
9058 
9059  unit_name += count;
9060  string path = dir + "/" + unit_name + ".bfxm";
9062  {
9063  Mesh *m = Mesh::LoadMesh( path.c_str(), Vector(1,1,1), faction, flightgrp );
9064  meshes->push_back( m );
9065  #ifdef DEBUG_MESH_ANI
9066  cerr << "Animated Mesh: " << path << " loaded - with: " << m->getVertexList()->GetNumVertices() << " vertices." << endl;
9067  #endif
9068  }
9069  else
9070  break;
9071  i++;
9072  }
9073 
9074 
9075  if( meshes->size() != 0 )
9076  {
9077  //FIXME: an animation is created only for the first submesh
9078  string animationName;
9079  sprintf( count, "%lu", meshes->size() );
9080  if(!animationExt)
9081  animationName = string(count); //if there is no extension given, the animations are called by their load order, 1, 2 ,3 ....10..
9082  else
9083  animationName = animationExt;
9084  addAnimation(meshes, animationName.c_str());
9085 
9086  int numFrames = meshes->size();
9088  sprintf( count, "%u", unitCount );
9089  uniqueUnitName = unitDst->name + string(count);
9091  cerr << "Animation data loaded for unit: " << string(filename) << ", named " << uniqueUnitName << " - with: " << numFrames << " frames." << endl;
9092  return true;
9093  } else {
9094  delete meshes;
9095  return false;
9096  }
9097 }
bool MeshAnimation::isAnimatedMesh ( ) const

Definition at line 9202 of file unit_generic.cpp.

References animatedMesh.

Referenced by ChangeAnimation().

9203 {
9204  return animatedMesh;
9205 }
bool MeshAnimation::isContinuousLoop ( ) const

Definition at line 9227 of file unit_generic.cpp.

References infiniteLoop.

Referenced by AnimationStep().

9228 {
9229  return infiniteLoop;
9230 }
unsigned int MeshAnimation::numAnimations ( )

Definition at line 9217 of file unit_generic.cpp.

References vecAnimations.

Referenced by ChangeAnimation().

9218 {
9219  return vecAnimations.size();
9220 }
void MeshAnimation::SetAniSpeed ( float  speed)

Definition at line 9232 of file unit_generic.cpp.

References speed, and timeperframe.

9233 {
9234  timeperframe = speed;
9235 }
void MeshAnimation::StartAnimation ( unsigned int  how_many_times = 0,
int  numAnimation = 0 
)

Definition at line 9155 of file unit_generic.cpp.

References animationRuns(), done, infiniteLoop, and StopAnimation().

9156 {
9157  bool infiniteLoop;
9158  if(!how_many_times)
9159  infiniteLoop = true;
9160  else
9161  infiniteLoop = false;
9162 
9163  if(animationRuns())
9164  StopAnimation();
9165 
9166  done = false;
9167 }
void MeshAnimation::StopAnimation ( )

Definition at line 9169 of file unit_generic.cpp.

References done.

Referenced by clear(), and StartAnimation().

9170 {
9171  done = true;
9172 }
double MeshAnimation::timePerFrame ( ) const

Definition at line 9212 of file unit_generic.cpp.

References timeperframe.

9213 {
9214  return timeperframe;
9215 }
void MeshAnimation::ToggleAnimatedMesh ( bool  on)

Definition at line 9222 of file unit_generic.cpp.

References animatedMesh.

9223 {
9224  animatedMesh = on;
9225 }
void MeshAnimation::UpdateFrames ( )
static

Definition at line 9134 of file unit_generic.cpp.

References GetElapsedTime(), and Units.

Referenced by GameStarSystem::Draw().

9135 {
9136  std::map< string, Unit * >::iterator pos;
9137  for(pos = Units.begin(); pos != Units.end(); ++pos)
9138  {
9139  pos->second->pMeshAnimation->curtime += GetElapsedTime();
9140  if (pos->second->pMeshAnimation->curtime >= pos->second->pMeshAnimation->timePerFrame()) {
9141  pos->second->pMeshAnimation->AnimationStep();
9142  pos->second->pMeshAnimation->curtime = 0.0;
9143  }
9144  }
9145 }

Member Data Documentation

unsigned int MeshAnimation::activeAnimation
protected

Definition at line 1634 of file unit_generic.h.

Referenced by AnimationStep(), and ChangeAnimation().

unsigned int MeshAnimation::activeMesh
protected

Definition at line 1637 of file unit_generic.h.

Referenced by AnimationStep().

bool MeshAnimation::animatedMesh
protected

Definition at line 1633 of file unit_generic.h.

Referenced by addAnimation(), isAnimatedMesh(), and ToggleAnimatedMesh().

double MeshAnimation::curtime

Definition at line 1646 of file unit_generic.h.

bool MeshAnimation::done
protected

Definition at line 1636 of file unit_generic.h.

Referenced by animationRuns(), StartAnimation(), and StopAnimation().

bool MeshAnimation::infiniteLoop
protected

Definition at line 1639 of file unit_generic.h.

Referenced by isContinuousLoop(), and StartAnimation().

unsigned int MeshAnimation::loopCount
protected

Definition at line 1640 of file unit_generic.h.

Referenced by AnimationStep().

unsigned int MeshAnimation::nextactiveMesh
protected

Definition at line 1638 of file unit_generic.h.

Referenced by AnimationStep().

double MeshAnimation::timeperframe
protected

Definition at line 1635 of file unit_generic.h.

Referenced by framesPerSecond(), SetAniSpeed(), and timePerFrame().

string MeshAnimation::uniqueUnitName
protected

Definition at line 1642 of file unit_generic.h.

Referenced by AnimationStep(), clear(), and Init().

unsigned int MeshAnimation::unitCount = 0
static

Definition at line 1648 of file unit_generic.h.

Referenced by Init().

Unit* MeshAnimation::unitDst
protected

Definition at line 1643 of file unit_generic.h.

Referenced by AnimationStep(), and Init().

std::map< string, Unit * > MeshAnimation::Units
static

Definition at line 1650 of file unit_generic.h.

Referenced by clear(), Init(), and UpdateFrames().

std::vector< string > MeshAnimation::vecAnimationNames
protected

Definition at line 1631 of file unit_generic.h.

Referenced by addAnimation(), clear(), getAnimationName(), and getAnimationNumber().

std::vector< std::vector<Mesh *> *> MeshAnimation::vecAnimations
protected

Definition at line 1630 of file unit_generic.h.

Referenced by addAnimation(), AnimationStep(), clear(), and numAnimations().


The documentation for this class was generated from the following files: