OpenGL ES : Drawing Text...

Member
Posts: 21
Joined: 2006.11
Post: #1
What is the preferred way to draw text on an iPhone?

I've created a view that inherits from iPhoneOSGameView and can get
TriangleFans and Lines etc drawing.

My Ortho view is setup as....
glOrtho(0.0f, 320, 460, 0.0f, -1.0f, 1.0f);

I tried using the UIVIiew.DrawString() to display some text on the screen, but nothing appears at the location specified.

Are bitmap fonts the preferred way or is there a better way. A link or sample code would be greatly apprecaited.

Merry Christmas to all.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
Apple has sample code for this.
Quote this message in a reply
Member
Posts: 21
Joined: 2006.11
Post: #3
Would you happen to know the sample name that demonstrates this? I did look through the OpenGL ES examples for iPhone and none of them display text using bitmap fonts or DrawString ( unless I messed one ). They all seem to use UILabel to display text. Is that the only way to do it?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #4
Hmm, I'm not seeing it... There used to be a utility class in one of the samples called something like Texture2D which drew text into a CGBitmapContext (don't remember exactly how) and used the result as a texture.
Quote this message in a reply
Moderator
Posts: 335
Joined: 2002.04
Post: #5
OneSadCookie Wrote:Hmm, I'm not seeing it... There used to be a utility class in one of the samples called something like Texture2D which drew text into a CGBitmapContext (don't remember exactly how) and used the result as a texture.

The texture2d class used to be in the old lander sample that got pulled because of the 'bug' in how it set up the gl textures using the old memory leaking way.

Digging up the old code, the text bit looked like this..

Code:
@implementation Texture2D (Text)

- (id) initWithString:(NSString*)string dimensions:(CGSize)dimensions alignment:(UITextAlignment)alignment fontName:(NSString*)name fontSize:(CGFloat)size
{
    NSUInteger                width,
                            height,
                            i;
    CGContextRef            context;
    void*                    data;
    CGColorSpaceRef            colorSpace;
    UIFont *                font;
    
    font = [UIFont fontWithName:name size:size];
    
    width = dimensions.width;
    if((width != 1) && (width & (width - 1))) {
        i = 1;
        while(i < width)
        i *= 2;
        width = i;
    }
    height = dimensions.height;
    if((height != 1) && (height & (height - 1))) {
        i = 1;
        while(i < height)
        i *= 2;
        height = i;
    }
    
    colorSpace = CGColorSpaceCreateDeviceGray();
    data = calloc(height, width);
    context = CGBitmapContextCreate(data, width, height, 8, width, colorSpace, kCGImageAlphaNone);
    CGColorSpaceRelease(colorSpace);
    
    
    CGContextSetGrayFillColor(context, 1.0, 1.0);
    CGContextTranslateCTM(context, 0.0, height);
    CGContextScaleCTM(context, 1.0, -1.0); //NOTE: NSString draws in UIKit referential i.e. renders upside-down compared to CGBitmapContext referential
    UIGraphicsPushContext(context);
        [string drawInRect:CGRectMake(0, 0, dimensions.width, dimensions.height) withFont:font lineBreakMode:UILineBreakModeWordWrap alignment:alignment];
    UIGraphicsPopContext();
    
    self = [self initWithData:data pixelFormat:kTexture2DPixelFormat_A8 pixelsWide:width pixelsHigh:height contentSize:dimensions];
    
    CGContextRelease(context);
    free(data);
    
    return self;
}

Personally though I prefer to do all my text drawing in GL directly as it's easier to design game specific fonts.
Quote this message in a reply
Member
Posts: 21
Joined: 2006.11
Post: #6
Zwilnik Wrote:Personally though I prefer to do all my text drawing in GL directly as it's easier to design game specific fonts.

Hi Zwilnik,
I'd rather not use any memory leaking code if possible. Could you clarify what you mean by doing all your "text drawing in GL directly"? Do you have links or sample code on how this might be achieved as I'm interested in doing game specific fonts as well? Or is the code you posted the bug free version of the original lander example?

Thanks.
Quote this message in a reply
Member
Posts: 40
Joined: 2009.05
Post: #7
savage Wrote:Hi Zwilnik,
I'd rather not use any memory leaking code if possible. Could you clarify what you mean by doing all your "text drawing in GL directly"? Do you have links or sample code on how this might be achieved as I'm interested in doing game specific fonts as well? Or is the code you posted the bug free version of the original lander example?

Thanks.

There's a tutorial here on how to do bitmap fonts in OpenGL [url="http://www.71squared.com/2009/04/iphone-game-programming-tutorial-4-bitmap-font-class/"]http://www.71squared.com/2009/04/iphone-game-programming-tutorial-4-bitmap-font-class/
[/url]
Quote this message in a reply
Member
Posts: 21
Joined: 2006.11
Post: #8
iamflimflam1 Wrote:There's a tutorial here on how to do bitmap fonts in OpenGL [url="http://www.71squared.com/2009/04/iphone-game-programming-tutorial-4-bitmap-font-class/"]http://www.71squared.com/2009/04/iphone-game-programming-tutorial-4-bitmap-font-class/
[/url]

Thanks for the link. I've just been watching that particular episode and it looks like that code is based on the memory leaking Texture2D class, or maybe I'm wrong.
Quote this message in a reply
Member
Posts: 43
Joined: 2009.10
Post: #9
savage Wrote:Thanks for the link. I've just been watching that particular episode and it looks like that code is based on the memory leaking Texture2D class, or maybe I'm wrong.

I don't think *that* aspect of the code has any leaks. I believe the problem was in the drawing aspect of that code which is not displayed in this thread and does not have anything to do with the generation of the text bitmap data to be used as the texture. Basically the problem as I recall was that every time the draw method of the Texture2D class was called, it would create a new texture buffer which meant that the video memory was duplicated on every draw and never freed.

In other words, I don't think the text generation contains the memory leak Smile
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenGL Cube drawing: VBOs, instanced geometry, octrees Symbol$ 5 8,886 Dec 23, 2011 01:43 PM
Last Post: Skorche
  Drawing menu versus drawing game johncmurphy 4 4,232 Jan 6, 2010 09:14 AM
Last Post: AnotherJake
  openGL n00b looking for FBO drawing examples or direction auptown 5 7,122 Nov 4, 2009 02:16 AM
Last Post: Eskema
  Drawing shapes opengl es jjslay 2 4,037 Aug 14, 2009 11:07 AM
Last Post: jjslay
  Drawing To Offscreen Images With OpenGL ES muleskinner 3 4,668 Mar 3, 2009 12:58 PM
Last Post: muleskinner