Hi. I'm currently making a little game just for fun and I have something I want to do but just don't know how to go about doing it in a good way.

I want to load in a level from a text file where:

0 = Wall (Can't walk here)
1 = Walkable
S = Startpoint
e.t.c~

What I want to do is load the map in to 2 char arrays (a matrice, if you will), one for X and one for Y. That way I can loop through the matrice quite smoothly (I hope) to check for the said conditions.

Now making the conditions themselves probably won't be a problem for me, I am more worried about how I will go about loading in the map and using the text in the textfile as I wish.

Any tips for a programmer that just started having the headache that is windows and wants to create a simple game?

Saving and loading maps coulden't be easier with C++ :) Say you have your map in a char array, this code would save that array to a bitmap.

#include <iostream>
#include <fstream>

int main() {
   char map[] = {
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 1, 1, 1, 1, 1, 1, 1, 1, 0,
       0, 1, 1, 1, 1, 1, 1, 1, 1, 0,
       0, 1, 1, 1, 1, 1, 1, 1, 1, 0,
       0, 1, 1, 1, 1, 1, 1, 1, 1, 0,
       0, 1, 1, 1, 1, 1, 1, 1, 1, 0,
       0, 1, 1, 1, 1, 1, 1, 1, 1, 0,
       0, 1, 1, 1, 1, 1, 1, 1, 1, 0,
       0, 1, 1, 1, 1, 1, 1, 1, 1, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0
   };

   std::ofstream out("level.txt"); // Open stream
   out.write(map, sizeof( map )); // Write data
   out.close(); // Close stream
}

Then reading it is just as easy, except for the fact that you need to know how big the map will be before reading it, but in this case, it will just be 100:

#include <iostream>
#include <fstream>

int main() {
   char map[100];
   std::ifstream in("level.txt"); // Open stream
   in.read(map, sizeof( map )); // Read data from file
   in.close(); // Close stream
}

Hope this helps.

Heya. Yeah, it's kinda what I'm looking for. But how do I work with a specified format of the text in the textfile? Something like this is what I'm thinking of that I will work with, only bigger format.

10 -->
|
v
= 100 tiles

Each 10 in length is ended with a break (which would actually make it 11 in length with the blankspace, aside from last one) so that it looks kinda like this in the textfile:

1111111111
1s00010001
1110111011
1000010011
1011110111
1001000001
1001111101
1100001001
1001000011
1111111111

I guess it really doesn't matter if I used a one-dimension array or not but I'd prefer to split it if it was possible, either way it would be nice to know how to.

Something like this, only I don't know how to go about it now.

#include <iostream>
#include <fstream>

int main() 
{
    char level[10]10]; // [x][y]


    std::ifstream infile("level.txt"); // open

    // What now?

    infile.close(); // close
}

Is this what your looking for :)?

#include <iostream>
#include <fstream>

int main() 
{
    char level[10][10]; // [x][y]

    std::ifstream infile("level.txt"); // open
    char ch; // Temp char

    // What now?   -->  Read it manually
    for (int y = 0; y < 10; ++y) {
       for (int x = 0; x < 10; ++x) {
          infile.get(ch);
          if (ch != '\n') {
             level[x][y] = ch;
          } else {
             --x;
             continue;
          }
       }
    }

    // Display the map
    for (int y = 0; y < 10; ++y) {
       for (int x = 0; x < 10; ++x) {
          std::cout << level[x][y];
       }
       std::cout << '\n';
    }

    infile.close(); // close
    std::cin.ignore();
}

Yes, that's how I want it :)

Just thinking about how simple it is makes me feel dumber by the second, but thanks a lot for the help!

I'll keep you tuned on the lil' game if there's anything else bothering me. But now it's time for some sleep, so bye for now :)

I want same like gotnoname but I want it in binary form please how?

I tried this code but when I load it, it does not show me my control message. Please where is the error? When i try show the map, some chars was missing.
Save my map

#include <iostream>
#include <fstream>

int main() {
   char map[20][18]= {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
   };

   std::ofstream out; // Open stream
   out.open("map.txt", std::ios_base::out | std::ios_base::binary);
   if(out.write((char *) &map, sizeof( map )))  // Write data
   std::cout<<"Zapisane OK\n";
   out.close(); // Close stream
}

Load my map, here is a problem!

#include <iostream>
#include <fstream>

int main() {
   char map[20][18];
   std::ifstream in; // Open stream
   in.open("map.txt", std::ios_base::in | std::ios_base::binary);
   if(in.read((char *) &map, sizeof( map )))
   std::cout<<"Nacitanie OK\n"; // Read data from file


   in.close(); // Close stream
}

I have already solved it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.