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:
Imports the header file that contains the MyNSGLProcAddress function from Listing B-1.
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.
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.
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:
Imports the header file that contains the MyAGLGetProcAdress function from Listing B-2.
Declares function pointers for the functions of interest.
Initializes each function pointer to NULL.
Calls the initialization function defined in Listing B-2.
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.
Sets each of the function pointers to NULL when they are no longer needed.
Deallocates the function pointers when they are no longer needed by calling the deallocation function defined in Listing B-2.
Last updated: 2008-06-09