Jump to content
  • Advertisement
Sign in to follow this  
skulldrudgery

Loading an array from a file

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

Alright, for some reason I like doing games in ASCII. Maybe it's because I'm a terrible artist. But I want to make "map" files and load them. I was thinking to use a 2d array to do it. But I don't know how I would go about doing that. I guess it should be in binary mode. But beyond that, I'm lost. Edit: C++ - win32 console

Share this post


Link to post
Share on other sites
Advertisement
It can be done in text mode too.


save

(store x and y dimensions of map on line 1)
(y lines of x length)

load
(read array dimension)
(size array x by y)
(load each array indice one at a time, y * x times)

Share this post


Link to post
Share on other sites
The array can't just be fed as-is to a stream, and you may find a need to manually insert whitespace at some points (can't think right now :s) but you have the right basic idea. To output the array you'll need to iterate over it and output the characters one at a time:


for (int i = 0; i < height; ++i) {
for (int j = 0; j < width; ++j) {
mout << map[j];
}
}


There are nicer and "cooler" ways to do this, but I am presenting what should be most obvious and conceptually easiest.

Share this post


Link to post
Share on other sites
this is what i am doing for the maps in my zelda clone. and it works well, just don't forget to output a space after every array entry output.

Share this post


Link to post
Share on other sites
Ok, I got it.


ofstream mout( "map.txt" );

mout << height << '\0' << width << endl; // not necessary?

for ( int i = 0; i < height; i++ ) {

for ( int j = 0; j < width; j++ )
mout << map[ i ][ j ] << '\0';

mout << endl;
}



Thanks for the help.

BTW if I use this as part of a class called "Screen" for example. And I use const ints for height and width, should they be global variables or private class members?

Share this post


Link to post
Share on other sites
Edit: Warning! Outputting '\0' to a text file (i.e. one intended to be read back in with the stream extraction >> operator, and/or viewed/edited in a text editor) is a bad idea. You almost certainly want to output ' ' (or perhaps a newline) instead.

Consider private, static class members.

However, if it's intended that there is logically only one Screen, and you are pretty confident of this part of your design, consider desigining it as a namespace rather than a class. You can make use of the C++ linking model, with a bit of care, to "hide" namespace-global data and helper functions as desired, the same way you'd use private in a class. You will also save the extra code associated with maintaining/caching/passing around (an) object instance(s), as well as any extra code you might already be contemplating for a Singleton implementation. :) (Of course, this comes at the cost of flexibility; significant reworking of the system is required if it becomes necessary to make actual Screen objects. But you know, it's never happened to me...)

Share this post


Link to post
Share on other sites
Sign in to follow this  

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