smcameron / space-nerds-in-space

Multi-player spaceship bridge simulator game. Captain your starship through adventures with your friends. See https://smcameron.github.io/space-nerds-in-space
GNU General Public License v2.0
729 stars 73 forks source link

Game startup is slow and ugly #310

Closed smcameron closed 2 years ago

smcameron commented 2 years ago

The slowness is due to model loading, and specifically I think, PNG loading/decompressing. I profiled it, and read_stl_file() was the function taking most of the time prior to rendering, and within that, the function for reading PNG files. It's also "ugly", because when the window is first created, it kind of disturbs the screen a bit, but doesn't actually draw anything until all the models and PNGs are loaded, which can be several to many seconds, depending how fast your system is. Takes quite a while on a Raspberry Pi, but a noticeable number of seconds even on a fast system.

I tried to at least fix the ugliness by immediately hiding the window after creating it (SDL_HideWindow()), and only unhiding it after the models and textures were loaded (SDL_ShowWindow()) -- see 5b7664eeb01499affbaa25bbb9a175e56b1f4a13 -- and this worked great on my laptop, but didn't work at all on my Desktop system -- the window was hidden but SDL_ShowWindow() would not unhide it for some as yet unknown reason. Seems like some bug in the older version of SDL2 that's on my desktop system. I thought perhaps I needed to SDL_ShowWindow() at least once prior to SDL_HideWindow()... but that didn't fix it.

Ideally, the model and image loading could be done in a background thread, but this is pretty difficult. Alternately, multiple models and PNGs could be loaded in parallel, and all join before firing up the renderer. However, there's still the problem that OpenGL doesn't like being called from threads other than the thread which creates the OpenGL context. So... speeding up the model loading is a bit of a hard problem.

smcameron commented 2 years ago

This fixes the ugliness, but not the slowness:

smcameron commented 2 years ago

This doesn't fix the slowness, but it does pop up a splash screen right away with a progress bar that advances so you at least know something's happening. I think I'm going to call it done. The slowness is a whole 'nother problem, really.