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

Author Topic: SFML 2.0 Slow loading textures from file  (Read 4805 times)

0 Members and 1 Guest are viewing this topic.

snoozer87

  • Newbie
  • *
  • Posts: 13
    • View Profile
SFML 2.0 Slow loading textures from file
« on: June 05, 2012, 03:11:32 pm »
Dear community,

I'm writing an application that has to load a lot of 'HI-RES' textures in (1280x800), 35 of them at the moment. This is an application that has to work on a windows 7 tablet and I have noticed it takes a quite a long time to load in all of them (aprox. 30 to 60 seconds).

I just want to know if this is normal and if not, what is the best way to load in textures (at the moment I am loading all of them in a separate thread while showing a loading screen in the main one). At the end I place all textures into a Vector, which allocated to the right size (maybe this is slowing things down, but I don't think so as I tried putting the vector code in comments);

An example:
void ThreadLoading::loadGame(){
        t_All_Textures.clear();
        s_All_Sprites.clear();
        t_All_Textures.reserve(35);
        s_All_Sprites.reserve(2);
       
        //TEXTURES
        //Global background
        t_pPlayBackground.loadFromFile(".//Resources//PlayScreen//background.png");

        //Status ribbons (Available,owned and sold)
        t_pStatusAvailable.loadFromFile(".//Resources//PlayScreen//available.png");
        t_pStatusOwned.loadFromFile(".//Resources//PlayScreen//owned.png");
        t_pStatusSold.loadFromFile(".//Resources//PlayScreen//sold.png");
        t_pStatusFree.loadFromFile(".//Resources//PlayScreen//available.png");

        //Normal property/chance/community chest backgrounds
        t_pProperty0.loadFromFile(".//Resources//PlayScreen//0.png");
        t_pProperty1.loadFromFile(".//Resources//PlayScreen//1.png");
        t_pProperty2.loadFromFile(".//Resources//PlayScreen//2.png");
        t_pProperty3.loadFromFile(".//Resources//PlayScreen//3.png");
        t_pProperty4.loadFromFile(".//Resources//PlayScreen//4.png");
        t_pProperty5.loadFromFile(".//Resources//PlayScreen//5.png");
        t_pProperty6.loadFromFile(".//Resources//PlayScreen//6.png");
        t_pProperty7.loadFromFile(".//Resources//PlayScreen//7.png");
        t_pProperty8.loadFromFile(".//Resources//PlayScreen//8.png");
        t_pProperty9.loadFromFile(".//Resources//PlayScreen//9.png");
        t_pProperty10.loadFromFile(".//Resources//PlayScreen//10.png");
       
        //Buy backgrounds
        t_pProperty1_buy.loadFromFile(".//Resources//PlayScreen//1b.png");
        t_pProperty3_buy.loadFromFile(".//Resources//PlayScreen//3b.png");
        t_pProperty5_buy.loadFromFile(".//Resources//PlayScreen//5b.png");
        t_pProperty6_buy.loadFromFile(".//Resources//PlayScreen//6b.png");
        t_pProperty8_buy.loadFromFile(".//Resources//PlayScreen//8b.png");
        t_pProperty9_buy.loadFromFile(".//Resources//PlayScreen//9b.png");

        //Buyerror backgrounds
        t_pProperty1_buyError.loadFromFile(".//Resources//PlayScreen//1eb.png");
        t_pProperty3_buyError.loadFromFile(".//Resources//PlayScreen//3eb.png");
        t_pProperty5_buyError.loadFromFile(".//Resources//PlayScreen//5eb.png");
        t_pProperty6_buyError.loadFromFile(".//Resources//PlayScreen//6eb.png");
        t_pProperty8_buyError.loadFromFile(".//Resources//PlayScreen//8eb.png");
        t_pProperty9_buyError.loadFromFile(".//Resources//PlayScreen//9eb.png");

        //Playermenu background, different colours for each player
        t_pPlayerMenu1.loadFromFile(".//Resources//PlayScreen//playermenu1.png");
        t_pPlayerMenu2.loadFromFile(".//Resources//PlayScreen//playermenu2.png");
        t_pPlayerMenu3.loadFromFile(".//Resources//PlayScreen//playermenu3.png");
        t_pPlayerMenu4.loadFromFile(".//Resources//PlayScreen//playermenu4.png");

        //Player pawns
        t_pPlayerPawnCar.loadFromFile(".//Resources//PlayScreen//car.png");
        t_pPlayerPawnIron.loadFromFile(".//Resources//PlayScreen//iron.png");
        t_pPlayerPawnHat.loadFromFile(".//Resources//PlayScreen//hat.png");
        t_pPlayerPawnBarrow.loadFromFile(".//Resources//PlayScreen//barrow.png");
....
 

Best regards
Yannick
« Last Edit: June 05, 2012, 03:29:24 pm by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML 2.0 Slow loading textures from file
« Reply #1 on: June 05, 2012, 03:39:45 pm »
Quote
I just want to know if this is normal
It is.

Quote
At the end I place all textures into a Vector
So you duplicate all the textures at the end? Why don't you load directly the final instances (ie. the ones that are in the vector)?

You can also try the .jpg format: since files are smaller, it might be faster to load.
Laurent Gomila - SFML developer

snoozer87

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: SFML 2.0 Slow loading textures from file
« Reply #2 on: June 05, 2012, 04:37:59 pm »
Unless vector.push_back creates a copy, I'm not copying the textures. the reason I'm doing is this is so I can easily pass all textures to my main thread :).

In my main thread I put all textures in local variables, so I can use them and then I clean up the passed vector (freeing memory).

Example

Loadingthread
Code: [Select]
....
//Player pawns
t_pPlayerPawnCar.loadFromFile(".//Resources//PlayScreen//car.png");
t_pPlayerPawnIron.loadFromFile(".//Resources//PlayScreen//iron.png");
t_pPlayerPawnHat.loadFromFile(".//Resources//PlayScreen//hat.png");
t_pPlayerPawnBarrow.loadFromFile(".//Resources//PlayScreen//barrow.png");
.....
t_All_Textures.push_back(t_pPlayerPawnCar);
t_All_Textures.push_back(t_pPlayerPawnIron);
t_All_Textures.push_back(t_pPlayerPawnHat);
t_All_Textures.push_back(t_pPlayerPawnBarrow);
....
m_pScreen->setResources(t_All_Textures);

Main thread
Code: [Select]
void PlayScreen::setResources(std::vector<sf::Texture> AllTextures){
....
m_carPawn=AllTextures[15];
m_ironPawn=AllTextures[16];
m_hatPawn=AllTextures[17];
m_barrowPawn=AllTextures[18];
....
//CLEANUP AND FREE MEMORY
AllTextures.clear();
std::vector<sf::Texture>().swap(AllTextures);
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML 2.0 Slow loading textures from file
« Reply #3 on: June 05, 2012, 05:00:26 pm »
Quote
Unless vector.push_back creates a copy
It does.
Laurent Gomila - SFML developer

snoozer87

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: SFML 2.0 Slow loading textures from file
« Reply #4 on: June 05, 2012, 05:09:41 pm »
I see, so doing this would be better practice?

AllTextures.push_back(t_pPlayerPawnBarrow.loadFromFile(".//Resources//PlayScreen//barrow.png"));

*EDIT*
Wait, now that I see the code it looks wrong. Either it won't work, or it'll be exactly the same as before probably :)

*EDIT2*
Nvm that would never work ...
« Last Edit: June 05, 2012, 05:17:16 pm by snoozer87 »

snoozer87

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: SFML 2.0 Slow loading textures from file
« Reply #5 on: June 05, 2012, 05:34:15 pm »
K I'm using pointers now, so the vector will copy the pointer, but not the whole object :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML 2.0 Slow loading textures from file
« Reply #6 on: June 05, 2012, 06:28:37 pm »
You don't have to use pointers
vector.push_back(sf::Texture());
vector.back().loadFromFile(...);
Laurent Gomila - SFML developer