Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Initializing Entry Points

Listing B-3 shows how to use the MyNSGLGetProcAddress function from Listing B-1 to obtain a few OpenGL entry points. A detailed explanation for each numbered line of code appears following the listing.

Listing B-3  Using NSGLGetProcAddress to obtain an OpenGL entry point

#import "MyNSGLGetProcAddress.h" // 1
static void InitEntryPoints (void);
static void DeallocEntryPoints (void);
 
// Function pointer type definitions
typedef void (*glBlendColorProcPtr)(GLclampf red,GLclampf green,
                        GLclampf blue,GLclampf alpha);
typedef void (*glBlendEquationProcPtr)(GLenum mode);
 typedef void (*glDrawRangeElementsProcPtr)(GLenum mode, GLuint start,
                GLuint end,GLsizei count,GLenum type,const GLvoid *indices);
 
glBlendColorProcPtr pfglBlendColor = NULL; // 2
glBlendEquationProcPtr pfglBlendEquation = NULL;
glDrawRangeElementsProcPtr pfglDrawRangeElements = NULL;
 
static void InitEntryPoints (void) // 3
{
    pfglBlendColor = (glBlendColorProcPtr) MyNSGLGetProcAddress
                                    ("glBlendColor");
    pfglBlendEquation = (glBlendEquationProcPtr)MyNSGLGetProcAddress
                                ("glBlendEquation");
    pfglDrawRangeElements = (glDrawRangeElementsProcPtr)MyNSGLGetProcAddress
                                ("glDrawRangeElements");
}
// -------------------------
static void DeallocEntryPoints (void) // 4
{
    pfglBlendColor = NULL;
    pfglBlendEquation = NULL;
    pfglDrawRangeElements = NULL;;
}

Here's what the code does:

  1. Imports the header file that contains the MyNSGLProcAddress function from Listing B-1.

  2. Declares function pointers for the functions of interest. Note that each function pointer uses the prefix pf to distinguish it from the function it points to. Although using this prefix is not a requirement, it's best to avoid using the exact function names.

  3. Initializes the entry points. This function repeatedly calls the MyNSGLProcAddress function to obtain function pointers for each of the functions of interest—glBlendColor, glBlendEquation, and glDrawRangeElements.

  4. Sets each of the function pointers to NULL when they are no longer needed.

Listing B-4 demonstrates how to use the function aglGetProcAddress to obtain a few OpenGL entry points. Note that the approach used by this code is similar to that used in Listing B-3. A detailed explanation for each numbered line of code appears following the listing.

Listing B-4  Using AGL to obtain an OpenGL entry point

#include "MyAGLGetProcAddress.h" // 1
 
static OSStatus InitEntryPoints (void);
static void DeallocEntryPoints (void);
 
typedef void (*glBlendColorProcPtr)(GLclampf red,GLclampf green,
                        GLclampf blue,GLclampf alpha); // 2
typedef void (*glBlendEquationProcPtr)(GLenum mode);
typedef void (*glDrawRangeElementsProcPtr)(GLenum mode,GLuint start,
                        GLuint end,GLsizei count,GLenum type,
                        const GLvoid *indices);
 
glBlendColorProcPtr pfglBlendColor = NULL; // 3
glBlendEquationProcPtr pfglBlendEquation = NULL;
glDrawRangeElementsProcPtr pfglDrawRangeElements  = NULL;
 
static OSStatus InitEntryPoints (void)
{
    OSStatus err = MyAGLInitEntryPoints(); // 4
    if (noErr == err) { // 5
        pfglBlendColor = (glBlendColorProcPtr)
                    MyAGLGetProcAddress ("glBlendColor");
        pfglBlendEquation = (glBlendEquationProcPtr)
                    MyAGLGetProcAddress ("glBlendEquation");
        pfglDrawRangeElements = (glDrawRangeElementsProcPtr)
               MyAGLGetProcAddress("glDrawRangeElements");
    }
    return err;
}
 
static void DeallocEntryPoints (void)
{
    pfglBlendColor = NULL; // 6
    pfglBlendEquation = NULL;
    pfglDrawRangeElements = NULL;
    MyAGLDellocEntryPoints (); // 7
}

Here's what the code does:

  1. Imports the header file that contains the MyAGLGetProcAdress function from Listing B-2.

  2. Declares function pointers for the functions of interest.

  3. Initializes each function pointer to NULL.

  4. Calls the initialization function defined in Listing B-2.

  5. After checking for an error condition, obtains the function pointers for the functions of interest by calling the MyAGLGetProcAddress function defined in Listing B-2.

  6. Sets each of the function pointers to NULL when they are no longer needed.

  7. Deallocates the function pointers when they are no longer needed by calling the deallocation function defined in Listing B-2.



< Previous PageNext Page > Hide TOC


Last updated: 2008-06-09




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice