0

Hey guys,

I need a function that reads a text file and stores the character someway, (I am assuming a 2D Array would be best?) then a function that reads through the characters and if the char is something specific (like "X") then it would draw a square (QUAD) 10wx10h multiplied by the location.

Example:

XXXXXX
X....X
X....X
X....X
X....X
XXXXXX

Would draw:

[][][][][][]
[]        []
[]        []
[]        []
[]        []
[][][][][][]

X is the square, and . is a space (Nothing there, it would be skipped)

Im basically creating a map based on a text file.

CODE SO FAR:

void readMap(std::string MapLoc) {
    int gridMap[95] [55];

    std::ifstream MapLoc;
    for (int i =0, int j =0; i < 95, j < 55; i++, j++)
        {

        }
}

Incomplete obviously, but I attempted.

Edited by Doogledude123: Added Code So Far

2
Contributors
7
Replies
12
Views
4 Years
Discussion Span
Last Post by Doogledude123
0

I read that over, and honestly, I'm not completely understanding it.

Could you write some example code for what I want and explain using comments?

0

I don't think you'd learn very much if I did.

Start with the constructor and use the in and binary flags. Then, create a character array (buffer) and use the read() method to fill the buffer. You could either set the buffer to the same size as gridMap (ie. 5225 = 95*55) and call read() once or a smaller value, like the row length, and call read() whenever you need. Then call close() when you're done.

FYI, your for-loop wouldn't iterate through each element. It would move diagonally through the grid like this:

  1. i = 0, j = 0
  2. i = 1, j = 1
  3. i = 2, j = 2
  4. etc.

You should use two for-loops like so:

for(int i = 0; i < 95; i++)
{
    for(int j = 0; j < 55; j++)
    {

    }
}
0
std::ifstream MapLoc, std::ifstream::in, std::ifstream::binary;

Cannot be defined in currentscope error on in and binary

0
void drawMap(std::string MapLoc) {
    char* gridMap[95] [55];

    ifstream (MapLoc, ifstream::in, ifstream::binary);

    for(int i = 0; i < 95; i++)
        {
        for(int j = 0; j < 55; j++)
            {
                if (gridMap[i] [j] == "X")
                {
                    //Draw Wall
                }
                else if (gridMap[i] [j] == ".")
                {
                    //Draw Space
                }
                else if (gridMap[i] [j] == "C")
                {
                    //Draw Char
                }
                else if (gridMap[i] [j] == "O")
                {
                    //Draw Opponent
                }
            }
        }
}

Correct?

0

Nevermind, just realized I havent read the text yet.

void drawMap(std::string MapLoc) {
    char* gridMap[95] [55];

    ifstream Map(MapLoc, ifstream::in, ifstream::binary);

    while (Map.good())
    {
    for(int i = 0; i < 95; i++)
        {
        for(int j = 0; j < 55; j++)
            {
                if (gridMap[i] [j] == "X")
                {
                    //Draw Wall
                    glColor4ub(255,255,255,255);
                    glBegin(GL_QUADS);
                    glVertex2f(10*i,10*j);
                    glVertex2f(10*i,10*j);
                    glVertex2f(10*i,10*j);
                    glVertex2f(10*i,10*j);
                    glEnd();
                }
                else if (gridMap[i] [j] == ".")
                {
                    //Draw Space
                    glColor4ub(0,0,0,255);
                    glBegin(GL_QUADS);
                    glVertex2f(10*i,10*j);
                    glVertex2f(10*i,10*j);
                    glVertex2f(10*i,10*j);
                    glVertex2f(10*i,10*j);
                    glEnd();
                }
                else if (gridMap[i] [j] == "C")
                {
                    //Draw Char
                }
                else if (gridMap[i] [j] == "O")
                {
                    //Draw Opponent
                }
            }
        }
    }
    Map.close();
}

I have that so far, could you give the rest? :P

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.