Game Loop

The central flow construct in a video game is its game loop. In a single iteration of the game loop, all the necessary logic is performed in updating the game state, including processing of user input, before the game scene is finally rendered to the display.

The window of time during which an iteration of the game loop takes place is commonly referred to as a frame.

## Crash 1

The game loop routine is located at `0x11FC4` in Crash 1. From the `main` routine, the game loop routine is passed the level id of an 'initial level' as its argument; in Crash 1 this value is 19-the level ID for the title sequence, main menu, and then possibly map if 'a game is started'.

### Outline

```
sub_80011FC4(levelid)
{
  A) sub_80026140 - Game initialization
  B) sub_80015B58 - Paging and subsystem initialization
  C) sub_80011DD0 - Load entries and/or create main game objects
                    (HUD/display, Crash, Aku Aku, Shadows, Boxes, Fruit)
  GAME LOOP:
  {
    1) Code for handling game pause/start button press
    2) If Crash object does not exist:
      A) sub_8002E98C - Create HUD and initialize level
    3) Code for loading a new level if necessary
    4) Update pages
      A) sub_800134C8 - UpdatePages()
    5) Spawn all objects in current zone [for those that have their respawn bit set]
      A) sub_80025928 - SpawnObjects()
    6) Update camera
      A) sub_8002EC68 - level dependent routine A
      B) sub_8002EBB4 - level dependent routine B
      C) sub_8002B2BC - UpdateCamera()
    7) Update matrices
      A) sub_80017A14 - UpdateMatrices()
         - recalculate camera translation, rotation, scale, and light matrices
         - create world models for current zone
    8) Create transformed primitives for current zone world models
       dependent on current zone render mode:
         sub_80019508 - default world model primitive creation routine
         sub_80019BCC - w.m.p.c.r high road/road to nowhere depth shader variant
         sub_80019DE0 - w.m.p.c.r ripple effect vertex position shader variant
         sub_80019F90 - w.m.p.c.r lightning effect vertex shader variant
         sub_8001A0CC - w.m.p.c.r darken depth effect shader variant
         sub_8001A2E0 - w.m.p.c.r darken effect shader variant
    9) Update all objects and create transformed primitives for them
      A) sub_8001D5EC - UpdateObjects()
    10) If current level is title screen (0x19) then do title screen routine
      A) sub_800326D8 - title screen routine
    11) Send transformed primitives to GPU for rendering
      A) sub_80016E5C - GPU routine
  }
}

```

### Game initialization

The game loop routine begins with a call to the game initialization routine, located at `0x26140`. This initializes all global game variables.

### Paging and subsystem initialization

After executing the game initialization routine, the game still has no data to work with: nothing from the disc has been loaded into memory other than the currently executing binary. At this point, the paging and subsystem initialization routine is called. This routine has 2 input parameters, the first being the level id of a level whose content shall be paged from disc, and the second a pointer to the location of storage for the resultant paging system structure. The arguments specified in the call are the current level's ID (passed as the argument to the game loop routine), and a pointer to the game's global paging system structure-located at `0x5C528`. After execution of the paging and subsystem initialization routine, the paging system structure will include all the necessary information for paging data from disc, specifically that data located in the current level's corresponding NSF file.

### Load executables for and/or create main game objects

Once paging has been initialized, the appropriate paging routines are used to load all GOOL executable entry data for the main or 'universal' game objects (HUD/display, Crash, Aku Aku, shadows, boxes, fruit) from the current level's corresponding NSF file (i.e. the current NSF file) into main memory. Most NSF files contain the same copy of data for each of these objects (hence the term universal), with a few exceptions: for example, The Great Hall does not contain any box objects in its layout, so its NSF file does not contain the GOOL executable for the box object. Consequently, no box objects are spawned in an instance of the game on level 2C. The routine located at `0x11DD0` loads all existing universal game object executables into main memory.