Jump to content
  • Advertisement
Sign in to follow this  
d h k

[ SDL ] Opening Map File

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

Hello everybody, I want to try to load a map into my SDL project, that consist of a simple text file renamed to "*.map". This ascii-file includes information for the tileset. I am pretty new to SDL and to handling files, so I am sure there are some mistakes in the function. That's why I am posting here... :)
int LoadMap ( char	filename[255] )
{

	char	buffer[255];
	FILE	*MapFile;
	int		x;
	int		y;

	if ( ( MapFile = fopen ( filename, "r" ) ) == NULL )
	{
		printf ( "ERROR opening file %s\n\n", filename );
		return -1;
	}

	fgets ( buffer, 1, MapFile );

	while ( !feof ( MapFile ) )
	{
		fgets ( buffer, 255, MapFile );
		
		if ( buffer[0] == '_' )
			tile[x][y].tile_sprite = 1;
		else if ( buffer[0] == 'T' )
			tile[x][y].tile_sprite = 2;
		else if ( buffer[0] == '-' )
			tile[x][y].tile_sprite = 3;
		else if ( buffer[0] == 'X' )
			tile[x][y].tile_sprite = 4;

		if ( x < TILES_X )
			x++;
		else
		{
			y++;
			x = 0;
		}
	}

	fclose ( MapFile );

	return	1;
}

Am I doing something obviously wrong? This function seems to load nothing. I know my tiles are working just fine and my *.map file is valid and in the right place as well...

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by d h k
Am I doing something obviously wrong? This function seems to load nothing.

Quote:

int x;
int y;


What are your variables initialize to? Here's a little rewrite with explanations


// Pass in a const char*
int LoadMap ( const char* filename )
{
// Make this 256 for the EOF as well
char buffer[256];
// Clear out the memory
memset( buffer, 0, 256 );
FILE *MapFile;
int x = 0;
int y = 0;

// Later if you use binary mode, you will use "rb"
if ( ( MapFile = fopen ( filename, "r" ) ) == NULL )
{
printf ( "ERROR opening file %s\n\n", filename );
return -1;
}
// I have no idea what this does...comments?!
fgets ( buffer, 1, MapFile );

while ( !feof ( MapFile ) )
{
// Get 255 bytes
fgets ( buffer, 255, MapFile );

// Loop though all the data now!
// We loop to strlen(buffer) in case 255 bytes are not read
for( int ctr = 0; ctr < strlen(buffer); ctr++ )
{
if ( buffer[ctr] == '_' )
tile[x][y].tile_sprite = 1;
else if ( buffer[ctr] == 'T' )
tile[x][y].tile_sprite = 2;
else if ( buffer[ctr] == '-' )
tile[x][y].tile_sprite = 3;
else if ( buffer[ctr] == 'X' )
tile[x][y].tile_sprite = 4;

if ( x < TILES_X )
x++;
else
{
y++;
x = 0;
}
}
// Clear out the memory
memset( buffer, 0, 256 );
}

fclose ( MapFile );

return 1;
}





That should work a little better, as you can see, you aren't processing any data, that's why it's not working [wink] Any questions on what I've done free free to ask.

Share this post


Link to post
Share on other sites
Wow, thanks.

It throws out a warning though ( warning C4018: '<' : Conflict between signed and unsigned ) and it seems to render the tiles in a really wierd way, not in the right order, seems almost random...

I am really not sure what the line with fgets does, I copied the code for the function more or less from the SPRITE Cone3d tutorial that I did earlier where he reads animation data from a *.dat file...

EDIT: The warning is in the line "for( int ctr = 0; ctr < strlen(buffer); ctr++ )"...

Share this post


Link to post
Share on other sites
Quote:
Original post by d h k
It throws out a warning though ( warning C4018: '<' : Conflict between signed and unsigned ) and it seems to render the tiles in a really wierd way, not in the right order, seems almost random...


Ok then it's a matter of the logics of your code, which I did not check over. You will probabally want to do something using rows and cols rather than X and Y [smile]. For example:

int LoadMap ( const char* filename )
{
char buffer[256];
memset( buffer, 0, 256 );
FILE *MapFile;
int row = 0;
int col = 0;

if ( ( MapFile = fopen ( filename, "r" ) ) == NULL )
{
printf ( "ERROR opening file %s\n\n", filename );
return -1;
}

// I have no idea what this does...comments?!
fgets ( buffer, 1, MapFile );

while ( !feof ( MapFile ) )
{
fgets ( buffer, 255, MapFile );
for( int ctr = 0; ctr < strlen(buffer); ctr++ )
{
if ( buffer[ctr] == '_' )
tile[row][col].tile_sprite = 1;
else if ( buffer[ctr] == 'T' )
tile[row][col].tile_sprite = 2;
else if ( buffer[ctr] == '-' )
tile[row][col].tile_sprite = 3;
else if ( buffer[ctr] == 'X' )
tile[row][col].tile_sprite = 4;

if ( col < TILES_X )
col++;
else
{
row++;
col = 0;
}
}
// Clear out the memory
memset( buffer, 0, 256 );
}

fclose ( MapFile );

return 1;
}





Now I have that one line that's still in question:

// I have no idea what this does...comments?!
fgets ( buffer, 1, MapFile );

What does your Map file look like? You will probabally need to remove that line perhaps.

As for the warning ignore it, it's just saying you should have a conversion. You can do this: ctr < (int)strlen(buffer);

Share this post


Link to post
Share on other sites
I am really not sure what the line with fgets does, I copied the code for the function more or less from the SPRITE Cone3d tutorial that I did earlier where he reads animation data from a *.dat file...

This is my map file:


XXXXXXXXXXXXXXXXXXX
X X
X X
X X
X X
X X
X X
X X
X X
X X
X X
X X
X X
X X
X TT TT TTX
XTTTTTTT__TT__TT__X
X_______--__--__--X
X-----------------X
XXXXXXXXXXXXXXXXXXX



Thanks for your great help!

Share this post


Link to post
Share on other sites
No problem! Go ahead and remove that first fgets ( buffer, 1, MapFile ); it's not needed for you. See what results from that.

Share this post


Link to post
Share on other sites
I got it. My #define TILES_X was one too much, thus giving it all a wierd looking offset...

Thanks alot.

Share this post


Link to post
Share on other sites
Ok, I think it's because you are also processing other entities in the file. Make these changes:

else if ( buffer[0] == 'X' )
tile[x][y].tile_sprite = 4;
// Add this in
else
continue;


When it reaches a newline, '\n', that might throw it off since it's note handeled at all. See if that does it.

Share this post


Link to post
Share on other sites
Like I said, it works perfect now, but I still have that warning...

Not that I couldn't ignore it :) but...

Share this post


Link to post
Share on other sites
Ok for the warning, if it's saying that it's a signed/unsigned mismatch, you can either cast the offending data type or change the other operand:

for( int ctr = 0; ctr < (int)strlen(buffer); ctr++ )
or
for( size_t ctr = 0; ctr < strlen(buffer); ctr++ )

That should take care of that warning.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!