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 
     26 int   gAppAlive   = 1;
     27 
     28 static int  sWindowWidth  = 320;
     29 static int  sWindowHeight = 480;
     30 static int  sDemoStopped  = 0;
     31 static long sTimeOffset   = 0;
     32 static int  sTimeOffsetInit = 0;
     33 static long sTimeStopped  = 0;
     34 
     35 static long
     36 _getTime(void)
     37 {
     38     struct timeval  now;
     39 
     40     gettimeofday(&now, NULL);
     41     return (long)(now.tv_sec*1000 + now.tv_usec/1000);
     42 }
     43 
     44 /* Call to initialize the graphics state */
     45 void
     46 Java_com_example_SanAngeles_DemoRenderer_nativeInit( JNIEnv*  env )
     47 {
     48     importGLInit();
     49     appInit();
     50     gAppAlive    = 1;
     51     sDemoStopped = 0;
     52     sTimeOffsetInit = 0;
     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 void
     75 Java_com_example_SanAngeles_DemoGLSurfaceView_nativePause( JNIEnv*  env )
     76 {
     77     sDemoStopped = !sDemoStopped;
     78     if (sDemoStopped) {
     79         /* we paused the animation, so store the current
     80          * time in sTimeStopped for future nativeRender calls */
     81         sTimeStopped = _getTime();
     82     } else {
     83         /* we resumed the animation, so adjust the time offset
     84          * to take care of the pause interval. */
     85         sTimeOffset -= _getTime() - sTimeStopped;
     86     }
     87 }
     88 
     89 /* Call to render the next GL frame */
     90 void
     91 Java_com_example_SanAngeles_DemoRenderer_nativeRender( JNIEnv*  env )
     92 {
     93     long   curTime;
     94 
     95     /* NOTE: if sDemoStopped is TRUE, then we re-render the same frame
     96      *       on each iteration.
     97      */
     98     if (sDemoStopped) {
     99         curTime = sTimeStopped + sTimeOffset;
    100     } else {
    101         curTime = _getTime() + sTimeOffset;
    102         if (sTimeOffsetInit == 0) {
    103             sTimeOffsetInit = 1;
    104             sTimeOffset     = -curTime;
    105             curTime         = 0;
    106         }
    107     }
    108 
    109     //__android_log_print(ANDROID_LOG_INFO, "SanAngeles", "curTime=%ld", curTime);
    110 
    111     appRender(curTime, sWindowWidth, sWindowHeight);
    112 }
    113