![]() |
|
This thread is locked; no one can reply to it.
![]() ![]() |
1
2
|
Loading a map from a text file |
Paladin
Member #6,645
December 2005
![]() |
Ok, so I've had a problem for awhile, I'm trying to load a map from a text file. The map has 24 X 24 numbers and each number represents a tile. This is my current loading code (I'm using C) :
When I take out map<i>[j] = (int)ch; it works, but then again that's the key part. When it does work, it just ends up loading the same tile repeated. Can anyone help? (The source is attached) |
SonShadowCat
Member #1,548
September 2001
![]() |
Why not use a for loop and make the whole code much cleaner? There is no real need to check for EOF if you know the amount of tiles beforehand. Anyway, getc() returns an int so there is no need to make ch a char and cast it later. Other than that I don't know what to say. BTW, the source isn't attached. |
Paladin
Member #6,645
December 2005
![]() |
I actually used this for a bit
And it didn't work so well either. |
BAF
Member #2,981
December 2002
![]() |
Quote:
I would use something more like (untested):
Although I have more information in my maps, that's a basic map loader. Untested though, but should work. [edit] |
SonShadowCat
Member #1,548
September 2001
![]() |
BAF
Member #2,981
December 2002
![]() |
What's too late? |
SonShadowCat
Member #1,548
September 2001
![]() |
I was. Posted something but your reply was more extensive. |
Paul whoknows
Member #5,081
September 2004
![]() |
instead of if(i < 24) i++; else { i = 0; j++; } try this for(i=0; i<24; i++) { for(j=0; j<24; j++) { map<i>[j] = (int)ch; } }
____ "The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner. |
Paladin
Member #6,645
December 2005
![]() |
Ok I tried that, and it didn't work. This is really annoying. Is there any way to do it in a similiar way I was doing it? EDIT: Paul, I tried your method as well, and it didn't work either. >< |
Paul whoknows
Member #5,081
September 2004
![]() |
This usually works, but is untested, so be careful!
____ "The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner. |
Paladin
Member #6,645
December 2005
![]() |
No, it didn't work. Once again it just repeated the tiles, but this one had patches of grass in a black spot on the left. |
LennyLen
Member #5,313
December 2004
![]() |
I've tested the following with a "map.txt" file that I've attached, and it works.
Also attached is the output from the program, which shows it works.
|
Paladin
Member #6,645
December 2005
![]() |
Actually when I use your attachment, the map.txt is a bunch of squares and the output has numbers ranging from 0 - around 15. I tried this code myself, and wrote it to my output. This: 000000000000000000000000 000000000000000000000000 000000000022220000000000 000000000022220000000000 000000000000000000000000 333333333333333333333333 333333333333333333333333 111111111113311111111111 111111111113311111111111 111111111113311111111111 111111111113311111111111 111111111113311111111111 111111111113311111111111 111111111113311111111111 111111111113311111111111 111111111113311111111111 111111111113311111111111 333333333333333333333333 333333333333333333333333 000000000000000000000000 000000000022220000000000 000000000022220000000000 000000000000000000000000 000000000000000000000000 turned into this: 484848484848484848484848 484848484848484848484848 131048484848484848484848 484848484848484848484848 484813104848484848484848 484850505050484848484848 484848481310484848484848 484848485050505048484848 484848484848131048484848 484848484848484848484848 484848484848484813105151 515151515151515151515151 515151515151515151511310 515151515151515151515151 515151515151515151515151 131049494949494949494949 495151494949494949494949 494913104949494949494949 494949515149494949494949 494949491310494949494949 494949494951514949494949 494949494949131049494949 494949494949495151494949 494949494949494913104949 494949494949494949515149 494949494949494949491310 494949494949494949494951 514949494949494949494949 131049494949494949494949 495151494949494949494949 494913104949494949494949 494949515149494949494949 494949491310494949494949 494949494951514949494949 494949494949131049494949 494949494949495151494949 494949494949494913105151 515151515151515151515151 515151515151515151511310 515151515151515151515151 515151515151515151515151 131048484848484848484848 484848484848484848484848 484813104848484848484848 484850505050484848484848 484848481310484848484848 484848485050505048484848 484848484848131048484848
Except that the above code was all on one line. I still can't see why it's doing this. |
A J
Member #3,025
December 2002
![]() |
48 is ASCII for 0 fopen( .., "rb" ); is the likely cause ___________________________ |
Paladin
Member #6,645
December 2005
![]() |
I actually suspected that, but I changed rb to r, then r to r+ and it all came out with ascii. |
LennyLen
Member #5,313
December 2004
![]() |
Quote: Actually when I use your attachment, the map.txt is a bunch of squares and the output has numbers ranging from 0 - around 15. The map.txt file is 576 (24x24) characters. Each character can be a number between 0 and 255 (though the generator I used only generated numbers between 0 and 20). You cannot see these characters (your text editor displays them as squares) as they are non-displayable characters. Try getting a better text editor with a hex-edit mode so you can see the values: If you look at the hex values, and convert them to decimal, you will see that they corrspond to the decimal values in the output file. Obviously this approach cannot be taken if you have more than 256 values for the map data. Here is how this map.txt file was created: [edit] Quote: Except that the above code was all on one line. I still can't see why it's doing this. You are not svaing the numbers 0, 1, 2, etc. to file, you are saving the characters '0', '1', '2', etc to file. when you read these as integers instead of characters, you will get there corresponding ASCII codes; 48, 49, 50, etc. If you want to read the numbers 0, 1, 2, then you will need to write them as the chars with value 0, 1, 2, etc, which is what I did. Alternatively, you can read them as chars, then subtract 48 to convert them to the numbers you want. [edit2] The 1310 that keeps repeating through your output is where it's reading the end of line chars 13 and 10 (under DOS/Windows). Either write your files in a method that does not include these characters, or you will need to read each line individually to strip the end of line characters.
|
Paladin
Member #6,645
December 2005
![]() |
Ok, so I have this current code: and when I load map: 000000000000000000000000 000000000000000000000000 000000000022220000000000 000000000022220000000000 000000000000000000000000 333333333333333333333333 333333333333333333333333 111111111113311111111111 111111111113311111111111 111111111113311111111111 111111111113311111111111 111111111113311111111111 111111111113311111111111 111111111113311111111111 111111111113311111111111 111111111113311111111111 111111111113311111111111 333333333333333333333333 333333333333333333333333 000000000000000000000000 000000000022220000000000 000000000022220000000000 000000000000000000000000 000000000000000000000000 I get 000000000000000000000000 -38000000000000000000000 000-38000000000022220000 000000-38000000000022220 000000000-38000000000000 000000000000-38333333333 333333333333333-38333333 333333333333333333-38111 111111113311111111111-38 111111111113311111111111 -38111111111113311111111 111-38111111111113311111 111111-38111111111113311 111111111-38111111111113 311111111111-38111111111 113311111111111-38111111 111113311111111111-38111 111111113311111111111-38 111111111113311111111111 -38333333333333333333333 333-38333333333333333333 333333-38000000000000000 000000000-38000000000022 220000000000-38000000000 022220000000000-38000000 000000000000000000-38000 000000000000000000000-49 I tried a few things, but I couldn't seem to figure out why it has the negative numbers there. Sorry Lenny that I didn't catch the binary part, I should have picked that up. |
Richard Phipps
Member #1,632
November 2001
![]() |
If you are only going to have a few tiles then just use bitmaps like I did for my Free Dungeons tutorial. It makes it much easier to change your maps in any paint program. |
Paladin
Member #6,645
December 2005
![]() |
Hahaha, I was actually going to do that, but I really just wanted to learn how to load a map from a textfile rather from a bmp. I would actually rather use your bmp method since it's easy, but I am trying to learn. |
Richard Phipps
Member #1,632
November 2001
![]() |
Manually entering numbers in a textfile is too awkward though. You'd have to write a map editor to do complex maps and then you might as well either use binary data directly with the map editor, or just use bitmaps if you have less than around 30 different tile types. |
Paladin
Member #6,645
December 2005
![]() |
I actually plan on writing a map editor once I'm done with this. I'll probably convert to binary once I get this working. Would you happen to know what my current problem is?:-/ |
ImLeftFooted
Member #3,935
October 2003
![]() |
Your file reader is reading newlines. Newlines are characters that appear at the end of a line and can be one or two characters. Heres an easy way to fix your code.
By the way, you really don't want to use the mode "r+", a simple "r" should do just fine. You should also really replace 'c - 48' with 'atoi(c)', its much cleaner and portable. |
Paladin
Member #6,645
December 2005
![]() |
Originally, I used atoi, but it never worked. It would always crash for some reason. BTW, your method worked. Thanks a lot! Would you happen to know why atoi wouldn't work? I replaced it like you said and I have stdlib.h included. |
BAF
Member #2,981
December 2002
![]() |
atoi wants a null terminated string, not a char AFAIK. |
Paladin
Member #6,645
December 2005
![]() |
So is |
|
1
2
|