2

I try to write a little game using android and have some problems with threading. The mainloop of the game runs in this own thread and basically just does something like this:

  public void run() {
    while (true) {
      NativeGameLib.gameTick(t);
    }
  }

Now I want to pass touch inputs to my NativeGameLib. I thought I use the onTouchEvent() of the view in the UI-thread, create a runable inside and let it execute on the main-loop thread.

But I don't really get how to do it. I have seen that there is a Handler and a Looper class and that I can use the handler to post runables to the messageQueue and the Looper.loop() function to process the queue.

As far as I understand it, the loop() function endlessly checks for new messages and therefore blocks the thread. So, how can I combine it with my thread. I want to do something like this in the thread:

  public void run() {
    while (true) {
      processMessageQueue();
      NativeGameLib.gameTick(t);
    }
  }

Any ideas? Thanks.

D-rk
  • 5,513
  • 1
  • 37
  • 55
  • All i can think of at the moment is solving it using two threads. one HandlerThread which gets runables to process the input events and a second thread which periodically also generates a runable containing the NativeGameLib.gameTick(t); call. But I think its not really elegant. – D-rk Dec 25 '11 at 13:27
  • take a look at sdk examples: C:\android-sdk\samples\android-10\JetBoy C:\android-sdk\samples\android-10\LunarLander – Selvin Dec 25 '11 at 13:31

1 Answers1

0

Did you check View.Post API ?

http://developer.android.com/reference/android/view/View.html#post(java.lang.Runnable)

Vinoth
  • 5,687
  • 11
  • 44
  • 56
  • i dont think this helps me in this case. i need to process input events in the my game-loop thread and since the thread is not a view i cannot use the view.post() here. – D-rk Dec 25 '11 at 13:01