• Advertisement
Sign in to follow this  

Reading tile info from file

This topic is 4328 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

I am developing a pacman clone and working on reading the tiles from a file. Its not working the way I hoped. For some reason when I check the first character it comes up as 40'('. It should read in (. Below is my how my text file looks: (hhhhhhhhhhhhhhhhh) |.................| |p().(h>.o.<h).()p| |.[].rh>.b.<hr.[].| |....|.......|....| |.<>.|.<hdh>.|.<>.| |....|...|...|....| [hh).rh>.b.<hl.(hh] eee|.|eeeeeee|.|eee eee|.be(>e<)eb.|eee eee|.ee|eee|ee.|eee eee|.oe[hhh]eo.|eee eee|.|eeeeeee|.|eee (hh].be<hdh>eb.[hh) |....eeee|eeee....| |.<dhh>eebee<hhd>.| |p.|...eeeee...|.p| |>.b.o.<hdh>.o.b.<| |....|...|...|....| |.<>.[h>.|.<h].<>.| |........|........| [hhhhhhhhhhhhhhhhh] Here is my code:

//open up tilemap file
FILE *f = fopen("Data/tilemap.txt","r");
if (f == NULL)
	exit(0);

while(!feof(f))
{
	for(i=0;i<19;i++)
	{
		for(j=0;j<22;j++)
		{
			//insert into tile map array
			cTemp = fgetc(f);
			if(cTemp == 'p' || cTemp == 'e' || cTemp == '.')
			{
				Level[i][j].blocked = false;
			}
			else
			{
				Level[i][j].blocked = true;
			}
			Level[i][j].type = cTemp;
		}
	}
}

//close file
fclose(f);


Share this post


Link to post
Share on other sites
Advertisement
40 is the decimal value of the '(' character code. Looks to me like everything is working correctly.

You can find the numeric values of other characters here. Note that those values are only valid for ASCII encoding and compatible Unicode formats (UTF-7, UTF-8); certain Unicode formats may have different values.

Share this post


Link to post
Share on other sites
I would think that the first char would be 40, but it shows 40'('. Does this mean that the debugger is translating the code into the real value and then showing them side by side?

Share this post


Link to post
Share on other sites
Also, I would expect that I would need to ignore all character values under 35 so that it will not load in new line, character feeds etc. Where would I need to put this in my code?

Share this post


Link to post
Share on other sites
Yep, that's correct - the debugger will show both the number value and the corresponding character.

For the second question, tweak it thusly:

		for(j=0;j<22;j++)
{
//insert into tile map array
do
{
cTemp = fgetc(f);
} while(cTemp < 35);

if(cTemp == 'p' || cTemp == 'e' || cTemp == '.')
{
Level[i][j].blocked = false;
}
else
{
Level[i][j].blocked = true;
}
Level[i][j].type = cTemp;
}



This will read one character, check if it is less than 35, and if so, read another character. It will continue trying to read until it finds what it wants.

Be warned though that it won't behave nicely if the file doesn't have enough data in it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement