SFML community forums

Help => Graphics => Topic started by: Ejack on April 20, 2018, 03:05:34 pm

Title: sf::text and vectors help
Post by: Ejack on April 20, 2018, 03:05:34 pm
Hi guys, new here and to SFML as well as being a novice with c++, Im trying to make sf::Text work with a vector, usually I would use an array like so:

sf::Text example[10];

but as the size must be dynamic I cant use an array and have tried using a vector like so:

// x is a number retrieved from another class
int vector_no = x + 2;

std::vector<sf::Text> myVector( vector_no );

If you know a way to make this dynamic or get the vector or something else to work please let me know, thankyou.
Title: Re: sf::text and vectors help
Post by: Geheim on April 20, 2018, 04:02:38 pm
Welcome to the forums!

So what do you mean by "get the vector to work"?

You already construct a vector with vector_no many elements and this vector can grow/shrink dynamically.
I don't see a problem there...
Title: Re: sf::text and vectors help
Post by: Arcade on April 20, 2018, 04:06:45 pm
Also, just a heads up, these forums are more intended for SFML discussion. General C++ questions, such as how to use a vector, are usually better asked elsewhere. Getting a good book on C++ and reading through it would be the best option. I recommend trying to learn C++ a little better before diving into a library like SFML.

Having said that, if your question is how to put additional elements in the vector "dynamically", a basic example could be something like the code below.
std::vector<sf::Text> myVector;

sf::Text myNewText;
// configure myText however you want to here

myVector.push_back(myNewText);
 

Just be aware that this example is copying myText into the vector, which is fine, but you probably won't want to do exactly this for other things like sf::Font or sf::Texture. I'll leave that as an exercise for the reader  ;)
Title: Re: sf::text and vectors help
Post by: Ejack on April 20, 2018, 04:35:57 pm
Thanks for the reply, I don't think I explained my problem very well, usually I would do this if I had a set number of time to iterate:

sf::Text example[10];

for (int j = 0, j < 10, j ++)
{
example[j].setFont(font);
example[j].setPosition(sf::vector2f( j , j ));
example[j].setFillColour(sf::Colour::White);
example[j].setString(std::to_string(j));
}

However, as I do not know how many iterations I will have until later in my program I was looking for something like this:

//number from different class
int iterations;

sf::Text std::vector<?> example //I know this does not work but I am unsure how to represent what I mean

for (int j = 0, j < iterations, j ++)
{
example[j].setFont(font);
example[j].setPosition(sf::vector2f( j , j ));
example[j].setFillColour(sf::Colour::White);
example[j].setString(std::to_string(j))
}

edit: changed all the 'i' in the loops to 'j' as it was turning the text italic

Title: Re: sf::text and vectors help
Post by: Geheim on April 20, 2018, 04:47:22 pm
Well, as Arcade already said, you can just add (push_back) your new texts later on.
Or you could re-create your vector once you know how many elements you need.

Just follow the recommendation, read up on std::vector (http://en.cppreference.com/w/cpp/container/vector) and you will find everything you need to know.