Android: Huge multi-layer scrolling backgrounds
I'm developing a side-scrolling android game and I've co开发者_Go百科me across one hiccup.
The game is rendering graphics on the Canvas object. The game features about 6 scrolling layers, the main ground layer, a collision layer, a foreground layer, and two background layers, which all scroll with variable speed for camera depth.
Now, the problem resides here: The game's graphics feature huge unique levels that are drawn out in their entirety. It is not tilebased, no graphics are repeated. However, when loading an entire graphic layer, I receive an out of memory exception. To remedy this, I've sliced up all my layers to 200 x 300 unique rectangular tiles, and the game only loads what's visible when it is visible. It can't load all the tiles at once, again because it will throw an Out of Memory exception. So of course, that requires that I dispose from memory tiles when they are no longer visible, otherwise i'll just keep adding these tiles to the memory and again hit that Out of Memory exception. This means that I have to use BitmapFactory to create a new bitmap from a resource within the game loop for every tile that becomes visible. Of course, with these tile bitmaps initialized within the game loop, whenever tiles leave visibility I have to deal with Garbage Collector lag.
Is there a better way to go about this solely using Canvas and keeping my unique "macro" tiles and not resorting to reusable micro tiles? Would it benefit using OpenGL instead? How or why?
OpenGL would allow you some explicit control over when textures are loaded/unloaded from memory. You may be able to make use of some of OpenGL's features to develop a 'paging' system for bringing in and unloading textures. That would allow you to avoid the garbage collector.
But you are generally doing the correct thing - irrelevant graphics are removed from graphics memory to make room for those that need displaying.
精彩评论