iPhone Gameloop 从单独的线程渲染更新

发布于 2024-07-14 05:59:58 字数 623 浏览 6 评论 0原文

我是 iPhone 开发新手。 我的游戏循环设置如下。

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

我的基本游戏刻度/渲染看起来像这样

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

  CGRect wrect = [self bounds];

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

  [pool release];       
}

我的渲染函数被调用。 但是什么也没有绘制(我正在使用 Core Graphics 在派生的 UIView 上绘制一些线条)。

如果我通过计时器调用更新,那么一切都很好。

你能告诉我为什么通过线程完成渲染失败吗? 是否可以通过线程使其工作?

谢谢 富有的

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

◇流星雨 2024-07-21 05:59:58

你不能(好吧,不应该)直接调用 -drawRect: 。 相反,使用 -setNeedsDisplay; 然后,您的视图将在下次通过事件循环进行更新。 如果您在单独的线程中运行它,您可能需要使用performSelectorOnMainThread:。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文