T2DActivity.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #import "T2DViewController.h"
  23. #import "platformAndroid/platformGL.h"
  24. #include "platformAndroid/AndroidWindow.h"
  25. #include "platformAndroid/platformAndroid.h"
  26. #include "graphics/dgl.h"
  27. extern AndroidPlatState platState;
  28. #define USE_DEPTH_BUFFER 0
  29. extern int _AndroidRunTorqueMain();
  30. extern bool createMouseMoveEvent(S32 i, S32 x, S32 y, S32 lastX, S32 lastY);
  31. extern bool createMouseDownEvent(S32 touchNumber, S32 x, S32 y, U32 numTouches);
  32. extern bool createMouseUpEvent(S32 touchNumber, S32 x, S32 y, S32 lastX, S32 lastY, U32 numTouches); //EFM
  33. extern void createMouseTapEvent( S32 nbrTaps, S32 x, S32 y );
  34. extern void _AndroidGameInnerLoop();
  35. extern void _AndroidGameResignActive();
  36. extern void _AndroidGameBecomeActive();
  37. extern void _AndroidGameWillTerminate();
  38. // Store current orientation for easy access
  39. extern void _AndroidGameChangeOrientation(S32 newOrientation);
  40. //TODO: android
  41. /*
  42. UIDeviceOrientation currentOrientation;
  43. */
  44. bool _AndroidTorqueFatalError = false;
  45. //-Mat we should update the accelereometer once per frame
  46. extern U32 AccelerometerUpdateMS;
  47. extern void _AndroidGameInnerLoop();
  48. bool createFramebuffer() {
  49. glGenFramebuffersOES(1, &viewFramebuffer);
  50. glGenRenderbuffersOES(1, &viewRenderbuffer);
  51. glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
  52. glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
  53. //TODO: android
  54. //[self.context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)self.view.layer];
  55. glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, viewRenderbuffer);
  56. glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &backingWidth);
  57. glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &backingHeight);
  58. if (USE_DEPTH_BUFFER) {
  59. glGenRenderbuffersOES(1, &depthRenderbuffer);
  60. glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
  61. glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
  62. glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
  63. }
  64. if(glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {
  65. //TODO: android
  66. //NSLog(@"failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
  67. return NO;
  68. }
  69. return YES;
  70. }
  71. void destroyFramebuffer() {
  72. glDeleteFramebuffersOES(1, &viewFramebuffer);
  73. viewFramebuffer = 0;
  74. glDeleteRenderbuffersOES(1, &viewRenderbuffer);
  75. viewRenderbuffer = 0;
  76. if(depthRenderbuffer) {
  77. glDeleteRenderbuffersOES(1, &depthRenderbuffer);
  78. depthRenderbuffer = 0;
  79. }
  80. }
  81. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  82. void finishGLSetup()
  83. {
  84. //TODO: android
  85. //self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
  86. //if (!self.context) {
  87. // NSLog(@"Failed to create ES context");
  88. //}
  89. if( AccelerometerUpdateMS <= 0 ) {
  90. //Luma: This variable needs to be store MS value, not Seconds value
  91. AccelerometerUpdateMS = 33; // 33 ms
  92. }
  93. //[EAGLContext setCurrentContext:self.context];
  94. createFramebuffer();
  95. platState.multipleTouchesEnabled = true;
  96. //[self.view setMultipleTouchEnabled:YES];
  97. _AndroidTorqueFatalError = false;
  98. if(!_AndroidRunTorqueMain( appDelegate, self.view, self ))
  99. {
  100. _AndroidTorqueFatalError = true;
  101. return;
  102. }
  103. }
  104. void finishShutdown()
  105. {
  106. //TODO: android
  107. // Release any retained subviews of the main view.
  108. //if ([EAGLContext currentContext] == self.context) {
  109. // [EAGLContext setCurrentContext:nil];
  110. //}
  111. //self.context = nil;
  112. }
  113. void update()
  114. {
  115. _AndroidGameInnerLoop();
  116. }
  117. extern Vector<Event *> TouchMoveEvents;
  118. Vector<Point2I> lastTouches;
  119. // Handle touch and keyboard input from android OS
  120. static int32_t engine_handle_input(struct android_app* app, AInputEvent* event) {
  121. struct engine* engine = (struct engine*)app->userData;
  122. if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) {
  123. if ((AMotionEvent_getAction(event) & AMOTION_EVENT_ACTION_MASK) == AMOTION_EVENT_ACTION_DOWN) {
  124. size_t touchCount = AMotionEvent_getPointerCount(event);
  125. for (U8 i = 0; i < touchCount; i++)
  126. {
  127. Point2I point;
  128. point.x = AMotionEvent_getX(event, i);
  129. point.y = AMotionEvent_getY(event, i);
  130. if (lastTouches.size() < i)
  131. lastTouches.push_back(point);
  132. else
  133. {
  134. lastTouches[i].x = point.x;
  135. lastTouches[i].y = point.y;
  136. }
  137. S32 orientation = _AndroidGameGetOrientation();
  138. if (UIDeviceOrientationIsPortrait(orientation))
  139. {
  140. point.y -= _AndroidGetPortraitTouchoffset();
  141. }
  142. createMouseDownEvent(i, point.x, point.y, touchCount);
  143. }
  144. }
  145. if ((AMotionEvent_getAction(event) & AMOTION_EVENT_ACTION_MASK) == AMOTION_EVENT_ACTION_UP) {
  146. size_t touchCount = AMotionEvent_getPointerCount(event);
  147. for (U8 i = 0; i < touchCount; i++)
  148. {
  149. Point2I point;
  150. point.x = AMotionEvent_getX(event, i);
  151. point.y = AMotionEvent_getY(event, i);
  152. Point2I prevPoint = lastTouches[i];
  153. S32 orientation = _AndroidGameGetOrientation();
  154. if (UIDeviceOrientationIsPortrait(orientation))
  155. {
  156. point.y -= _AndroidGetPortraitTouchoffset();
  157. prevPoint.y -= _AndroidGetPortraitTouchoffset();
  158. }
  159. createMouseUpEvent(i, point.x, point.y, prevPoint.x, prevPoint.y, touchCount);
  160. //Luma: Tap support
  161. if (touchCount > 0)
  162. {
  163. // this was a tap, so create a tap event
  164. createMouseTapEvent(touchCount, (int) point.x, (int) point.y);
  165. }
  166. }
  167. }
  168. if ((AMotionEvent_getAction(event) & AMOTION_EVENT_ACTION_MASK) == AMOTION_EVENT_ACTION_MOVE) {
  169. size_t touchCount = AMotionEvent_getPointerCount(event);
  170. for (U8 i = 0; i < touchCount; i++)
  171. {
  172. Point2I point;
  173. point.x = AMotionEvent_getX(event, i);
  174. point.y = AMotionEvent_getY(event, i);
  175. Point2I prevPoint = lastTouches[i];
  176. S32 orientation = _AndroidGameGetOrientation();
  177. if (UIDeviceOrientationIsPortrait(orientation))
  178. {
  179. point.y -= _AndroidGetPortraitTouchoffset();
  180. prevPoint.y -= _AndroidGetPortraitTouchoffset();
  181. }
  182. createMouseMoveEvent(i, point.x, point.y, prevPoint.x, prevPoint.y);
  183. lastTouches[i].x = point.x;
  184. lastTouches[i].y = point.y;
  185. }
  186. }
  187. if ((AMotionEvent_getAction(event) & AMOTION_EVENT_ACTION_MASK) == AMOTION_EVENT_ACTION_CANCEL) {
  188. size_t touchCount = AMotionEvent_getPointerCount(event);
  189. for (U8 i = 0; i < touchCount; i++)
  190. {
  191. Point2I point;
  192. point.x = AMotionEvent_getX(event, i);
  193. point.y = AMotionEvent_getY(event, i);
  194. Point2I prevPoint = lastTouches[i];
  195. S32 orientation = _AndroidGameGetOrientation();
  196. if (UIDeviceOrientationIsPortrait(orientation))
  197. {
  198. point.y -= _AndroidGetPortraitTouchoffset();
  199. prevPoint.y -= _AndroidGetPortraitTouchoffset();
  200. }
  201. createMouseUpEvent(i, point.x, point.y, prevPoint.x, prevPoint.y, touchCount);
  202. //Luma: Tap support
  203. if (touchCount > 0)
  204. {
  205. // this was a tap, so create a tap event
  206. createMouseTapEvent(touchCount, (int) point.x, (int) point.y);
  207. }
  208. }
  209. }
  210. return 1;
  211. } else if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_KEY) {
  212. int action = AKeyEvent_getAction(event);
  213. int key_val = AKeyEvent_getKeyCode(event);
  214. int metastate = AKeyEvent_getMetaState(event);
  215. switch(action)
  216. {
  217. case AKEY_EVENT_ACTION_DOWN:
  218. if (key_val == AKEYCODE_VOLUME_UP) {
  219. //TODO: android
  220. //ChangeVolume(true);
  221. } else if (key_val == AKEYCODE_VOLUME_DOWN) {
  222. //TODO: android
  223. //ChangeVolume(false);
  224. } else if (key_val == AKEYCODE_BACK) {
  225. //android back button
  226. } else {
  227. //TODO: android
  228. //convertAndroidToWindowsKeyCode(key_val);
  229. }
  230. break;
  231. case AKEY_EVENT_ACTION_UP:
  232. if (key_val == AKEYCODE_VOLUME_UP) {
  233. //TODO: android
  234. //ChangeVolume(true);
  235. } else if (key_val == AKEYCODE_VOLUME_DOWN) {
  236. //TODO: android
  237. //ChangeVolume(false);
  238. } else if (key_val == AKEYCODE_BACK) {
  239. //android back button
  240. } else {
  241. //TODO: android
  242. //convertAndroidToWindowsKeyCode(key_val);
  243. }
  244. break;
  245. }
  246. return 1;
  247. }
  248. return 0;
  249. }
  250. int _AndroidGetScreenWidth() {
  251. return engine.width;
  252. }
  253. int _AndroidGetScreenHeight() {
  254. return engine.height;
  255. }
  256. bool _AndroidGetFileDescriptor(const char* fileName, int32_t *mDescriptor, off_t *mStart, off_t* mLength) {
  257. AAsset* asset = AAssetManager_open(engine.app->activity->assetManager, fileName, AASSET_MODE_UNKNOWN);
  258. if (asset != NULL) {
  259. *mDescriptor = AAsset_openFileDescriptor(asset, mStart, mLength);
  260. AAsset_close(asset);
  261. return true;
  262. }
  263. *mDescriptor = 0;
  264. *mStart = 0;
  265. *mLength = 0;
  266. return false;
  267. }
  268. char* _AndroidLoadFile(const char* fileName, int *size) {
  269. AAsset *asset;
  270. uint8_t buf[1024];
  271. char* buffer = NULL;
  272. *size = 0;
  273. asset = AAssetManager_open(engine.app->activity->assetManager, fileName, AASSET_MODE_UNKNOWN);
  274. if(asset != NULL){
  275. *size = AAsset_getLength(asset);
  276. if (*size <= 0)
  277. return NULL;
  278. buffer = new char[*size + 1];
  279. int count = 0;
  280. while(true)
  281. {
  282. int read = AAsset_read(asset, buf, 1024);
  283. if (read <= 0)
  284. break;
  285. memcpy(buffer+count,buf, read);
  286. count += read;
  287. }
  288. buffer[*size] = '\0';
  289. AAsset_close(asset);
  290. }
  291. return buffer;
  292. }
  293. void _AndroidGetDeviceIPAddress(char* address) {
  294. int fd;
  295. struct ifreq ifr;
  296. strcpy(address, "error");
  297. // Attaches the current thread to the JVM.
  298. jint lResult;
  299. jint lFlags = 0;
  300. JavaVM* lJavaVM = engine.app->activity->vm;
  301. JNIEnv* lJNIEnv = engine.app->activity->env;
  302. JavaVMAttachArgs lJavaVMAttachArgs;
  303. lJavaVMAttachArgs.version = JNI_VERSION_1_6;
  304. lJavaVMAttachArgs.name = "NativeThread";
  305. lJavaVMAttachArgs.group = NULL;
  306. lResult=lJavaVM->AttachCurrentThread(&lJNIEnv, &lJavaVMAttachArgs);
  307. if (lResult == JNI_ERR) {
  308. return;
  309. }
  310. // Retrieves NativeActivity.
  311. jobject lNativeActivity = engine.app->activity->clazz;
  312. jclass ClassNativeActivity = lJNIEnv->GetObjectClass(lNativeActivity);
  313. // Retrieves Context.INPUT_METHOD_SERVICE.
  314. jclass ClassContext = lJNIEnv->FindClass("android/content/Context");
  315. jfieldID FieldWIFI_SERVICE =
  316. lJNIEnv->GetStaticFieldID(ClassContext,
  317. "WIFI_SERVICE", "Ljava/lang/String;");
  318. jobject WIFI_SERVICE =
  319. lJNIEnv->GetStaticObjectField(ClassContext,
  320. FieldWIFI_SERVICE);
  321. // Runs getSystemService(Context.WIFI_SERVICE).
  322. jclass ClassWifiManager = lJNIEnv->FindClass(
  323. "android/net/wifi/WifiManager");
  324. jclass ClassWifiInfo = lJNIEnv->FindClass(
  325. "android/net/wifi/WifiInfo");
  326. jmethodID MethodGetSystemService = lJNIEnv->GetMethodID(
  327. ClassNativeActivity, "getSystemService",
  328. "(Ljava/lang/String;)Ljava/lang/Object;");
  329. jobject lWifiManager = lJNIEnv->CallObjectMethod(
  330. lNativeActivity, MethodGetSystemService,
  331. WIFI_SERVICE);
  332. // Runs wifiManager.getConnectionInfo()
  333. jmethodID MethodGetConnectionInfo = lJNIEnv->GetMethodID(
  334. ClassWifiManager, "getConnectionInfo",
  335. "()Landroid/net/wifi/WifiInfo;");
  336. jobject lWifiInfo = lJNIEnv->CallObjectMethod(
  337. lWifiManager, MethodGetConnectionInfo);
  338. // Runs wifiInfo.getIpAddress()
  339. jmethodID MethodGetIPAddress = lJNIEnv->GetMethodID(
  340. ClassWifiInfo, "getIpAddress",
  341. "()I");
  342. jint lIPAddress = lJNIEnv->CallIntMethod(
  343. lWifiInfo, MethodGetIPAddress);
  344. int ip = lIPAddress;
  345. // Finished with the JVM.
  346. lJavaVM->DetachCurrentThread();
  347. char buffer[32];
  348. sprintf(buffer, "%d.%d.%d.%d", (ip & 0xFF), (ip >> 8 & 0xFF), (ip >> 16 & 0xFF), (ip >> 24 & 0xFF));
  349. strcpy(address, buffer);
  350. }
  351. - (void)applicationDidFinishLaunching:(UIApplication *)application {
  352. [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
  353. //Also we set the currentRotation up so its not invalid
  354. currentOrientation = [UIDevice currentDevice].orientation;
  355. //So we make a selector to handle that, called didRotate (lower down in the code)
  356. [[NSNotificationCenter defaultCenter] addObserver:self
  357. selector:@selector(didRotate:)
  358. name:UIDeviceOrientationDidChangeNotification
  359. object:nil];
  360. }
  361. - (void)applicationWillResignActive:(UIApplication *)application
  362. {
  363. _AndroidGameResignActive();
  364. }
  365. - (void)applicationDidBecomeActive:(UIApplication *)application
  366. {
  367. if(!_AndroidTorqueFatalError)
  368. _AndroidGameBecomeActive();
  369. }
  370. - (void)applicationWillTerminate:(UIApplication *)application
  371. {
  372. _AndroidGameWillTerminate();
  373. [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
  374. }
  375. - (void)didRotate:(NSNotification *)notification
  376. {
  377. //Default to landscape left
  378. UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
  379. if(currentOrientation != orientation)
  380. {
  381. //Change the orientation
  382. currentOrientation = orientation;
  383. //Tell the rest of the engine
  384. _AndroidGameChangeOrientation(currentOrientation);
  385. }
  386. }
  387. - (void) runMainLoop
  388. {
  389. _AndroidGameInnerLoop();
  390. }
  391. */
  392. void supportLandscape( bool enable)
  393. {
  394. //TODO: android
  395. //platState.viewController->mOrientationLandscapeLeftSupported = enable;
  396. //platState.viewController->mOrientationLandscapeRightSupported = enable;
  397. }
  398. ConsoleFunction(supportLandscape, void, 2, 2, "supportLandscape( bool ) "
  399. "enable or disable Landscape")
  400. {
  401. bool enable = true;
  402. if( argc > 1 )
  403. enable = dAtob( argv[1] );
  404. supportLandscape(enable);
  405. }
  406. void supportPortrait( bool enable )
  407. {
  408. //TODO: android
  409. //platState.viewController->mOrientationPortraitSupported = enable;
  410. //platState.viewController->mOrientationPortraitUpsideDownSupported = enable;
  411. }
  412. ConsoleFunction(supportPortrait, void, 2, 2, "supportPortrait( bool ) "
  413. "enable or disable portrait")
  414. {
  415. bool enable = true;
  416. if( argc > 1 )
  417. enable = dAtob( argv[1] );
  418. supportPortrait(enable);
  419. }