Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Woody FX

How to read from a file??

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

If i were to write a 10*10 map in Notepad and saved it as map.txt Let s = Sand Let r = Rock Let t = Tree this is what i think the map.txt should contain to make my map! s,s,s,s,s,s,s,s,s,s, s,s,s,r,s,s,s,s,s,s, s,s,s,s,s,s,s,t,t,t, s,s,t,s,s,s,s,s,s,s, s,s,s,s,s,s,s,r,s,s, s,s,s,s,s,s,s,s,s,s, s,s,s,r,r,s,s,s,s,s, s,s,s,s,r,s,s,s,s,s, s,s,s,s,r,s,s,s,s,s, s,s,s,s,s,s,s,s,s,s, My code to take it in would look something like for (int x=0, x<10, x++) { for (int y=0, y<10, y++) { tilemap[x][y]= ("the relevent bit of code to copy each value in to the right space in the tile map array"); } } i know this is a very simple way of doing a map but i want to start small....but cant figure out how to copy the relevant value from the .txt file to the tilemap array!! Does it matter that my lines in the file only go 20 characters across? How would i get it to go down to the new lines so it wont be feeding blanks into the array and also how to handle the commas between each letter. Is using letters a bad way to represent your tile, i noticed alot of people use numbers! Thanks Brian

Share this post


Link to post
Share on other sites
Advertisement
OK the following code is off the top of my head, so if there are any problems it should be easy to fix.

I''m assuming that your file has 2 bytes at the end of a line (CF + LL).

  
void ReadFile()
{
int x,y;
FILE *fin;

fin = fopen("map.txt","rb");
if( fin == NULL )
{
// Error has occured

}
else
{
for (x=0, x<10, x++)
{
for (y=0, y<10, y++)
{
tilemap[x][y] = fgetc(fin);
fgetc(fin); // Catch that comma

}
fgetc(fin); // Catch CF

fgetc(fin); // Catch LF

}
fclose(fin);
}
}


et vola a, hopefully, working file loading code.

As for the simple way of doing tiles, it''s not have a search on the web for a game called Kye, it uses text files for it''s maps, and a game I''m writing uses this technique as well.

Using letters gives you an extra 16 tiles (a-z) or 42 (a-z,A-Z) or even 52 (0-9,a-z,A-Z) compared to just using numbers (0-9), assuming that you are using one digit per tile. If you what to use more then you would have to write a piece of code that reads until a comma and then returns the number read.

Oh about the code above you might want to place an offset on the tiles so that a=0,b=1,c=2, etc this will help your tile rendering code a bit as well.

When I find my code in tons of trouble,
Friends and colleages come to me,
Speaking words of wisdom:
"Write in C."

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Why are you making a home-brew map engine for a game when there''s already plenty good ones out there? Search the web for "Mappy" - it''s a free program that lets you create maps and integrate it into a game. I highly reccomend it. It supports animated, isometric, and hexogonal tiles.

Share this post


Link to post
Share on other sites
you guys really don''t know what "self-satisfaction" means... a self-written working piece of code, now matter how simple, makes you happier and more satisfied than any alreadywrittensupermegaengine... that''s my opinion...

Share this post


Link to post
Share on other sites
If you are using C++, you could alter your map slightly and it is very easy.
The map:

ssssssssss
sssrssssss
sssssssttt
sstsssssss
sssssssrss
ssssssssss
sssrrsssss
ssssrsssss
ssssrsssss
ssssssssss


Here is the code to load it:
  
char map[10][10];
int i,j;
fstream in("map.txt",ios_base::in);

for(i=0;i<10;i++) {
for(j=0;j<10;j++) {
in >> map[i][j];
}
}
in.close();

If you want your map to be int instead of char you can:
  
int map[10][10];
char temp;
int i,j;
fstream in("map.txt",ios_base::in);

for(i=0;i<10;i++) {
for(j=0;j<10;j++) {
in >> temp;
if(temp==''s'') {
map[i][j]=1;
}
else if(temp=''r'') {
map[i][j]=2;
}
else if(temp=''t'') {
map[i][j]=3;
}
}
}
in.close();



---
Make it work.
Make it fast.

"Commmmpuuuuterrrr.." --Scotty Star Trek IV:The Voyage Home

Share this post


Link to post
Share on other sites
Or if you liked, I could post the source to a Tokenising library I wrote for things just like this.

You''d read the file into a char*[] array

And call my class CTokens.Tokenise(char*).

This then creates a list of tokens that you can scroll through. Useful in IRC and other type libraries

Share this post


Link to post
Share on other sites
Bye a book!! LOL!!

I own loads of books...been in college for last 4 years doin a Bs in Applied Computing...

Now my Head is fried...just 2 things i need before i can reach the next level and have my map displayed...

Get rid of that DirectX 7 error that keeps occuring because i''m using DirectX 8 SDK and that little bit of code to read the map in!!!!

Share this post


Link to post
Share on other sites
Bye a book!! LOL!!

I own loads of books...been in college for last 4 years doin a Bs in Applied Computing...

Now my Head is fried...just 2 things i need before i can reach the next level and have my map displayed...

Get rid of that DirectX 7 error that keeps occuring because i''m using DirectX 8 SDK and that little bit of code to read the map in!!!!

Then i can move on and start the game logic...been stuck here a while...so need to just get it out of the way!!!

Have a load of other Projects that i''m trying to get through as well as this.. i''ve little time to struggle with something that is quiet simple and obvious to others and i can be simply helped with!!



If you dont want to reply Pyabo then dont, everything is not as black and white as you''d se it!.
If you do not want to reply then dont! Thanks.

Why not use Mappy...well because would not be appreciated by my Lectures and that means i''d fail!!

"self-satisfaction" I''ve no time for satisfaction tryin to get true this cousrse!!

Thanks Steven and Captain Jester!!

How come you have no commas in you map... you dont need them then i take it :-D

Converting to ints what is the benefit!!

Thanks for all your help... will take alook at Kye aswell!

Share this post


Link to post
Share on other sites

  • 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!