2 * Copyright (c) 2009-2010 jMonkeyEngine
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "jmeClasses.h"
36 * Author: Normen Hansen
39 jclass jmeClasses::PhysicsSpace;
40 jmethodID jmeClasses::PhysicsSpace_preTick;
41 jmethodID jmeClasses::PhysicsSpace_postTick;
42 jmethodID jmeClasses::PhysicsSpace_addCollisionEvent;
44 jclass jmeClasses::PhysicsGhostObject;
45 jmethodID jmeClasses::PhysicsGhostObject_addOverlappingObject;
47 jclass jmeClasses::Vector3f;
48 jmethodID jmeClasses::Vector3f_set;
49 jmethodID jmeClasses::Vector3f_toArray;
50 jmethodID jmeClasses::Vector3f_getX;
51 jmethodID jmeClasses::Vector3f_getY;
52 jmethodID jmeClasses::Vector3f_getZ;
53 jfieldID jmeClasses::Vector3f_x;
54 jfieldID jmeClasses::Vector3f_y;
55 jfieldID jmeClasses::Vector3f_z;
57 jclass jmeClasses::Quaternion;
58 jmethodID jmeClasses::Quaternion_set;
59 jmethodID jmeClasses::Quaternion_getX;
60 jmethodID jmeClasses::Quaternion_getY;
61 jmethodID jmeClasses::Quaternion_getZ;
62 jmethodID jmeClasses::Quaternion_getW;
63 jfieldID jmeClasses::Quaternion_x;
64 jfieldID jmeClasses::Quaternion_y;
65 jfieldID jmeClasses::Quaternion_z;
66 jfieldID jmeClasses::Quaternion_w;
68 jclass jmeClasses::Matrix3f;
69 jmethodID jmeClasses::Matrix3f_set;
70 jmethodID jmeClasses::Matrix3f_get;
71 jfieldID jmeClasses::Matrix3f_m00;
72 jfieldID jmeClasses::Matrix3f_m01;
73 jfieldID jmeClasses::Matrix3f_m02;
74 jfieldID jmeClasses::Matrix3f_m10;
75 jfieldID jmeClasses::Matrix3f_m11;
76 jfieldID jmeClasses::Matrix3f_m12;
77 jfieldID jmeClasses::Matrix3f_m20;
78 jfieldID jmeClasses::Matrix3f_m21;
79 jfieldID jmeClasses::Matrix3f_m22;
81 jclass jmeClasses::DebugMeshCallback;
82 jmethodID jmeClasses::DebugMeshCallback_addVector;
85 //JNIEnv* jmeClasses::env;
86 JavaVM* jmeClasses::vm;
88 void jmeClasses::initJavaClasses(JNIEnv* env) {
90 // fprintf(stdout, "Check Java VM state\n");
92 // int res = vm->AttachCurrentThread((void**) &jmeClasses::env, NULL);
94 // fprintf(stdout, "** ERROR: getting Java env!\n");
95 // if (res == JNI_EVERSION) fprintf(stdout, "GetEnv Error because of different JNI Version!\n");
100 if(PhysicsSpace!=NULL) return;
101 fprintf(stdout, "Bullet-Native: Initializing java classes\n");
103 // jmeClasses::env = env;
106 PhysicsSpace = env->FindClass("com/jme3/bullet/PhysicsSpace");
107 if (env->ExceptionCheck()) {
108 env->Throw(env->ExceptionOccurred());
112 PhysicsSpace_preTick = env->GetMethodID(PhysicsSpace, "preTick_native", "(F)V");
113 PhysicsSpace_postTick = env->GetMethodID(PhysicsSpace, "postTick_native", "(F)V");
114 PhysicsSpace_addCollisionEvent = env->GetMethodID(PhysicsSpace, "addCollisionEvent","(Lcom/jme3/bullet/collision/PhysicsCollisionObject;Lcom/jme3/bullet/collision/PhysicsCollisionObject;J)V");
115 if (env->ExceptionCheck()) {
116 env->Throw(env->ExceptionOccurred());
120 PhysicsGhostObject = env->FindClass("com/jme3/bullet/objects/PhysicsGhostObject");
121 if (env->ExceptionCheck()) {
122 env->Throw(env->ExceptionOccurred());
125 PhysicsGhostObject_addOverlappingObject = env->GetMethodID(PhysicsGhostObject, "addOverlappingObject","(Lcom/jme3/bullet/collision/PhysicsCollisionObject;)V");
126 if (env->ExceptionCheck()) {
127 env->Throw(env->ExceptionOccurred());
131 Vector3f = env->FindClass("com/jme3/math/Vector3f");
132 Vector3f_set = env->GetMethodID(Vector3f, "set", "(FFF)Lcom/jme3/math/Vector3f;");
133 Vector3f_toArray = env->GetMethodID(Vector3f, "toArray", "([F)[F");
134 Vector3f_getX = env->GetMethodID(Vector3f, "getX", "()F");
135 Vector3f_getY = env->GetMethodID(Vector3f, "getY", "()F");
136 Vector3f_getZ = env->GetMethodID(Vector3f, "getZ", "()F");
137 Vector3f_x = env->GetFieldID(Vector3f, "x", "F");
138 Vector3f_y = env->GetFieldID(Vector3f, "y", "F");
139 Vector3f_z = env->GetFieldID(Vector3f, "z", "F");
141 Quaternion = env->FindClass("com/jme3/math/Quaternion");
142 if (env->ExceptionCheck()) {
143 env->Throw(env->ExceptionOccurred());
146 Quaternion_set = env->GetMethodID(Quaternion, "set", "(FFFF)Lcom/jme3/math/Quaternion;");
147 Quaternion_getW = env->GetMethodID(Quaternion, "getW", "()F");
148 Quaternion_getX = env->GetMethodID(Quaternion, "getX", "()F");
149 Quaternion_getY = env->GetMethodID(Quaternion, "getY", "()F");
150 Quaternion_getZ = env->GetMethodID(Quaternion, "getZ", "()F");
151 Quaternion_x = env->GetFieldID(Quaternion, "x", "F");
152 Quaternion_y = env->GetFieldID(Quaternion, "y", "F");
153 Quaternion_z = env->GetFieldID(Quaternion, "z", "F");
154 Quaternion_w = env->GetFieldID(Quaternion, "w", "F");
156 Matrix3f = env->FindClass("com/jme3/math/Matrix3f");
157 if (env->ExceptionCheck()) {
158 env->Throw(env->ExceptionOccurred());
161 Matrix3f_set = env->GetMethodID(Matrix3f, "set", "(IIF)Lcom/jme3/math/Matrix3f;");
162 Matrix3f_get = env->GetMethodID(Matrix3f, "get", "(II)F");
163 Matrix3f_m00 = env->GetFieldID(Matrix3f, "m00", "F");
164 if (env->ExceptionCheck()) {
165 env->Throw(env->ExceptionOccurred());
168 Matrix3f_m01 = env->GetFieldID(Matrix3f, "m01", "F");
169 Matrix3f_m02 = env->GetFieldID(Matrix3f, "m02", "F");
170 Matrix3f_m10 = env->GetFieldID(Matrix3f, "m10", "F");
171 Matrix3f_m11 = env->GetFieldID(Matrix3f, "m11", "F");
172 Matrix3f_m12 = env->GetFieldID(Matrix3f, "m12", "F");
173 Matrix3f_m20 = env->GetFieldID(Matrix3f, "m20", "F");
174 Matrix3f_m21 = env->GetFieldID(Matrix3f, "m21", "F");
175 Matrix3f_m22 = env->GetFieldID(Matrix3f, "m22", "F");
177 DebugMeshCallback = env->FindClass("com/jme3/bullet/util/DebugMeshCallback");
178 if (env->ExceptionCheck()) {
179 env->Throw(env->ExceptionOccurred());
183 DebugMeshCallback_addVector = env->GetMethodID(DebugMeshCallback, "addVector", "(FFFII)V");
184 if (env->ExceptionCheck()) {
185 env->Throw(env->ExceptionOccurred());
190 void jmeClasses::throwNPE(JNIEnv* env) {
191 if (env == NULL) return;
192 jclass newExc = env->FindClass("java/lang/NullPointerException");
193 env->ThrowNew(newExc, "");