OSDN Git Service

merge from MikuMikuStudio nativebullet.
[mikumikustudio/libgdx-mikumikustudio.git] / extensions / gdx-bullet / jni / src / mikumikustudio / jmeClasses.cpp
1 /*
2  * Copyright (c) 2009-2010 jMonkeyEngine
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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.
31  */
32 #include "jmeClasses.h"
33 #include <stdio.h>
34
35 /**
36  * Author: Normen Hansen
37  */
38 //public fields
39 jclass jmeClasses::PhysicsSpace;
40 jmethodID jmeClasses::PhysicsSpace_preTick;
41 jmethodID jmeClasses::PhysicsSpace_postTick;
42 jmethodID jmeClasses::PhysicsSpace_addCollisionEvent;
43
44 jclass jmeClasses::PhysicsGhostObject;
45 jmethodID jmeClasses::PhysicsGhostObject_addOverlappingObject;
46
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;
56
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;
67
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;
80
81 jclass jmeClasses::DebugMeshCallback;
82 jmethodID jmeClasses::DebugMeshCallback_addVector;
83
84 //private fields
85 //JNIEnv* jmeClasses::env;
86 JavaVM* jmeClasses::vm;
87
88 void jmeClasses::initJavaClasses(JNIEnv* env) {
89 //    if (env != NULL) {
90 //        fprintf(stdout, "Check Java VM state\n");
91 //        fflush(stdout);
92 //        int res = vm->AttachCurrentThread((void**) &jmeClasses::env, NULL);
93 //        if (res < 0) {
94 //            fprintf(stdout, "** ERROR: getting Java env!\n");
95 //            if (res == JNI_EVERSION) fprintf(stdout, "GetEnv Error because of different JNI Version!\n");
96 //            fflush(stdout);
97 //        }
98 //        return;
99 //    }
100     if(PhysicsSpace!=NULL) return;
101     fprintf(stdout, "Bullet-Native: Initializing java classes\n");
102     fflush(stdout);
103 //    jmeClasses::env = env;
104     env->GetJavaVM(&vm);
105
106     PhysicsSpace = env->FindClass("com/jme3/bullet/PhysicsSpace");
107     if (env->ExceptionCheck()) {
108         env->Throw(env->ExceptionOccurred());
109         return;
110     }
111
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());
117         return;
118     }
119     
120     PhysicsGhostObject = env->FindClass("com/jme3/bullet/objects/PhysicsGhostObject");
121     if (env->ExceptionCheck()) {
122         env->Throw(env->ExceptionOccurred());
123         return;
124     }
125     PhysicsGhostObject_addOverlappingObject = env->GetMethodID(PhysicsGhostObject, "addOverlappingObject","(Lcom/jme3/bullet/collision/PhysicsCollisionObject;)V");
126     if (env->ExceptionCheck()) {
127         env->Throw(env->ExceptionOccurred());
128         return;
129     }
130
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");
140
141     Quaternion = env->FindClass("com/jme3/math/Quaternion");
142     if (env->ExceptionCheck()) {
143         env->Throw(env->ExceptionOccurred());
144         return;
145     }
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");
155
156     Matrix3f = env->FindClass("com/jme3/math/Matrix3f");
157     if (env->ExceptionCheck()) {
158         env->Throw(env->ExceptionOccurred());
159         return;
160     }
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());
166         return;
167     }
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");
176
177     DebugMeshCallback = env->FindClass("com/jme3/bullet/util/DebugMeshCallback");
178     if (env->ExceptionCheck()) {
179         env->Throw(env->ExceptionOccurred());
180         return;
181     }
182     
183     DebugMeshCallback_addVector = env->GetMethodID(DebugMeshCallback, "addVector", "(FFFII)V");
184     if (env->ExceptionCheck()) {
185         env->Throw(env->ExceptionOccurred());
186         return;
187     }
188 }
189
190 void jmeClasses::throwNPE(JNIEnv* env) {
191     if (env == NULL) return;
192     jclass newExc = env->FindClass("java/lang/NullPointerException");
193     env->ThrowNew(newExc, "");
194     return;
195 }