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

Author Topic: sf::Image::LoadFromFile() pretty slow...  (Read 1871 times)

0 Members and 1 Guest are viewing this topic.

Cloppy

  • Newbie
  • *
  • Posts: 5
    • View Profile
sf::Image::LoadFromFile() pretty slow...
« on: December 13, 2010, 09:57:43 pm »
Hi,
I animated a little 2D character via .png files as frames.
So at start of program, I load all Images for the Unit Class (14 frames right now) and then i animate it per elapsed frame time, which is no problem.
But the fourteen LoadFromFile() calls take ages. Something about 8 seconds (summed up), which is (at least in my opinion) much time for this amount of images.

Is there any way to speed things up?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::Image::LoadFromFile() pretty slow...
« Reply #1 on: December 13, 2010, 10:09:19 pm »
Can you show the relevant code?

Just a guess: do you have a std::vector<sf::Image>? If so, calling reserve() should solve your problem.
Laurent Gomila - SFML developer

Cloppy

  • Newbie
  • *
  • Posts: 5
    • View Profile
sf::Image::LoadFromFile() pretty slow...
« Reply #2 on: December 13, 2010, 10:11:15 pm »
Yes right guess.

Code: [Select]
for(int i = 1; i <= numOfFrames; i++) {
temp = i;

sf::Image* tempImage = new sf::Image;

file.append(path);
file.append(itoa(temp, buffer, 10));
file.append(".png");

if(!tempImage->LoadFromFile(file))
printf("Unable to load Frame\n");

vReturn.push_back(*tempImage);

printf("%f\n", clock.GetElapsedTime());
clock.Reset();

file.clear();
}


Reserve()? Where's that? Couldn't find it in sf::Image

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::Image::LoadFromFile() pretty slow...
« Reply #3 on: December 13, 2010, 10:14:37 pm »
No, this is in std::vector. Your problem is that whenever the vector needs to allocate new memory, it moves (copy) all its elements to the new location in memory. So when you load your 8th image, it's like your reloading the 7 other images as well. Calling reserve pre-allocates enough memory, so your vector will never move.

Code: [Select]
vReturn.reserve(numOfFrames);

By the way, you have a memory leak: you allocate images with new, then copy them in the vector and never delete the instances you have allocated. Don't use new here, you don't need it.
Laurent Gomila - SFML developer

Cloppy

  • Newbie
  • *
  • Posts: 5
    • View Profile
sf::Image::LoadFromFile() pretty slow...
« Reply #4 on: December 13, 2010, 10:29:41 pm »
yeah saw that right after I posted the code and fixed it.
Old and noncommented code sucks :D

Okay, now it's pretty fast. Still 0.2 seconds per load, but that's okay, i guess.

But another problem appears right now, the App.Draw() kills the Framerate.

Code: [Select]
App.Draw(vUnits[i].s_Unit);

vUnits is a vector of instances of the unitclass, and s_Unit is a sf::Sprite.
Takes 0.3 seconds per call, so I got a Framerate around 2.
Appeared just a few minutes ago, didn't change something.

I have no idea where the problem is.

Edit: Performance Analyzer says, that nvoglv32.dll takes 82% of the samplings.

Edit2: Okay, seems to be done. Just needed to update nVidia driver.

 

anything