bosiakov
bosiakov

Reputation: 67

Android. libgdx. Loading large amounts of graphics

In my libgdx game I use a lot of “heavy” graphics. It is a huge canvases with many frames of animation. This canvases have resolutions greater then 1000*1000. There is a lot of graphics like this (function loadTextures() is about 200 lines of code). Even on high-end devices, loading the game takes a lot of time, and at most weak devices game can’t even load and hangs on loading graphics.

How to optimize all this stuff? How do you solve issue like this?

Upvotes: 0

Views: 1184

Answers (1)

Xoppa
Xoppa

Reputation: 8113

First make sure that your textures never exceed the size of 1024x1024 and always use power of two dimensions. If you have multiple smaller (or non POT) textures, pack them into one larger texture (of 1024x1024 max).

Having many textures (of 1024x1024 each) will eat lots of memory, so you should probably reconsider your approach. For example you can load/unload textures just in time (using AssetManager) at certain points in your game (call System.gc(); afterwards, to keep the garbage collector somewhat under control).

If that's not a possibility (e.g. all textures are used for the same animation), e.g. consider downsizing the textures, only animating the parts that actually change or using boned animations (for example, look at Spine, http://esotericsoftware.com/).

The best approach for your situation may vary. But in general, consider the following guidelines if you need more than one texture:

  • At startup, only load the textures that are needed throughout the whole game or by the main/menu screen.
  • When switching screen (level) unload textures which aren't needed anymore and load the textures that are specific for that screen/leveen (meanwhile you can show a loading screen e.g.).
  • Avoid using textures that are oversized (e.g. having a texture loaded of 1024x1024 while it actually renders at 200x200 on the screen is a waste of memory)
  • Avoid loading textures that are partially identical (e.g. when having multiple face images, where only the eyes are different, consider splitting the face into the head and separate eyes images).

Upvotes: 1

Related Questions