Sign in to follow this  

how to load map from text file

This topic is 4750 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 know this has been asked amny times but the search function in the forums does not work I just keep getting sql search error. So any whay wat is a basic way to load a map array from a text file? I now can get my tile mpa to display using an array inside the source but now I want to make map files. so can any one explain how this is spose to be done? sorry for asking a question that is asked alot but like I said I cant search for it.

Share this post


Link to post
Share on other sites
You better start using binary files right away, they're smaller and easier to parse. Just write your in-code array to a file and write a new routine that can read it back. Afterwards you can start on writing your own exporter/converters for import of generic data.
Hope this helps.

Share this post


Link to post
Share on other sites
Quote:
Original post by Prototype
write a new routine that can read it back


this is the part I need help with. would I make to for loops the same way I draw them or ho do I get my tile function to read from my file read function or are they going to be the same?

Share this post


Link to post
Share on other sites
I strongly recommend you use a text based file format, at least to start with. Its much easier IMHO to be able to open the file in notepad to check/edit it, and its also easier to watch the parsing in your debugger and make sure everything is working.

file: map.txt
First line contains the width and height of the map. Following that are height lines, each containing width ints.

5 5
1 1 1 1 1
1 0 0 0 1
1 0 0 0 1
1 0 0 0 1
1 1 1 1 1



Now to parse it:

class Map
{
int * mTileData;
int mWidth;
int mHeight;

public:

void SetTile(int x, int y, int data)
{
mTileData[(y * mWidth) + x] = data;
}

bool Load(const std::string & filename)
{
ifstream mapfile(filename.c_str());

if (mapfile.is_open() == false) return false;

mapfile >> mWidth >> mHeight;

mTileData = new int[mWidth * mHeight];

for (int y = 0; y < mHeight; y++)
{
for (int x = 0; x < mWidth; x++)
{
int temp;
mapfile >> temp;
SetTile(x, y, temp);
}
}

return true;
}
};



Alan

Share this post


Link to post
Share on other sites
You just write out your data sequentially and read it back in the same way. When reading you can just increase a counter until the end-of-file is reached.
Even better is to add a header to your file with basic information such as # of items etc. Basically you will have your own file format this way.

Share this post


Link to post
Share on other sites
Quote:
Original post by kingpinzs
I know this has been asked amny times but the search function in the forums does not work I just keep getting sql search error.


You could always google just this site by typing site:www.gamedev.net [your search terms]. For example, site:www.gamedev.net XML will show all results from this site that mention XML. This is pretty useful as it'll search all forum topics, articles, journals, etc for the mention of XML. I've found it to be pretty handy on many occasions. I hope you find this useful :)

Share this post


Link to post
Share on other sites
sure

first of all if this is going to be a big map then you need to get a map editor that will export your map to text files if you want to know were to find one let me know. then you need a file parser whitch will skip over all the [] {}, crap that is no doubt in your array because none of those are ints. (or you could make a program to strip arrays of them like i did)
then just read in the file one int at a time modifying the string so that it dosnt contain any of the above mentioned chars (only if you dont want to had edit them) then create an ofstream to spit them back into a file making sure that there is a space in between the ints then when you want to load it again read the file back. this is all very confusing even to me so how bout some code


#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string reader = "";
string name = "";
ifstream fin;
ofstream fout;
string::size_type pos;
fin.open("bla.txt");
cout << "this new map will be called:";
cin >> name;
fout.open(name.c_str());
while(fin >> reader)
{
if((reader != ",") && (reader != "{") && (reader != "}") && (reader != ";") && reader != "}," && (reader != "};"))
{
pos = reader.find (",",0);
if(pos !=string::npos)
{
reader.erase(reader.length()-1,1);
};
fout << reader << "\n";
};
};
fout.close();
fin.close();

system("pause");
};



thats the write

and heres the read back in my project


int Map::Set_Equal(const char* SZ_FILENAME)
{
ifstream fin;
string reader = "";
fin.open(SZ_FILENAME);
for(int i = 0; i < 50; i++)
{
for(int j = 0; j < 50; j++)
{
fin >> A_Map[i][j];
};
};
while(fin >> reader)
{
if(reader == "END")
{
break;
};
Map_Images.push_back(SDL_LoadBMP(reader.c_str()));
};
};




hope you can get what i mean[smile]

NOTE: that if you dont have spaces in between your number (not just commas spaces)this wont work

Share this post


Link to post
Share on other sites
I'll just explain the logic behind what every ones saying here in case your not 100% on what the codes doing.

The easiest way is to step through your data when writing it is to use nested loops.

The array that holds your level data in memory will look like a bigger version of this

(|/| = 1 array element)

|/|/|/|/|/|/|/|/|/|
|/|/|/|/|/|/|/|/|/|
|/|/|/|/|/|/|/|/|/|
|/|/|/|/|/|/|/|/|/|
|/|/|/|/|/|/|/|/|/|
|/|/|/|/|/|/|/|/|/|
|/|/|/|/|/|/|/|/|/|
|/|/|/|/|/|/|/|/|/|
|/|/|/|/|/|/|/|/|/|

you need to fill your horizontal cells sequentialy then move to the next row down and fill them sequentialy. i.e
fill Row 0 cells 1,2,3,4,5,6,7,8,etc
then fill Row 1 cells 1,2,3,4,5,6,7,8,etc.
then fill Row 2 cells 1,2,3,4,5,6,7,8,etc.
and so on.

this loop:

for(int i = 0; i < mapHeight; i++)// Increments i which we will use to refference the Array Row we want to write to.
{
for(int j = 0; j < MapWidth; j++)//This will increment j that we use to reffer to the array cell of the particular row we need
{
ReadDataFromFile to MapArrayAtCoord[i][j]; //reads data in and puts it in cell [i][j]
};
};

the loop will do this:
i = 0 -> j = 0 ->writes line from file to Array[Row 0][Column 0].

still inside the inner loop(it has to do it as many times as the Array has columns.) j is incremented while i stays the same.... so now it writes the next line from file to Array[Row 0][Column 1]. and so on until it has writen up to the end of the top row of MapArray elements.
It then drops out of the inner (j incrementing loop) and increments i. it starts again on the inside loop(resetting to 0) so now its writing to [Row 1][Column 0] then [Row 1] [Column 2] and so on until it fills up all the elements on that row then goes to the next row.

sorry if im insulting your inteligence with this but its always best to know what your codes doing rather than just patchworking it together from posted code.

Share this post


Link to post
Share on other sites
so this it what i am getting

first you opoen the text file

using
fin.open("text.txt"); fin.close();
OR
ifstream Mapfile("text.txt");

then need to read the file using

for(int y=0; y<45; y++)
{for (int x=0; x<25; x++)
{
mapfile >> map[y][x]; or map[y][x].tile;
}
}

Share this post


Link to post
Share on other sites
well I have been working on loading my text file.

I can get it open but I iam having trouble reading that data into my array.


iam not sure how to
ReadDataFromFile to MapArrayAtCoord[i][j];

how can I make a basic console program to just print the x and y to the screen?

Share this post


Link to post
Share on other sites
The easiest way to do this would be if your array were single dimension and you "faked" the 2d part using math. Then the output to file and input to file would be as simple as:

// write to file
int map[map_width*map_height];
FILE *fp;
fp = fopen( "map.bin", "wb" );
fwrite( map, map_width*map_height, sizeof( int ), fp );
fclose(fp);

//read from file
int map[map_width*map_height];
FILE *fp;
fp = fopen( "map.bin", "rb" );
fread( map, map_width*map_height, sizeof( int ), fp );
fclose(fp);


It doesn't get any easier than that

Share this post


Link to post
Share on other sites
this is what I tryed. Can some one tell me what is wrong with it? Because the id's are not the same as in the text file.


#include <iostream>
#include <stdlib.h>
#include <fstream>

using namespace std;
int y, x;
int test;
int main(int argc, char *argv[])
{


ifstream MapFile("map.txt");
char map[16][16];

for(int y = 0 ; y <16; y++)
{for(int x = 0 ; x <16; x++)
{
MapFile >> map[y][x];
test = map[y][x];
cout <<y<<" "<<x<<endl;
cout <<"map id: "<< MapFile<<endl;
cout <<" "<<test<<"id: ";
}}

MapFile.close();

system("PAUSE");
return 0;
}

Share this post


Link to post
Share on other sites

This topic is 4750 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this