So in my asteroids game I get some major frame rate drops when I have 10+ asteroids being drawn and when running at 1ghz on battery. When I run the computer at full 1.86ghz the drop is less significant but still noticeable. All the same I want to make my code fast enough to do other things then move lines and be able run on slower computers.
since each asteroid is made up of 12 lines, 10 asteroids is 120 shapes then. And is that what's slowing it down? Is it the overhead for each line to be an individual drawable? Would it be faster to not recreate the shapes each frame and either use pointers to the x and ys?
If I did it in shape::polygons they would be turning concave and convex because of the rotation I have, and I think that may not draw properly.
Here's what happens every frame in the member functions:
void Asteroid::draw(sf::RenderWindow &win)
{
sf::Shape astShape[12];
int drawN = 12;
//Draw asteroid's inner rectangle
astShape[0] = sf::Shape::Line(x[0],y[0],x[1],y[1],1,sf::Color(255, 255, 255));
astShape[1] = sf::Shape::Line(x[1],y[1],x[2],y[2],1,sf::Color(255, 255, 255));
astShape[2] = sf::Shape::Line(x[2],y[2],x[3],y[3],1,sf::Color(255, 255, 255));
astShape[3] = sf::Shape::Line(x[3],y[3],x[0],y[0],1,sf::Color(255, 255, 255));
//Connect inner rectangle to top vertex
astShape[4] = sf::Shape::Line(x[0],y[0],x[4],y[4],1,sf::Color(255, 255, 255));
astShape[5] = sf::Shape::Line(x[1],y[1],x[4],y[4],1,sf::Color(255, 255, 255));
astShape[6] = sf::Shape::Line(x[2],y[2],x[4],y[4],1,sf::Color(255, 255, 255));
astShape[7] = sf::Shape::Line(x[3],y[3],x[4],y[4],1,sf::Color(255, 255, 255));
//Connect inner rectangle to bottom vertex
astShape[8] = sf::Shape::Line(x[0],y[0],x[5],y[5],1,sf::Color(255, 255, 255));
astShape[9] = sf::Shape::Line(x[1],y[1],x[5],y[5],1,sf::Color(255, 255, 255));
astShape[10] = sf::Shape::Line(x[2],y[2],x[5],y[5],1,sf::Color(255, 255, 255));
astShape[11] = sf::Shape::Line(x[3],y[3],x[5],y[5],1,sf::Color(255, 255, 255));
for(int i =0; i < drawN; i++)
win.Draw(astShape[i]);
}
void Asteroid::move(int winX, int winY)
{
//Rotate asteroid
y2Angle += spinSpeed;//Increase the angle by the amount of spinSpeed
y4Angle += spinSpeed;
y5Angle += spinSpeed;
y6Angle += spinSpeed;
y[1] = centerY + radius * sin(y2Angle);//Trig = hypotenuse * sin(destination angle)
y[3] = centerY + radius * sin(y4Angle);
y[5] = centerY + radius * sin(y5Angle);
y[4] = centerY + radius * sin(y6Angle);
//Move asteroid1 down
for (int i = 0; i < n; i++)
y[i] += asteroidMoveY;
centerY += asteroidMoveY;
}