SFML problem

I have been following CodingMadeEasy, now I'm on tutorial #28 but I got a problem with loadCounter.x because I think it should be 3.

https://www.youtube.com/watch?v=YJ9WTw5AShE&list=PLHJE4y54mpC5j_x90UkuoMZOdmmL9-_rg&index=28

I am using Ubuntu 14.04 if it would make a difference

Map1
1
2
3
4
tiles.png
x,x x,x x,x
x,x 1,0 1,1
1,0 0,1 0,1



main.cpp
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  #include <SFML/Graphics.hpp>
#include <iostream>
#include <fstream>
#include <cctype>
#include <string>
#include <vector>

int main()
{
    std::ifstream openfile("Map1");

    sf::Texture tileTexture;
    sf::Sprite tiles;

    sf::Vector2i map[100][100];
    sf::Vector2i loadCounter = sf::Vector2i(0, 0);

    if(openfile.is_open())
    {
        std::string tileLocation;
        openfile >> tileLocation;
        tileTexture.loadFromFile(tileLocation);
        tiles.setTexture(tileTexture);
        while(!openfile.eof())
        {
            std::string str;
            openfile >> str;
            char x = str[0], y = str[2];
            std::cout << "X: " << x << "Y: " << y << std::endl;
            if(!isdigit(x) || !isdigit(y))
                map[loadCounter.x][loadCounter.y] = sf::Vector2i(-1, -1);
            else
                map[loadCounter.x][loadCounter.y] = sf::Vector2i(x - '0', y - '0');

            if (openfile.peek() == '\n')
            {
                loadCounter.x = 0;
                loadCounter.y++;
            }
            else
                loadCounter.x++;
        }
        loadCounter.y++;
    }
    sf::RenderWindow Window(sf::VideoMode(640, 480, 32), "Loading Maps[Intermediate]");

    while(Window.isOpen())
    {
        sf::Event Event;
        while(Window.pollEvent(Event))
        {
            switch (Event.type)
            {
            case sf::Event::Closed:
                Window.close();
                break;
            }
        }
        Window.clear(sf::Color::Cyan);
        //This is just testing if it works
        std::cout << loadCounter.x << std::endl;
        std::cout << loadCounter.y << std::endl;
        for(int i = 0; i < loadCounter.x; i++)
        {
            for(int j = 0; j < loadCounter.y; j++)
            {
                if(map[i][j].x != -1 && map[i][j].y != -1)
                {
                    tiles.setPosition(i * 32, j * 32);
                    tiles.setTextureRect(sf::IntRect(map[i][j].x * 32, map[i][j].x * 32, 32, 32));
                    Window.draw(tiles);
                }
            }
        }

        Window.display();
    }
}
Last edited on
1
2
3
4
tiles.setPosition(i * 32, j * 32);
//                                          here's 1 problem ˇ
tiles.setTextureRect(sf::IntRect(map[i][j].x * 32, map[i][j].x * 32, 32, 32));
Window.draw(tiles);

It should be
 
tiles.setTextureRect(sf::IntRect(map[i][j].x * 32, map[i][j].y * 32, 32, 32));


But it still prints only 1 number =(
Last edited on
Topic archived. No new replies allowed.