Jump to content
  • Advertisement
Sign in to follow this  
Koger

Loading text map

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

Hi everybody Im new in programming and as such I have a problem. It may seem stupid or simple, but I cant find any solution, so maybe any of you can help me :) Im not englishman so excuse me for any of my mistakes. Im coding in c++ using SDL library. Im trying to make function that would read kind of map of mine. Problem is, it just didnt work and crush program. Map is simple, all information it cotains is number of colums and rows of map and type of every tile. Its like this for example: COLS:4 ROWS:4 1233 3321 1111 2231 Function reads in directory and returns pointer to unsigned char array. It looks like that: Declarations: Unsigned char *board; Int rows,cols; Function itself: char unsigned * LoadMap(char *dir) { char filename[256]; char buffer[256]; FILE *fp; sprintf(filename,"data/maps/%s",dir); fp=fopen(filename,"r"); fgets(buffer,256,fp); sscanf(buffer," COLS:%d ROWS:%d ",cols,rows); board=new unsigned char[rows*cols]; for(int i=0;i<rows;i++) { fgets(buffer,256,fp); for(int j=0;j<cols;j++) { board[j+i*cols]=buffer[j]; } } fclose(fp); return board; } Thats all. I was looking for solution through many threads but didnt find anything working for me. Thanks for any advises.

Share this post


Link to post
Share on other sites
Advertisement
I notice that there's no error-checking or debug output in your code. For example, you could check to see if the file is loaded successfully, and print out the values (such as 'rows' and 'cols') as they are read to see if they're correct.

Also, the function returns a pointer to the allocated board array, but according to what you posted 'board' is a global variable (this isn't incorrect per se - it's just a bit suspicious).

Anyway, you mentioned you're coding in C++. I'm guessing you're using C idioms and constructs because you're new to the language (perhaps you're working from a C tutorial), but if your intent is to learn C++, you should be writing C++ code :)

If I have time later I'll whip up an example (unless someone else does it first, which is quite likely). For now though, here's a quick list of things to look into: string, getline(), stringstream, ostream/cout, ifstream, and vector (all in the std namespace).

Share this post


Link to post
Share on other sites
As the above poster said , you should try to code c++ code since you are coding in c++.I myself am not very familiar with c ... only a 6month course in uni and still that was some time ago , so I can not say what is wrong with your code :(

But I can give you an example of a tile map creation function in C++ using SDL since I am making a game these days using exactly that.I will just copy paste my function here.

AS you can see it is quite simple.I am using just 3 tiles now since I am still creating the engine feeling , and using placeholder tiles.I hope you get the basic idea from it and hope it can help you a little.

Ofcourse from the code you can understand that there is another class representing the tile.Also many global variables , like TILE_HEIGHT / TILE_WIDTH e.t.c.


/*TileMap creation*/
bool createMap( Tile *tiles[], char* mapName )
{
//The offset of the tiles
int x = 0, y = 0;

//Open the file where the map is saved
std::ifstream map( mapName);

//If it couldn't be loaded
if( map == NULL )
{
return false;
}

//Star looping
for( int t = 0; t < TOTAL_TILES; t++ )
{
//Will start checking all the tiles one by one
char tile ;

//Read tile from map file
map >> tile;

//If there was a problem in reading the file
if( map.fail() == true )
{
return false;
}

//Finally create the tile according to what character it has
if( tile == GREEN )
{
tiles[ t ] = new Tile( x, y, TILE_WIDTH , TILE_HEIGHT , loadImage("tiles/green.bmp"), screen );
}
else if( tile == ROAD )
{
tiles[ t ] = new Tile( x, y, TILE_WIDTH , TILE_HEIGHT , loadImage("tiles/road.bmp") , screen );
}
else if( tile == SPACE )
{
tiles[ t ] = new Tile( x, y, TILE_WIDTH , TILE_HEIGHT , loadImage("tiles/space.bmp") , screen );
}

//If we don't recognize the tile type
else
{
return false;
}

//we are moving to the next tile to the right
x += TILE_WIDTH;

//If we've gone too far...
if( x >= LEVEL_WIDTH )
{
//Move back but ...
x = 0;

//Also change Row
y += TILE_HEIGHT;
}
}

//Close the file
map.close();

//If the map was loaded fine
return true;
}


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!