You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
201 lines
5.5 KiB
C++
201 lines
5.5 KiB
C++
15 years ago
|
/*
|
||
|
OpenMW - The completely unofficial reimplementation of Morrowind
|
||
|
Copyright (C) 2008-2010 Nicolay Korslund
|
||
|
Email: < korslund@gmail.com >
|
||
|
WWW: http://openmw.sourceforge.net/
|
||
|
|
||
|
This file (nif_file.h) is part of the OpenMW package.
|
||
|
|
||
|
OpenMW is distributed as free software: you can redistribute it
|
||
|
and/or modify it under the terms of the GNU General Public License
|
||
|
version 3, as published by the Free Software Foundation.
|
||
|
|
||
|
This program 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
|
||
|
version 3 along with this program. If not, see
|
||
|
http://www.gnu.org/licenses/ .
|
||
|
|
||
|
*/
|
||
|
|
||
12 years ago
|
#ifndef OPENMW_COMPONENTS_NIF_NIFFILE_HPP
|
||
|
#define OPENMW_COMPONENTS_NIF_NIFFILE_HPP
|
||
15 years ago
|
|
||
13 years ago
|
#include <OgreResourceGroupManager.h>
|
||
|
#include <OgreDataStream.h>
|
||
13 years ago
|
#include <OgreVector2.h>
|
||
13 years ago
|
#include <OgreVector3.h>
|
||
|
#include <OgreVector4.h>
|
||
|
#include <OgreMatrix3.h>
|
||
13 years ago
|
#include <OgreQuaternion.h>
|
||
|
#include <OgreStringConverter.h>
|
||
15 years ago
|
|
||
15 years ago
|
#include <stdexcept>
|
||
15 years ago
|
#include <vector>
|
||
13 years ago
|
#include <cassert>
|
||
13 years ago
|
|
||
12 years ago
|
#include <boost/weak_ptr.hpp>
|
||
|
#include <boost/shared_ptr.hpp>
|
||
|
#include <boost/make_shared.hpp>
|
||
|
#include <boost/detail/endian.hpp>
|
||
|
|
||
13 years ago
|
#include <libs/platform/stdint.h>
|
||
15 years ago
|
|
||
15 years ago
|
#include "record.hpp"
|
||
12 years ago
|
#include "niftypes.hpp"
|
||
|
#include "nifstream.hpp"
|
||
15 years ago
|
|
||
15 years ago
|
namespace Nif
|
||
|
{
|
||
|
|
||
15 years ago
|
class NIFFile
|
||
|
{
|
||
13 years ago
|
enum NIFVersion {
|
||
|
VER_MW = 0x04000002 // Morrowind NIFs
|
||
15 years ago
|
};
|
||
|
|
||
13 years ago
|
/// Nif file version
|
||
|
int ver;
|
||
15 years ago
|
|
||
13 years ago
|
/// File name, used for error messages
|
||
|
std::string filename;
|
||
15 years ago
|
|
||
13 years ago
|
/// Record list
|
||
|
std::vector<Record*> records;
|
||
15 years ago
|
|
||
13 years ago
|
/// Parse the file
|
||
|
void parse();
|
||
15 years ago
|
|
||
12 years ago
|
class LoadedCache;
|
||
|
friend class LoadedCache;
|
||
|
|
||
|
// attempt to protect NIFFile from misuse...
|
||
|
struct psudo_private_modifier {}; // this dirty little trick should optimize out
|
||
|
NIFFile (NIFFile const &);
|
||
|
void operator = (NIFFile const &);
|
||
|
|
||
13 years ago
|
public:
|
||
|
/// Used for error handling
|
||
|
void fail(const std::string &msg)
|
||
15 years ago
|
{
|
||
13 years ago
|
std::string err = "NIFFile Error: " + msg;
|
||
|
err += "\nFile: " + filename;
|
||
|
throw std::runtime_error(err);
|
||
15 years ago
|
}
|
||
|
|
||
13 years ago
|
void warn(const std::string &msg)
|
||
|
{
|
||
12 years ago
|
std::cerr << "NIFFile Warning: " << msg <<std::endl
|
||
|
<< "File: " << filename <<std::endl;
|
||
13 years ago
|
}
|
||
|
|
||
12 years ago
|
typedef boost::shared_ptr <NIFFile> ptr;
|
||
|
|
||
13 years ago
|
/// Open a NIF stream. The name is used for error messages.
|
||
12 years ago
|
NIFFile(const std::string &name, psudo_private_modifier);
|
||
|
~NIFFile();
|
||
|
|
||
|
static ptr create (const std::string &name);
|
||
|
static void lockCache ();
|
||
|
static void unlockCache ();
|
||
15 years ago
|
|
||
12 years ago
|
struct CacheLock
|
||
15 years ago
|
{
|
||
12 years ago
|
CacheLock () { lockCache (); }
|
||
|
~CacheLock () { unlockCache (); }
|
||
|
};
|
||
15 years ago
|
|
||
13 years ago
|
/// Get a given record
|
||
|
Record *getRecord(size_t index)
|
||
|
{
|
||
|
Record *res = records.at(index);
|
||
|
assert(res != NULL);
|
||
|
return res;
|
||
|
}
|
||
15 years ago
|
|
||
13 years ago
|
/// Number of records
|
||
12 years ago
|
size_t numRecords() { return records.size(); }
|
||
15 years ago
|
};
|
||
15 years ago
|
|
||
13 years ago
|
|
||
|
template<typename T>
|
||
|
struct KeyT {
|
||
|
float mTime;
|
||
|
T mValue;
|
||
|
T mForwardValue; // Only for Quadratic interpolation
|
||
|
T mBackwardValue; // Only for Quadratic interpolation
|
||
|
float mTension; // Only for TBC interpolation
|
||
|
float mBias; // Only for TBC interpolation
|
||
|
float mContinuity; // Only for TBC interpolation
|
||
|
};
|
||
|
typedef KeyT<float> FloatKey;
|
||
|
typedef KeyT<Ogre::Vector3> Vector3Key;
|
||
13 years ago
|
typedef KeyT<Ogre::Vector4> Vector4Key;
|
||
13 years ago
|
typedef KeyT<Ogre::Quaternion> QuaternionKey;
|
||
|
|
||
12 years ago
|
template<typename T, T (NIFStream::*getValue)()>
|
||
13 years ago
|
struct KeyListT {
|
||
|
typedef std::vector< KeyT<T> > VecType;
|
||
|
|
||
|
static const int sLinearInterpolation = 1;
|
||
|
static const int sQuadraticInterpolation = 2;
|
||
|
static const int sTBCInterpolation = 3;
|
||
|
|
||
|
int mInterpolationType;
|
||
|
VecType mKeys;
|
||
|
|
||
12 years ago
|
void read(NIFStream *nif, bool force=false)
|
||
13 years ago
|
{
|
||
|
size_t count = nif->getInt();
|
||
13 years ago
|
if(count == 0 && !force)
|
||
|
return;
|
||
13 years ago
|
|
||
|
mInterpolationType = nif->getInt();
|
||
|
mKeys.resize(count);
|
||
|
if(mInterpolationType == sLinearInterpolation)
|
||
|
{
|
||
|
for(size_t i = 0;i < count;i++)
|
||
|
{
|
||
|
KeyT<T> &key = mKeys[i];
|
||
|
key.mTime = nif->getFloat();
|
||
|
key.mValue = (nif->*getValue)();
|
||
|
}
|
||
|
}
|
||
|
else if(mInterpolationType == sQuadraticInterpolation)
|
||
|
{
|
||
|
for(size_t i = 0;i < count;i++)
|
||
|
{
|
||
|
KeyT<T> &key = mKeys[i];
|
||
|
key.mTime = nif->getFloat();
|
||
|
key.mValue = (nif->*getValue)();
|
||
|
key.mForwardValue = (nif->*getValue)();
|
||
|
key.mBackwardValue = (nif->*getValue)();
|
||
|
}
|
||
|
}
|
||
|
else if(mInterpolationType == sTBCInterpolation)
|
||
|
{
|
||
|
for(size_t i = 0;i < count;i++)
|
||
|
{
|
||
|
KeyT<T> &key = mKeys[i];
|
||
|
key.mTime = nif->getFloat();
|
||
|
key.mValue = (nif->*getValue)();
|
||
|
key.mTension = nif->getFloat();
|
||
|
key.mBias = nif->getFloat();
|
||
|
key.mContinuity = nif->getFloat();
|
||
|
}
|
||
|
}
|
||
|
else
|
||
12 years ago
|
nif->file->warn("Unhandled interpolation type: "+Ogre::StringConverter::toString(mInterpolationType));
|
||
13 years ago
|
}
|
||
|
};
|
||
12 years ago
|
typedef KeyListT<float,&NIFStream::getFloat> FloatKeyList;
|
||
|
typedef KeyListT<Ogre::Vector3,&NIFStream::getVector3> Vector3KeyList;
|
||
|
typedef KeyListT<Ogre::Vector4,&NIFStream::getVector4> Vector4KeyList;
|
||
|
typedef KeyListT<Ogre::Quaternion,&NIFStream::getQuaternion> QuaternionKeyList;
|
||
13 years ago
|
|
||
15 years ago
|
} // Namespace
|
||
15 years ago
|
#endif
|