Skip to content

Engine Loop

Shahriar Shahrabi edited this page May 11, 2020 · 3 revisions

ToyRenderer Engine Loop

The code flow in the engine is relativly simple at the moment. The game starts with the Entry Point which in turn creates an instance of the ToyRendererApp class. This instance is a class which inherits from the ToyRendererApp class, creating this instance calls the constructor for the ToyRendererApp class, first, which is defined in the engine and then the child constructor, which is the constructor defined in the game. The engine uses the constructor of the ToyRendererApp class to set up its logic, such as setting up the window, Input Master, Time manager etc. The game uses the constructor of the inherited child class to construct the scene. As an example for this, have a look at the Starter Scene.

Once the ToyRendererApp instance is created, its Run function is called. This run function first calls for each Gameobject in the active scene, the Start function. The GameObject.Start in turns goes through every Component the GameObject has and also calls its Start function. The Start function is there for initialization of logic and values. Although some initialization can be done in the constructor of each component, to the time of the construction of the component, there is no grantee that the entire scene is already constructed, which makes the constructor not good for setting up dependencies between different components and game objects. For that type of construction, the Start function exists as a point where it can be guaranteed that the Scene is fully constructed.

After the Start, the main game loop starts. In the main loop, in order the Update, Render and OnGui functions of each GameObject and Component are called. In case there is an exit flag in the loop, first the Engine resources are cleaned up and then the application destructor.

Here is a summary of the above in a diagram: enter image description here

The order of what is rendered when, essentially the Render Loop is controlled by the Camera class. Scene Camera is an Component attached to a gameobject, it is treated as an ordinary Component as far as the Engine Loop is concerned. However within the OnRender code of the camera a certain order is enforced for rendering. For more information have a look at the documentation for the camera class.