The VideoThe video below shows this in action.
The ConceptThe world is much bigger than what you can see through your eyes. You hear a sound. Where did it come from? Over "there". But you can't see that right now. You have to move "there", look around, see what you find. Is it an enemy? A friend? A portal to the bonus round? By only showing your player a portion of the bigger world, they are goaded into exploring the parts they cannot see. This way lies a path to immersion and entertainment. A Viewport is a slice of the bigger world. The diagram below shows the basic concept of how this works.
Classes and SequencesThe [tt]Viewport[/tt] does not live by itself in the ecosystem of the game architecture. It is a component that participates in the architecture. The diagram below shows the major components used in the Missile Demo application.
Main SceneThe [tt]MainScene[/tt] (top left) is the container for all the visual elements ([tt]CCLayer[/tt]-derived objects) and owner of an abstract interface, the [tt]MovingEntityIFace[/tt]. Only one instance exists at a time. The [tt]MainScene[/tt] creates a new one when signaled by the [tt]DebugMenuLayer[/tt] (user input) to change the [tt]Entity[/tt]. Commands to the [tt]Entity[/tt] are also executed via the [tt]MainScene[/tt]. The [tt]MainScene[/tt] also acts as the holder of the Box2D world reference. Having the [tt]MainScene[/tt] tie everything together is perfectly acceptable for a small single-screen application like this demonstration. In a larger multi-scene system, some sort of UI Manager approach would be used.
Viewport and NotifierThe [tt]Viewport[/tt] (lower right) is a Singleton. This is a design choice. The motivations behind it are:
- There is only one screen the user is looking at.
- Lots of different parts of the graphics system may use the [tt]Viewport[/tt].
- It is much more convenient to do it as a "global" singleton than to pass the reference around to all potential consumers.
- Deriving it from the [tt]SingletonDynamic[/tt] template ensures that it follows the Init/Reset/Shutdown model used for all the Singleton components. It's life cycle is entirely predictable: it always exists.