Loading images in another thread?
While adding image support for a mod, I came across performance problems when loading images. (on every menu open). Opening and closing a bunch of files is slow - of course. I tried to remedy this by doing it in another thread. This made me need to split up my image reading and my createTexture since createTexture can't be called in another thread:
// Create texture // texFileName - texture file name, it's best to specify full texture path and use PNG textures // returns internal texture id // Texture deletion is performed automatically when game reloads scripts // Can be called only in the same thread as natives IMPORT int createTexture(const char* texFileName);
All good, done that, but at menu load my game still freezes for a half second. After being rather confused, I found out that
createTextureitself causes this. Is there any way I can keep my game fast while still creating textures?
script.cppthat has the threading stuff in it.
checkThread()checks if a task was done.
With 22 images in my folder, it's very noticeable. As in, the game freezes a bit after the menu is drawn.
Edit - Seems like I had two duds, two 2MB PNGs. After resizing and JPG-ing them, the problem was less. Then I also just made it check for existing textures before calling createTexture again, and it now opens smoothly while still detecting newly added images. The initial startup is still pretty slow sadly, as it can add 0.5 seconds of freeze when all textures are created.