Coding for fast execution

This section gives examples of coding techniques that you will find in SDL tutorials and demonstrations.

  1. Masks

    Masks are used for bitwise Boolean operations as in the following example.

    MyByte := MyByte AND 15;
    The mask 1510 (000011112) preserves the last 4 bits (second nibble) of MyByte and clears the first nibble. The code is equivalent to
    MyByte := MyByte MOD 16;.
  2. Shift Operations

    Shift operations achieve fast integer multiplication and division by powers of two. See the following examples.

    MyInt := MyInt shl 1;  //MyInt is doubled by the shift left
    MyInt := MyInt shl 4;  // MyInt is multiplied by 16 (by 2^4) 
    MyInt := MyInt shr 1;  //Equivalent to MyInt := MyInt DIV 2
    MyInt := MyInt shr 4;  //Equivalent to MyInt := MyInt DIV 16  
    
  3. Lookup Tables

    Trigonometry functions take some time to evaluate and you are unlikely to require values for angles other that whole numbers of degrees. You can save processing time during a game by calculating them in advance then looking up the calculated values when needed. See Jason McMillen's Artillery demonstration later in this tutorial.

Programming - a skill for life!

Topics you need to understand before programming with SDL