Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Map Model Preload With Progress Bar

Recommended Posts

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);
		cout << "Could not open map file to read." << endl;

	//  Put MDL paths/files in a list and get count
		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)

	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;

		//  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->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


//	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;


  • Like 1
Link to post
Share on other sites

Thanks.  As usual for sample code, I tried to make it as short and straightforward as possible, avoiding functions and classes and such.  I'm hoping it's not too hard to translate to Lua since I used Leadwerks functions where possible.  cout can be replaced with System:Print or removed altogether (it's just a sanity check).

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...