Reading level data from txt file

Hello i am making this 2d game with c++ SDL and opengl. I am using glQuads to render 2d quadrilaterals to the screen. There are numeruos squares of different sizes that my character jumps on to get across. The glQuads function requires coordinates.
1
2
3
4
5
6
7
glBegin(GL_QUADS); 
    
glVertex2f(x,y);
glVertex2f(x+width, y);
glVertex2f(x+width,y+height);
glVertex2f(x, y+height);
glEnd(); //End drawing 

Each line is the coordinates for each corner in the square. I have a map class with a draw() function in it that just draws the x, y, width height, and when i call it i can pass values for them, so as i call the map.draw() i re-size and position my square.
I call this function in my main.cpp where a main game loop is happening. How could i simply write the coordinates in a text document, and a ifstream objects loop through each number assigns it to x, y , width height and draws it? So every 4 numbers in the text document would be a new square. eg:
100 200 10 10
could be in my txt. I have tried many different ways for hours.
Thankyou very much, any help would be appreciated.
Thankyou gain in advance :)
oooo dont worry guys i had one more crack at it and i got it! wast so hard.
Tell me if you think this code could be improved though please (:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void Map::loadMap() {
 


  
   MapFile.open("level1.txt");

float x, y, width, height;
while (!MapFile.eof())
{

  MapFile >> x;
   MapFile >> y;
    MapFile >> width;
     MapFile >> height;

draw("platform", x, y, width, height);
}


   MapFile.close();
}
Last edited on
Well, you can store map data into a struct like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
struct MapTile {
    unsigned int Texture;
    unsigned int isSolid;
    unsigned int X;    // Just edit this line and make it float to use a float, obv
    unsigned int Y;    // Same here too.
    // You can add width and height too, just follow the next comment
};
struct MapData {
    MapTile * Tiles;
    unsigned long TileCount;
};
void DeallocMap(MapData &mapData) {
    if(mapData.Tiles)
        delete[] mapData.Tiles;
    mapData.Tiles = 0;
    mapData.TileCount = 0;
}
void GetToNewLine(ifstream &is) {
    char Buf = 0;
    while(!is.eof())
    {
        is >> Buf;
        if(Buf == '\n')
            return;
    }
}
bool LoadMap(MapData &mapData, const char * Filename) {
    DeallocMap(mapData);
    ifstream MapFile;
    MapFile.open(Filename);
    // Read in the Tiles Count.
    MapFile >> (mapData.TileCount);
    if(!mapData.TileCount)
    {
        MapFile.close();
        return 0; // File is empty, or no Tiles are stored inside
    }
    GetToNewLine(MapFile); // Goes to new line
    mapData.Tiles = new MapTile[mapData.TileCount]; // Get the required memory
    for(unsigned long i = 0; i < mapData.TileCount; i++)
    {
        if(MapFile.eof()) {
            MapFile.close();
            mapData.TileCount = i;
            return 1;
        }
        MapFile >> (mapData.Tiles[i].Texture);
        MapFile >> (mapData.Tiles[i].isSolid);
        MapFile >> (mapData.Tiles[i].X);
        MapFile >> (mapData.Tiles[i].Y);
        // To add Width and Height, add here a line like these four over this comment
        GetToNewLine(MapFile); // Goes to new line
    }
}

and use it like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
    //...
    MapData mapData = {0}; // Needed, else crash.
    if(!LoadMap(mapData,"level1.txt"))
        {;}// Error

    /* Pseudocode */
    SDL_Surface * Textures[/* How many map tile textures you will use */];
    LoadTextures(Textures); // Load them
    while(Draw)
    {
        for(unsigned int i = 0; i < mapData.TileCount; i++)
        {
            Draw(Textures[ (mapData.Tiles[i].Texture) ], mapData.Tiles[i].X, mapData.Tiles[i].Y, (Width of Texture), (Height of Texture) );
            // Obv, if you want to use width and height, also edit this line.
        }
    }
    DeallocMap(mapData); // Leave the Requested Memory
}


And in your file:
1
2
3
4
3 // Tiles. Remember not to add comments.
0 /* First Texture */ 5 10 /* X 5, Y 10. If you use width and height also store them */
1 /* Second Texture */ 25 111
5 /* 6th tex */ 123 339


I did almost no error checking. They are up to you.
Last edited on
oh wow this is great, Thankyou very much for writing and explaining all of that. I did learn things from that. Unfortunately, i didnt understand it all as a whole, however I did get a fair bit of it ): ahah. How long have you been programming?
Around 2.5 years. If you don't know what something does, just ask ^.
Topic archived. No new replies allowed.