Ogrekit共享库main.cpp源码

举报
ShaderJoy 发表于 2021/12/30 00:08:26 2021/12/30
【摘要】 /*-------------------------------------------------------------------------------This file is part of OgreKit.http://gamekit.googlecode.com/Copyright (c) 2006-2010 zcu...


  
  1. /*
  2. -------------------------------------------------------------------------------
  3. This file is part of OgreKit.
  4. http://gamekit.googlecode.com/
  5. Copyright (c) 2006-2010 zcube(JiSeop Moon).
  6. Contributor(s): harkon.kr.
  7. -------------------------------------------------------------------------------
  8. gamekit\Samples\AndroidDemo\Shared\Main.cpp
  9. -------------------------------------------------------------------------------
  10. */
  11. #include "OgreKit.h"
  12. #include "Ogre.h"
  13. #include "android/AndroidInputManager.h"
  14. #include <stdlib.h>
  15. #include <pthread.h>
  16. //extern static void onJavaDetach(void* arg);
  17. #include <jni.h>
  18. #include <stdlib.h>
  19. #include "AndroidLogListener.h"
  20. #include <android/log.h>
  21. #define LOG_TAG "OgreKit"
  22. #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
  23. #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
  24. #define LOG_FOOT LOGI("%s %s %d", __FILE__, __FUNCTION__, __LINE__)
  25. const gkString gkDefaultBlend = "/sdcard/gk_android.blend";
  26. const gkString gkDefaultConfig = "/sdcard/OgreKitStartup.cfg";
  27. class OgreKit : public gkCoreApplication, public gkMessageManager::GenericMessageListener
  28. {
  29. public:
  30. gkString m_blend;
  31. gkScene* m_scene;
  32. OIS::AndroidInputManager* m_input;
  33. public:
  34. OgreKit(): m_blend(gkDefaultBlend), m_scene(0), m_input(0) { };
  35. virtual ~OgreKit() {}
  36. bool init(const gkString& blend,JavaVM* vm);
  37. void keyReleased(const gkKeyboard& key, const gkScanCode& sc);
  38. void injectKey(int action, int uniChar, int keyCode) { if (m_input) m_input->injectKey(action, uniChar, keyCode); }
  39. void injectTouch(int action, float x, float y) { if (m_input) m_input->injectTouch(action, x, y); }
  40. void injectAcceleration(float x,float y, float z) { if (m_input) m_input->injectAcceleration(x,y,z);}
  41. void setOffsets(int x, int y) { if (m_input) m_input->setOffsets(x,y); }
  42. void setWindowSize(int w, int h) { if (m_input) m_input->setWindowSize(w,h); }
  43. void handleMessage(gkMessageManager::Message* message);
  44. JNIEnv* GetEnv();
  45. // jstring JNU_NewStringNative(JNIEnv *env, char *str);
  46. private:
  47. JavaVM* mJVM;
  48. bool setup(void);
  49. jmethodID mFireString;
  50. };
  51. //
  52. //jstring OgreKit::JNU_NewStringNative(JNIEnv *env, char *str)
  53. //{
  54. // jstring result;
  55. // jbyteArray bytes = 0;
  56. // int len;
  57. // if ((env)->EnsureLocalCapacity(2) < 0) {
  58. // return NULL; /* out of memory error */
  59. // }
  60. // len = strlen(str);
  61. // bytes = (env)->NewByteArray(len);
  62. // if (bytes != NULL) {
  63. // (env)->SetByteArrayRegion( bytes, 0, len,
  64. // (jbyte *)str);
  65. // result = (env)->NewObject( Class_java_lang_String,
  66. // MID_String_init, bytes);
  67. // (env)->DeleteLocalRef(bytes);
  68. // return result;
  69. // } /* else fall through */
  70. // return NULL;
  71. //}
  72. void OgreKit::handleMessage(gkMessageManager::Message* message){
  73. LOGI("HANDLE MSG %s ",message->m_subject.c_str());
  74. JNIEnv* env = this->GetEnv();
  75. jclass ANDROID_MAIN = (env)->FindClass( "org/gamekit/jni/Main");
  76. if (!ANDROID_MAIN)
  77. {
  78. LOGI("COULDNT FIND MAIN!!!");
  79. return ;
  80. }
  81. //调用Java静态方法
  82. mFireString = (env)->GetStaticMethodID(ANDROID_MAIN, "fireStringMessage", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
  83. // jstring from = this->JNU_NewStringNative(env,message->m_subject.c_str());
  84. jstring from = env->NewStringUTF(message->m_from.c_str());
  85. jstring to = env->NewStringUTF(message->m_to.c_str());
  86. jstring subject = env->NewStringUTF(message->m_subject.c_str());
  87. jstring body = env->NewStringUTF(message->m_body.c_str());
  88. env->CallStaticVoidMethod(ANDROID_MAIN,mFireString, from,to,subject,body);
  89. }
  90. JNIEnv* OgreKit::GetEnv()
  91. {
  92. JNIEnv* env = NULL;
  93. if (mJVM) (mJVM)->GetEnv((void**)&env, JNI_VERSION_1_2);
  94. return env;
  95. }
  96. bool OgreKit::init(const gkString& blend,JavaVM* vm)
  97. {
  98. gkPrintf("----------- OgreKit Android Demo init -----------------");
  99. LOG_FOOT;
  100. gkString cfgfname;
  101. mJVM = vm;
  102. // Parse command line
  103. m_blend = gkDefaultBlend;
  104. if (!blend.empty()) m_blend = blend;
  105. getPrefs().wintitle = gkString("OgreKit Demo (Press Escape to exit)[") + m_blend + gkString("]");
  106. getPrefs().blendermat=true;
  107. // getPrefs().viewportOrientation="portrait";
  108. // m_prefs.disableSound=false;
  109. gkPath path = cfgfname;
  110. LOG_FOOT;
  111. // overide settings if found
  112. if (path.isFileInBundle())
  113. getPrefs().load(path.getPath());
  114. LOG_FOOT;
  115. bool ok = initialize();
  116. LOG_FOOT;
  117. gkMessageManager::getSingleton().addListener(this);
  118. return ok;
  119. }
  120. bool OgreKit::setup(void)
  121. {
  122. LOG_FOOT;
  123. gkBlendFile* blend = gkBlendLoader::getSingleton().loadFile(gkUtils::getFile(m_blend), gkBlendLoader::LO_ALL_SCENES);
  124. if (!blend)
  125. {
  126. LOGI("File loading failed.\n");
  127. return false;
  128. }
  129. LOG_FOOT;
  130. m_scene = blend->getMainScene();
  131. if (!m_scene)
  132. {
  133. LOGI("No usable scenes found in blend.\n");
  134. return false;
  135. }
  136. LOG_FOOT;
  137. m_scene->createInstance();
  138. LOG_FOOT;
  139. // add input hooks
  140. gkWindow* win = gkWindowSystem::getSingleton().getMainWindow();
  141. m_input = static_cast<OIS::AndroidInputManager*>(win->getInputManager());
  142. LOG_FOOT;
  143. return true;
  144. }
  145. OgreKit okit;
  146. //Ogre::LogManager gLogManager;
  147. AndroidLogListener gLogListener;
  148. gkString file;
  149. jboolean init(JNIEnv* env, jobject thiz, jstring arg)
  150. {
  151. file = gkDefaultBlend;
  152. const char* str = env->GetStringUTFChars(arg, 0);
  153. if (str)
  154. {
  155. file = str;
  156. env->ReleaseStringUTFChars(arg, str);
  157. }
  158. return JNI_TRUE;
  159. }
  160. JavaVM* vmPointer;
  161. jboolean render(JNIEnv* env, jobject thiz, jint drawWidth, jint drawHeight, jboolean forceRedraw)
  162. {
  163. //LOG_FOOT;
  164. static bool first = true;
  165. if (first)
  166. {
  167. first = false;
  168. LOG_FOOT;
  169. okit.getPrefs().winsize.x = drawWidth;
  170. okit.getPrefs().winsize.y = drawHeight;
  171. gkLogger::enable("OgreKitDemo.log", true);
  172. Ogre::LogManager::getSingleton().getDefaultLog()->addListener(&gLogListener);
  173. LOG_FOOT;
  174. // LOGI("****** %s ******", file.c_str());
  175. LOG_FOOT;
  176. okit.getPrefs().verbose = true;
  177. if (!okit.init(file,vmPointer))
  178. {
  179. LOG_FOOT;
  180. // error
  181. return JNI_FALSE;
  182. }
  183. gkLogger::write("window");
  184. gkWindow* win = gkWindowSystem::getSingleton().getMainWindow();
  185. gkLogger::write("window");
  186. okit.m_input = static_cast<OIS::AndroidInputManager*>(win->getInputManager());
  187. gkLogger::write("window");
  188. LOG_FOOT;
  189. gkLogger::write("steploop");
  190. gkEngine::getSingleton().initializeStepLoop();
  191. gkLogger::write("window");
  192. LOG_FOOT;
  193. okit.setWindowSize(drawWidth, drawHeight);
  194. gkLogger::write("window");
  195. }
  196. // gkLogger::write("steponeframe");
  197. if (gkEngine::getSingleton().stepOneFrame())
  198. return JNI_TRUE;
  199. else
  200. return JNI_FALSE;
  201. }
  202. void cleanup(JNIEnv* env)
  203. {
  204. LOG_FOOT;
  205. gkEngine::getSingleton().finalizeStepLoop();
  206. }
  207. jboolean inputEvent(JNIEnv* env, jobject thiz, jint action, jfloat mx, jfloat my)
  208. {
  209. // LOG_FOOT;
  210. okit.injectTouch(action, mx, my);
  211. return JNI_TRUE;
  212. }
  213. jboolean keyEvent(JNIEnv* env, jobject thiz, jint action, jint unicodeChar, jint keyCode, jobject keyEvent)
  214. {
  215. // LOG_FOOT;
  216. okit.injectKey(action, unicodeChar, keyCode);
  217. return JNI_TRUE;
  218. }
  219. void setOffsets(JNIEnv* env, jobject thiz, jint x, jint y)
  220. {
  221. // LOGI("%s %d %d", __FUNCTION__, x, y);
  222. okit.setOffsets(x,y);
  223. }
  224. void sendSensor(JNIEnv *env, jclass thiz, jint type, jfloat x, jfloat y, jfloat z) {
  225. // LOGI("%d %f %f %f", __FUNCTION__, type,x, y,z);
  226. okit.injectAcceleration(x,y,z);
  227. }
  228. /*
  229. * Class: org_gamekit_jni_GameKitJNI
  230. * Method: sendMessage
  231. * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
  232. */
  233. void sendMessage
  234. (JNIEnv *env, jclass thiz, jstring jfrom, jstring jto, jstring jsubject, jstring jbody){
  235. const char* str = env->GetStringUTFChars(jfrom, 0);
  236. gkString from = str;
  237. env->ReleaseStringUTFChars(jfrom,str);
  238. const char* str2 = env->GetStringUTFChars(jto, 0);
  239. gkString to = str2;
  240. env->ReleaseStringUTFChars(jto,str2);
  241. const char* str3 = env->GetStringUTFChars(jsubject, 0);
  242. gkString subject = str3;
  243. env->ReleaseStringUTFChars(jsubject,str3);
  244. const char* str4 = env->GetStringUTFChars(jbody, 0);
  245. gkString body = str4;
  246. env->ReleaseStringUTFChars(jbody,str4);
  247. if (gkMessageManager::getSingletonPtr()) {
  248. gkMessageManager::getSingletonPtr()->sendMessage(from,to,subject,body);
  249. }
  250. // LOGI("%s %s %s %s", from.c_str(),to.c_str(),subject.c_str(),body.c_str());
  251. }
  252. jint JNI_OnLoad(JavaVM* vm, void* reserved)
  253. {
  254. // EXPORT_JNI_OnLoad(vm,reserved);
  255. JNIEnv *env;
  256. vmPointer = vm;
  257. LOGI("JNI_OnLoad called");
  258. if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK)
  259. {
  260. LOGE("Failed to get the environment using GetEnv()");
  261. return -1;
  262. }
  263. JNINativeMethod methods[] =
  264. {
  265. {
  266. "init",
  267. "(Ljava/lang/String;)Z",
  268. (void *) init
  269. },
  270. {
  271. "render",
  272. "(IIZ)Z",
  273. (void *) render
  274. },
  275. {
  276. "inputEvent",
  277. "(IFFLandroid/view/MotionEvent;)Z",
  278. (void *) inputEvent
  279. },
  280. {
  281. "keyEvent",
  282. "(IIILandroid/view/KeyEvent;)Z",
  283. (void *) keyEvent
  284. },
  285. {
  286. "cleanup",
  287. "()V",
  288. (void *) cleanup
  289. },
  290. {
  291. "setOffsets",
  292. "(II)V",
  293. (void *) setOffsets
  294. },
  295. {
  296. "sendSensor",
  297. "(IFFF)V",
  298. (void *) sendSensor
  299. },
  300. {
  301. "sendMessage",
  302. "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V",
  303. (void *) sendMessage
  304. }
  305. };
  306. jclass k;
  307. k = (env)->FindClass ("org/gamekit/jni/Main");
  308. (env)->RegisterNatives(k, methods, 8);
  309. return JNI_VERSION_1_4;
  310. }


文章来源: panda1234lee.blog.csdn.net,作者:panda1234lee,版权归原作者所有,如需转载,请联系作者。

原文链接:panda1234lee.blog.csdn.net/article/details/8261949

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。