views:

1069

answers:

2

Hi,

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

A: 

I don't know the answer to your specific question, but check out cocos2d-iphone.

It is a great 2D game framework, and you can find several code samples on the site that may help you.

Dana Holt
+4  A: 

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:.

Ben Gottlieb