Search code examples
javaandroidopengl-esglsurfaceview

Java Android Game GLES 2.0 GLSurfaceView jerky


I made a small 2D game in Java with GLES 2.0 on the GLSurfaceView. But I have some problems with the performance. All the rendering and updating of the game state I did in the OnDrawFrame function, running in continuously mode. While writing the app I noticed some lags all 2 seconds even with using of a fixed or variable time step technique. I could compensate this by averaging the delta time, but sometimes I can still see that there is something wrong. First I thought I wrote bad code (Too much use of the GC or polygons ...) but than, after creating an empty app that simply log the FPS, I could see the same issue. All 2 seconds the FPS dropped for a small moment from 50-60 FPS to 15-30 FPS. Even on different devices (Nexus 7 2013, HTC Desire 500).

Has someone this problem, too? Is there a better way than GLSurfaceView, something that is optimized for game purposes? And if there is no solution is there a framework, library or game engine that takes over the low level stuff for me, for example the game loop creation part, so I don't have to worry about it anymore? Like in XNA the game class with the exception, that I want to render the game with plain opengl not with an abstraction like a SpriteBatch class.


Solution

  • There are graphics engines (which abstract away the GLES stuff), and game engines (which abstract away just about everything). If you're willing, and in fact insistent, on using plain OpenGL ES, then there's no reason to use them -- you're already doing the "low level stuff".

    The proper way to structure a game loop is described in detail in this appendix to the graphics architecture doc. In short, forget about GLSurfaceView, use Choreographer, and be prepared to skip frames. A simple example implementation can be found in Grafika's "record GL app" or "hardware scaler exerciser" activities.

    Ignore FPS, watch milliseconds per frame. If you're seeing big pauses, you need to figure out what's causing them. The essential tool on Android is systrace.