Hey, I need some guidance please with solving a problem I'm trying to tackle.
I'm not sure of the best way to draw a stl::vector of objects in a grid.
I'm trying to create space invaders, I store my enemies in a vector* but I want to draw them to the screen in a grid like shape. I'm not sure what is the best approach for this. Not sure if I should be using a 2D array here,
Hey. I did Space Invaders a few months back as a project for my school. Using SFML and c++.
What I did was this. I loaded in one sprite/alien. Then I created a vector which is filled with a numer of these sprites, I called it the RowVector. Say 7. Then a final Vector which is filled with a number of RowVectors.
I'll try and give you a visual representation.
Load in an alien sprite, lets say it looks like this: (-*-).
In the rowVector, I fill it with 7 (-*-).
So now it looks like this:
(-*-) (-*-) (-*-) (-*-) (-*-) (-*-) (-*-)
Then for my final vector, the columnVector, I fill it with the row rows. Say 3 of them. I get this
There are obviously a number of ways of doing this, but this is what I went with. Hope it helps a little bit though.
Edit: To add more. You load in the first alien sprite on a position on the screen.
In the rowVector thing. Since you're loading in 7 of the alien sprites. Each tiem you load in one, using the vector.pushback( ) function, you do sprite.setPosition() and move the next one a little bit X axis while remaining the same on the Y axis, so you get a row.
The other way around on the final vector, which loads them in as columns. You just change the Y so the rows are drawn under eachother.
Just to clarify, the vector is just a vector of objects that I will be iterating through, each time I loop through the vector I push in a new object/Enemy and set its position on the X + an offset etc.
And then do the same for the other vector of objects (columns).