Since this was a hot topic recently, I've written code to preload model files from a map and display a progress bar. Then, when you load the map, it should load much faster since you already "preloaded" the models and the ones in the map will be instances (copies) of existing ones.
This is an alternative to Map::Load's hook, which may be a better way to go about this.
#include "Leadwerks.h"
using namespace Leadwerks;
std::string mapfile = "maps/map.map";
string model_file[100]; // Stores list of model files in map
Model *preload_model[100]; // Array to preload model files to
int modelcount=0; // Counts total number of models found in map file
int main(int argc, const char *argv[])
{
Leadwerks::Window* window = Leadwerks::Window::Create();
Context* context = Context::Create(window);
// Load map as just a file
Stream* stream = FileSystem::ReadFile(mapfile);
if(stream==NULL)
{
cout << "Could not open map file to read." << endl;
exit(1);
}
// Put MDL paths/files in a list and get count
while(!stream->EOF())
{
string line = stream->ReadLine();
// If what we read is a model line (ends with .mdl)
if(line.size()>3 && line.compare(line.size() - 4, 4, ".mdl") == 0)
model_file[modelcount++]=line;
}
stream->Release();
cout << "Number of model files in map: " << modelcount << endl;
for(int i=0; i<modelcount; i++)
{
cout << "Preloading file #" << i << ":" << model_file[i] << endl << endl;
preload_model[i]=NULL;
preload_model[i]=Model::Load(model_file[i]);
// You can check if model was properly loaded here (should not be NULL)
preload_model[i]->Hide(); // Preload model should not be seen
// Draw progress bar
context->SetColor(0.0, 0.0, 0.0);
context->Clear();
context->SetColor(1.0, 0.0, 0.0);
float barwidth = (float)(context->GetWidth())-20.0;
barwidth *= (float)(i+1);
barwidth /= (float)modelcount;
context->DrawRect(10, 10, (int)barwidth, 20);
// Remove the below delay line. It's put in just to show progress bar effect
Sleep(500);
context->Sync();
}
// You can then load the map here and it should be much faster
// since you already loaded the models in it above
// Map::Load(mapfile);
// Game code here
return 0;
}