Vertex array tile sprites

Pages: 12
Ok so this is my vector loop, so i would do something like this for my code except make it a multidimensional vector:

1
2
3
4
5
6
7
8
9
10
11
    vector<int> vec;

    vec.push_back(10);
    vec.push_back(100);
    vec.push_back(60);

    // this is what you are doing:
    for(int i = 0; i < vec.size(); ++i)
    {
        cout << vec[i] << endl;
    }
Last edited on
That loop is correct. Here, vec.size() is 3, so the loop will run 3 times:

- once where i=0
- once where i=1
- once where i=2

So you will will be accessing vec[0], vec[1], and vec[2]. Which are the only 3 elements in the vector. If you attempted to access vec[3] that would be out of bounds.
Ok well I tried to output the grid like this but it didnt work. Everything compiled ok though. I know the second for loop only outputs 1 row but i dont see it on screen.

1
2
3
4
5
6
7
8
9
10
11
//Input grid into vector
    for(int i = 0; i < GridSize; i++)
    {
        spr_Vector.push_back(blankGridSprite);
    }

    //Output to map
    for(int j = 0; j < spr_Vector.size(); j++)
    {
        window.draw(spr_Vector[j]);
    }
Last edited on
Where do you set the position?
I was just about to post that I forgot about that, so I added it and my game lags when i move the screen, and it seems like the sprites are all in one spot. I did this and when i entered my map it was waaaayy bigger than i wanted so i'm guessing thatits drawing the map too many times, I just have to find out where.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Input grid into vector
    for(int i = 0; i < GridSize; i++)
    {
        spr_Vector.push_back(blankGridSprite);
    }

    //Output to map

    for(int i = 0; i < spr_Vector.size(); i++)
    {
        for(int j = 0; j < spr_Vector.size(); j++)
        {
            blankGridSprite.setPosition(j * 32, i * 32);
            //blankGridSprite.setTextureRect(sf::IntRect(j * 32, i * 32, 32, 32));
            window.draw(blankGridSprite);
        }
    }


EDIT: I think it keeps repeating the push back loop.
Last edited on
Fixed it, Put the push back for loop outside the main event while loop. But why is my program lightening fast when i set the framerate to 60? Here is the entire code:

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <SFML/Graphics.hpp>
#include <iostream>
#include <list>
#include <vector>

using namespace std;

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Tile Editor");
    sf::Event event;
    sf::Mouse mouse;
    sf::Clock clock;
    sf::Time time;
    sf::View scrollScreen;
    list<sf::Sprite> setTile;
    list<sf::Sprite>::iterator iter;
    vector<sf::Sprite> spr_Vector;
    int GridSize;
    window.setFramerateLimit(60);
    scrollScreen.reset(sf::FloatRect(0, 0, window.getSize().x, window.getSize().y));


    sf::Texture blankGridTexture;
    sf::Sprite blankGridSprite;
    if(!blankGridTexture.loadFromFile("Resources/Tiles/Grid.png"))
    {

    }
    blankGridSprite.setTexture(blankGridTexture);
    blankGridSprite.setOrigin(16, 16);

    GridSize = 10;

    //Input grid into vector
    for(int i = 0; i < GridSize; i++)
    {
        spr_Vector.push_back(blankGridSprite);
    }

    while(window.isOpen())
    {
        while(window.pollEvent(event))
        {
            switch(event.type)
            {
                case sf::Event::Closed:
                {
                    window.close();
                }break;
                case sf::Event::Resized:
                {
                    sf::FloatRect viewArea(0, 0, event.size.width, event.size.height);
                    window.setView(sf::View(viewArea));
                    scrollScreen.reset(sf::FloatRect(0, 0, window.getSize().x, window.getSize().y));
                }break;
            }
        }
    window.clear(sf::Color::White);

    time = clock.getElapsedTime();

    sf::Vector2i pixel_pos = sf::Mouse::getPosition(window);
    sf::Vector2f coord_pos = window.mapPixelToCoords(pixel_pos);

    cout << "Row: " << rint(coord_pos.x / 32) << ",";
    cout << "Column: " << rint(coord_pos.y / 32) << endl;

    scrollScreen.setViewport(sf::FloatRect(0, 0, 1.0f, 1.0f));


    //Load Grass
    sf::Texture tileTexture;
    sf::Sprite tileSprite;
    if(!tileTexture.loadFromFile("Resources/Tiles/Grass.png"))
    {

    }
    tileSprite.setTexture(tileTexture);
    tileSprite.setOrigin(16, 16);

    if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
    {
        scrollScreen.move(-10, 0);
    }
    else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
    {
        scrollScreen.move(10, 0);
    }
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
    {
        scrollScreen.move(0, -10);
    }
    else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
    {
        scrollScreen.move(0, 10);
    }

    //Output to map

    for(int i = 0; i < spr_Vector.size(); i++)
    {
        for(int j = 0; j < spr_Vector.size(); j++)
        {
            blankGridSprite.setPosition(j * 32, i * 32);
            //blankGridSprite.setTextureRect(sf::IntRect(j * 32, i * 32, 32, 32));
            window.draw(blankGridSprite);
        }
    }

/*
    if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
    {
        cout << "Clicked" << endl;
        //Get Position of the mouse then divide by 32 and round down to integer, then times it by 32 to set grid.
        tileSprite.setPosition(rint(coord_pos.x / 32) * 32, rint(coord_pos.y / 32) * 32);
        setTile.push_back(tileSprite);
    }
*/
    if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
    {
        //tileSprite.setPosition(rint(coord_pos.x / 32) * 32, rint(coord_pos.y / 32) * 32);
        //setTile.push_back(tileSprite);
        //tileGrid[4][2].setPosition();
    }

    if(sf::Keyboard::isKeyPressed(sf::Keyboard::L))
    {
    }

    iter = setTile.begin();

    while(iter != setTile.end())
    {
        window.draw(*iter);
        iter++;

    }

    cout << setTile.size() << endl;
    cout << time.asSeconds() << endl;

    window.setView(scrollScreen);
    clock.restart();
    window.display();
    }

    return 0;
}
But why is my program lightening fast when i set the framerate to 60?


How fast is it?

You should be having 60 updates every second... which is pretty normal for a program (but insanely fast for a human)
Nevermind It's going at the right speed, i just thought it was going crazy. So now I just need to figure out how to set a tile. I'm a bit confused with this again. Here is my code with all the unnecessary stuff removed:

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <SFML/Graphics.hpp>
#include <iostream>
#include <list>
#include <vector>

using namespace std;

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Tile Editor");
    sf::Event event;
    sf::Mouse mouse;
    sf::Clock clock;
    sf::Time time;
    sf::View scrollScreen;
    list<sf::Sprite> setTile;
    list<sf::Sprite>::iterator iter;
    vector<sf::Sprite> spr_Vector;
    int GridSize;
    window.setFramerateLimit(60);
    scrollScreen.reset(sf::FloatRect(0, 0, window.getSize().x, window.getSize().y));


    sf::Texture blankGridTexture;
    sf::Sprite blankGridSprite;
    if(!blankGridTexture.loadFromFile("Resources/Tiles/Grid.png"))
    {

    }
    blankGridSprite.setTexture(blankGridTexture);
    blankGridSprite.setOrigin(16, 16);

    GridSize = 30;

    //Input grid into vector
    for(int i = 0; i < GridSize; i++)
    {
        spr_Vector.push_back(blankGridSprite);
    }

    while(window.isOpen())
    {
        while(window.pollEvent(event))
        {
            switch(event.type)
            {
                case sf::Event::Closed:
                {
                    window.close();
                }break;
                case sf::Event::Resized:
                {
                    sf::FloatRect viewArea(0, 0, event.size.width, event.size.height);
                    window.setView(sf::View(viewArea));
                    scrollScreen.reset(sf::FloatRect(0, 0, window.getSize().x, window.getSize().y));
                }break;
            }
        }
    window.clear(sf::Color::White);

    time = clock.getElapsedTime();

    sf::Vector2i pixel_pos = sf::Mouse::getPosition(window);
    sf::Vector2f coord_pos = window.mapPixelToCoords(pixel_pos);

    cout << "Row: " << rint(coord_pos.x / 32) << ",";
    cout << "Column: " << rint(coord_pos.y / 32) << endl;

    scrollScreen.setViewport(sf::FloatRect(0, 0, 1.0f, 1.0f));

    //Load Grass
    sf::Texture tileTexture;
    sf::Sprite tileSprite;
    if(!tileTexture.loadFromFile("Resources/Tiles/Grass.png"))
    {

    }
    tileSprite.setTexture(tileTexture);
    tileSprite.setOrigin(16, 16);

    if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
    {
        scrollScreen.move(-10, 0);
    }
    else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
    {
        scrollScreen.move(10, 0);
    }
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
    {
        scrollScreen.move(0, -10);
    }
    else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
    {
        scrollScreen.move(0, 10);
    }

    //Output to map

    for(int i = 0; i < spr_Vector.size(); i++)
    {
        for(int j = 0; j < spr_Vector.size(); j++)
        {
            blankGridSprite.setPosition(j * 32, i * 32);
            //blankGridSprite.setTextureRect(sf::IntRect(j * 32, i * 32, 32, 32));
            window.draw(blankGridSprite);
        }
    }

    if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
    {
        //tileSprite.setPosition(rint(coord_pos.x / 32) * 32, rint(coord_pos.y / 32) * 32);
        blankGridSprite.setTexture();
    }

    //cout << setTile.size() << endl;
    //cout << time.asSeconds() << endl;

    window.setView(scrollScreen);
    clock.restart();
    window.display();
    }

    return 0;
}
So now I just need to figure out how to set a tile. I'm a bit confused with this again


You have a vector.
Each sprite in that vector represents 1 tile in the map, right?

Therefore, to draw the map... all you need to do is draw all the sprites in that vector.
And to change the map... all you need to do is change the sprites in that vector.
I already drew the map, but I want to change a sprite texture at the mouse position, but I cant seem to figure it out because i have to get the mouse position then set the texture and i cant seem to combine the stuff together to make it work right.
I already drew the map,


Doesn't look like it to me. I assume the vector is spr_Vector, right? You are never drawing anything from that vector.
Yes thats my sprite vector, so I do this?

1
2
3
4
5
6
7
8
9
10
11
for(int i = 0; i < spr_Vector.size(); i++)
    {
        for(int j = 0; j < spr_Vector.size(); j++)
        {
            //blankGridSprite.setPosition(j * 32, i * 32);
            spr_Vector[j * 32];
            spr_Vector[i * 32];
            //blankGridSprite.setTextureRect(sf::IntRect(j * 32, i * 32, 32, 32));
            window.draw(blankGridSprite);
        }
    }


that only sets 1 tile.
The [] operator just accesses one element in a vector.

 
spr_Vector[0]

This accesses element [0]. But it doesn't do anything with it.


spr_Vector[j * 32];

If j==8, then this accesses element [256]... which is OUT OF BOUNDS since your vector only has 30 elements (meaning element [29] is the last legal one you can access).


This line is actually drawing a sprite:

 
window.draw(blankGridSprite);


But look closely at it. What exactly are you drawing? It's not anything from your vector.
Last edited on
Ah, ok, so I removed window.draw(blankGridSprite); and replaced it with spr_Vector[8] and it drew a sprite, so to draw the entire grid by using an iterator?
You can use an iterator... yes. Or you can use the index. Though you seem to have trouble grasping the concept of indexing.
Yeah, My brain is still stuck on console programming it's hard for me to think in 2d terms. What do I need to do?
Get it working in 1D terms. You should be able to.

You have an array a vector with 30 sprites in it. That is enough sprites to draw one row of tiles (I assume your rows are 30 tiles wide, right?)

So draw that row. Then make it so you can change the tiles in that row.

Once you have it working, then you can add the 2nd dimension to it.



As for how to do it... all you really have to do is iterate over each sprite in the vector -- set them to the correct position... and draw them. Easy peasy.

You've already demonstrated you know how to do this in this post: http://www.cplusplus.com/forum/beginner/141329/2/#msg746714

The only difference is... instead of printing the element by giving it to cout... you will instead be drawing it by passing it to window.draw().



For editing, you just have to figure out which of your 30 sprites the user clicked on... and then access that individual sprite and change its properties.

EDIT:

Did you not see my PM?
Last edited on
Alright i'll see what i can come up with, also I should show you my for loop that's adding the sprites to the vector, just to make sure that its right:

This is outside the game loop.

1
2
3
4
for(int i = 0; i < GridSize; i++)
    {
        spr_Vector.push_back(blankGridSprite);
    }


No i didnt see the pm, but I looked at it now, What is an IRC thing?
Last edited on
Alright i'm at the end of my rope now I just don't know what to do. I was working on the number vector to see if i could output multiple versions of the same element, and I got it working and so I tried the same thing with the sprite vector and it doesnt work. So now i'm completley lost, not even a single shred of an idea of what to do next. Can i get another hint?

Here is the number vector

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    vector<int> vec;
    int gridSize = 30;

    vec.push_back(gridSize);

    // this is what you are doing:
    for(int i = 0; i < vec.size(); ++i)
    {
        for(int j = 0; j < vec[i]; ++j)
        {
            cout << vec[i] << endl;
        }
    }

    return 0;
}

Last edited on
check your PM
Topic archived. No new replies allowed.
Pages: 12