Passing Images/Sprites to a function

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]);
        }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//Main.cpp

    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;
        }
//
// ...
//

//Game loop

    for (int c = 0; c < global.AsteroidCount; c++)
    {
        as.Draw(c, global.AsteroidCount);
    }
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.
Reading the image from disk every time does seem horribly inefficient.

Something like this modification to your first set of code should do the trick:

1
2
3
4
5
6
7
8
9
10
11
12
13
void Asteroid::Draw(int c, int AsteroidCount,sf::Sprite& asteroidSprite )
{
    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]);
        }
}


calling it as

1
2
3
4
5
6
//Game loop

    for (int c = 0; c < global.AsteroidCount; c++)
    {
        as.Draw(c, global.AsteroidCount, asteroidSprite );
    }

Awesome! That worked great! Thank you
Topic archived. No new replies allowed.