Archived

This topic is now archived and is closed to further replies.

Reading from memory, not from disk...?

This topic is 5006 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Until now I have been reading all game files (configs, tilemaps etc.) from the disk. This is of course the simplest method. For tilemaps and similar stuff I used code like this:
FILE* mf = fopen( "Tilemap.map", "rb" );
for( uint e=0; e<NUMBER_OF_CELLS; e++ )
	fread( &mMap.tiles[e].type, 1, sizeof(uint), mf );
fclose(mf);
For config files (simple txts) I used code like this:
ifstream fin("Config.cfg");
string str1 = "";
while( fin >> str1 )
{
	if( str1 == "TILES" )
	{
		char filename[256];
		fin >> filename;
		LoadMap(filename);
	}// end if


	if( str1 == "TITLE" )
	{
		string strTitle;
		getline( fin, strTitle );
		strcpy( mMission.introText, strTitle.c_str() );
	}// end if

}// end while

That works well but now I want to pack all those files into a single package so that the user can''t access it. I''ve already written a packer and have a function to read these pack files. This function then returns a pointer to the data (in memory) where the desired file (e.g. "Config.cfg") is stored. But what I don''t know is how to change my "fread" / "fin >> str1" so that they don''t read from the harddisk but from the memory location.... Can anyone help me out??? Thanks a lot!

Share this post


Link to post
Share on other sites
The fread solution is quite simple.

Instead of having a file cursor, which fread uses, you can use a BYTE size pointer to a large block of memory.

Each time you load in a distinct tile, you will increment the pointer by the size of the tile. It''s easy to use structures this way. The current tile will be at the current pointer position.

It is generally faster to do one huge fread for an entire file or file section, and then load the data from memory.

Share this post


Link to post
Share on other sites