Panda3D
 All Classes Functions Variables Enumerations
android_main.cxx
1 // Filename: android_main.cxx
2 // Created by: rdb (12Jan13)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "config_android.h"
16 #include "config_util.h"
17 #include "virtualFileMountAndroidAsset.h"
18 #include "virtualFileSystem.h"
19 #include "filename.h"
20 
21 #include "config_display.h"
22 //#define OPENGLES_1
23 //#include "config_androiddisplay.h"
24 
25 #include <android_native_app_glue.h>
26 
27 //struct android_app* panda_android_app = NULL;
28 
29 extern int main(int argc, char **argv);
30 
31 ////////////////////////////////////////////////////////////////////
32 // Function: android_main
33 // Description: This function is called by native_app_glue to
34 // initialize the program. It simply stores the
35 // android_app object and calls main() normally.
36 ////////////////////////////////////////////////////////////////////
37 void android_main(struct android_app* app) {
38  panda_android_app = app;
39 
40  // Attach the current thread to the JVM.
41  JNIEnv *env;
42  ANativeActivity* activity = app->activity;
43  int status = activity->vm->AttachCurrentThread(&env, NULL);
44  if (status < 0 || env == NULL) {
45  android_cat.error() << "Failed to attach thread to JVM!\n";
46  return;
47  }
48 
49  // Fetch the data directory.
50  jclass activity_class = env->GetObjectClass(activity->clazz);
51  jmethodID get_appinfo = env->GetMethodID(activity_class, "getApplicationInfo", "()Landroid/content/pm/ApplicationInfo;");
52 
53  jobject appinfo = env->CallObjectMethod(activity->clazz, get_appinfo);
54  jclass appinfo_class = env->GetObjectClass(appinfo);
55 
56  // Fetch the path to the data directory.
57  jfieldID datadir_field = env->GetFieldID(appinfo_class, "dataDir", "Ljava/lang/String;");
58  jstring datadir = (jstring) env->GetObjectField(appinfo, datadir_field);
59  const char *data_path = env->GetStringUTFChars(datadir, NULL);
60 
61  Filename::_internal_data_dir = data_path;
62  android_cat.info() << "Path to data: " << data_path << "\n";
63 
64  env->ReleaseStringUTFChars(datadir, data_path);
65 
66  // Fetch the path to the library directory.
67  jfieldID libdir_field = env->GetFieldID(appinfo_class, "nativeLibraryDir", "Ljava/lang/String;");
68  jstring libdir = (jstring) env->GetObjectField(appinfo, libdir_field);
69  const char *lib_path = env->GetStringUTFChars(libdir, NULL);
70 
71  string dtool_name = string(lib_path) + "/libp3dtool.so";
73  android_cat.info() << "Path to dtool: " << dtool_name << "\n";
74 
75  env->ReleaseStringUTFChars(libdir, lib_path);
76 
77  // Get the path to the APK.
78  jmethodID methodID = env->GetMethodID(activity_class, "getPackageCodePath", "()Ljava/lang/String;");
79  jstring code_path = (jstring) env->CallObjectMethod(activity->clazz, methodID);
80 
81  const char* apk_path;
82  apk_path = env->GetStringUTFChars(code_path, NULL);
83  android_cat.info() << "Path to APK: " << apk_path << "\n";
84 
85  // Mount the assets directory.
86  PT(VirtualFileMountAndroidAsset) asset_mount;
87  asset_mount = new VirtualFileMountAndroidAsset(app->activity->assetManager, apk_path);
88  VirtualFileSystem *vfs = VirtualFileSystem::get_global_ptr();
89  vfs->mount(asset_mount, "/android_asset", 0);
90 
91  // Release the apk_path.
92  env->ReleaseStringUTFChars(code_path, apk_path);
93 
94  // Now add the asset directory to the model-path.
95  get_model_path().append_directory("/android_asset");
96 
97  // Create bogus argc and argv, then call our main function.
98  char *argv[] = {NULL};
99  int argc = 0;
100  main(argc, argv);
101 
102  // Detach the thread before exiting.
103  activity->vm->DetachCurrentThread();
104 }
A hierarchy of directories and files that appears to be one continuous file system, even though the files may originate from several different sources that may not be related to the actual OS&#39;s file system.
static void set_dtool_name(const string &name)
Do not use.