I've been trying to write a Draw() function for my game to call and I'm not quite sure how to pass asteroidSprite into the function. I'm changed it to define the Image and Sprite in the function, which works, but obviously is incredibly inefficient. Can anyone shed some light on the correct way to do this?
Also, it might be useful to mention I'm using SFML.
Thank you in advance!
asteroidSprite not declared in this scope.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
//Asteroid.cpp
void Asteroid::Draw(int c, int AsteroidCount)
{
if (IsDrawable[c] == true)
{
if (IsSelect[c] == true)
{
Window.Draw(sf::Shape::Circle(RenderAsteroidX[c], RenderAsteroidY[c], 20.f, sf::Color(125,255,0,100), 1.f, sf::Color::Green));
}
asteroidSprite[c].SetPosition(RenderAsteroidX[c],RenderAsteroidY[c]);
asteroidSprite[c].Rotate(1);
Window.Draw(asteroidSprite[c]);
}
}
Pass the object into the function by reference; this will avoid making a copy of it, which is your concern judging by "but obviously is incredibly inefficient".
I would caution that premature optimisation is the root of all evil; do you absolutely know for sure that making a copy of the sprite is going to cause problems?
I'm fairly new to programming and I suppose I'm just not certain as to how to pass the object into the function by reference in this case.
The way I got it to work was by putting
1 2 3 4 5 6 7 8 9 10 11
sf::Image asteroidImage;
if(!asteroidImage.LoadFromFile("images/asteroid.png")) {return EXIT_FAILURE;}
sf::Sprite asteroidSprite[AsteroidCount];
for (int x = 0; x < AsteroidCount; x++)
{
sf::Sprite TempSAster(asteroidImage);
TempSAster.SetCenter(17, 15);
TempSAster.SetScale(.4,.4);
asteroidSprite[x] = TempSAster;
}
Inside the Draw() function.
Which results in the Sprite being redefined every game tick -> this is where my "incredibly inefficient" comment came from.