Android engine for Android: how to synchronize the game loop and the canvas update stream?

I wanted to rewrite my simple game engine to work on Android, and I wonder how I can synchronize two working streams. Now I have the following:

enter image description here

  • Runner is the main action, the entry point to this game;
  • CanvasView is just a canvas on which to draw;
  • GameWorld - as the name implies - a class that stores current information about the state of the game. For now, let's just say that it also contains a level.
  • GameLoop is a separate thread that updates the state of the game;
  • CanvasThread is a separate thread that runs to draw the current level of CanvasView.

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

  • Is it possible to run the onDraw () method of CanvasThread on demand? In the current state of the engine, it simply restarts when the previous one is completed.
  • I want to implement some kind of three-way handshake between GameLoop and CanvasThread, something similar to:

    • GameLoop -> CanvasThread: Stop Updating
    • Canvas → GameLoop: normal, stopped
    • GameLoop -> CanvasThread: ok, you can resume.

What is the best way to do this? I am a complete Java / Android newbie, so my way of tuning the engine is most likely not the best / best. If you have any design suggestions, I would be happy to appreciate them.

Thanks.

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

+4
source share
3 answers

Android has an easy way to handle threads. You can use Looper and Handler to send messages or complete Runnables between threads.

Check out Android Guts: an introduction to Loopers and Handlers at mindtherobot.com for an introduction.

You can send an "empty" message to report something or indicate some arguments. You can send a message immediately or with some delay.

If you create a game loop by sending messages to yourself, it would be easy to enter messages from other threads.

+2
source

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

+2
source

I believe that it will be much easier for you if they do not contact.

Instead, put a couple of restrictions to keep yourself in order:

  • Make the game loop very tight. If you are going to update your canvas, say, 30 fps (about 33 ms), make your game cycle no longer than 10 or 20 ms.
  • Put a lock in your game loop and canvas update. Now, everyone will wait for the other to finish before doing his thing. You will not have a canvas with 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 } } } 
+2
source

All Articles