SFML community forums

Help => Graphics => Topic started by: Jove on March 15, 2011, 03:48:58 pm

Title: Newb Sprite question
Post by: Jove on March 15, 2011, 03:48:58 pm
First post here. Great forum, I have learned so much from the many posts.

I'm a complete newbie to PC coding, C++ and SFML, but loving it so far!

If I am executing a loop like this, am I generating copies like the Sprite tutorial warns of?

Ignore the Scale/Center stuff, they're in there for testing.



Code: [Select]
   sf::Image Ball;
    if (!Ball.LoadFromFile("ball.png"))
        return EXIT_FAILURE;

---------------------------------------------


for (i=0; i<20; i++)
{
sf::Sprite Test(Ball);
Test.SetColor(sf::Color(55, 55, 55, 255));
Test.SetPosition(TestX[i], TestY[i]);
Test.SetScale(.3f, .3f);
Test.SetCenter(28, 21);
App.Draw(Test);

TestY[i] += Speed / 6;

if (Speed > 0 && (TestY[i] > 625)) TestY[i]-=640;
else
if (Speed < 0 && (TestY[i] < -30)) TestY[i]+=640;
}
Title: Newb Sprite question
Post by: pdinklag on March 15, 2011, 04:17:43 pm
Not copies, but you're generating a new Sprite in every loop iteration, which isn't necessary whatsoever. Why not create one Sprite and use that throughout the loop like so:
Code: [Select]

      sf::Sprite Test(Ball);
      for (i=0; i<20; i++)
      {
         [...]
      }

You simply move the creation of the Sprite to before the loop enters. It will be available inside the loop from that point on, and there's nothing else you need to change!

Oh, btw, no need to mark your question as a "newb" question, in fact, that will demotivate a good amount of people to even read. Be a little more confident! We have all started at some point. ;)
Title: Newb Sprite question
Post by: Jove on March 15, 2011, 04:34:54 pm
Wow thanks. Cut the CPU usage in half too!

I'll have to take a look at my starfield code and see if the same can be done there (uses Draw Rectangles).

Mark
Title: Pun
Post by: mecablaze on March 16, 2011, 06:41:37 am
The code's getting better, by Jove!

 :lol: