-------------------------------------------------------------------------------------- Description: Saves current particle data into a CMF file. Arguments: resPath - Resource path of the CMF file to save particle data to. --------------------------------------------------------------------------------------
| 29 | // resPath - Resource path of the CMF file to save particle data to. |
| 30 | // -------------------------------------------------------------------------------------- |
| 31 | void Tr2RuntimeInstanceData::SaveToCMF( const char* resPath ) const |
| 32 | { |
| 33 | if( !m_data ) |
| 34 | { |
| 35 | return; |
| 36 | } |
| 37 | |
| 38 | std::wstring resPathW = (const wchar_t*)CA2W( resPath ); |
| 39 | std::wstring filename = BePaths->ResolvePathForWritingW( resPathW ); |
| 40 | |
| 41 | #if WITH_GRANNY |
| 42 | if( filename.size() >= 4 && filename.compare( filename.size() - 4, 4, L".gr2" ) == 0 ) |
| 43 | { |
| 44 | return SaveToGranny( resPath ); |
| 45 | } |
| 46 | #endif |
| 47 | |
| 48 | cmf::MemoryAllocator allocator; |
| 49 | cmf::BufferManager bufferAllocator( allocator ); |
| 50 | cmf::Span<cmf::VertexElement> cmfVertexDecl = allocator.AllocateSpan<cmf::VertexElement>( m_layout.m_items.size() ); |
| 51 | |
| 52 | if( !ConvertVertexDeclToCMF( m_layout, cmfVertexDecl, (uint32_t)cmfVertexDecl.size() ) ) |
| 53 | { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | uint32_t vertexBufferStride = m_stride; |
| 58 | uint32_t vertexBufferSize = vertexBufferStride * m_count; |
| 59 | |
| 60 | cmf::Data cmfData; |
| 61 | cmfData.meshes = allocator.AllocateSpan<cmf::Mesh>( 1 ); |
| 62 | |
| 63 | cmf::Mesh& cmfMesh = cmfData.meshes[0] = {}; |
| 64 | cmfMesh.name = allocator.AllocateString( m_name ); |
| 65 | cmfMesh.decl = cmfVertexDecl; |
| 66 | cmfMesh.lods = allocator.AllocateSpan<cmf::MeshLod>( 1 ); |
| 67 | |
| 68 | cmf::MeshLod& cmfMeshLod = cmfMesh.lods[0] = {}; |
| 69 | cmfMeshLod.vb = bufferAllocator.AddBuffer( m_data.get(), vertexBufferSize, vertexBufferStride ); |
| 70 | |
| 71 | auto file = cmf::BuildFile( cmfData, bufferAllocator ); |
| 72 | |
| 73 | FILE* outFile; |
| 74 | auto err = fopen_s( &outFile, CW2A( filename.c_str() ), "wb" ); |
| 75 | |
| 76 | if( err != 0 ) |
| 77 | { |
| 78 | CCP_LOGERR( "Failed to open output file: %ls\n", filename.c_str() ); |
| 79 | return; |
| 80 | } |
| 81 | if( fwrite( file.data(), 1, file.size(), outFile ) != file.size() ) |
| 82 | { |
| 83 | CCP_LOGERR( "Failed to write output file: %ls\n", filename.c_str() ); |
| 84 | fclose( outFile ); |
| 85 | return; |
| 86 | } |
| 87 | fclose( outFile ); |
| 88 | } |
nothing calls this directly
no test coverage detected