Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Newb Sprite question  (Read 2733 times)

0 Members and 1 Guest are viewing this topic.

Jove

  • Full Member
  • ***
  • Posts: 114
    • View Profile
    • http://www.jestofevekites.com/
Newb Sprite question
« 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;
}
{much better code}

pdinklag

  • Sr. Member
  • ****
  • Posts: 330
  • JSFML Developer
    • View Profile
    • JSFML Website
Newb Sprite question
« Reply #1 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. ;)
JSFML - The Java binding to SFML.

Jove

  • Full Member
  • ***
  • Posts: 114
    • View Profile
    • http://www.jestofevekites.com/
Newb Sprite question
« Reply #2 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
{much better code}

mecablaze

  • Newbie
  • *
  • Posts: 5
    • View Profile
Pun
« Reply #3 on: March 16, 2011, 06:41:37 am »
The code's getting better, by Jove!

 :lol: