jmeClasses.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. /**
  34. * Author: Normen Hansen
  35. */
  36. //public fields
  37. jclass jmeClasses::PhysicsSpace;
  38. jmethodID jmeClasses::PhysicsSpace_preTick;
  39. jmethodID jmeClasses::PhysicsSpace_postTick;
  40. jclass jmeClasses::Vector3f;
  41. jmethodID jmeClasses::Vector3f_set;
  42. jmethodID jmeClasses::Vector3f_toArray;
  43. jmethodID jmeClasses::Vector3f_getX;
  44. jmethodID jmeClasses::Vector3f_getY;
  45. jmethodID jmeClasses::Vector3f_getZ;
  46. jfieldID jmeClasses::Vector3f_x;
  47. jfieldID jmeClasses::Vector3f_y;
  48. jfieldID jmeClasses::Vector3f_z;
  49. jclass jmeClasses::Quaternion;
  50. jmethodID jmeClasses::Quaternion_set;
  51. jmethodID jmeClasses::Quaternion_getX;
  52. jmethodID jmeClasses::Quaternion_getY;
  53. jmethodID jmeClasses::Quaternion_getZ;
  54. jmethodID jmeClasses::Quaternion_getW;
  55. jfieldID jmeClasses::Quaternion_x;
  56. jfieldID jmeClasses::Quaternion_y;
  57. jfieldID jmeClasses::Quaternion_z;
  58. jfieldID jmeClasses::Quaternion_w;
  59. jclass jmeClasses::Matrix3f;
  60. jmethodID jmeClasses::Matrix3f_set;
  61. jmethodID jmeClasses::Matrix3f_get;
  62. jfieldID jmeClasses::Matrix3f_m00;
  63. jfieldID jmeClasses::Matrix3f_m01;
  64. jfieldID jmeClasses::Matrix3f_m02;
  65. jfieldID jmeClasses::Matrix3f_m10;
  66. jfieldID jmeClasses::Matrix3f_m11;
  67. jfieldID jmeClasses::Matrix3f_m12;
  68. jfieldID jmeClasses::Matrix3f_m20;
  69. jfieldID jmeClasses::Matrix3f_m21;
  70. jfieldID jmeClasses::Matrix3f_m22;
  71. jclass jmeClasses::DebugMeshCallback;
  72. jmethodID jmeClasses::DebugMeshCallback_addVector;
  73. //private fields
  74. //JNIEnv* jmeClasses::env;
  75. JavaVM* jmeClasses::vm;
  76. void jmeClasses::initJavaClasses(JNIEnv* env) {
  77. // if (env != NULL) {
  78. // fprintf(stdout, "Check Java VM state\n");
  79. // fflush(stdout);
  80. // int res = vm->AttachCurrentThread((void**) &jmeClasses::env, NULL);
  81. // if (res < 0) {
  82. // fprintf(stdout, "** ERROR: getting Java env!\n");
  83. // if (res == JNI_EVERSION) fprintf(stdout, "GetEnv Error because of different JNI Version!\n");
  84. // fflush(stdout);
  85. // }
  86. // return;
  87. // }
  88. if(PhysicsSpace!=NULL) return;
  89. fprintf(stdout, "Bullet-Native: Initializing java classes\n");
  90. fflush(stdout);
  91. // jmeClasses::env = env;
  92. env->GetJavaVM(&vm);
  93. PhysicsSpace = env->FindClass("com/jme3/bullet/PhysicsSpace");
  94. if (env->ExceptionCheck()) {
  95. env->Throw(env->ExceptionOccurred());
  96. return;
  97. }
  98. PhysicsSpace_preTick = env->GetMethodID(PhysicsSpace, "preTick_native", "(F)V");
  99. PhysicsSpace_postTick = env->GetMethodID(PhysicsSpace, "postTick_native", "(F)V");
  100. if (env->ExceptionCheck()) {
  101. env->Throw(env->ExceptionOccurred());
  102. return;
  103. }
  104. Vector3f = env->FindClass("com/jme3/math/Vector3f");
  105. Vector3f_set = env->GetMethodID(Vector3f, "set", "(FFF)Lcom/jme3/math/Vector3f;");
  106. Vector3f_toArray = env->GetMethodID(Vector3f, "toArray", "([F)[F");
  107. Vector3f_getX = env->GetMethodID(Vector3f, "getX", "()F");
  108. Vector3f_getY = env->GetMethodID(Vector3f, "getY", "()F");
  109. Vector3f_getZ = env->GetMethodID(Vector3f, "getZ", "()F");
  110. Vector3f_x = env->GetFieldID(Vector3f, "x", "F");
  111. Vector3f_y = env->GetFieldID(Vector3f, "y", "F");
  112. Vector3f_z = env->GetFieldID(Vector3f, "z", "F");
  113. Quaternion = env->FindClass("com/jme3/math/Quaternion");
  114. if (env->ExceptionCheck()) {
  115. env->Throw(env->ExceptionOccurred());
  116. return;
  117. }
  118. Quaternion_set = env->GetMethodID(Quaternion, "set", "(FFFF)Lcom/jme3/math/Quaternion;");
  119. Quaternion_getW = env->GetMethodID(Quaternion, "getW", "()F");
  120. Quaternion_getX = env->GetMethodID(Quaternion, "getX", "()F");
  121. Quaternion_getY = env->GetMethodID(Quaternion, "getY", "()F");
  122. Quaternion_getZ = env->GetMethodID(Quaternion, "getZ", "()F");
  123. Quaternion_x = env->GetFieldID(Quaternion, "x", "F");
  124. Quaternion_y = env->GetFieldID(Quaternion, "y", "F");
  125. Quaternion_z = env->GetFieldID(Quaternion, "z", "F");
  126. Quaternion_w = env->GetFieldID(Quaternion, "w", "F");
  127. Matrix3f = env->FindClass("com/jme3/math/Matrix3f");
  128. if (env->ExceptionCheck()) {
  129. env->Throw(env->ExceptionOccurred());
  130. return;
  131. }
  132. Matrix3f_set = env->GetMethodID(Matrix3f, "set", "(IIF)Lcom/jme3/math/Matrix3f;");
  133. Matrix3f_get = env->GetMethodID(Matrix3f, "get", "(II)F");
  134. Matrix3f_m00 = env->GetFieldID(Matrix3f, "m00", "F");
  135. if (env->ExceptionCheck()) {
  136. env->Throw(env->ExceptionOccurred());
  137. return;
  138. }
  139. Matrix3f_m01 = env->GetFieldID(Matrix3f, "m01", "F");
  140. Matrix3f_m02 = env->GetFieldID(Matrix3f, "m02", "F");
  141. Matrix3f_m10 = env->GetFieldID(Matrix3f, "m10", "F");
  142. Matrix3f_m11 = env->GetFieldID(Matrix3f, "m11", "F");
  143. Matrix3f_m12 = env->GetFieldID(Matrix3f, "m12", "F");
  144. Matrix3f_m20 = env->GetFieldID(Matrix3f, "m20", "F");
  145. Matrix3f_m21 = env->GetFieldID(Matrix3f, "m21", "F");
  146. Matrix3f_m22 = env->GetFieldID(Matrix3f, "m22", "F");
  147. DebugMeshCallback = env->FindClass("com/jme3/bullet/util/DebugMeshCallback");
  148. if (env->ExceptionCheck()) {
  149. env->Throw(env->ExceptionOccurred());
  150. return;
  151. }
  152. DebugMeshCallback_addVector = env->GetMethodID(DebugMeshCallback, "addVector", "(FFFII)V");
  153. if (env->ExceptionCheck()) {
  154. env->Throw(env->ExceptionOccurred());
  155. return;
  156. }
  157. }
  158. void jmeClasses::throwNPE(JNIEnv* env) {
  159. if (env == NULL) return;
  160. jclass newExc = env->FindClass("java/lang/NullPointerException");
  161. env->ThrowNew(newExc, "");
  162. return;
  163. }