Trouble with ifstream...

closed account (10oTURfi)
I have problem loading this .txt
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 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 1 1 1 1 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 1 1 0 0 0 1 1 1 1 0 0 1 1
0 0 1 1 0 0 0 0 0 0 0 0 0 0 0
0 0 1 1 0 0 0 0 0 0 0 0 0 0 0
0 0 1 1 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

into 2D array.

This is the code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void Map::LoadTableFromFile(std::string FileName)
{
    std::ifstream File(FileName);
    int a, j = 0, k = 0;
    while(File >> a)
    {
        MapTable[j][k] = a;
        k++;
        if((j % BROJ_BLOKOVA_SIRINA) == 0)//this constant is equal to 15
        {
            j++;
            k = 0;
        }
    }
}


It compiles and doesnt crash here, but judging by what follows in program, it isnt done propertly.

Program crashes somewhere in this loop, which is (probably?) caused by above code.
1
2
3
4
5
6
7
8
9
10
11
12
    for(unsigned x = 0; x < BROJ_BLOKOVA_SIRINA; x++) 
    {
        for(unsigned y = 0; y < BROJ_BLOKOVA_VISINA; y++) 
        {
            int tileId = MapTable[x][y];
            sf::Texture Texture = MapTileset[tileId];
            sf::Sprite Sprite;
            Sprite.SetTexture(Texture);
            Sprite.SetPosition((float)x * 32, (float)y * 32);
            _Window.Draw(Sprite);
        }
    }


Help would be appreciated.
I think is better to post all code. I don't understand why is wrong. The reading of that file or the manipulators?
closed account (10oTURfi)
Sigh... Its quite large, but ok, you asked for it!
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
//Map.h
#ifndef MAP_H
#define MAP_H

#include <SFML\Graphics.hpp>
#include "Include.h"

class Map
{
    void LoadTableFromFile(std::string FileName);
    void LoadTilesetFromFile(std::string FileName);
    void ShowMap();
    std::string MapName;
    sf::RenderWindow _Window;
    int MapTable[BROJ_BLOKOVA_VISINA][BROJ_BLOKOVA_SIRINA];
    //std::vector<sf::Texture> MapTileset; //todo
    sf::Texture MapTileset[2]; //PH

public:
    Map(std::string TableFileName, std::string TilesetFileName, std::string Name);

    sf::Window &GetWindow() { return _Window; }
};

#endif 

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
55
56
57
58
59
60
61
//Map.cpp
#include "map.h"
#include <iostream>

Map::Map(std::string TableFileName, std::string TilesetFileName, std::string Name)
{
    MapName = Name;
    LoadTableFromFile(TableFileName);
    std::cerr << "asd" << std::endl;
    LoadTilesetFromFile(TilesetFileName);
    ShowMap();
}

void Map::LoadTableFromFile(std::string FileName)
{
    std::ifstream File(FileName);
    int a, j = 0, k = 0;
    while(File >> a)
    {
        MapTable[j][k] = a;
        k++;
        if((j % BROJ_BLOKOVA_SIRINA) == 0)
        {
            j++;
            k = 0;
        }
    }
}

void Map::LoadTilesetFromFile(std::string FileName)
{
    std::ifstream File;
    File.open(FileName);
    std::string ImgFileName;
    int a = 0;
    while(File >> ImgFileName)
    {
        MapTileset[a].LoadFromFile(ImgFileName);
        a++;
    }
}

void Map::ShowMap()
{
    _Window.Create(sf::VideoMode(1024, 768, 32), MapName);
    //todo: user input koji video mode staviti, a ne fixed
    for(unsigned x = 0; x < BROJ_BLOKOVA_SIRINA; x++) 
    {
        for(unsigned y = 0; y < BROJ_BLOKOVA_VISINA; y++) 
        {
            int tileId = MapTable[x][y];
            sf::Texture Texture = MapTileset[tileId];
            sf::Sprite Sprite;
            Sprite.SetTexture(Texture);
            Sprite.SetPosition((float)x * 32, (float)y * 32);
            _Window.Draw(Sprite);
        }
    }

    _Window.Display();
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//Main.cpp
#include "map.h"

int main()
{
    Map TestMap("TestTable.txt", "TestTileset.txt", "TestMap");
    //todo: dinamicno alociraj mape da ih mozes delete[], a promjene pohrani u fajl

    sf::Event Event;
    while (TestMap.GetWindow().IsOpened()) 
    {
        while (TestMap.GetWindow().PollEvent(Event)) 
        { 
            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Keyboard::Escape)) 
                TestMap.GetWindow().Close(); 
        } 
    }

    return 0;
}


I'm not gonna post file containing constants...
Have you thought of using Binary Files?

With what your doing, it may be a little simpler to use a Binary File and load it with a struct.

VThis is the site I leard it fromV
http://www.gamedev.net/page/resources/_/technical/general-programming/simple-file-io-using-c-r1127


And in the struct you can use a bool array.
Last edited on
closed account (10oTURfi)
Mmm no, that wouldnt solve my problem. Bool array is dreadful idea beacuse there will be more than 2 tiles in my game... Tiles with IDs 0 and 1 are only ones I created atm.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void Map::LoadTableFromFile(std::string FileName)
{
    std::ifstream File(FileName);
    int a, j = 0, k = 0;
    while(File >> a)
    {
        MapTable[j][k] = a;
        k++;
        if((j % BROJ_BLOKOVA_SIRINA) == 0)//this constant is equal to 15
        {
            j++;
            k = 0;
        }
    }
}
That will only increment 'k', till the end of file. I think there is no problem because of continuous allocation, but still you should fix it.

1
2
3
4
5
6
7
8
9
10
11
12
13
//int MapTable[BROJ_BLOKOVA_VISINA][BROJ_BLOKOVA_SIRINA];
    for(unsigned x = 0; x < BROJ_BLOKOVA_SIRINA; x++) 
    {
        for(unsigned y = 0; y < BROJ_BLOKOVA_VISINA; y++) 
        {
            int tileId = MapTable[x][y];
            sf::Texture Texture = MapTileset[tileId];
            sf::Sprite Sprite;
            Sprite.SetTexture(Texture);
            Sprite.SetPosition((float)x * 32, (float)y * 32);
            _Window.Draw(Sprite);
        }
    }
The limits are swapped. The first loop should be till 'VISINA', the second to 'SIRINA'

//todo: user input koji video mode staviti, a ne fixed Translation please. I've got a thing against dynamic allocation, xP
closed account (10oTURfi)
//todo: user input koji video mode staviti, a ne fixed

ignore this one, i often put 'todo's and later figure out its useless


//todo: dinamicno alociraj mape da ih mozes delete[], a promjene pohrani u fajl

TODO: Dynamicaly allocate Map objects so you can delete[] them, and store changes to file.

The limits are swapped. The first loop should be till 'VISINA', the second to 'SIRINA'

Thanks! It looks like it IS swapped.
I am amazed how you noticed it out without knowing meaning of constant :O
You declare your array:
 
int MapTable[BROJ_BLOKOVA_VISINA][BROJ_BLOKOVA_SIRINA];

But you use your array:
1
2
3
4
5
6
7
8
    for(unsigned x = 0; x < BROJ_BLOKOVA_SIRINA; x++) 
    {
        for(unsigned y = 0; y < BROJ_BLOKOVA_VISINA; y++) 
        {
            int tileId = MapTable[x][y];
            // ...
        }
    }

You appear to have mixed up your x and y dimensions.
I actually looked at your profile, figured out that that was croatian and used google, ;)
Then I looked at the array declaration.

Ja, I quoted the wrong todo. But yeah, ¿why do you want dynamic allocation?
closed account (10oTURfi)
I want to destroy unused map when player leaves it to free up resources. Map objects will get rather large soon.
Last edited on
Topic archived. No new replies allowed.