treasure_hunt_jni.cc 3.99 KB
Newer Older
Ruben Jesus Garcia Hernandez's avatar
Ruben Jesus Garcia Hernandez committed
1 2 3 4
/*Uses code from Stackoverflow which uses the MIT license and the CC BY-SA 3.0*/
/*These licenses are compatible with Apache 2.0*/

/*
5
# Copyright 2016-2018 Ruben Jesus Garcia Hernandez
Ruben Jesus Garcia Hernandez's avatar
Ruben Jesus Garcia Hernandez committed
6 7
*/

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/* Copyright 2017 Google Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <android/log.h>
#include <jni.h>

#include <memory>

#include "vr/gvr/capi/include/gvr.h"
#include "vr/gvr/capi/include/gvr_audio.h"
30
#include "NOMADVRLib/atoms.hpp" //for TMPDIR
31

Ruben Jesus Garcia Hernandez's avatar
Ruben Jesus Garcia Hernandez committed
32
#include "treasure_hunt_renderer.h" 
33

34 35 36 37
#define JNI_METHOD(return_type, method_name) \
  JNIEXPORT return_type JNICALL              \
      Java_com_google_vr_ndk_samples_treasurehunt_MainActivity_##method_name

38 39 40 41
//http://stackoverflow.com/questions/12900695/how-to-obtain-jni-interface-pointer-jnienv-for-asynchronous-calls
    JavaVM* javaVM = nullptr;
    jobject jo;

42 43 44 45 46 47 48 49 50 51 52
namespace {

inline jlong jptr(TreasureHuntRenderer *native_treasure_hunt) {
  return reinterpret_cast<intptr_t>(native_treasure_hunt);
}

inline TreasureHuntRenderer *native(jlong ptr) {
  return reinterpret_cast<TreasureHuntRenderer *>(ptr);
}
}  // anonymous namespace

53 54 55 56 57 58 59 60 61 62 63 64
//http://stackoverflow.com/questions/28622036/android-ndk-calling-java-functions-from-c
/*void GetUrl (const char *url, const char *path) 
{
    jstring jurl = genv->NewStringUTF(url);
    jstring jpath = genv->NewStringUTF(path);

    jclass jc = genv->FindClass("NOMAD");
    jmethodID mid = genv->GetStaticMethodID(jc, "GetUrl", "(Ljava/lang/String;Ljava/lang/String;)V");

    genv->CallStaticVoidMethod(jc, mid, jurl, jpath);	
}*/

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
extern "C" {

JNI_METHOD(jlong, nativeCreateRenderer)
(JNIEnv *env, jclass clazz, jobject class_loader, jobject android_context,
 jlong native_gvr_api) {
  std::unique_ptr<gvr::AudioApi> audio_context(new gvr::AudioApi);
  audio_context->Init(env, android_context, class_loader,
                      GVR_AUDIO_RENDERING_BINAURAL_HIGH_QUALITY);

  return jptr(
      new TreasureHuntRenderer(reinterpret_cast<gvr_context *>(native_gvr_api),
                               std::move(audio_context)));
}

JNI_METHOD(void, nativeDestroyRenderer)
(JNIEnv *env, jclass clazz, jlong native_treasure_hunt) {
  delete native(native_treasure_hunt);
}

JNI_METHOD(void, nativeInitializeGl)
(JNIEnv *env, jobject obj, jlong native_treasure_hunt) {
  native(native_treasure_hunt)->InitializeGl();
}

JNI_METHOD(void, nativeDrawFrame)
(JNIEnv *env, jobject obj, jlong native_treasure_hunt) {
  native(native_treasure_hunt)->DrawFrame();
}

JNI_METHOD(void, nativeOnTriggerEvent)
(JNIEnv *env, jobject obj, jlong native_treasure_hunt) {
  native(native_treasure_hunt)->OnTriggerEvent();
}

JNI_METHOD(void, nativeOnPause)
(JNIEnv *env, jobject obj, jlong native_treasure_hunt) {
  native(native_treasure_hunt)->OnPause();
}

JNI_METHOD(void, nativeOnResume)
(JNIEnv *env, jobject obj, jlong native_treasure_hunt) {
  native(native_treasure_hunt)->OnResume();
}

109
//https://library.vuforia.com/articles/Solution/How-To-Communicate-Between-Java-and-C-using-the-JNI
110
JNI_METHOD(void, nativeSetConfigFile)
111
(JNIEnv *env, jobject obj, jstring s, jstring e) {
112 113
	env->GetJavaVM(&javaVM);
	jo=env->NewGlobalRef(obj);
114
	configPath = env->GetStringUTFChars(s , NULL ) ;
115
	TMPDIR=env->GetStringUTFChars(e , NULL ) ;
116 117 118

}

119 120 121 122 123
JNI_METHOD(void, nativeLoadConfigFile)
(JNIEnv *env, jobject obj, jlong native_treasure_hunt) {
  native(native_treasure_hunt)->loadConfigFile();
}

124 125 126 127 128
JNI_METHOD(void, nativekeyPress)
(JNIEnv *env, jobject obj, jlong native_treasure_hunt, jint key) {
  int k=key;
  native(native_treasure_hunt)->keyPress(k);
}
129

130
}  // extern "C"