Public Group

This topic is 4874 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 on other sites
It can be done in text mode too.

save

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

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

##### Share on other sites
So do I just use ofstream?

char map[height][width];ofstream mout( "map.txt" );mout << height << width << map;

Edit: Doraahaahhhh! Typos...

##### 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 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 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 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...)

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 13
• 92
• 11
• ### Forum Statistics

• Total Topics
632972
• Total Posts
3009626
• ### Who's Online (See full list)

There are no registered users currently online

×