Question

I'm new to iPhone development. I have a game loop setup as follows.

(void)CreateGameTick:(NSTimeInterval) in_time
{
  [NSThread detachNewThreadSelector:@selector(GameTick) toTarget:self withObject:nil];
}

My basic game tick/render looks like this

(void)GameTick
{
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  CGRect wrect = [self bounds];

  while( m_running )
  {
    [self drawRect: wrect];
  }

  [pool release];       
}

My render function gets called. However nothing gets drawn (I am using Core Graphics to draw some lines on a derived UIView).

If I call my update via a timer then all is well and good.

Can you tell me why the render fails when done via threads? And is it possible to make it work via threads?

Thanks Rich

Was it helpful?

Solution

You can't (well, shouldn't) call -drawRect: directly. Instead, use -setNeedsDisplay; your view will then be updated the next time through the event loop. If you're running this in a separate thread, you may need to use performSelectorOnMainThread:.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top