Hi,
Is it possible to use a for loop to load some .cmo objects asynchronously?
The following code is from the Visual Studio 3D Starter Kit (Windows 8.1 Store App):
// Load the scene objects.
auto loadMeshTask = Mesh::LoadFromFileAsync(
m_graphics,
L"gamelevel.cmo",
L"",
L"",
m_meshModels)
.then([this]()
{
return Mesh::LoadFromFileAsync(
m_graphics,
L"teapot.cmo",
L"",
L"",
m_meshModels,
false // Do not clear the vector of meshes
);
})
.then([this]()
{
// Initialize animated models.
// ...
});
// Loads a scene from the specified file, returning a vector of mesh objects.
static concurrency::task<void> LoadFromFileAsync(
Graphics& graphics,
const std::wstring& meshFilename,
const std::wstring& shaderPathLocation,
const std::wstring& texturePathLocation,
std::vector<Mesh*>& loadedMeshes,
bool clearLoadedMeshesVector = true
)
{
auto folder = Windows::ApplicationModel::Package::Current->InstalledLocation;
// Start by loading the file.
return concurrency::create_task(folder->GetFileAsync(Platform::StringReference(meshFilename.c_str())))
// Then open the file.
.then([](Windows::Storage::StorageFile^ file)
{
return Windows::Storage::FileIO::ReadBufferAsync(file);
})
// Then read the meshes.
.then([&graphics, shaderPathLocation, texturePathLocation, &loadedMeshes](Windows::Storage::Streams::IBuffer^ buffer)
{
auto reader = Windows::Storage::Streams::DataReader::FromBuffer(buffer);
reader->ByteOrder = Windows::Storage::Streams::ByteOrder::LittleEndian;
reader->UnicodeEncoding = Windows::Storage::Streams::UnicodeEncoding::Utf8;
// Read how many meshes are part of the scene.
std::shared_ptr<UINT> remainingMeshesToRead = std::make_shared<UINT>(reader->ReadUInt32());
if (*remainingMeshesToRead == 0)
{
return concurrency::create_task([](){});
}
// For each mesh in the scene, load it from the file.
return concurrency::extras::create_iterative_task([reader, &graphics, shaderPathLocation, texturePathLocation, &loadedMeshes, remainingMeshesToRead]()
{
return Mesh::ReadAsync(reader, graphics, shaderPathLocation, texturePathLocation)
.then([&loadedMeshes, remainingMeshesToRead](Mesh* mesh) -> bool
{
if (mesh != nullptr)
{
loadedMeshes.push_back(mesh);
}
// Return true to continue iterating, false to stop.
*remainingMeshesToRead = *remainingMeshesToRead - 1;
return *remainingMeshesToRead > 0;
});
});
});
}
This code loads the gamelevel.cmo and the teapot.cmo files. But i have about 20 .cmo files to load.
Thanks,
Regards,
Daniel Klein