Advertisement

Loading a map for tile engine...

Started by February 09, 2003 10:56 PM
5 comments, last by Punx409 21 years, 9 months ago
For loading maps in my I have a file that would look like this for a 10x10 map where each number represents a different tile.
  0000000000
0000000000
1111111111
2222222222
0000000000
3333333333
0000000000
4444444444
5555555555
6666666666  
Now this works fine but only if I''m going to have a maximum of 9 tiles. When a tile number hits the double digits it screws all up. How would I make it so I could have tile numbers higher than 9?
If you could post the data structure you''re using to hold your map data I could probably give you more specific help.

You''re probably using an array of either bytes or ints. If that''s the case, all you''ll need to do is allocate more memory to hold the data. Post a bit of code and I''ll try to help you out.



DracosX:

Master of the General Protection Fault
DracosX:Master of the General Protection Fault
Advertisement
Easy, use a comma seperated file.

ie:

1,99,23,4
24,5,3,66

OR
if you know you have less than 100 tiles, use two digits.

ie:

00 01 02
99 22 33

etc..
use a char array so you can have letters as well as numbers represent tiles.
It all depends on what your using to load the information from file. In one of my projects I used the iostream objects (ifstream and ofstream) to create text-based maps like yours- with the exception that I put in SPACES between my numbers. An example of what you would see in my map file:

30 200 4278190080 1 1
40 0 20 22 55 199 201 231
55 23 123 4123 213 12 23 423
...

The reason this works is, that the number is read as any sequence as digits, separated by whitespace: spaces or return carriages. The first line always gives some information about the map configuration, with individual tiles underneath- so it doesn't have the same number of digits as the other lines.

To save something like this with the ofstream object, it's as simple as:
ofstream File("map1.map",ios::trunc|ios::binary,filebuf::sh_read||filebuf::sh_write);for(int y = 0; y < MapHeight; y++){	for(int x = 0; x < MapWidth; x++)		File << map[x][y] << " ";	File << "\r\n";}  

Loading information from the file is almost identical:
ifstream File("map1.map", ios::in|ios::binary, filebuf::sh_read||filebuf::sh_write);for(int y = 0; y < MapHeight; y++)	for(int x = 0; x < MapWidth; x++)		File >> map[x][y];  
Feel free to drop me a line with whatever your working on (email is best) and I'll give you any pointers I can.
---email--- Tok ----surf----
~The Feature Creep of the Family~

[edited by - Tok on February 10, 2003 1:25:11 AM]
--------------------------~The Feature Creep of the Family~
Alternatively...
char Map[100][25];void Save(LPCSTR FileName){    FILE *file = fopen(FileName, "wb");    fwrite(⤅,sizeof(Map),1,file);    fclose(f);} 


Yo Tyler, I hear your mum''s goin out with... SQUEEK!
the future is just like the past, just later. - TANSTAAFL
Advertisement
Easy.

Don''t make your map in text. Make it in binary. You can store up to 256 tiles instead of just 10. (Don''t forget the power of ''0''; 0-9 is 10).

I use VB to create map editors for my games (and other ressource editors) since it''s a very easy language to use and gets the job done fast, despite slower execution. But who needs a hyper fluid 250+ FPS when you''re just creating a map? This isn''t the finished product.

You can easily create a map that way and store it in a format you wouldn''t really be able to understand under normal circumstances. Short of using a hex-editor to toy around with some hex.

This topic is closed to new replies.

Advertisement