Advertisement

map loading from file

Started by December 04, 2000 06:42 PM
5 comments, last by lpsoftware 24 years, 1 month ago
Hey. I've set up a very simple routine that loads a set of numbers for a world map into an array from a file. In the file, the array is set up as the number "ID's" separated by a space. Of course, there's much more to it, but I'll only show the relevant code.


ifstream fin;

fin.open("map1.txt");

//loop through the file and store each tile ID in layer 1	
for (y = 0; y < Map.height; y++)
{
	for (x = 0; x < Map.width; x++)
	{
		fin >> Map.layer1[y][x];
	}
}

fin.close();

  
So, after this loop, Map.layer1[][] should hold all the map info that was held in the file. To test if the map info was actually in the array of Map.layer1[][], I did this simple test to output the map array to another file:


ofstream fout;

fout.open("test.txt");

//loop through the file and print each lookup number	
for (y = 0; y < Map.height; y++)
{
	for (x = 0; x < Map.width; x++)
	{
		fout << Map.layer1[y][x] << " ";

                if (x == Map.width - 1)
                {
                    fout << "\n";
                }
	}
}

fout.close();

  
Sure enough, when I opened "test.txt" the correct map info was there, with a space in between the lookup numbers and a newline after each row. HOWEVER, for some reason, when doing the actual rendering of my map, the map does not render! I don't know why, since *after* the rendering function that I used to render the world, I did the same exact output test above and the correct map data showed up. ALSO, an array declared from within the code that contained the same exact map information worked fine when rendered. It would be great if someone could point out my flaws. Thanks a lot. Martin Edited by - lpsoftware on 12/4/00 6:45:35 PM
______________Martin EstevaolpSoftware
It seems like your error is in your rendering routines? Exactly what do you do with your maparray in the rendering routines?
Advertisement
I don''t think that there''s anything wrong with my rendering. All I do is loop through the array and render the map. It isn''t 2D, but the methods are very similar. As I said, a map array declared from within the code that had the same exact map info as the file worked fine with the rendering. And it''s the same type of array.


Thanks.
I''ll keep on trying.
______________Martin EstevaolpSoftware
If I understand this correctly:

The dynamic array DOES contain valid data, but won''t render..
A static array with same data renders correctly..

If that''s the case then you should check your array allocation..bear with me, I had a similar problem and it drove me nuts

I''m assuming that you are dynamically allocating the array..if not this probably won''t apply..
Here''s what happened to me:

I had a null pointer for an array of structs

SOME_STRUCT *structs;

Later when I determined the number of array elements, I would allocate:

structs = new SOME_STRUCT[num_structs];

This worked great..except for when I tried to access the data from another source file..even though I had declared:

extern SOME_STRUCT *structs;

in the header file, the data was still garbage..

I debugged the hell out that app..I''d step through the code, see exactly where the data was being correctly assigned, get to the part where I needed to use it, and it would be wrong..all my ints were like negative 8 zillion, etc..

I could access the data just fine in other parts of the code, just like you can output the data to another file and it works correctly..

On the verge of insanity I ripped apart all the code and started from scratch..SAME damn problem..BUT then I tried:

extern SOME_STRUCT *structs[];

in the header and it worked..
And that REALLY pissed me off

I''ve never had to specify brackets BEFORE when declaring a null pointer for a dynamically allocated array, but in this case I had to, no choice..I never found out why it wouldn''t take, but I DO use brackets more often now just to be safe..

If you can access the data correctly in one part of your code and not in another, it may not be in your code, it could easily be a small quirk in the compiler..BTW I use VC++(which is easily forgivable

Hope this helps..

"Like all good things, it starts with a monkey.."
"Like all good things, it starts with a monkey.."
monkeyman, I know you're going to hate me when you read this, but I just want to thank you for trying to help in writing all that stuff (very useful for the future, probably!). In my class (the "Map" class in my code), I declared the array as char instead of int! Everything works fine now with the arrays declared as int. I'm so stupid.

Of course, there are still questions Why *wouldn't* it work as a char array? I had been using a char array declared in the code until now, so what difference would it make in the rendering? Remember that the data worked fine with every other part of the code.

Thanks,
Martin

Edited by - lpsoftware on December 4, 2000 8:48:41 PM
______________Martin EstevaolpSoftware
Chars are pretty limited for anything except text..there''s some really cool STL string stuff, so I can see how it would be tempting to use chars for all kinds of data(my first engine used strings to store geometry, but it''s really best to use ints or floats for stuff like that..



"Like all good things, it starts with a monkey.."
"Like all good things, it starts with a monkey.."
Advertisement
aha, you edited the question while I was answering

My best guess would be in my first response above..I had the same issue with this one particular array..remember how I said I had declared the null pointer in the header?..well, I could access the data from SEVERAL different source files EXCEPT for the one I needed too
I don''t know if there was any specific similarity to your bug, but I remember the pain quite well

"Never question when your code works..only when it does not"
-Anonymous



"Like all good things, it starts with a monkey.."
"Like all good things, it starts with a monkey.."

This topic is closed to new replies.

Advertisement