# 2.2.5 2D Rendering Process

The N64 displays 2D images using the same process it uses to display polygons. In reality, 2D images are just polygons with texture data pasted on them.

## Kinds of 2D Images

To display a 2D image, the N64 uses two kinds of library functions:

- Bitmap display functions that draw bitmaps by using drawing parameters specified in the game application.
- Sprite display functions that use built-in hardware capabilities to draw four-sided figures (sprites) using a small number of parameters. The 2D images drawn can be moved in the X or Y directions or be rotated.

## Procedure

The actual procedure is best illustrated with a sample program (2d.c). You can find the files that make up this 2D sample in the following directory on the same CD that holds this manual:

>     \N64Manuals\pc\allman\samples\2d\

Note that the gfx function is not a standard N64 function. It is defined within the 2D sample for use by that sample.

## Main 2D Drawing Routine

The main drawing routine forms the executing core for the 2D image drawing sample. The process is completed in three steps that are executed over and over in a loop:

1. Construct the display list in the CPU.
2. Transfer the display list to the RCP for execution. The RCP creates the actual display data and outputs the video signal by way of the frame buffer, VI, and video DAC as previously outlined in this chapter.
3. Keep the CPU and RCP processes in sync, and return to step 1.

The following code points to the display list, sets the sprite size, and sets up the loop:

    void entry(void)
    {
     Gfx *gp;  /* points to display list */
     u16 w, h;

     w=64; h=64;  /* sets up sprite size */
     while(1){

### Step 1 -- Construct the display list

The following code reserves the necessary memory area for the construction of the display list, sets up the RCP execution process for sprite drawing in the reserved area, and provides a termination process for the constructed display list. Each code snippet is followed by s short explanation.

     /* Start to construct a display list */
    gp = gfxBegin(1024);

This code checks to see if a display list has already been constructed. If a display list hasn't been completed, this code reserves the GBI command area for new construction. If it has been completed, this code moves on to step 2, the process that transfers the display list to the RCP.

     /* Set the drawing mode for RSP and RDP */
    gp = setup_SP_DP(gp);

This code constructs a command that sets the necessary RCP drawing mode, and then adds that command to the display list.

     /* Accept the texture pattern */
    gp = load_texture(gp,w,h);

This code sets up a texture pattern loading command in the display list.

     /* Write the texture pattern */
    gp = draw_texture(gp,124,92,w,h);

This code sets up a texture drawing command in the display list.

     /* End the construction of display list */
    gfxEnd(gp);

This code terminates the display list.

Caution: Watch out for an unterminated display list. If it is transferred to the RCP, it will cause the RCP to hang (stop responding). Also, be sure to put the gDPFullSync function at the end of each display list. Otherwise, the RDP end message won't ever come.

### Step 2 -- Transfer the display list to the RCP to execute the drawing process

The following code transfers the display list to the RCP where the display list is interpreted and executed.

     /* Transfer display list to RCP */
    gfxFlush( );

This code transfers the display list to the RCP where it is executed. This function also provides the frame buffer switch that writes the created image data into the frame buffer.

### Step 3 -- Synchronize the CPU with the RCP

The following function ensures coordination between the CPU and RSP:

     /* Wait for the retrace */
    gfxWaitSync( );
     }
    }

## Techniques for Construction of the Display List

The actual construction of the display list uses one of these processes:

- Set the RSP and RDP Drawing Modes
- Set and Read the Texture
- Set the Drawing Sequence of the Bitmap Pattern

### Set the RSP and RDP Drawing Modes

The following routines set the RCP drawing mode that actually creates the commands that render the drawing reflected in the display list.

The gSP and gDP functions are included in the N64 library; for more information about them, please see the online N64 Function Reference Manual (HTML manual pages).

    static Gfx *setup_SP_DP(Gfx *gp)
    {
    /* Set all sorts */

    /* Set the texturing parameter */
    gSPTexture(gp++,0x8000,0x8000,0,
       G_TX_RENDERTILE,G_ON);

    /* The synchronous setting between the
       rendering and the sub-attribute */
    gDPPipeSync(gp++);

    /* Set the RDP cycle type */
    gDPSetCycleType(gp++,G_CYC_COPY);

    /* Set the rendering mode of the
       blender within RDP */
    gDPSetRenderMode(gp++,G_RM_NOOP,G_RM_NOOP2 );

    /* Set the texture LOD */
    gDPSetTextureLOD(gp++,G_TL_TILE);

    /* Set the perspective of the texture map */
    gDPSetTexturePersp(gp++,G_TP_NONE);

    /* Set the detail type */
    gDPSetTextureDetail(gp++,G_TD_CLAMP);

    /* Set the texture filter type */
    gDPSetTextureFilter(gp++,G_TF_BILERP);

    /* Set the conversion mode of
       the color space */
    gDPSetTextureConvert(gp++,G_TC_FILT);

    /* Set the compare mode of the alpha value */
    gDPSetAlphaCompare(gp++,G_AC_NONE);

    /* Set the dithering mode of the color data */
    gDPSetColorDither(gp++,G_CD_DISABLE);

    /* Set the dithering mode of the alpha value */
    gDPSetAlphaDither(gp++,G_AD_NOISE);
    return gp;
    }

### Set and Read the Texture

The gDP functions are included in the N64 library; for more information about them, please see the online N64 Function Reference Manual (HTML manual pages).

    static Gfx *load_texture(Gfx *gp,u16 w,u16 h)
    {
    /* Read TLUT */

    /* Set the texture look-up table */
    gDPSetTextureLUT(gp++,G_TT_RGBA16);

    /* Read the texture look-up table */
    gDPLoadTLUT_pal16(gp++,0,texturetlut);

    /* Read the bitmap pattern */
    gDPLoadTextureTile_4b(gp++,texture,
       G_IM_FMT_CI,w,h,
       0,0,w-1,h-1,0,
       G_TX_WRAP | G_TX_NOMIRROR,G_TX_WRAP |
       G_TX_NOMIRROR,
       G_TX_NOMASK,G_TX_NOMASK,G_TX_NOLOD,
       G_TX_NOLOD);

    return gp;
    }

### Set the Drawing Sequence for a Bitmap Pattern

The following code sets the drawing sequence for an accepted texture image. The gSP functions are included in the N64 library; for more information about them, please see the online N64 Function Reference Manual(HTML manual pages).

    static Gfx *draw_texture(Gfx *gp,
       u16 left,u16 top,u16 w,u16 h)
    {
    /* The bitmap pattern drawing */
    gSPTextureRectangle(gp++,
       left<<2,top<<2,((left+w)<<2)-1,

       ((top+h)<<2)-1,
       G_TX_RENDERTILE,
       0,0,4<<10,1<<10);
       return gp;
    }

Nintendo® Confidential
Copyright © 1999
Nintendo of America Inc. All Rights Reserved
Nintendo and N64 are registered trademarks of Nintendo
Last Updated March, 1999