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

Author Topic: Slow framerate when moving lots of sf::Shape about  (Read 2335 times)

0 Members and 1 Guest are viewing this topic.

aquaglow

  • Newbie
  • *
  • Posts: 15
    • View Profile
Slow framerate when moving lots of sf::Shape about
« on: July 02, 2011, 09:02:26 pm »
Code: [Select]
           // Are we firing the gun?
            if (fireGun){
            int targetX = Input.GetMouseX();
            int targetY = Input.GetMouseY();
            float angleShip = atan2(targetX - sVec.x, targetY - sVec.y);
            gunfireAngles.push_back(angleShip);
           
            // Initially set to 0,0 and then move, to avoid the nuisance of local/global positioning later
            // http://www.sfml-dev.org/forum/viewtopic.php?t=4954
            float x = 0;
            float y = 0;            
            float x2 = sin(angleShip) * 12;
            float y2 = cos(angleShip) * 12;
           
            sf::Shape fire = sf::Shape::Line(x, y, x2, y2, 2, sf::Color(255, 255, 255), 0, sf::Color(0, 0, 0));
            fire.SetPosition(sVec.x, sVec.y);
            bullets.push_back(fire);
            }
           
            // Move the position of existing gunfire along so it travels
  std::list<sf::Shape>::iterator b = bullets.begin();
  std::list<float>::iterator gf = gunfireAngles.begin();
  while (b != bullets.end()) {
            float movingX = sin(*gf) * 0.5;
            float movingY = cos(*gf) * 0.5;
            b->Move(movingX, movingY);      

  // check if a bullet is still shown inside the window. If not, erase it
  sf::Vector2f bulletVec = b->GetPosition();
            if (bulletVec.x < -10 || bulletVec.x > 810 || bulletVec.y < -10 || bulletVec.y > 610){
            b = bullets.erase(b);
            gf = gunfireAngles.erase(gf);
            }

  b++;
  gf++;
  }




I'm currently using a list of sf::Shape::Line as bullets in my game. When a few 100 are displayed on the screen, the movement noticeably slows down (I haven't put any sort of frame limiting on yet). Is this to be expected?

Each loop I move every bullet slightly further along its trajectory, and remove any which are outside the screen bounds.

Could it be my repeated calls to sin() and cos() making it slow? From reading the advice seems to be that with modern processors pre-calculated lookup tables shouldn't be needed.

I don't have any easy way to do profiling at the moment (having trouble compiling with the profiling option), but will persevere.

aquaglow

  • Newbie
  • *
  • Posts: 15
    • View Profile
Slow framerate when moving lots of sf::Shape about
« Reply #1 on: July 02, 2011, 10:09:44 pm »
Please ignore my post, I worked out what was wrong. I got the gprof profiler working, and because I wasn't limiting the framerate it was calling the bullet loop thousands of times a second.

I've now applied App.SetFramerateLimit(60) and made the speed/rotation of everything relative to App.GetFrameTime() and it's fine. It stays a constant 59 fps even with 1000 bullets  :shock: