Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use multiple threads within my OpenGL ES game?

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?

like image 653
Ali Avatar asked Dec 09 '22 10:12

Ali


2 Answers

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

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.

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.

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

like image 163
Brad Larson Avatar answered Dec 11 '22 23:12

Brad Larson


It really depends on lots of things. First of all you won't get any performance boost if you're using several threads (since all modern iOS-devices has single core processors, though iPad2 has 2). Second several threads make any application much more sophisticated, since you have to keep track of shared resources, synchronize threads and lots more.

Use several threads only if you really need this. For example you're loading lots of textures into memory from file and it might take several seconds. If you do this on the same thread, where there rendering is performed, then all rendering would hang for that time. So it's better to do that on a separate thread and then give loaded data to the main one. Or you might update physics on a separate thread with higher update rate (however you benefit from this only on several cores).

If you're a novice developer and making some simple game don't use multiple threads. This stuff requires lot of experience.

like image 30
Max Avatar answered Dec 11 '22 23:12

Max