Flood of touch events fixed?

Tutorials concerning the OpenGL® ES cross-platform API for full-function 2D and 3D graphics on the Google-Android platform.

Flood of touch events fixed?

Postby Kyros » Tue Nov 03, 2009 4:05 pm

Hi all,

While using a GLSurfaceView and onTouchEvent I used to get a flood of events when touching the screen.
This resulted in me needed to sleep this otherwise it affected framerate.

I noticed recently it doesnt do this anymore.
I was wondering if this flooding was a bug that is now fixed or whether it can be switched on or off now?

The reason I ask is that both could be useful to have.
Object selection might be one use without flooding but dragging an object would need a flood of events.
Kyros
Junior Developer
Junior Developer
 
Posts: 13
Joined: Wed Oct 21, 2009 11:49 am

Top

Postby alias » Thu Nov 05, 2009 4:45 am

I don't know, but can you help me on this? I cannot get the sleep "trick" to work, though I have seen it mentioned.

Do I need a separate thread to handle physics/drawing and a separate thread to handle input? When I try to sleep inside my onTouchEvent method, it just locks the screen completely until the sleep is over. I don't understand how to put them in separate threads, since they both want a lock on surfaceholder...

Is there any example implementation of this? I am very stuck, and my game is sluggish/unplayable on touch events.
alias
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Nov 05, 2009 4:39 am

Postby SanderS » Sat Nov 28, 2009 2:07 am

I'm also wondering how this is done. If someone could please help?
SanderS
Freshman
Freshman
 
Posts: 6
Joined: Tue Oct 20, 2009 8:23 pm

Postby hardcoras » Sat Nov 28, 2009 3:36 pm

You need at least two separate threads: UI thread and rendering/game update thread. From API 1.5 GLSurfaceView class handles that for you (creates new rendering thread). It would be better to remove game update into a separate thread also, but it is up to you.

onTouchEvent method runs on UI thread. In this method just handle the input and put the thread to sleep for a short period of time (e.g. 20-40ms: sleep(20)). When the UI thread awakes and onTouchEvent method is run again, all input events while thread was asleep will be accumulated into MotionEvent's history (@see MotionEvent.getHistorySize(), MotionEvent.getHistorical*()).

And don't forget synchronization when you pass input events data to the rendering/game update thread(s).
hardcoras
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Sat Nov 14, 2009 2:31 pm
Location: Lithunia

Postby SanderS » Sat Nov 28, 2009 4:51 pm

Thanks for the fast reply. But I don't fully understand it, because the UI thread creates, for example, the surfaceview. And from the game thread I update yje surfaceview. But if the UI thread is paused, so is also the surfaceview...

At the moment I have the following:

1. The activity startGame is started. startGame calls the surfaceview gameView. startGame als starts the thread gameThread.

2. Game thread starts a loop, and in every loop all the calculations are done and the following is done:

Code: Select all
while(_run) {
  try {
    c = _surfaceHolder.lockCanvas(null);
    synchronized(_surfaceHolder) {
      mgameView.onDraw(c);
      runGameCode(c);
    }
  } finally {
    if(c != null){
      _surfaceHolder.unlockCanvasAndPost(c);
    }
  }
}


mgameView.onDraw(c); calls the onDraw function in the gameView which clears the background and draws it again. runGameCode(c) executes all the calcultaions in the gameThread.

Inside the startGame activity I have the onTouchEvent which at the end: Thread.sleep(16);
But the higher I put the number, there is still quite a big framedrop if I move my finger. Even when I take no history.

So how should the structure of the game than be?
SanderS
Freshman
Freshman
 
Posts: 6
Joined: Tue Oct 20, 2009 8:23 pm

Postby hardcoras » Sun Nov 29, 2009 9:05 pm

Sorry if i'm getting something wrong because I don't code using Canvas, I'm using OpenGL with GLSurfaceView. So I don't have any problems like surfaceHolder locking, starting new Thread, etc. But i'll try to help anyway.

I don't know how yours onTouchEvent() method looks, but I think your are acquiring a lock on monitor object and then putting the UI Thread to sleep without releasing the lock on monitor. So while your UI thread sleeps the Game Thread can not continue as well because it can't acquire a lock on monitor object (surfaceHolder).
If i'm right your onTouchEvent() has code like:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. onTouchEvent(...){
  2.  
  3.   synchronized(_surfaceHolder) {
  4.  
  5.     handleInput;
  6.  
  7.     sleep(16);
  8.  
  9.   }
  10.  
  11. }
Parsed in 0.029 seconds, using GeSHi 1.0.8.4


You have two choices: a) move sleep code from synchronization block (I recommend); b) wait with timeout on a monitor object.

Hope that helps, if i'm wrong, give some more details to resolve this issue.
Good luck.

ps. sorry for my poor english.
hardcoras
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Sat Nov 14, 2009 2:31 pm
Location: Lithunia

Top

Return to Android 2D/3D Graphics - OpenGL Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests