I am developing openGL application on iPad and i came up to one quite large problem. I used already existing posts to learn that i can use UILabel to generate text into opengl texture, so i did that. The problem appeared when i had to use five or more labels in different places of texture. When my application must generate five of those textures consisting of five labels each it takes some power. I generate one texture in one loop max, but that creates a small lag. When i load five textures, the lag gets five times more irritating.
Now when i told the story, let me ask you a question:
How could i relieve my CPU and perhaps generate text in some other way? Its static, just for displaying info.
I did try using other thread, but i came up to even more problems, so i would rather prefer some text generating alternative that multi threading.
Here is a part of that texture generator:
//------------ DATE --------------------------------
[dateLabel setBounds:CGRectMake(0, 0, 95, 10)];
[dateLabel setFrame:CGRectMake(0, 0, 95, 10)];
[dateLabel setBackgroundColor:[UIColor clearColor]];
[dateLabel setFont:[UIFont systemFontOfSize:12]];
[dateLabel setLineBreakMode:UILineBreakModeWordWrap];
[dateLabel setNumberOfLines:1];
dateLabel.textColor = [UIColor whiteColor];
dateLabel.text = [self.delegate dateAtIndex:index];
UIGraphicsBeginImageContext(CGSizeMake(95, 10));
[dateLabel.layer renderInContext:UIGraphicsGetCurrentContext()];
//[dateLabel release]; // We can now release the view
UIImage* date = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
As you can see, i set the position, size, text and then create UIImage out of it. I create even more of those images (five in fact), and then i render all those images to other context which is my last texture.
Perhaps my lack of experience led me to some stupid solution here.
I figured it out. I had to turn to multi threading anyway. It appears that text in OpenGL can be rendered in many ways. But all of them requires different approach and implementation. You can generate text letter by letter, you can use existing libraries and so on. But UILabel method still beats them all when you only need static text on some texture.
Anyway, i managed to relieve the CPU by using NSOperationQueue. I fed all the processing to operation queue and the images it generated, saved in NSMutableArray. Only then i used them for generating textures.
Hooray!