开发者

Android game engine design: how to synchronize game loop and canvas updating thread?

I wanted to rewrite my simple game engine to run on Android and I am wondering how can I synchronize two running threads. Right now I have the following:

Android game engine design: how to synchronize game loop and canvas updating thread?

  • Runner is the main activity, entry point for this game;
  • CanvasView is just a canvas on which drawing is being made;
  • GameWorld is - as the name suggests - class which stores the current information about the state of the game. For now, lets just say that it also contains a Level.
  • GameLoop is a separate thread which is updating the game state;
  • CanvasThread is a separate thread, which is being run to draw the current Level on CanvasView.
  • 开发者_如何学Python

As the level is just a simple array, CanvasThread just iterates through the array and draws it on screen. And I have few questions regarding this:

  • is there a possibility to run the onDraw() method of CanvasThread on demand? In current state of the engine it is just being relaunched when the execution of previous one is finished.
  • I want to implement some kind of three way handshake between GameLoop and CanvasThread, something similar to:

    1. GameLoop -> CanvasThread: please stop updating
    2. Canvas -> GameLoop: ok, stopped
    3. GameLoop -> CanvasThread: ok, you may resume.

What is the best way to do so? I am a total Java / Android newbie so my way of setting the engine is most probably not the best / optimal one. If you have any suggestions to the design, I will gladly appreciate them.

Thank you.

PS: If I had violated all best practices while creating the diagram above, please forgive me.


Android has an easy way to handle thread communication. You can use a Looper and a Handler to send messages, or complete Runnables, between the Threads.

Look at Android Guts: Intro to Loopers and Handlers on mindtherobot.com for an introduction.

You can send an "empty" message to signal something, or supply some arguments. You can send the message immediately, or with some delay.

If you design your game loop by sending messages to itself, it would be easy to inject messages from other threads.


What I created long long time ago. Maybe it helps a little http://code.google.com/p/candroidengine/


I believe you'll find it a lot easier if they don't communicate.

Instead, put a couple constraints on to keep yourself sane:

  1. Make a very tight game loop. If you're going to be updating your canvas at, let's say, 30 fps (about 33 ms) make your game loop take no longer than 10 or 20 ms.
  2. Put a lock on your game loop and canvas update. Now, each one will wait for the other to finish before doing their thing. You won't end up with a canvas that has the half the screen representing before the loop and the other half of the screen after the loop.

Example:

class GameLoop extends Thread {

    public void run() {
        while(true) {
            synchronized(theGameWorldObject) {
                // update game info here
            }
        }
    }

}

class Canvas {
    updateCanvas() { // or whatever you call it
        synchronized(theGameWorldObject) {
            // update canvas here
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜