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;
}
[ SDL ] Opening Map File
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... :)
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...
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.
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++ )"...
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++ )"...
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);
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:
Thanks for your great help!
This is my map file:
XXXXXXXXXXXXXXXXXXXX XX XX XX XX XX XX XX XX XX XX XX XX XX TT TT TTXXTTTTTTT__TT__TT__XX_______--__--__--XX-----------------XXXXXXXXXXXXXXXXXXXX
Thanks for your great help!
No problem! Go ahead and remove that first fgets ( buffer, 1, MapFile ); it's not needed for you. See what results from that.
I got it. My #define TILES_X was one too much, thus giving it all a wierd looking offset...
Thanks alot.
Thanks alot.
Ok, I think it's because you are also processing other entities in the file. Make these changes:
When it reaches a newline, '\n', that might throw it off since it's note handeled at all. See if that does it.
else if ( buffer[0] == 'X' ) tile[x][y].tile_sprite = 4;// Add this inelse continue;
When it reaches a newline, '\n', that might throw it off since it's note handeled at all. See if that does it.
Like I said, it works perfect now, but I still have that warning...
Not that I couldn't ignore it :) but...
Not that I couldn't ignore it :) but...
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement