Loading maps

This is a discussion on Loading maps within the C++ Programming forums, part of the General Programming Boards category; I'm writing my first rpg and I read that it is much better to load maps rather than including them ...

  1. #1
    Registered User divineleft's Avatar
    Join Date
    Jul 2006
    Posts
    158

    Loading maps

    I'm writing my first rpg and I read that it is much better to load maps rather than including them in the compilation because it reduces the .exe size, which makes sense. I'm not to sure how to do that. Here is an example of what my maps look like:

    Code:
     if (map ==1){
    
    
        vector<string> trail1(8);
        trail1[0] = " ________ ____ ";
        trail1[1] = "|       / /   |";
        trail1[2] = "|      / /    |";
        trail1[3] = "|     | |     |";
        trail1[4] = "|     | |     |";
        trail1[5] = "|     | |     |";
        trail1[6] = "|     | |     |";
        trail1[7] = "|_____| |_____|";
    
        trail1[y].replace(x, 1, "X");
    
        for (int index = 0; index <8; ++index)
        {
    
            cout << trail1[index] << endl;
        }
     }
    That is saved as a seperate file and is included during the compilation. I was wondering how I could get the map to load when the user enters that area rather than it already being in the .exe.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    You would probably store this in a textfile:
    Code:
     ________ ____ 
    |       / /   |
    |      / /    |
    |     | |     |
    |     | |     |
    |     | |     |
    |     | |     |
    |_____| |_____|
    Then you just need to read it line by line:
    Code:
    // assume this typedef somewhere
    typedef std::vector<std::string> MapType;
    
    // reading into trail
    MapType trail;
    std::ifstream in("textfile.txt");
    std::string str;
    while (std::getline(in, str))
    {
    	trail.push_back(str);
    }
    
    // making the replacement
    trail[y].replace(x, 1, "X"); // y must be less than trail.size()
    
    // to print
    for (MapType::iterator i = trail.begin(); i != trail.end(); ++i)
    {
    	std::cout << *i << std::endl;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cargo loading system
    By kyle rull in forum C Programming
    Replies: 1
    Last Post: 04-20-2009, 12:16 PM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Loading a bitmap (Without using glaux)
    By Shamino in forum Game Programming
    Replies: 7
    Last Post: 03-16-2006, 08:43 AM
  4. need help with .md3 file loading
    By Shadow12345 in forum Game Programming
    Replies: 2
    Last Post: 12-06-2002, 03:06 PM
  5. Loading Screen
    By pinkcheese in forum Windows Programming
    Replies: 2
    Last Post: 04-06-2002, 10:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21