Skip to main content
GameDev.net gamedev.net
🔒 Locked

Loading Maps from a Text File

Started by KG_Brad Jun 20, 2007 at 6:15 PM 3 replies 1.5k views
Original Post
KG_Brad
KG_Brad
Currently, I'm writing a tile engine for platformers. In the old version, I had the maps hard coded into a header file. In my current version, I want the maps to be loaded from a text file into an array. I have a working function for loading the maps into memory, now I need to know how to store them in an array so that I can draw the tiles. I read a method on another forum but it didn't work, so I'll post my code so people can identify the problem:
int render_map()
{
	int rval = 0;

     	for (map.i=0;map.i<5;++map.i)
     	{
     		for(map.t=0;map.t<5;++map.t)
           	{
                	rval = fgetc(mapfile);

              		MapArray[map.i][map.t] = rval;
              		
              		if (MapArray[map.i][map.t] == 0) putpixel(screen,map.i,map.t,WHITE);
     		}                
     	}
}

The problem is that it's just not drawing anything on the screen. I have a feeling that it's not reading the file the way it should. I appreciate any help that I can get. Thanks, -KG_Brad
Quote: Of course justice isn't equal. Rich people can afford lawyers, where poor people get McLawyers.
Dwiel
Dwiel
My guess is that you want to change the test == 0 to == '0'. This is because in your text file with the data you write the character 0 not the data value 0. The character 0 is stored as an ASCII value/number in this case the decimal number 48. The same goes for all other characters. Read up on the difference between a text file and a binary file.
KG_Brad
KG_Brad
That did it! Thanks a bunch, Dweil!
Quote: Of course justice isn't equal. Rich people can afford lawyers, where poor people get McLawyers.
shadowisadog
shadowisadog
Hello. I know this is a bit off topic and I know that your problem has already been addressed but I noticed a few other problems (or rather limitations) that I would like to talk about.

First of all you mention the fact that you are making an engine. This is a great goal to be sure, but if you are just starting out then this is not a wise idea. You should focus on creating games instead of engines. Once you write several games then you can factor out related code to form an engine, but until you have the experience doing this then I would seriously consider holding off.

Secondly your code provides no support for layers. I would advise against using just an integer value for each tile and instead make a tile structure or class so you can store more information later. Also I would highly recommend using a level editor like Mappy (which has quite a few bindings available for it) versus making a text file by hand.

I also see problems with drawing the map as you load the file because then you can’t update it in any way. Ideally you would have code to load the map and rendering code separate so that you can redraw the map as needed (for things like scrolling which is definitely needed in a platform game). I also suggest using an object oriented approach instead of using functions, but that is going off on a tangent.

I recommend checking out Smooth Scrolling a Tile Based Map for some nice ideas on how to do tile based scrolling.

I see issues with you using “magic numbers” in your for loops. If you wanted to change the size of the map then you have to change the code which is never a good idea.

Finally I do not know which compiler you are using but if you have a return type of int for a function then you really should return a value. If you don’t return a value then you should use a void function..

I hope this is helpful and good luck :) .
KG_Brad
KG_Brad
Thanks for the advice, shadow. Actually, my loading and rendering functions are seperate. I make the map scrollable by drawing every tile to a large buffer, then I change the buffer's x and y coordinates.
Quote: Of course justice isn't equal. Rich people can afford lawyers, where poor people get McLawyers.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.