MGK Library Manual


Graphics Library Structure










Objects in the mgk Library

Usage Examples

mainproc should be executed as follows.

  1. Initialize the default allocator.

    Since the default allocator is used when allocating memory, it must be initialized first before anything else.

        static MsoHeapAllocator     heap_allocator;
        void                        *heap_ptr;
        heap_ptr = _codeSegmentEnd;
        msoHeapAllocatorInit( &heap_allocator,
                              heap_ptr, 0x80380000 - ( u32 )heap_ptr );
        msoSetDefaultAllocator( _msoUpCast( MsoAllocator, &heap_allocator ) );

    The heap area is managed by MsoHeapAllocator.

    MsoHeapAllocator is initialized by msoHeamAllocatorInit and set in the default allocator by msoSetDefaultAllocator.

    In the above example, the memory area from immediately after codeSegment to 0x80380000 is reserved as the heap area.

  2. Initialize the application.

        #define     MAX_OBJS        (4)
        MgkObj      gObjArray[ MAX_OBJS ];
        mgkAppInit( 2, 32768, 65536, 256, 16384, MAX_OBJS, gObjArray, NULL );
        mgkAppInitGroups( );

    The application is initialized by mgkAppInit.

    The subsequent mgkAppInitGroups initializes MrnGroup.

    Initialization of MrnGroup has historically been rather complicated, but this is the simplest initialization method.

    In this example, the following settings are made.

    • Double buffer mode
    • Gfx length = 32768 per frame
    • Frame resource size = 65536 bytes
    • Instance queue size = 256
    • Conversion buffer size = 16384 bytes
    • Maximum number of objects = MAX_OBJS
    • Object array = gObjArray

  3. Load the object.

        mgkObjLoad( &gObjArray[ 0 ], ( u32 )_stageSegmentRomStart,
                                     ( u32 )_stageSegmentRomEnd );
        mgkObjLoad( &gObjArray[ 1 ], ( u32 )_animalSegmentRomStart,
                                     ( u32 )_animalSegmentRomEnd );

    Use mgkObjLoad to load the NVF to the object.

    In the above example, the NVF in _stageSegment is loaded to gObjArray[0], and the NVF in

    _animalSegment is loaded to gObjArray[1].

  4. Set the camera.

        mgkObjCreateCamera( &gObjArray[ 2 ], 45.0f, 1.333333333f, 5.0f, 2000.0 );

    The camera itself takes up one object.

    mgkObjCreateCamera turns an object into a camera object.

    Camera control is possible by using mgjObjLookAt, etc. on a camera object.

    The following settings are made in this example.

    • Field of view (Y) = 45
    • Aspect ratio = 1.3333333333
    • Clip near = 5
    • Clip far = 2000

  5. Main loop

    The method for independently constructing the main loop is introduced here.

        while ( 1 )
            nuContDataGetExAll( gMgiApp.cont_data );
            _mgkAppBeginFrame( );
            /* Object movement */
            mgkObjMove( &gObjArray[ 1 ], x, y, z );
            /* Face camera in direction of object */
            mgkObjLookAt( &gObjArray[ 2 ],
                          0.0f, 0.0f, 1000.0f,      /* Camera coordinates */
                          x, y, z,                  /* POV coordinates */
                          0.0f, 1.0f, 0.0f );       /* Upward vector */
            mgkAppEvalAll( );
            mgkAppDraw( );
            _mgkAppEndFrame( );

    The main loop must be processed in the following order.

    1. Increment gMuiApp.frame_count.

    2. Read the controller status.

    3. Declare the start of the frame with _mgkAppBeginFrame.

    4. Perform various processing, such as object movement, etc.

    5. Perform hierarchy evaluation with mgkAppEvalAll.

    6. Create gfx with mgkAppDraw.

    7. Declare the end of the frame with _mgkAppEndFrame.

    There are no retrace stand-by's anywhere in this code, but since _mgkAppEndFrame is called in synchronization with the scheduler, processing proceeds as a whole in 1/60 units (as long as a frame is not dropped).