Cocoa alternative to aglUseFont

Posts: 1
Joined: 2009.10
Post: #1
I've been searching for something like this for days now, but with no luck, and my attempts to roll my own solutions have failed.

I want to convert a font to a series of glBitmaps. The old aglUseFont function did this, but it has been deprecated with 10.5, and to the best of my knowledge, it never worked with Cocoa in the first place.

I want to recreate this function to convert NSFont objects into the same series of display lists. My approach has been to print each character to an in-memory NSImage and then use the NSImage data to generate the glBitmap. I used something like this before for a collision detection routine, and it worked great.

The problem I'm having is that the text does not seem to be printing to the NSImage. The relevant code (somewhat hacky, since I've been playing with this for a few days) is this:

void BuildFont() throw ()
    const unsigned buffer_width = 32;
    const unsigned buffer_height = 32;
    NSImage *buffer = [[NSImage alloc] initWithSize:NSMakeSize(buffer_width, buffer_height)];
    NSFont *font = [NSFont fontWithName:@"Andale Mono" size:16];
    [buffer lockFocus];
    [font setInContext:[NSGraphicsContext currentContext]];
    for (unsigned glyph = 0; glyph < 94; glyph++)
        [[NSColor blackColor] set];
        NSRectFill(NSMakeRect(0, 0, buffer_width, buffer_height));
        [[NSColor whiteColor] set];
        [[NSString stringWithFormat:@"%c", ' ' + glyph] drawAtPoint:NSZeroPoint withAttributes:nil];
        NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect(0, 0, buffer_width, buffer_height)];
        unsigned char mono_data[buffer_width * buffer_height] = {0};
        for (unsigned y = 0, pixel = 0; y < buffer_height; y++)
            for (unsigned x = 0; x < buffer_width; x++, pixel++)
                NSUInteger pixel_data[4];
                [bitmap getPixel:pixel_data atX:x y:y];
                if (pixel_data[0] != 0 ||
                    pixel_data[1] != 0 ||
                    pixel_data[2] != 0)
                    mono_data[pixel] = 1;
        glNewList(system_font + glyph, GL_COMPILE);
            glBitmap(buffer_width, buffer_height, 0.0, 0.0, 16.0, 0.0, mono_data);
        [bitmap release];
    [buffer unlockFocus];
    [buffer release];

There are a lot of things wrong with this code, right now I'm just trying to get results, and then I'll fix the bigger issues.

I know that at least some drawing is taking place, because the NSRectFill call is working. The problem is that the data is always a solid color, and no font drawing appears to be taking place. What am I doing wrong here? Is there a better way to approach this problem? External libraries are not an option, so please don't even recommend them, I need to keep this in pure Cocoa.
Quote this message in a reply
Posts: 5,143
Joined: 2002.04
Post: #2
It's not a replacement for aglUseFont, but it is an easy way to draw text in OpenGL with Cocoa:
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  alternative to glTexSubImage2D()? NelsonMandella 4 4,440 Aug 17, 2009 01:54 PM
Last Post: NelsonMandella