Home | History | Annotate | Download | only in jni
      1 /* San Angeles Observation OpenGL ES version example
      2  * Copyright 2009 The Android Open Source Project
      3  * All rights reserved.
      4  *
      5  * This source is free software; you can redistribute it and/or
      6  * modify it under the terms of EITHER:
      7  *   (1) The GNU Lesser General Public License as published by the Free
      8  *       Software Foundation; either version 2.1 of the License, or (at
      9  *       your option) any later version. The text of the GNU Lesser
     10  *       General Public License is included with this source in the
     11  *       file LICENSE-LGPL.txt.
     12  *   (2) The BSD-style license that is included with this source in
     13  *       the file LICENSE-BSD.txt.
     14  *
     15  * This source is distributed in the hope that it will be useful,
     16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
     18  * LICENSE-LGPL.txt and LICENSE-BSD.txt for more details.
     19  */
     20 #include <jni.h>
     21 #include <sys/time.h>
     22 #include <time.h>
     23 #include <android/log.h>
     24 #include <stdint.h>
     25 #include "importgl.h"
     26 #include "app.h"
     27 
     28 int   gAppAlive   = 1;
     29 
     30 static int  sWindowWidth  = 320;
     31 static int  sWindowHeight = 480;
     32 static int  sDemoStopped  = 0;
     33 static long sTimeOffset   = 0;
     34 static int  sTimeOffsetInit = 0;
     35 static long sTimeStopped  = 0;
     36 
     37 static long
     38 _getTime(void)
     39 {
     40     struct timeval  now;
     41 
     42     gettimeofday(&now, NULL);
     43     return (long)(now.tv_sec*1000 + now.tv_usec/1000);
     44 }
     45 
     46 /* Call to initialize the graphics state */
     47 void
     48 Java_com_example_SanAngeles_DemoRenderer_nativeInit( JNIEnv*  env )
     49 {
     50     importGLInit();
     51     appInit();
     52     gAppAlive  = 1;
     53 }
     54 
     55 void
     56 Java_com_example_SanAngeles_DemoRenderer_nativeResize( JNIEnv*  env, jobject  thiz, jint w, jint h )
     57 {
     58     sWindowWidth  = w;
     59     sWindowHeight = h;
     60     __android_log_print(ANDROID_LOG_INFO, "SanAngeles", "resize w=%d h=%d", w, h);
     61 }
     62 
     63 /* Call to finalize the graphics state */
     64 void
     65 Java_com_example_SanAngeles_DemoRenderer_nativeDone( JNIEnv*  env )
     66 {
     67     appDeinit();
     68     importGLDeinit();
     69 }
     70 
     71 /* This is called to indicate to the render loop that it should
     72  * stop as soon as possible.
     73  */
     74 
     75 void _pause()
     76 {
     77   /* we paused the animation, so store the current
     78    * time in sTimeStopped for future nativeRender calls */
     79     sDemoStopped = 1;
     80     sTimeStopped = _getTime();
     81 }
     82 
     83 void _resume()
     84 {
     85   /* we resumed the animation, so adjust the time offset
     86    * to take care of the pause interval. */
     87     sDemoStopped = 0;
     88     sTimeOffset -= _getTime() - sTimeStopped;
     89 }
     90 
     91 
     92 void
     93 Java_com_example_SanAngeles_DemoGLSurfaceView_nativeTogglePauseResume( JNIEnv*  env )
     94 {
     95     sDemoStopped = !sDemoStopped;
     96     if (sDemoStopped)
     97         _pause();
     98     else
     99         _resume();
    100 }
    101 
    102 void
    103 Java_com_example_SanAngeles_DemoGLSurfaceView_nativePause( JNIEnv*  env )
    104 {
    105     _pause();
    106 }
    107 
    108 void
    109 Java_com_example_SanAngeles_DemoGLSurfaceView_nativeResume( JNIEnv*  env )
    110 {
    111     _resume();
    112 }
    113 
    114 /* Call to render the next GL frame */
    115 void
    116 Java_com_example_SanAngeles_DemoRenderer_nativeRender( JNIEnv*  env )
    117 {
    118     long   curTime;
    119 
    120     /* NOTE: if sDemoStopped is TRUE, then we re-render the same frame
    121      *       on each iteration.
    122      */
    123     if (sDemoStopped) {
    124         curTime = sTimeStopped + sTimeOffset;
    125     } else {
    126         curTime = _getTime() + sTimeOffset;
    127         if (sTimeOffsetInit == 0) {
    128             sTimeOffsetInit = 1;
    129             sTimeOffset     = -curTime;
    130             curTime         = 0;
    131         }
    132     }
    133 
    134     //__android_log_print(ANDROID_LOG_INFO, "SanAngeles", "curTime=%ld", curTime);
    135 
    136     appRender(curTime, sWindowWidth, sWindowHeight);
    137 }
    138