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 ;

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

}
}
``````

Incomplete obviously, but I attempted.

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?

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++)
{

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

Cannot be defined in currentscope error on in and binary

Nevermind about the error, got it :D

Writing rest of code now

``````void drawMap(std::string MapLoc) {
char* gridMap ;

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?

Nevermind, just realized I havent read the text yet.

``````void drawMap(std::string MapLoc) {
char* gridMap ;

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);
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);
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

Be a part of the DaniWeb community

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