PlatformQNX.cpp 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  1. #ifdef __QNX__
  2. #include "Base.h"
  3. #include "Platform.h"
  4. #include "FileSystem.h"
  5. #include "Game.h"
  6. #include <sys/keycodes.h>
  7. #include <screen/screen.h>
  8. #include <input/screen_helpers.h>
  9. #include <bps/bps.h>
  10. #include <bps/event.h>
  11. #include <bps/screen.h>
  12. #include <bps/navigator.h>
  13. #include <bps/accelerometer.h>
  14. #include <bps/orientation.h>
  15. #include <bps/virtualkeyboard.h>
  16. #define TOUCH_COUNT_MAX 4
  17. using namespace std;
  18. struct timespec __timespec;
  19. static long __timeStart;
  20. static long __timeAbsolute;
  21. static bool __vsync = WINDOW_VSYNC;
  22. static screen_context_t __screenContext;
  23. static screen_window_t __screenWindow;
  24. static screen_event_t __screenEvent;
  25. static int __screenWindowSize[2];
  26. static EGLDisplay __eglDisplay = EGL_NO_DISPLAY;
  27. static EGLContext __eglContext = EGL_NO_CONTEXT;
  28. static EGLSurface __eglSurface = EGL_NO_SURFACE;
  29. static EGLConfig __eglConfig = 0;
  30. static int __orientationAngle;
  31. static bool __multiTouch = false;
  32. static const char* __glExtensions;
  33. PFNGLBINDVERTEXARRAYOESPROC glBindVertexArray = NULL;
  34. PFNGLDELETEVERTEXARRAYSOESPROC glDeleteVertexArrays = NULL;
  35. PFNGLGENVERTEXARRAYSOESPROC glGenVertexArrays = NULL;
  36. PFNGLISVERTEXARRAYOESPROC glIsVertexArray = NULL;
  37. namespace gameplay
  38. {
  39. // Gets the Keyboard::Key enumeration constant that corresponds to the given QNX key code.
  40. static Keyboard::Key getKey(int qnxKeycode)
  41. {
  42. switch (qnxKeycode)
  43. {
  44. case KEYCODE_SYSREQ:
  45. return Keyboard::KEY_SYSREQ;
  46. case KEYCODE_BREAK:
  47. return Keyboard::KEY_BREAK;
  48. case KEYCODE_MENU:
  49. return Keyboard::KEY_MENU;
  50. case KEYCODE_KP_ENTER:
  51. return Keyboard::KEY_KP_ENTER;
  52. case KEYCODE_PAUSE:
  53. return Keyboard::KEY_PAUSE;
  54. case KEYCODE_SCROLL_LOCK:
  55. return Keyboard::KEY_SCROLL_LOCK;
  56. case KEYCODE_PRINT:
  57. return Keyboard::KEY_PRINT;
  58. case KEYCODE_ESCAPE:
  59. return Keyboard::KEY_ESCAPE;
  60. case KEYCODE_BACKSPACE:
  61. return Keyboard::KEY_BACKSPACE;
  62. case KEYCODE_BACK_TAB:
  63. return Keyboard::KEY_BACK_TAB;
  64. case KEYCODE_TAB:
  65. return Keyboard::KEY_TAB;
  66. case KEYCODE_RETURN:
  67. return Keyboard::KEY_RETURN;
  68. case KEYCODE_CAPS_LOCK:
  69. return Keyboard::KEY_CAPS_LOCK;
  70. case KEYCODE_LEFT_SHIFT:
  71. case KEYCODE_RIGHT_SHIFT:
  72. return Keyboard::KEY_SHIFT;
  73. case KEYCODE_LEFT_CTRL:
  74. case KEYCODE_RIGHT_CTRL:
  75. return Keyboard::KEY_CTRL;
  76. case KEYCODE_LEFT_ALT:
  77. case KEYCODE_RIGHT_ALT:
  78. return Keyboard::KEY_ALT;
  79. case KEYCODE_LEFT_HYPER:
  80. case KEYCODE_RIGHT_HYPER:
  81. return Keyboard::KEY_HYPER;
  82. case KEYCODE_INSERT:
  83. return Keyboard::KEY_INSERT;
  84. case KEYCODE_HOME:
  85. return Keyboard::KEY_HOME;
  86. case KEYCODE_PG_UP:
  87. return Keyboard::KEY_PG_UP;
  88. case KEYCODE_DELETE:
  89. return Keyboard::KEY_DELETE;
  90. case KEYCODE_END:
  91. return Keyboard::KEY_END;
  92. case KEYCODE_PG_DOWN:
  93. return Keyboard::KEY_PG_DOWN;
  94. case KEYCODE_LEFT:
  95. return Keyboard::KEY_LEFT_ARROW;
  96. case KEYCODE_RIGHT:
  97. return Keyboard::KEY_RIGHT_ARROW;
  98. case KEYCODE_UP:
  99. return Keyboard::KEY_UP_ARROW;
  100. case KEYCODE_DOWN:
  101. return Keyboard::KEY_DOWN_ARROW;
  102. case KEYCODE_NUM_LOCK:
  103. return Keyboard::KEY_NUM_LOCK;
  104. case KEYCODE_KP_PLUS:
  105. return Keyboard::KEY_KP_PLUS;
  106. case KEYCODE_KP_MINUS:
  107. return Keyboard::KEY_KP_MINUS;
  108. case KEYCODE_KP_MULTIPLY:
  109. return Keyboard::KEY_KP_MULTIPLY;
  110. case KEYCODE_KP_DIVIDE:
  111. return Keyboard::KEY_KP_DIVIDE;
  112. case KEYCODE_KP_HOME:
  113. return Keyboard::KEY_KP_HOME;
  114. case KEYCODE_KP_UP:
  115. return Keyboard::KEY_KP_UP;
  116. case KEYCODE_KP_PG_UP:
  117. return Keyboard::KEY_KP_PG_UP;
  118. case KEYCODE_KP_LEFT:
  119. return Keyboard::KEY_KP_LEFT;
  120. case KEYCODE_KP_FIVE:
  121. return Keyboard::KEY_KP_FIVE;
  122. case KEYCODE_KP_RIGHT:
  123. return Keyboard::KEY_KP_RIGHT;
  124. case KEYCODE_KP_END:
  125. return Keyboard::KEY_KP_END;
  126. case KEYCODE_KP_DOWN:
  127. return Keyboard::KEY_KP_DOWN;
  128. case KEYCODE_KP_PG_DOWN:
  129. return Keyboard::KEY_KP_PG_DOWN;
  130. case KEYCODE_KP_INSERT:
  131. return Keyboard::KEY_KP_INSERT;
  132. case KEYCODE_KP_DELETE:
  133. return Keyboard::KEY_KP_DELETE;
  134. case KEYCODE_F1:
  135. return Keyboard::KEY_F1;
  136. case KEYCODE_F2:
  137. return Keyboard::KEY_F2;
  138. case KEYCODE_F3:
  139. return Keyboard::KEY_F3;
  140. case KEYCODE_F4:
  141. return Keyboard::KEY_F4;
  142. case KEYCODE_F5:
  143. return Keyboard::KEY_F5;
  144. case KEYCODE_F6:
  145. return Keyboard::KEY_F6;
  146. case KEYCODE_F7:
  147. return Keyboard::KEY_F7;
  148. case KEYCODE_F8:
  149. return Keyboard::KEY_F8;
  150. case KEYCODE_F9:
  151. return Keyboard::KEY_F9;
  152. case KEYCODE_F10:
  153. return Keyboard::KEY_F10;
  154. case KEYCODE_F11:
  155. return Keyboard::KEY_F11;
  156. case KEYCODE_F12:
  157. return Keyboard::KEY_F12;
  158. case KEYCODE_SPACE:
  159. return Keyboard::KEY_SPACE;
  160. case KEYCODE_RIGHT_PAREN:
  161. return Keyboard::KEY_RIGHT_PARENTHESIS;
  162. case KEYCODE_ZERO:
  163. return Keyboard::KEY_ZERO;
  164. case KEYCODE_EXCLAM:
  165. return Keyboard::KEY_EXCLAM;
  166. case KEYCODE_ONE:
  167. return Keyboard::KEY_ONE;
  168. case KEYCODE_AT:
  169. return Keyboard::KEY_AT;
  170. case KEYCODE_TWO:
  171. return Keyboard::KEY_TWO;
  172. case KEYCODE_NUMBER:
  173. return Keyboard::KEY_NUMBER;
  174. case KEYCODE_THREE:
  175. return Keyboard::KEY_THREE;
  176. case KEYCODE_DOLLAR:
  177. return Keyboard::KEY_DOLLAR;
  178. case KEYCODE_FOUR:
  179. return Keyboard::KEY_FOUR;
  180. case KEYCODE_PERCENT:
  181. return Keyboard::KEY_PERCENT;
  182. case KEYCODE_FIVE:
  183. return Keyboard::KEY_FIVE;
  184. case KEYCODE_CIRCUMFLEX:
  185. return Keyboard::KEY_CIRCUMFLEX;
  186. case KEYCODE_SIX:
  187. return Keyboard::KEY_SIX;
  188. case KEYCODE_AMPERSAND:
  189. return Keyboard::KEY_AMPERSAND;
  190. case KEYCODE_SEVEN:
  191. return Keyboard::KEY_SEVEN;
  192. case KEYCODE_ASTERISK:
  193. return Keyboard::KEY_ASTERISK;
  194. case KEYCODE_EIGHT:
  195. return Keyboard::KEY_EIGHT;
  196. case KEYCODE_LEFT_PAREN:
  197. return Keyboard::KEY_LEFT_PARENTHESIS;
  198. case KEYCODE_NINE:
  199. return Keyboard::KEY_NINE;
  200. case KEYCODE_EQUAL:
  201. return Keyboard::KEY_EQUAL;
  202. case KEYCODE_PLUS:
  203. return Keyboard::KEY_PLUS;
  204. case KEYCODE_LESS_THAN:
  205. return Keyboard::KEY_LESS_THAN;
  206. case KEYCODE_COMMA:
  207. return Keyboard::KEY_COMMA;
  208. case KEYCODE_UNDERSCORE:
  209. return Keyboard::KEY_UNDERSCORE;
  210. case KEYCODE_MINUS:
  211. return Keyboard::KEY_MINUS;
  212. case KEYCODE_GREATER_THAN:
  213. return Keyboard::KEY_GREATER_THAN;
  214. case KEYCODE_PERIOD:
  215. return Keyboard::KEY_PERIOD;
  216. case KEYCODE_COLON:
  217. return Keyboard::KEY_COLON;
  218. case KEYCODE_SEMICOLON:
  219. return Keyboard::KEY_SEMICOLON;
  220. case KEYCODE_QUESTION:
  221. return Keyboard::KEY_QUESTION;
  222. case KEYCODE_SLASH:
  223. return Keyboard::KEY_SLASH;
  224. case KEYCODE_GRAVE:
  225. return Keyboard::KEY_GRAVE;
  226. case KEYCODE_TILDE:
  227. return Keyboard::KEY_TILDE;
  228. case KEYCODE_LEFT_BRACE:
  229. return Keyboard::KEY_LEFT_BRACE;
  230. case KEYCODE_LEFT_BRACKET:
  231. return Keyboard::KEY_LEFT_BRACKET;
  232. case KEYCODE_BAR:
  233. return Keyboard::KEY_BAR;
  234. case KEYCODE_BACK_SLASH:
  235. return Keyboard::KEY_BACK_SLASH;
  236. case KEYCODE_RIGHT_BRACE:
  237. return Keyboard::KEY_RIGHT_BRACE;
  238. case KEYCODE_RIGHT_BRACKET:
  239. return Keyboard::KEY_RIGHT_BRACKET;
  240. case KEYCODE_QUOTE:
  241. return Keyboard::KEY_QUOTE;
  242. case KEYCODE_APOSTROPHE:
  243. return Keyboard::KEY_APOSTROPHE;
  244. case 0x20AC:
  245. return Keyboard::KEY_EURO;
  246. case KEYCODE_POUND_SIGN:
  247. return Keyboard::KEY_POUND;
  248. case KEYCODE_YEN_SIGN:
  249. return Keyboard::KEY_YEN;
  250. case KEYCODE_MIDDLE_DOT:
  251. return Keyboard::KEY_MIDDLE_DOT;
  252. case KEYCODE_CAPITAL_A:
  253. return Keyboard::KEY_CAPITAL_A;
  254. case KEYCODE_A:
  255. return Keyboard::KEY_A;
  256. case KEYCODE_CAPITAL_B:
  257. return Keyboard::KEY_CAPITAL_B;
  258. case KEYCODE_B:
  259. return Keyboard::KEY_B;
  260. case KEYCODE_CAPITAL_C:
  261. return Keyboard::KEY_CAPITAL_C;
  262. case KEYCODE_C:
  263. return Keyboard::KEY_C;
  264. case KEYCODE_CAPITAL_D:
  265. return Keyboard::KEY_CAPITAL_D;
  266. case KEYCODE_D:
  267. return Keyboard::KEY_D;
  268. case KEYCODE_CAPITAL_E:
  269. return Keyboard::KEY_CAPITAL_E;
  270. case KEYCODE_E:
  271. return Keyboard::KEY_E;
  272. case KEYCODE_CAPITAL_F:
  273. return Keyboard::KEY_CAPITAL_F;
  274. case KEYCODE_F:
  275. return Keyboard::KEY_F;
  276. case KEYCODE_CAPITAL_G:
  277. return Keyboard::KEY_CAPITAL_G;
  278. case KEYCODE_G:
  279. return Keyboard::KEY_G;
  280. case KEYCODE_CAPITAL_H:
  281. return Keyboard::KEY_CAPITAL_H;
  282. case KEYCODE_H:
  283. return Keyboard::KEY_H;
  284. case KEYCODE_CAPITAL_I:
  285. return Keyboard::KEY_CAPITAL_I;
  286. case KEYCODE_I:
  287. return Keyboard::KEY_I;
  288. case KEYCODE_CAPITAL_J:
  289. return Keyboard::KEY_CAPITAL_J;
  290. case KEYCODE_J:
  291. return Keyboard::KEY_J;
  292. case KEYCODE_CAPITAL_K:
  293. return Keyboard::KEY_CAPITAL_K;
  294. case KEYCODE_K:
  295. return Keyboard::KEY_K;
  296. case KEYCODE_CAPITAL_L:
  297. return Keyboard::KEY_CAPITAL_L;
  298. case KEYCODE_L:
  299. return Keyboard::KEY_L;
  300. case KEYCODE_CAPITAL_M:
  301. return Keyboard::KEY_CAPITAL_M;
  302. case KEYCODE_M:
  303. return Keyboard::KEY_M;
  304. case KEYCODE_CAPITAL_N:
  305. return Keyboard::KEY_CAPITAL_N;
  306. case KEYCODE_N:
  307. return Keyboard::KEY_N;
  308. case KEYCODE_CAPITAL_O:
  309. return Keyboard::KEY_CAPITAL_O;
  310. case KEYCODE_O:
  311. return Keyboard::KEY_O;
  312. case KEYCODE_CAPITAL_P:
  313. return Keyboard::KEY_CAPITAL_P;
  314. case KEYCODE_P:
  315. return Keyboard::KEY_P;
  316. case KEYCODE_CAPITAL_Q:
  317. return Keyboard::KEY_CAPITAL_Q;
  318. case KEYCODE_Q:
  319. return Keyboard::KEY_Q;
  320. case KEYCODE_CAPITAL_R:
  321. return Keyboard::KEY_CAPITAL_R;
  322. case KEYCODE_R:
  323. return Keyboard::KEY_R;
  324. case KEYCODE_CAPITAL_S:
  325. return Keyboard::KEY_CAPITAL_S;
  326. case KEYCODE_S:
  327. return Keyboard::KEY_S;
  328. case KEYCODE_CAPITAL_T:
  329. return Keyboard::KEY_CAPITAL_T;
  330. case KEYCODE_T:
  331. return Keyboard::KEY_T;
  332. case KEYCODE_CAPITAL_U:
  333. return Keyboard::KEY_CAPITAL_U;
  334. case KEYCODE_U:
  335. return Keyboard::KEY_U;
  336. case KEYCODE_CAPITAL_V:
  337. return Keyboard::KEY_CAPITAL_V;
  338. case KEYCODE_V:
  339. return Keyboard::KEY_V;
  340. case KEYCODE_CAPITAL_W:
  341. return Keyboard::KEY_CAPITAL_W;
  342. case KEYCODE_W:
  343. return Keyboard::KEY_W;
  344. case KEYCODE_CAPITAL_X:
  345. return Keyboard::KEY_CAPITAL_X;
  346. case KEYCODE_X:
  347. return Keyboard::KEY_X;
  348. case KEYCODE_CAPITAL_Y:
  349. return Keyboard::KEY_CAPITAL_Y;
  350. case KEYCODE_Y:
  351. return Keyboard::KEY_Y;
  352. case KEYCODE_CAPITAL_Z:
  353. return Keyboard::KEY_CAPITAL_Z;
  354. case KEYCODE_Z:
  355. return Keyboard::KEY_Z;
  356. default:
  357. return Keyboard::KEY_NONE;
  358. }
  359. }
  360. /**
  361. * Returns the unicode value from the given QNX key code value.
  362. * Some non-printable characters also have corresponding unicode values, such as backspace.
  363. *
  364. * @param qnxKeyCode The keyboard key code.
  365. *
  366. * @return The unicode value or 0 if the keycode did not represent a unicode key.
  367. */
  368. static int getUnicode(int qnxKeyCode)
  369. {
  370. if (qnxKeyCode >= KEYCODE_PC_KEYS && qnxKeyCode <= UNICODE_PRIVATE_USE_AREA_LAST)
  371. {
  372. switch (qnxKeyCode)
  373. {
  374. case KEYCODE_BACKSPACE:
  375. return 0x0008;
  376. case KEYCODE_TAB:
  377. return 0x0009;
  378. case KEYCODE_KP_ENTER:
  379. case KEYCODE_RETURN:
  380. return 0x000A;
  381. case KEYCODE_ESCAPE:
  382. return 0x001B;
  383. // Win32 doesn't consider delete to be a key char.
  384. default:
  385. return 0;
  386. }
  387. }
  388. return qnxKeyCode;
  389. }
  390. extern void printError(const char* format, ...)
  391. {
  392. va_list argptr;
  393. va_start(argptr, format);
  394. vfprintf(stderr, format, argptr);
  395. fprintf(stderr, "\n");
  396. va_end(argptr);
  397. }
  398. EGLenum checkErrorEGL(const char* msg)
  399. {
  400. static const char* errmsg[] =
  401. {
  402. "EGL function succeeded",
  403. "EGL is not initialized, or could not be initialized, for the specified display",
  404. "EGL cannot access a requested resource",
  405. "EGL failed to allocate resources for the requested operation",
  406. "EGL fail to access an unrecognized attribute or attribute value was passed in an attribute list",
  407. "EGLConfig argument does not name a valid EGLConfig",
  408. "EGLContext argument does not name a valid EGLContext",
  409. "EGL current surface of the calling thread is no longer valid",
  410. "EGLDisplay argument does not name a valid EGLDisplay",
  411. "EGL arguments are inconsistent",
  412. "EGLNativePixmapType argument does not refer to a valid native pixmap",
  413. "EGLNativeWindowType argument does not refer to a valid native window",
  414. "EGL one or more argument values are invalid",
  415. "EGLSurface argument does not name a valid surface configured for rendering",
  416. "EGL power management event has occurred",
  417. };
  418. EGLenum error = eglGetError();
  419. fprintf(stderr, "%s: %s\n", msg, errmsg[error - EGL_SUCCESS]);
  420. return error;
  421. }
  422. Platform::Platform(Game* game)
  423. : _game(game)
  424. {
  425. }
  426. Platform::Platform(const Platform& copy)
  427. {
  428. // hidden
  429. }
  430. Platform::~Platform()
  431. {
  432. if (__eglDisplay != EGL_NO_DISPLAY)
  433. {
  434. eglMakeCurrent(__eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  435. }
  436. if (__eglSurface != EGL_NO_SURFACE)
  437. {
  438. eglDestroySurface(__eglDisplay, __eglSurface);
  439. __eglSurface = EGL_NO_SURFACE;
  440. }
  441. if (__eglContext != EGL_NO_CONTEXT)
  442. {
  443. eglDestroyContext(__eglDisplay, __eglContext);
  444. __eglContext = EGL_NO_CONTEXT;
  445. }
  446. if (__eglDisplay != EGL_NO_DISPLAY)
  447. {
  448. eglTerminate(__eglDisplay);
  449. __eglDisplay = EGL_NO_DISPLAY;
  450. }
  451. if (__screenWindow)
  452. {
  453. screen_destroy_window(__screenWindow);
  454. __screenWindow = NULL;
  455. }
  456. if (__screenEvent)
  457. {
  458. screen_destroy_event(__screenEvent);
  459. __screenEvent = NULL;
  460. }
  461. if (__screenContext)
  462. {
  463. screen_destroy_context(__screenContext);
  464. __screenContext = NULL;
  465. }
  466. }
  467. Platform* Platform::create(Game* game)
  468. {
  469. FileSystem::setResourcePath("./app/native/");
  470. Platform* platform = new Platform(game);
  471. bps_initialize();
  472. accelerometer_set_update_frequency(FREQ_40_HZ);
  473. navigator_request_events(0);
  474. navigator_rotation_lock(true);
  475. // Determine initial orientation angle.
  476. orientation_direction_t direction;
  477. orientation_get(&direction, &__orientationAngle);
  478. int rc = 0;
  479. int screenFormat = SCREEN_FORMAT_RGBA8888;
  480. int screenUsage = SCREEN_USAGE_DISPLAY|SCREEN_USAGE_OPENGL_ES2;
  481. int screenSwapInterval = WINDOW_VSYNC ? 1 : 0;
  482. int screenTransparency = SCREEN_TRANSPARENCY_NONE;
  483. int angle = atoi(getenv("ORIENTATION"));
  484. // Hard-coded to (0,0).
  485. int windowPosition[] =
  486. {
  487. 0, 0
  488. };
  489. EGLint eglConfigCount;
  490. // Hard-coded to 32-bit/OpenGL ES 2.0.
  491. const EGLint eglConfigAttrs[] =
  492. {
  493. EGL_RED_SIZE, 8,
  494. EGL_GREEN_SIZE, 8,
  495. EGL_BLUE_SIZE, 8,
  496. EGL_ALPHA_SIZE, 8,
  497. EGL_DEPTH_SIZE, 24,
  498. EGL_STENCIL_SIZE, 8,
  499. EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
  500. EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
  501. EGL_NONE
  502. };
  503. const EGLint eglContextAttrs[] =
  504. {
  505. EGL_CONTEXT_CLIENT_VERSION, 2,
  506. EGL_NONE
  507. };
  508. const EGLint eglSurfaceAttrs[] =
  509. {
  510. EGL_RENDER_BUFFER, EGL_BACK_BUFFER,
  511. EGL_NONE
  512. };
  513. // Create the screen context.
  514. rc = screen_create_context(&__screenContext, 0);
  515. if (rc)
  516. {
  517. perror("screen_create_context");
  518. goto error;
  519. }
  520. // Create the screen window.
  521. rc = screen_create_window(&__screenWindow, __screenContext);
  522. if (rc)
  523. {
  524. perror("screen_create_window");
  525. goto error;
  526. }
  527. // Set/get any window prooperties.
  528. rc = screen_set_window_property_iv(__screenWindow, SCREEN_PROPERTY_FORMAT, &screenFormat);
  529. if (rc)
  530. {
  531. perror("screen_set_window_property_iv(SCREEN_PROPERTY_FORMAT)");
  532. goto error;
  533. }
  534. rc = screen_set_window_property_iv(__screenWindow, SCREEN_PROPERTY_USAGE, &screenUsage);
  535. if (rc)
  536. {
  537. perror("screen_set_window_property_iv(SCREEN_PROPERTY_USAGE)");
  538. goto error;
  539. }
  540. screen_display_t screen_display;
  541. rc = screen_get_window_property_pv(__screenWindow, SCREEN_PROPERTY_DISPLAY, (void **)&screen_display);
  542. if (rc)
  543. {
  544. perror("screen_get_window_property_pv(SCREEN_PROPERTY_DISPLAY)");
  545. goto error;
  546. }
  547. screen_display_mode_t screen_mode;
  548. rc = screen_get_display_property_pv(screen_display, SCREEN_PROPERTY_MODE, (void**)&screen_mode);
  549. if (rc)
  550. {
  551. perror("screen_get_display_property_pv(SCREEN_PROPERTY_MODE)");
  552. goto error;
  553. }
  554. int size[2];
  555. rc = screen_get_window_property_iv(__screenWindow, SCREEN_PROPERTY_BUFFER_SIZE, size);
  556. if (rc)
  557. {
  558. perror("screen_get_window_property_iv(SCREEN_PROPERTY_BUFFER_SIZE)");
  559. goto error;
  560. }
  561. __screenWindowSize[0] = size[0];
  562. __screenWindowSize[1] = size[1];
  563. if ((angle == 0) || (angle == 180))
  564. {
  565. if (((screen_mode.width > screen_mode.height) && (size[0] < size[1])) ||
  566. ((screen_mode.width < screen_mode.height) && (size[0] > size[1])))
  567. {
  568. __screenWindowSize[1] = size[0];
  569. __screenWindowSize[0] = size[1];
  570. }
  571. }
  572. else if ((angle == 90) || (angle == 270))
  573. {
  574. if (((screen_mode.width > screen_mode.height) && (size[0] > size[1])) ||
  575. ((screen_mode.width < screen_mode.height) && (size[0] < size[1])))
  576. {
  577. __screenWindowSize[1] = size[0];
  578. __screenWindowSize[0] = size[1];
  579. }
  580. }
  581. else
  582. {
  583. perror("Navigator returned an unexpected orientation angle.");
  584. goto error;
  585. }
  586. rc = screen_set_window_property_iv(__screenWindow, SCREEN_PROPERTY_BUFFER_SIZE, __screenWindowSize);
  587. if (rc)
  588. {
  589. perror("screen_set_window_property_iv(SCREEN_PROPERTY_BUFFER_SIZE)");
  590. goto error;
  591. }
  592. rc = screen_set_window_property_iv(__screenWindow, SCREEN_PROPERTY_ROTATION, &angle);
  593. if (rc)
  594. {
  595. perror("screen_set_window_property_iv(SCREEN_PROPERTY_ROTATION)");
  596. goto error;
  597. }
  598. if (windowPosition[0] != 0 || windowPosition[1] != 0)
  599. {
  600. rc = screen_set_window_property_iv(__screenWindow, SCREEN_PROPERTY_POSITION, windowPosition);
  601. if (rc)
  602. {
  603. perror("screen_set_window_property_iv(SCREEN_PROPERTY_POSITION)");
  604. goto error;
  605. }
  606. }
  607. rc = screen_set_window_property_iv(__screenWindow, SCREEN_PROPERTY_TRANSPARENCY, &screenTransparency);
  608. if (rc)
  609. {
  610. perror("screen_set_window_property_iv(SCREEN_PROPERTY_TRANSPARENCY)");
  611. goto error;
  612. }
  613. // Double buffered.
  614. rc = screen_create_window_buffers(__screenWindow, 2);
  615. if (rc)
  616. {
  617. perror("screen_create_window_buffers");
  618. goto error;
  619. }
  620. // Create screen event object.
  621. rc = screen_create_event(&__screenEvent);
  622. if (rc)
  623. {
  624. perror("screen_create_event");
  625. goto error;
  626. }
  627. // Request screen events.
  628. screen_request_events(__screenContext);
  629. // Get the EGL display and initialize.
  630. __eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
  631. if (__eglDisplay == EGL_NO_DISPLAY)
  632. {
  633. perror("eglGetDisplay");
  634. goto error;
  635. }
  636. if (eglInitialize(__eglDisplay, NULL, NULL) != EGL_TRUE)
  637. {
  638. perror("eglInitialize");
  639. goto error;
  640. }
  641. if (eglChooseConfig(__eglDisplay, eglConfigAttrs, &__eglConfig, 1, &eglConfigCount) != EGL_TRUE || eglConfigCount == 0)
  642. {
  643. checkErrorEGL("eglChooseConfig");
  644. goto error;
  645. }
  646. __eglContext = eglCreateContext(__eglDisplay, __eglConfig, EGL_NO_CONTEXT, eglContextAttrs);
  647. if (__eglContext == EGL_NO_CONTEXT)
  648. {
  649. checkErrorEGL("eglCreateContext");
  650. goto error;
  651. }
  652. __eglSurface = eglCreateWindowSurface(__eglDisplay, __eglConfig, __screenWindow, eglSurfaceAttrs);
  653. if (__eglSurface == EGL_NO_SURFACE)
  654. {
  655. checkErrorEGL("eglCreateWindowSurface");
  656. goto error;
  657. }
  658. if (eglMakeCurrent(__eglDisplay, __eglSurface, __eglSurface, __eglContext) != EGL_TRUE)
  659. {
  660. checkErrorEGL("eglMakeCurrent");
  661. goto error;
  662. }
  663. // Set vsync.
  664. eglSwapInterval(__eglDisplay, screenSwapInterval);
  665. // Initialize OpenGL ES extensions.
  666. __glExtensions = (const char*)glGetString(GL_EXTENSIONS);
  667. if (strstr(__glExtensions, "GL_OES_vertex_array_object") || strstr(__glExtensions, "GL_ARB_vertex_array_object"))
  668. {
  669. // Disable VAO extension for now.
  670. glBindVertexArray = (PFNGLBINDVERTEXARRAYOESPROC)eglGetProcAddress("glBindVertexArrayOES");
  671. glDeleteVertexArrays = (PFNGLDELETEVERTEXARRAYSOESPROC)eglGetProcAddress("glDeleteVertexArrays");
  672. glGenVertexArrays = (PFNGLGENVERTEXARRAYSOESPROC)eglGetProcAddress("glGenVertexArraysOES");
  673. glIsVertexArray = (PFNGLISVERTEXARRAYOESPROC)eglGetProcAddress("glIsVertexArrayOES");
  674. }
  675. return platform;
  676. error:
  677. // TODO: cleanup
  678. //
  679. return NULL;
  680. }
  681. /**
  682. * Convert the timespec into milliseconds.
  683. */
  684. long timespec2millis(struct timespec *a)
  685. {
  686. return a->tv_sec*1000 + a->tv_nsec/1000000;
  687. }
  688. /**
  689. * Fires a mouse event or a touch event on the game.
  690. * If the mouse event is not consumed, a touch event is fired instead.
  691. *
  692. * @param mouseEvent The mouse event to fire.
  693. * @param touchEvent The touch event to fire.
  694. * @param x The x position of the touch in pixels.
  695. * @param y The y position of the touch in pixels.
  696. */
  697. void mouseOrTouchEvent(Mouse::MouseEvent mouseEvent, Touch::TouchEvent touchEvent, int x, int y)
  698. {
  699. if (!Game::getInstance()->mouseEvent(mouseEvent, x, y, 0))
  700. {
  701. Game::getInstance()->touchEvent(touchEvent, x, y, 0);
  702. }
  703. }
  704. int Platform::enterMessagePump()
  705. {
  706. int rc;
  707. int eventType;
  708. int flags;
  709. int value;
  710. int position[2];
  711. int domain;
  712. mtouch_event_t touchEvent;
  713. int touchId = 0;
  714. // Get the initial time.
  715. clock_gettime(CLOCK_REALTIME, &__timespec);
  716. __timeStart = timespec2millis(&__timespec);
  717. __timeAbsolute = 0L;
  718. _game->run(__screenWindowSize[0], __screenWindowSize[1]);
  719. // Message loop.
  720. while (true)
  721. {
  722. bps_event_t* event = NULL;
  723. while (true)
  724. {
  725. rc = bps_get_event(&event, 1);
  726. assert(rc == BPS_SUCCESS);
  727. if (event == NULL)
  728. break;
  729. domain = bps_event_get_domain(event);
  730. if (domain == screen_get_domain())
  731. {
  732. __screenEvent = screen_event_get_event(event);
  733. screen_get_event_property_iv(__screenEvent, SCREEN_PROPERTY_TYPE, &eventType);
  734. switch (eventType)
  735. {
  736. case SCREEN_EVENT_MTOUCH_TOUCH:
  737. {
  738. if (!__multiTouch)
  739. {
  740. screen_get_event_property_iv(__screenEvent, SCREEN_PROPERTY_POSITION, position);
  741. Game::getInstance()->touchEvent(Touch::TOUCH_PRESS, position[0], position[1], 0);
  742. }
  743. else
  744. {
  745. screen_get_mtouch_event(__screenEvent, &touchEvent, 0);
  746. Game::getInstance()->touchEvent(Touch::TOUCH_PRESS, touchEvent.x, touchEvent.y, touchEvent.contact_id);
  747. }
  748. break;
  749. }
  750. case SCREEN_EVENT_MTOUCH_RELEASE:
  751. {
  752. if (!__multiTouch)
  753. {
  754. screen_get_event_property_iv(__screenEvent, SCREEN_PROPERTY_POSITION, position);
  755. Game::getInstance()->touchEvent(Touch::TOUCH_RELEASE, position[0], position[1], 0);
  756. }
  757. else
  758. {
  759. screen_get_mtouch_event(__screenEvent, &touchEvent, 0);
  760. Game::getInstance()->touchEvent(Touch::TOUCH_RELEASE, touchEvent.x, touchEvent.y, touchEvent.contact_id);
  761. }
  762. break;
  763. }
  764. case SCREEN_EVENT_MTOUCH_MOVE:
  765. {
  766. if (!__multiTouch)
  767. {
  768. screen_get_event_property_iv(__screenEvent, SCREEN_PROPERTY_POSITION, position);
  769. Game::getInstance()->touchEvent(Touch::TOUCH_MOVE, position[0], position[1], 0);
  770. }
  771. else
  772. {
  773. screen_get_mtouch_event(__screenEvent, &touchEvent, 0);
  774. Game::getInstance()->touchEvent(Touch::TOUCH_MOVE, touchEvent.x, touchEvent.y, touchEvent.contact_id);
  775. }
  776. break;
  777. break;
  778. }
  779. case SCREEN_EVENT_POINTER:
  780. {
  781. static int mouse_pressed = 0;
  782. int buttons;
  783. int wheel;
  784. // A move event will be fired unless a button state changed.
  785. bool move = true;
  786. bool left_move = false;
  787. //This is a mouse move event, it is applicable to a device with a usb mouse or simulator
  788. screen_get_event_property_iv(__screenEvent, SCREEN_PROPERTY_BUTTONS, &buttons);
  789. screen_get_event_property_iv(__screenEvent, SCREEN_PROPERTY_SOURCE_POSITION, position);
  790. screen_get_event_property_iv(__screenEvent, SCREEN_PROPERTY_MOUSE_WHEEL, &wheel);
  791. // Handle left mouse. Interpret as touch if the left mouse event is not consumed.
  792. if (buttons & SCREEN_LEFT_MOUSE_BUTTON)
  793. {
  794. if (mouse_pressed & SCREEN_LEFT_MOUSE_BUTTON)
  795. {
  796. left_move = true;
  797. }
  798. else
  799. {
  800. move = false;
  801. mouse_pressed |= SCREEN_LEFT_MOUSE_BUTTON;
  802. mouseOrTouchEvent(Mouse::MOUSE_PRESS_LEFT_BUTTON, Touch::TOUCH_PRESS, position[0], position[1]);
  803. }
  804. }
  805. else if (mouse_pressed & SCREEN_LEFT_MOUSE_BUTTON)
  806. {
  807. move = false;
  808. mouse_pressed &= ~SCREEN_LEFT_MOUSE_BUTTON;
  809. mouseOrTouchEvent(Mouse::MOUSE_RELEASE_LEFT_BUTTON, Touch::TOUCH_RELEASE, position[0], position[1]);
  810. }
  811. // Handle right mouse
  812. if (buttons & SCREEN_RIGHT_MOUSE_BUTTON)
  813. {
  814. if ((mouse_pressed & SCREEN_RIGHT_MOUSE_BUTTON) == 0)
  815. {
  816. move = false;
  817. mouse_pressed |= SCREEN_RIGHT_MOUSE_BUTTON;
  818. Game::getInstance()->mouseEvent(Mouse::MOUSE_PRESS_RIGHT_BUTTON, position[0], position[1], 0);
  819. }
  820. }
  821. else if (mouse_pressed & SCREEN_RIGHT_MOUSE_BUTTON)
  822. {
  823. move = false;
  824. mouse_pressed &= ~SCREEN_RIGHT_MOUSE_BUTTON;
  825. Game::getInstance()->mouseEvent(Mouse::MOUSE_RELEASE_RIGHT_BUTTON, position[0], position[1], 0);
  826. }
  827. // Handle middle mouse
  828. if (buttons & SCREEN_MIDDLE_MOUSE_BUTTON)
  829. {
  830. if ((mouse_pressed & SCREEN_MIDDLE_MOUSE_BUTTON) == 0)
  831. {
  832. move = false;
  833. mouse_pressed |= SCREEN_MIDDLE_MOUSE_BUTTON;
  834. Game::getInstance()->mouseEvent(Mouse::MOUSE_PRESS_MIDDLE_BUTTON, position[0], position[1], 0);
  835. }
  836. }
  837. else if (mouse_pressed & SCREEN_MIDDLE_MOUSE_BUTTON)
  838. {
  839. move = false;
  840. mouse_pressed &= ~SCREEN_MIDDLE_MOUSE_BUTTON;
  841. Game::getInstance()->mouseEvent(Mouse::MOUSE_RELEASE_MIDDLE_BUTTON, position[0], position[1], 0);
  842. }
  843. // Fire a move event if none of the buttons changed.
  844. if (left_move)
  845. {
  846. mouseOrTouchEvent(Mouse::MOUSE_MOVE, Touch::TOUCH_MOVE, position[0], position[1]);
  847. }
  848. else if (move)
  849. {
  850. Game::getInstance()->mouseEvent(Mouse::MOUSE_MOVE, position[0], position[1], 0);
  851. }
  852. // Handle mouse wheel events
  853. if (wheel)
  854. {
  855. Game::getInstance()->mouseEvent(Mouse::MOUSE_WHEEL, position[0], position[1], -wheel);
  856. }
  857. break;
  858. }
  859. case SCREEN_EVENT_KEYBOARD:
  860. {
  861. screen_get_event_property_iv(__screenEvent, SCREEN_PROPERTY_KEY_FLAGS, &flags);
  862. screen_get_event_property_iv(__screenEvent, SCREEN_PROPERTY_KEY_SYM, &value);
  863. gameplay::Keyboard::KeyEvent evt = (flags & KEY_DOWN) ? gameplay::Keyboard::KEY_PRESS : gameplay::Keyboard::KEY_RELEASE;
  864. // Suppress key repeats
  865. if ((flags & KEY_REPEAT) == 0)
  866. {
  867. Game::getInstance()->keyEvent(evt, getKey(value));
  868. if (evt == gameplay::Keyboard::KEY_PRESS && flags & KEY_SYM_VALID)
  869. {
  870. int unicode = getUnicode(value);
  871. if (unicode)
  872. Game::getInstance()->keyEvent(gameplay::Keyboard::KEY_CHAR, unicode);
  873. }
  874. }
  875. break;
  876. }
  877. }
  878. }
  879. else if (domain == navigator_get_domain())
  880. {
  881. switch (bps_event_get_code(event))
  882. {
  883. case NAVIGATOR_SWIPE_DOWN:
  884. _game->menu();
  885. break;
  886. case NAVIGATOR_EXIT:
  887. _game->exit();
  888. break;
  889. }
  890. }
  891. }
  892. // If we are done, then exit.
  893. if (_game->getState() == Game::UNINITIALIZED)
  894. break;
  895. // Idle time (no events left to process) is spent rendering.
  896. // We skip rendering when the app is paused.
  897. if (_game->getState() != Game::PAUSED)
  898. {
  899. _game->frame();
  900. // Post the new frame to the display.
  901. // Note that there are a couple cases where eglSwapBuffers could fail
  902. // with an error code that requires a certain level of re-initialization:
  903. //
  904. // 1) EGL_BAD_NATIVE_WINDOW - Called when the surface we're currently using
  905. // is invalidated. This would require us to destroy our EGL surface,
  906. // close our OpenKODE window, and start again.
  907. //
  908. // 2) EGL_CONTEXT_LOST - Power management event that led to our EGL context
  909. // being lost. Requires us to re-create and re-initalize our EGL context
  910. // and all OpenGL ES state.
  911. //
  912. // For now, if we get these, we'll simply exit.
  913. rc = eglSwapBuffers(__eglDisplay, __eglSurface);
  914. if (rc != EGL_TRUE)
  915. {
  916. _game->exit();
  917. perror("eglSwapBuffers");
  918. break;
  919. }
  920. }
  921. }
  922. screen_stop_events(__screenContext);
  923. bps_shutdown();
  924. screen_destroy_context(__screenContext);
  925. return 0;
  926. }
  927. unsigned int Platform::getDisplayWidth()
  928. {
  929. return __screenWindowSize[0];
  930. }
  931. unsigned int Platform::getDisplayHeight()
  932. {
  933. return __screenWindowSize[1];
  934. }
  935. long Platform::getAbsoluteTime()
  936. {
  937. clock_gettime(CLOCK_REALTIME, &__timespec);
  938. long now = timespec2millis(&__timespec);
  939. __timeAbsolute = now - __timeStart;
  940. return __timeAbsolute;
  941. }
  942. void Platform::setAbsoluteTime(long time)
  943. {
  944. __timeAbsolute = time;
  945. }
  946. bool Platform::isVsync()
  947. {
  948. return __vsync;
  949. }
  950. void Platform::setVsync(bool enable)
  951. {
  952. eglSwapInterval(__eglDisplay, enable ? 1 : 0);
  953. __vsync = enable;
  954. }
  955. int Platform::getOrientationAngle()
  956. {
  957. return __orientationAngle;
  958. }
  959. void Platform::setMultiTouch(bool enabled)
  960. {
  961. __multiTouch = enabled;
  962. }
  963. bool Platform::isMultiTouch()
  964. {
  965. return __multiTouch;
  966. }
  967. void Platform::getAccelerometerValues(float* pitch, float* roll)
  968. {
  969. double tx, ty, tz;
  970. accelerometer_read_forces(&tx, &ty, &tz);
  971. // Hack landscape adjustment only.
  972. if (__orientationAngle == 0)
  973. {
  974. tx = -tx;
  975. ty = -ty;
  976. tz = -tz;
  977. }
  978. if (pitch != NULL)
  979. *pitch = atan(ty / sqrt(tx * tx + tz * tz)) * 180.0f * M_1_PI;
  980. if (roll != NULL)
  981. *roll = atan(tx / sqrt(ty * ty + tz * tz)) * 180.0f * M_1_PI;
  982. }
  983. void Platform::swapBuffers()
  984. {
  985. if (__eglDisplay && __eglSurface)
  986. eglSwapBuffers(__eglDisplay, __eglSurface);
  987. }
  988. void Platform::displayKeyboard(bool display)
  989. {
  990. if (display)
  991. virtualkeyboard_show();
  992. else
  993. virtualkeyboard_hide();
  994. }
  995. }
  996. #endif