SFML community forums

Help => General => Topic started by: K.F on July 23, 2012, 03:16:41 am

Title: is there a way for automatically initializing infinite sprites or shapes?
Post by: K.F on July 23, 2012, 03:16:41 am
Hi.

 Is there a way to let the user make any number of shapes or sprites without me initializing them first?

 For example, I am making 2D billiard game as exercise using SFML 2.0 and want the player to create the balls by clicking on the place where he wants the ball to be, I want a way for a ball sprite to be created and named "ball_01,ball_02" every time he clicks without me making them one by one.

 Other related question is how do I enter these sprites in a loop or if statement?

 For example, I want a loop or function for these sprites to check for collision with every other sprite without writing different if statements for each sprite?
Title: Re: is there a way for automatically initializing infinite sprites or shapes?
Post by: eXpl0it3r on July 23, 2012, 03:29:34 am
This is all possible with C++, but C++ isn't a language for which you can write your wishes in English and the compiler will turn it into your game. ;)
You need to learn how to use C++ first. This also involves thinking diffrently form a point of view of a programmer perspective.

And since may insiste on a solution, I give a bit text, if you don't understand it, you just realized you need to read more on C++.
You can use a STL vector to hold your sprites. Then whenever you click somewhere with mouse you can retrieve the coordinates and use them to place a new sprite which you then push onto the vector. For every frame you just iterate through the vector and draw each sprite. But make sure that the sprites and textures don't go out of scope, otherwise you would run into some problems.

Have fun learning more about C++! :)
Title: Re: is there a way for automatically initializing infinite sprites or shapes?
Post by: K.F on July 23, 2012, 04:16:01 am
I knew vectors could be used in this way for variables but the fact they could hold sprites as well is new to me. :P

Will try this soon, thank you very much.