SFML community forums

Help => Graphics => Topic started by: gamecoder.nz on October 21, 2020, 03:18:54 am

Title: Loading large textures
Post by: gamecoder.nz on October 21, 2020, 03:18:54 am
Hi everyone,

So I have a large map background which I have divided up into 3200x3200 images.  What I want to do is load these textures at runtime as the player moves.

I have tried to load them in threads but doing loadFromFile causes a lag.  Is there a better way to do this?
Title: Re: Loading large textures
Post by: Stauricus on October 21, 2020, 01:00:41 pm
I don't know if it is still true nowadays, but it is said that textures sized by the power of 2 are better handled by the graphics card: 2x2, 4x4, 8x8, 16x16, 32x32, 64x64, 128x128, 256x256, 512x512, 1024x1024, 2048x2048, 4096x4096 etc...
do you really need to use threads for that? how are you doing it? and have you tried not using it?
Title: Re: Loading large textures
Post by: Paul on October 21, 2020, 03:12:39 pm
Are you preloading images in advance before they are rendered?
Title: Re: Loading large textures
Post by: gamecoder.nz on October 22, 2020, 01:00:09 am
I just tried a 2048x2048 texture and there was no improvement.  I did consider loading the textures when the game starts but there are seasons in the game which require different textures so that's four different map textures which is a lot of textures to keep in memory.  I want a system where it only loads the textures needed as the player walks.
Title: Re: Loading large textures
Post by: Stauricus on October 22, 2020, 01:38:49 am
so, do you have a minimal code that reproduces the problem?
Title: Re: Loading large textures
Post by: eXpl0it3r on October 22, 2020, 09:57:33 am
 Reading data from disk is slow, as such you'll always have some time spent in the loading function.

If you really need it to be on the fly, you could do the load of an sf::Image in a separate thread and then call copyFromImage on a texture in the main thread.
You might still get a frame rate dip due to the amount of data, but it shouldn't necessarily cause too noticeable lag. This could be further refined, by splitting up the large image into multiple smaller ones and loading one after the other.

On the other hand, loading the images up front and keeping them in memory can make things easier and it's not like your PC couldn't handle a few MB or RAM usage. ;)