I'm trying to create game loop which runs in separate thread. I decided to use NSThread
object instead of CADisplayLink
and NSTimer
.
To test if OpenGLES
is working correctly I'm clearing the screen by solid color.
The problem here is that all is working fine with no loops, single render call from initWithFrame
. But when I put my render code inside game loop, or just simply create new thread and call render code from new thread there is nothing happens in the screen.
Here is my initWithFrame
function which is called from AppDelegate
's didFinishLaunchingWithOptions
:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[self setupLayer];
[self setupContext];
[self setScaleFactor];
[self initGame];
[NSThread detachNewThreadSelector:@selector(render) toTarget:self withObject:nil];
}
return self;
}
And this is render function:
- (void)render
{
glClearColor(1.0f, 0.0f, 1.0f, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
[context presentRenderbuffer:GL_RENDERBUFFER];
}
If I put render call instead of creating thread inside initWithFrame
, all works fine. But with current implementation I get black screen, despite render function is called.
I also tried using
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(render) object:nil];
[thread start];
It does not make pink screen to appear either.
Appreciate any advices how can I use threads to get OpenGl ES rendered to the screen.