Android Game Development – Game Loop in Separate Thread

androidgame developmentjavamultithreading

I am about to port a game that was initially written for iOs and I am having some doubts how certain things should be done.

I have been looking many of the examples of the game loops for android, and almost everywhere the game loop is designed to run in separate thread and not in main UI thread which seems fine and logical to me.
However, my game scene is combination of OpenGL graphics and standard android views (buttons, labels, etc…) and by design, the primary game logic and OpenGL drawing would be done in separate thread and rest of the standard stuff would be done in main UI thread.

In many situations, I will need to call functions that should run in game thread from main UI thread and vice versa. As a very basic, simple example, when user performs touch (which is detected in UI thread) the event should propagated and be processed by the game thread.

Do you know some good mechanisms or patterns on how to such cross thread interaction?
Is there a simpler solution to a game loop, maybe by not running loop in a separate thread?

Best Answer

If you run the loop in a separate thread you will need to keep it synchronized with everything else that will be going on. This should provide you with insight though: https://gamedev.stackexchange.com/questions/651/tips-for-writing-the-main-game-loop

I've not programmed on Android, but I have programmed in Java. If you want to see an example of my game loop you can get the code here: https://github.com/moorej/RetroPlexjava

Related Topic