我应该在我的 OpenGL ES 游戏中使用多个线程吗? [英] Should I use multiple threads within my OpenGL ES game?

查看:24
本文介绍了我应该在我的 OpenGL ES 游戏中使用多个线程吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款包含一个玩家和许多敌人的 iPhone 游戏.我使用 OpenGL ES 来显示游戏的视觉效果.

I am developing an iPhone game which contains one player and many enemies. I use OpenGL ES to display the game visuals.

对于我应该只使用一个线程还是使用多个线程来移动和控制玩家和敌人,我有点困惑.对于这个基本的游戏架构,您有什么建议?

I am a little confused as to whether I should only use one thread or use multiple threads for moving and controlling the player and the enemies. What do you recommend for this basic game architecture?

推荐答案

我不同意 Max 在这一点上的观点,并说你不应该完全忽略在应用程序中使用多线程.OpenGL ES 可以从后台线程中绘制,但您一次只能使用单个线程与 OpenGL ES 上下文进行通信.

I'm going to disagree with Max on this one and say that you shouldn't completely ignore using multithreading in your application. OpenGL ES can be drawn to from a background thread, but you can only communicate to an OpenGL ES context using a single thread at a time.

Apple 有一整节题为并发和 OpenGL ES" 在他们的 OpenGL ES Programming Guide for iOS 中.其中,他们描述了使用多线程提高 OpenGL ES 应用程序性能的几种方案.

Apple has an entire section entitled "Concurrency and OpenGL ES" in their OpenGL ES Programming Guide for iOS. In that, they describe several scenarios for improving performance of an OpenGL ES application using multithreading.

一般来说,建议您将耗时的操作移出主线程.主线程处理应用程序的用户界面,如果您阻止它,您将阻止标准 UI 元素更新和触摸事件注册.根据您的游戏(物理、AI 等)在幕后进行的处理量,您可能需要将其中的大部分内容移至后台线程.

In general, it's recommended that you move time-consuming operations off of the main thread. The main thread handles the user interface of your application, and if you block that you will prevent standard UI elements from updating and touch events from registering. Depending on how much processing is being done behind the scenes for your game (physics, AI, etc.), you may need to move significant chunks of that to a background thread.

多线程不是一个简单的话题,但 Apple 通过 Grand Central Dispatch 让这件事变得容易多了,所以我强烈建议阅读他们的 并发编程指南,其中描述了在应用程序中实现多线程的此技术和其他技术.此外,作为 iOS 开发课程的一部分,我还教授了一门关于多线程和 GCD 的课程,其视频可以是 在 iTunes U 上找到.

Multithreading is not a simple topic, but Apple has made this a lot easier through Grand Central Dispatch, so I highly recommend reading their Concurrency Programming Guide which describes this and other techniques for achieving multithreading in your application. Additionally, I taught a class on multithreading and GCD as part of my course on iOS development, the video of which can be found on iTunes U.

随着 Apple 推出多核设备,现在利用多线程和 GCD 将在未来为您带来巨大的回报.

As Apple rolls out multicore devices, taking advantage of multithreading and GCD now will greatly pay off for you in the future.

这篇关于我应该在我的 OpenGL ES 游戏中使用多个线程吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆