Advertisement

Help w/ loading files

Started by April 15, 2000 11:38 AM
5 comments, last by TrigonLoki 24 years, 8 months ago
I need help loading files. First, here is my code: BOOL CMap::LoadMapfromFile(char* szName) { char terrstring[33]; char npcstring[33]; char herostring[33]; char monsterfile[33]; char buildingfile[33]; char portalfile[33]; char maps[4][33]; USHORT layer1, layer2, layer3; int passable; int portal; char dummy; char buffer[7]; FILE *map = NULL; char* szPath = "data\\"; strcat(szPath, szName); map = fopen("c:\\msdev\\projects\\discoadventure\\debug\\data\\eldacron.map", "r"); if (map == NULL) { PostQuitMessage(0); return 0; } fscanf(map, "%s %s %s %s %s %s %s %s %s %s", terrstring, npcstring, herostring, monsterfile, buildingfile,portalfile,maps[0],maps[1],maps[2],maps[3]); for (int i_y = 0; i_y < 45; i_y++) { for (int i_x = 0; i_x < 60; i_x++) { fscanf(map, "%u %u %u %u %u", &layer1, &layer2, &layer3, &passable, &portal); LoadTile(i_x, i_y,layer1,0,0,passable, 0); } if (i_y % 10) { itoa(layer1, buffer, 10); OutputDebugString(buffer); OutputDebugString("\n"); if (layer1 == 1) OutputDebugString("1\n"); } } OutputDebugString(monsterfile); fclose(map); return dummy; } The file it''s reading looks something like this: terrain.bmp default.npc default.her monster.mon building.bmp default.por end.map end.map end.map end.map 17 0 0 1 0 17 0 0 1 0 17 0 0 1 0 9 0 0 1 0 17 0 0 1 0 17 0 0 1 0 17 0 0 1 0 Now, you see where I''m OutputDebugString''ing the layer1 variable? It shows that the layer1 variable is 64,500 in every instance (where it should be 17, or 9, or 1 or somesuch). This is where I''m puzzled, because I made a console demo of loading in these variables with almost precisely the same technique, and it displayed them correctly. I''m getting a little frustrated. Thanks for any help you might be able to offer, Trigon P.S. Sorry if the board messes up some of my code (and I know it will) You should be able to get the gist of it. I like food.
I like food.
One note that I forgot to add and the board kinda messed up:

In my file, those 5 numbers are all in aligned columns; so a
200 200 200 0 1 column takes up the same horizontal space as a
1 1 1 1 1 column. Get it?

I like food.
I like food.
Advertisement
Ignoring for the moment I would give the entire function a major overhaul (eg. why not build a struct to contain all the data and write/read the entire struct in one chunk?), a few things catch my eye:

1. your output debug messages are outside the x loop, so the loop reads in 60 lines of layer1 and the others before outputting layer1 (and then only if y is not divisible by 10 - ie. no output during the first iteration of the y loop). The reason you might be getting bad values is because you've read past the end of the file (which you're not checking for, BTW) by the time you get to the output message. (ie. the first output of layer1 is outputting the 120th layer1 from the file)

2. layer1 is defined as an USHORT, but itoa requires a signed int (though with these low numbers, you probably won't loose anything in the type conversion).

aig


Edited by - An Irritable Gent on 4/15/00 1:43:24 PM

Edited by - An Irritable Gent on 4/15/00 1:44:16 PM
aig
Well, lessee here...

The Debug Messages were otuside the x-loop because if I had printed all 2700 of them (my map is 60x45 tiles), my loading time would have increased dramatically. I changed the layer stuff to signed ints, now it says that layer1 is always 6749172.

It is reading things into my tile class with the LoadTile function, btw,and I used to do it directly, but I switched things around to try to get things to work.

I like food.
I like food.
If you know how to use the debugger, start by putting a break on the itoa line, and put a watch on layer1, buffer, as well as i_x and i_y (so you know which line you're on in the input file). Then step over the itoa line to see what happens. If that's working how it's supposed to (ie. the layer1 int really is garbage before the itoa, and itoa is doing it's job as expected), back up and put a break on the fscanf line and step over through the loop a few dozen times to see what you're actually reading in.

And don't worry about increasing the load time, if it helps debug, since you'll be taking out these outputs once you have it figured out.

I also notice you're passing to the function a filename, but you're not using it but rather reading a hardcoded file instead. If you meant to do this for the time being for debug purposes, OK. But I wonder if you've forgot about this and are actually reading from the hardcoded file when you think you're reading from the file passed in.

Ah, the wonderful art of debugging. ;-)

aig

Edited by - An Irritable Gent on 4/15/00 3:55:38 PM
aig
Well, since my app is fullscreen, I can''t use the debugger w/o freezing the whole IDE, but I checked to see if the Layer1 variable was indeed 6749172. It was, on all my checks. I got rid of the itoa''s, btw.

You''re right about the filename/hard-coded part. It was screwing up totally before, and I decided to see if it was just a bad filename. I hardcoded one in and then this popped up, so I''ve left it there for now.

I like food.
I like food.
Advertisement
It''s fixed.

Boy do I feel pretty now. I knew it was some stupid mistake.

My map file works like this:
Some header strings, blah, blah, blah
1 0 45 1 0
...
...
11 32 1 3 1
<--- Line break to start a new row of tiles
1 6 2 3 5

And whaddya know!? I was forgettin'' to read that newline in! It''s all fixed now, and it looks pretty dang cool, too. Thanks a lot!

I like food.
I like food.

This topic is closed to new replies.

Advertisement