Merge pull request #1635 from turol/unique_ptr

Use unique_ptr in B3DImporter
pull/1636/head^2
Kim Kulling 2017-12-17 09:03:03 +01:00 committed by GitHub
commit cb17a7d81e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 23 deletions

View File

@ -93,7 +93,6 @@ void DeleteAllBarePointers(std::vector<T>& x)
B3DImporter::~B3DImporter() B3DImporter::~B3DImporter()
{ {
DeleteAllBarePointers(_animations);
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -267,6 +266,21 @@ T *B3DImporter::to_array( const vector<T> &v ){
return p; return p;
} }
// ------------------------------------------------------------------------------------------------
template<class T>
T **unique_to_array( vector<std::unique_ptr<T> > &v ){
if( v.empty() ) {
return 0;
}
T **p = new T*[ v.size() ];
for( size_t i = 0; i < v.size(); ++i ){
p[i] = v[i].release();
}
return p;
}
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void B3DImporter::ReadTEXS(){ void B3DImporter::ReadTEXS(){
while( ChunkSize() ){ while( ChunkSize() ){
@ -295,8 +309,7 @@ void B3DImporter::ReadBRUS(){
/*int blend=**/ReadInt(); /*int blend=**/ReadInt();
int fx=ReadInt(); int fx=ReadInt();
aiMaterial *mat=new aiMaterial; std::unique_ptr<aiMaterial> mat(new aiMaterial);
_materials.push_back( mat );
// Name // Name
aiString ainame( name ); aiString ainame( name );
@ -333,6 +346,7 @@ void B3DImporter::ReadBRUS(){
mat->AddProperty( &texname,AI_MATKEY_TEXTURE_DIFFUSE(0) ); mat->AddProperty( &texname,AI_MATKEY_TEXTURE_DIFFUSE(0) );
} }
} }
_materials.emplace_back( std::move(mat) );
} }
} }
@ -386,8 +400,7 @@ void B3DImporter::ReadTRIS( int v0 ){
Fail( "Bad material id" ); Fail( "Bad material id" );
} }
aiMesh *mesh=new aiMesh; std::unique_ptr<aiMesh> mesh(new aiMesh);
_meshes.push_back( mesh );
mesh->mMaterialIndex=matid; mesh->mMaterialIndex=matid;
mesh->mNumFaces=0; mesh->mNumFaces=0;
@ -415,6 +428,8 @@ void B3DImporter::ReadTRIS( int v0 ){
++mesh->mNumFaces; ++mesh->mNumFaces;
++face; ++face;
} }
_meshes.emplace_back( std::move(mesh) );
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -500,11 +515,11 @@ void B3DImporter::ReadANIM(){
int frames=ReadInt(); int frames=ReadInt();
float fps=ReadFloat(); float fps=ReadFloat();
aiAnimation *anim=new aiAnimation; std::unique_ptr<aiAnimation> anim(new aiAnimation);
_animations.push_back( anim );
anim->mDuration=frames; anim->mDuration=frames;
anim->mTicksPerSecond=fps; anim->mTicksPerSecond=fps;
_animations.emplace_back( std::move(anim) );
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -531,7 +546,7 @@ aiNode *B3DImporter::ReadNODE( aiNode *parent ){
node->mParent=parent; node->mParent=parent;
node->mTransformation=tform; node->mTransformation=tform;
aiNodeAnim *nodeAnim=0; std::unique_ptr<aiNodeAnim> nodeAnim;
vector<unsigned> meshes; vector<unsigned> meshes;
vector<aiNode*> children; vector<aiNode*> children;
@ -549,11 +564,10 @@ aiNode *B3DImporter::ReadNODE( aiNode *parent ){
ReadANIM(); ReadANIM();
}else if( t=="KEYS" ){ }else if( t=="KEYS" ){
if( !nodeAnim ){ if( !nodeAnim ){
nodeAnim=new aiNodeAnim; nodeAnim.reset(new aiNodeAnim);
_nodeAnims.push_back( nodeAnim );
nodeAnim->mNodeName=node->mName; nodeAnim->mNodeName=node->mName;
} }
ReadKEYS( nodeAnim ); ReadKEYS( nodeAnim.get() );
}else if( t=="NODE" ){ }else if( t=="NODE" ){
aiNode *child=ReadNODE( node ); aiNode *child=ReadNODE( node );
children.push_back( child ); children.push_back( child );
@ -561,6 +575,10 @@ aiNode *B3DImporter::ReadNODE( aiNode *parent ){
ExitChunk(); ExitChunk();
} }
if (nodeAnim) {
_nodeAnims.emplace_back( std::move(nodeAnim) );
}
node->mNumMeshes= static_cast<unsigned int>(meshes.size()); node->mNumMeshes= static_cast<unsigned int>(meshes.size());
node->mMeshes=to_array( meshes ); node->mMeshes=to_array( meshes );
@ -586,7 +604,6 @@ void B3DImporter::ReadBB3D( aiScene *scene ){
_nodeAnims.clear(); _nodeAnims.clear();
DeleteAllBarePointers(_animations);
_animations.clear(); _animations.clear();
string t=ReadChunk(); string t=ReadChunk();
@ -622,7 +639,7 @@ void B3DImporter::ReadBB3D( aiScene *scene ){
aiNode *node=_nodes[i]; aiNode *node=_nodes[i];
for( size_t j=0;j<node->mNumMeshes;++j ){ for( size_t j=0;j<node->mNumMeshes;++j ){
aiMesh *mesh=_meshes[node->mMeshes[j]]; aiMesh *mesh = _meshes[node->mMeshes[j]].get();
int n_tris=mesh->mNumFaces; int n_tris=mesh->mNumFaces;
int n_verts=mesh->mNumVertices=n_tris * 3; int n_verts=mesh->mNumVertices=n_tris * 3;
@ -685,27 +702,28 @@ void B3DImporter::ReadBB3D( aiScene *scene ){
//nodes //nodes
scene->mRootNode=_nodes[0]; scene->mRootNode=_nodes[0];
_nodes.clear(); // node ownership now belongs to scene
//material //material
if( !_materials.size() ){ if( !_materials.size() ){
_materials.push_back( new aiMaterial ); _materials.emplace_back( std::unique_ptr<aiMaterial>(new aiMaterial) );
} }
scene->mNumMaterials= static_cast<unsigned int>(_materials.size()); scene->mNumMaterials= static_cast<unsigned int>(_materials.size());
scene->mMaterials=to_array( _materials ); scene->mMaterials = unique_to_array( _materials );
//meshes //meshes
scene->mNumMeshes= static_cast<unsigned int>(_meshes.size()); scene->mNumMeshes= static_cast<unsigned int>(_meshes.size());
scene->mMeshes=to_array( _meshes ); scene->mMeshes = unique_to_array( _meshes );
//animations //animations
if( _animations.size()==1 && _nodeAnims.size() ){ if( _animations.size()==1 && _nodeAnims.size() ){
aiAnimation *anim=_animations.back(); aiAnimation *anim = _animations.back().get();
anim->mNumChannels=static_cast<unsigned int>(_nodeAnims.size()); anim->mNumChannels=static_cast<unsigned int>(_nodeAnims.size());
anim->mChannels=to_array( _nodeAnims ); anim->mChannels = unique_to_array( _nodeAnims );
scene->mNumAnimations=static_cast<unsigned int>(_animations.size()); scene->mNumAnimations=static_cast<unsigned int>(_animations.size());
scene->mAnimations=to_array( _animations ); scene->mAnimations=unique_to_array( _animations );
} }
// convert to RH // convert to RH

View File

@ -49,6 +49,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <assimp/material.h> #include <assimp/material.h>
#include "BaseImporter.h" #include "BaseImporter.h"
#include <memory>
#include <vector> #include <vector>
struct aiNodeAnim; struct aiNodeAnim;
@ -116,15 +117,15 @@ private:
std::vector<unsigned> _stack; std::vector<unsigned> _stack;
std::vector<std::string> _textures; std::vector<std::string> _textures;
std::vector<aiMaterial*> _materials; std::vector<std::unique_ptr<aiMaterial> > _materials;
int _vflags,_tcsets,_tcsize; int _vflags,_tcsets,_tcsize;
std::vector<Vertex> _vertices; std::vector<Vertex> _vertices;
std::vector<aiNode*> _nodes; std::vector<aiNode*> _nodes;
std::vector<aiMesh*> _meshes; std::vector<std::unique_ptr<aiMesh> > _meshes;
std::vector<aiNodeAnim*> _nodeAnims; std::vector<std::unique_ptr<aiNodeAnim> > _nodeAnims;
std::vector<aiAnimation*> _animations; std::vector<std::unique_ptr<aiAnimation> > _animations;
}; };
} }