Merge "[ndk] Fixed sample SanAngeles to properly pause/resume"

This commit is contained in:
Andrew Hsieh
2012-10-12 09:26:27 -07:00
committed by Gerrit Code Review
2 changed files with 52 additions and 14 deletions

View File

@@ -47,9 +47,7 @@ Java_com_example_SanAngeles_DemoRenderer_nativeInit( JNIEnv* env )
{ {
importGLInit(); importGLInit();
appInit(); appInit();
gAppAlive = 1; gAppAlive = 1;
sDemoStopped = 0;
sTimeOffsetInit = 0;
} }
void void
@@ -71,19 +69,44 @@ Java_com_example_SanAngeles_DemoRenderer_nativeDone( JNIEnv* env )
/* This is called to indicate to the render loop that it should /* This is called to indicate to the render loop that it should
* stop as soon as possible. * stop as soon as possible.
*/ */
void _pause()
{
/* we paused the animation, so store the current
* time in sTimeStopped for future nativeRender calls */
sDemoStopped = 1;
sTimeStopped = _getTime();
}
void _resume()
{
/* we resumed the animation, so adjust the time offset
* to take care of the pause interval. */
sDemoStopped = 0;
sTimeOffset -= _getTime() - sTimeStopped;
}
void
Java_com_example_SanAngeles_DemoGLSurfaceView_nativeTogglePauseResume( JNIEnv* env )
{
sDemoStopped = !sDemoStopped;
if (sDemoStopped)
_pause();
else
_resume();
}
void void
Java_com_example_SanAngeles_DemoGLSurfaceView_nativePause( JNIEnv* env ) Java_com_example_SanAngeles_DemoGLSurfaceView_nativePause( JNIEnv* env )
{ {
sDemoStopped = !sDemoStopped; _pause();
if (sDemoStopped) { }
/* we paused the animation, so store the current
* time in sTimeStopped for future nativeRender calls */ void
sTimeStopped = _getTime(); Java_com_example_SanAngeles_DemoGLSurfaceView_nativeResume( JNIEnv* env )
} else { {
/* we resumed the animation, so adjust the time offset _resume();
* to take care of the pause interval. */
sTimeOffset -= _getTime() - sTimeStopped;
}
} }
/* Call to render the next GL frame */ /* Call to render the next GL frame */

View File

@@ -86,14 +86,29 @@ class DemoGLSurfaceView extends GLSurfaceView {
public boolean onTouchEvent(final MotionEvent event) { public boolean onTouchEvent(final MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) { if (event.getAction() == MotionEvent.ACTION_DOWN) {
nativePause(); nativeTogglePauseResume();
} }
return true; return true;
} }
@Override
public void onPause() {
super.onPause();
nativePause();
}
@Override
public void onResume() {
super.onResume();
nativeResume();
}
DemoRenderer mRenderer; DemoRenderer mRenderer;
private static native void nativePause(); private static native void nativePause();
private static native void nativeResume();
private static native void nativeTogglePauseResume();
} }
class DemoRenderer implements GLSurfaceView.Renderer { class DemoRenderer implements GLSurfaceView.Renderer {