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

Author Topic: sf::view is incredibly slow?  (Read 2402 times)

0 Members and 1 Guest are viewing this topic.

hipsterdufus

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
sf::view is incredibly slow?
« on: July 10, 2013, 06:54:25 am »
Hi all, I am working on the next big thing and everything was running pretty smoothly with no lag until I decided to attach an sf::view to the renderwindow. Basically the goal is to make the camera stay on the player (2d side scroller style). So the view center is updated with the player center every frame. Unfortunately this is unbelievably horribly slow. I went from my standard 60fps to probably around 1...no idea how something like this could happen. Is there something about the View class that I didn't catch? Do I need to go about moving the camera in another way - using my own class? Thanks for any help!

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Re: sf::view is incredibly slow?
« Reply #1 on: July 10, 2013, 09:02:45 am »
There is no reason that I am aware of that sf::View should cause such horrible slowdown. Please post complete and minimal code that reproduces this problem so that we may investigate the cause.
I use the latest build of SFML2

hipsterdufus

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: sf::view is incredibly slow?
« Reply #2 on: July 10, 2013, 10:17:01 am »
Sure, it is very simple:

In the constructor for my App class:

   // Initialize the view
   m_View.reset(sf::FloatRect(0,0,WINDOW_WIDTH,WINDOW_HEIGHT));
   m_View.setViewport(sf::FloatRect(0,0,1,1)); // Not sure if this is necessary
        mWindow.setView(m_View);

In the App.run() function (which updates at a 60Hz rate):

//
//Update world object positions before this...
//

m_View.setCenter(mWorld.getCharacter().getCenter());

//As you can imagine, this sets the view center to the player center.

I have found that the setCenter function is causing all the slowdown. But I'm not sure why. Maybe it's because I'm running in windowed mode? I've lowered the resolution but its still slow as can be. Maybe the setCenter function is just slow and not meant or able to be updated at a 60Hz rate? I'll keep playing with it...

Also, it doesn't seem to be centering the camera on the player. Maybe I'm missing something with the way this view is set up.
« Last Edit: July 10, 2013, 10:33:53 am by hipsterdufus »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::view is incredibly slow?
« Reply #3 on: July 10, 2013, 10:43:45 am »
Sure, it is very simple
That's neither complete nor minimal. Please read this post.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

hipsterdufus

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: sf::view is incredibly slow?
« Reply #4 on: July 10, 2013, 10:55:19 am »
Sorry, I'm a bit impatient most of the time. I've found that for some reason the line where I call setCenter:

m_View.setCenter(mWorld.getCharacter().getCenter());

Is very very slow because of the mWorld.getCharacter() call. This member function I made returns an object of class Character. If I replace it with a simple sf::Vector2f it goes back to being very fast. I guess I'm not quite smart enough to know why calling getCharacter().getCenter() is so slow but I think it's because it has to copy a large object anonymously here. Anyways, now the camera still does not track the player but I think that's something I can figure out. Thanks for the help!

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: sf::view is incredibly slow?
« Reply #5 on: July 10, 2013, 11:18:18 am »
Then return it by reference. Do you load a texture inside Character constructor?

hipsterdufus

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • Email
Re: sf::view is incredibly slow?
« Reply #6 on: July 10, 2013, 11:43:08 am »
Yes I do, I take it that's the issue? Accessing something from the Video Card?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::view is incredibly slow?
« Reply #7 on: July 10, 2013, 11:51:37 am »
Accessing something from the Video Card?
Not the access, but the loading is slow. Load resources only once.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything