Day 17: Bringing It on Stage – Part 1

Stage is the graphical front-end of our system. It handles OpenGL ES life cycle, and maintains the game flow. Today we start building upon Stage.java of day 6. However, since the implementation is lengthy, we only cover animation handling and how it is incorporated into the stage. Graphical aspects of this class will be discussed next day.

Animation handling

Stage is an interface between our graphical data structures and the OpenGL ES API (which is called EGL). Scenes are added to it, and it has to render and animate them in a fail-safe manner.

In day 16 we added a method called advance to the Scene class, the purpose of which was to advance or increment animations on every sprite. The question we would like to answer today is when and how this method is called.

A naive implementation would call the advance method in onDrawFrame, where the renderer is called to render the scene. To see why this is a bad practice, we should consider some facts:

  1. Rendering is handled by OpenGL ES on a thread dedicated for this purpose. And it is best to keep it this way.
  2. Advancing animations can become a lengthy task as our scenes grow in complexity. When the frame is ready to render, it is already too late to start processing it. It will cause hiccups or stutter in the animation.
  3. OpenGL draws frames at the highest frequency it physically can. By default, rendering is performed whenever the OpenGL is ready to draw, and the rate it is called at it not deterministic (this rate is well known as FPS or frames per second).

Continue reading