android/testproject/jni/app-android.cpp
author Paul Hunkin <paul@bieh.net>
Tue, 29 Jun 2010 01:30:11 +1200
changeset 4712 8319aa8fa4dc
parent 4711 ed040b480a9f
child 4713 ba38983b10c2
permissions -rw-r--r--
Code cleanup
paul@4711
     1
/*******************************************************************************
paul@4711
     2
                               Headers
paul@4711
     3
*******************************************************************************/
paul@4711
     4
#include <jni.h>
paul@4711
     5
#include <sys/time.h>
paul@4711
     6
#include <time.h>
paul@4711
     7
#include <android/log.h>
paul@4711
     8
#include <stdint.h>
paul@4711
     9
paul@4711
    10
#include <stdio.h>
paul@4711
    11
#include <stdlib.h>
paul@4711
    12
#include <math.h>
paul@4711
    13
paul@4711
    14
#include <pthread.h>
paul@4711
    15
paul@4711
    16
#include "importgl.h"
paul@4711
    17
#include "egl.h"
paul@4711
    18
paul@4711
    19
/*******************************************************************************
paul@4711
    20
                               Globals
paul@4711
    21
*******************************************************************************/
paul@4711
    22
static long _getTime(void){
paul@4711
    23
	struct timeval  now;
paul@4711
    24
	gettimeofday(&now, NULL);
paul@4711
    25
	return (long)(now.tv_sec*1000 + now.tv_usec/1000);
paul@4711
    26
}
paul@4711
    27
paul@4711
    28
JNIEnv* mEnv = NULL;
paul@4711
    29
JavaVM* mVM = NULL;
paul@4711
    30
paul@4711
    31
//Main activity
paul@4711
    32
jclass mActivityInstance;
paul@4711
    33
paul@4711
    34
//method signatures
paul@4711
    35
jmethodID midCreateGLContext;
paul@4711
    36
jmethodID midFlipBuffers;
paul@4711
    37
paul@4711
    38
extern "C" int SDL_main();
paul@4711
    39
paul@4711
    40
/*******************************************************************************
paul@4711
    41
                 Functions called by JNI
paul@4711
    42
*******************************************************************************/	
paul@4711
    43
paul@4712
    44
extern "C" void Java_org_libsdl_android_SDLActivity_nativeInit( JNIEnv*  env, jobject obj )
paul@4711
    45
{    
paul@4711
    46
	__android_log_print(ANDROID_LOG_INFO, "SDL", "JNI: NativeInit");
paul@4711
    47
paul@4711
    48
	mEnv = env;
paul@4711
    49
paul@4711
    50
    SDL_main();
paul@4711
    51
}
paul@4711
    52
paul@4711
    53
extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
paul@4711
    54
{
paul@4711
    55
    JNIEnv* env = NULL;
paul@4711
    56
    jint result = -1;
paul@4711
    57
paul@4711
    58
    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
paul@4711
    59
        return result;
paul@4711
    60
    }
paul@4711
    61
paul@4711
    62
    mEnv = env;
paul@4711
    63
paul@4711
    64
    __android_log_print(ANDROID_LOG_INFO, "SDL", "JNI: OnLoad");
paul@4711
    65
paul@4712
    66
    jclass cls = mEnv->FindClass ("org/libsdl/android/SDLActivity"); 
paul@4711
    67
    mActivityInstance = cls;
paul@4711
    68
    midCreateGLContext = mEnv->GetStaticMethodID(cls,"createGLContext","()V");
paul@4711
    69
    midFlipBuffers = mEnv->GetStaticMethodID(cls,"flipBuffers","()V");
paul@4711
    70
paul@4711
    71
    if(!midCreateGLContext || !midFlipBuffers){
paul@4711
    72
        __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Bad mids\n");
paul@4711
    73
    }else{
paul@4711
    74
        __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: Good mids\n");
paul@4711
    75
    }
paul@4711
    76
    
paul@4711
    77
    return JNI_VERSION_1_4;
paul@4711
    78
}
paul@4711
    79
paul@4711
    80
paul@4711
    81
paul@4711
    82
/*******************************************************************************
paul@4711
    83
                 Functions called by SDL
paul@4711
    84
*******************************************************************************/
paul@4711
    85
extern "C" void sdl_create_context(){
paul@4711
    86
	__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_create_context()\n");
paul@4711
    87
paul@4711
    88
    mEnv->CallStaticVoidMethod(mActivityInstance, midCreateGLContext ); 
paul@4711
    89
    __android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_create_context() return\n");
paul@4711
    90
paul@4711
    91
   // exit(1);
paul@4711
    92
}
paul@4711
    93
paul@4711
    94
extern "C" void sdl_render(){
paul@4711
    95
paul@4711
    96
    //When we get here, we've accumulated a full frame
paul@4711
    97
   //__android_log_print(ANDROID_LOG_INFO, "SDL", "SDL: sdl_render()");
paul@4711
    98
    
paul@4711
    99
    mEnv->CallStaticVoidMethod(mActivityInstance, midFlipBuffers ); 
paul@4711
   100
}
paul@4711
   101