lua_Platform.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. #include "Base.h"
  2. #include "ScriptController.h"
  3. #include "Platform.h"
  4. #include "lua_Platform.h"
  5. #include "lua_Global.h"
  6. namespace gameplay
  7. {
  8. void luaRegister_Platform()
  9. {
  10. ScriptController* sc = ScriptController::getInstance();
  11. const luaL_Reg lua_members[] =
  12. {
  13. {"enterMessagePump", lua_Platform_enterMessagePump},
  14. {NULL, NULL}
  15. };
  16. const luaL_Reg lua_statics[] =
  17. {
  18. {"create", lua_Platform_static_create},
  19. {"displayKeyboard", lua_Platform_static_displayKeyboard},
  20. {"getAbsoluteTime", lua_Platform_static_getAbsoluteTime},
  21. {"getAccelerometerValues", lua_Platform_static_getAccelerometerValues},
  22. {"getDisplayHeight", lua_Platform_static_getDisplayHeight},
  23. {"getDisplayWidth", lua_Platform_static_getDisplayWidth},
  24. {"isMultiTouch", lua_Platform_static_isMultiTouch},
  25. {"isVsync", lua_Platform_static_isVsync},
  26. {"keyEventInternal", lua_Platform_static_keyEventInternal},
  27. {"mouseEventInternal", lua_Platform_static_mouseEventInternal},
  28. {"setAbsoluteTime", lua_Platform_static_setAbsoluteTime},
  29. {"setMultiTouch", lua_Platform_static_setMultiTouch},
  30. {"setVsync", lua_Platform_static_setVsync},
  31. {"signalShutdown", lua_Platform_static_signalShutdown},
  32. {"sleep", lua_Platform_static_sleep},
  33. {"swapBuffers", lua_Platform_static_swapBuffers},
  34. {"touchEventInternal", lua_Platform_static_touchEventInternal},
  35. {NULL, NULL}
  36. };
  37. std::vector<std::string> scopePath;
  38. sc->registerClass("Platform", lua_members, NULL, lua_Platform__gc, lua_statics, scopePath);
  39. }
  40. static Platform* getInstance(lua_State* state)
  41. {
  42. void* userdata = luaL_checkudata(state, 1, "Platform");
  43. luaL_argcheck(state, userdata != NULL, 1, "'Platform' expected.");
  44. return (Platform*)((ScriptController::LuaObject*)userdata)->instance;
  45. }
  46. int lua_Platform__gc(lua_State* state)
  47. {
  48. // Get the number of parameters.
  49. int paramCount = lua_gettop(state);
  50. // Attempt to match the parameters to a valid binding.
  51. switch (paramCount)
  52. {
  53. case 1:
  54. {
  55. if ((lua_type(state, 1) == LUA_TUSERDATA))
  56. {
  57. void* userdata = luaL_checkudata(state, 1, "Platform");
  58. luaL_argcheck(state, userdata != NULL, 1, "'Platform' expected.");
  59. ScriptController::LuaObject* object = (ScriptController::LuaObject*)userdata;
  60. if (object->owns)
  61. {
  62. Platform* instance = (Platform*)object->instance;
  63. SAFE_DELETE(instance);
  64. }
  65. return 0;
  66. }
  67. else
  68. {
  69. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  70. lua_error(state);
  71. }
  72. break;
  73. }
  74. default:
  75. {
  76. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  77. lua_error(state);
  78. break;
  79. }
  80. }
  81. return 0;
  82. }
  83. int lua_Platform_enterMessagePump(lua_State* state)
  84. {
  85. // Get the number of parameters.
  86. int paramCount = lua_gettop(state);
  87. // Attempt to match the parameters to a valid binding.
  88. switch (paramCount)
  89. {
  90. case 1:
  91. {
  92. if ((lua_type(state, 1) == LUA_TUSERDATA))
  93. {
  94. Platform* instance = getInstance(state);
  95. int result = instance->enterMessagePump();
  96. // Push the return value onto the stack.
  97. lua_pushinteger(state, result);
  98. return 1;
  99. }
  100. else
  101. {
  102. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  103. lua_error(state);
  104. }
  105. break;
  106. }
  107. default:
  108. {
  109. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  110. lua_error(state);
  111. break;
  112. }
  113. }
  114. return 0;
  115. }
  116. int lua_Platform_static_create(lua_State* state)
  117. {
  118. // Get the number of parameters.
  119. int paramCount = lua_gettop(state);
  120. // Attempt to match the parameters to a valid binding.
  121. switch (paramCount)
  122. {
  123. case 1:
  124. {
  125. if ((lua_type(state, 1) == LUA_TUSERDATA || lua_type(state, 1) == LUA_TTABLE || lua_type(state, 1) == LUA_TNIL))
  126. {
  127. // Get parameter 1 off the stack.
  128. Game* param1 = ScriptController::getInstance()->getObjectPointer<Game>(1, "Game", false);
  129. void* returnPtr = (void*)Platform::create(param1);
  130. if (returnPtr)
  131. {
  132. ScriptController::LuaObject* object = (ScriptController::LuaObject*)lua_newuserdata(state, sizeof(ScriptController::LuaObject));
  133. object->instance = returnPtr;
  134. object->owns = false;
  135. luaL_getmetatable(state, "Platform");
  136. lua_setmetatable(state, -2);
  137. }
  138. else
  139. {
  140. lua_pushnil(state);
  141. }
  142. return 1;
  143. }
  144. else
  145. {
  146. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  147. lua_error(state);
  148. }
  149. break;
  150. }
  151. default:
  152. {
  153. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  154. lua_error(state);
  155. break;
  156. }
  157. }
  158. return 0;
  159. }
  160. int lua_Platform_static_displayKeyboard(lua_State* state)
  161. {
  162. // Get the number of parameters.
  163. int paramCount = lua_gettop(state);
  164. // Attempt to match the parameters to a valid binding.
  165. switch (paramCount)
  166. {
  167. case 1:
  168. {
  169. if (lua_type(state, 1) == LUA_TBOOLEAN)
  170. {
  171. // Get parameter 1 off the stack.
  172. bool param1 = ScriptController::luaCheckBool(state, 1);
  173. Platform::displayKeyboard(param1);
  174. return 0;
  175. }
  176. else
  177. {
  178. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  179. lua_error(state);
  180. }
  181. break;
  182. }
  183. default:
  184. {
  185. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  186. lua_error(state);
  187. break;
  188. }
  189. }
  190. return 0;
  191. }
  192. int lua_Platform_static_getAbsoluteTime(lua_State* state)
  193. {
  194. // Get the number of parameters.
  195. int paramCount = lua_gettop(state);
  196. // Attempt to match the parameters to a valid binding.
  197. switch (paramCount)
  198. {
  199. case 0:
  200. {
  201. double result = Platform::getAbsoluteTime();
  202. // Push the return value onto the stack.
  203. lua_pushnumber(state, result);
  204. return 1;
  205. break;
  206. }
  207. default:
  208. {
  209. lua_pushstring(state, "Invalid number of parameters (expected 0).");
  210. lua_error(state);
  211. break;
  212. }
  213. }
  214. return 0;
  215. }
  216. int lua_Platform_static_getAccelerometerValues(lua_State* state)
  217. {
  218. // Get the number of parameters.
  219. int paramCount = lua_gettop(state);
  220. // Attempt to match the parameters to a valid binding.
  221. switch (paramCount)
  222. {
  223. case 2:
  224. {
  225. if ((lua_type(state, 1) == LUA_TTABLE || lua_type(state, 1) == LUA_TLIGHTUSERDATA) &&
  226. (lua_type(state, 2) == LUA_TTABLE || lua_type(state, 2) == LUA_TLIGHTUSERDATA))
  227. {
  228. // Get parameter 1 off the stack.
  229. float* param1 = ScriptController::getInstance()->getFloatPointer(1);
  230. // Get parameter 2 off the stack.
  231. float* param2 = ScriptController::getInstance()->getFloatPointer(2);
  232. Platform::getAccelerometerValues(param1, param2);
  233. return 0;
  234. }
  235. else
  236. {
  237. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  238. lua_error(state);
  239. }
  240. break;
  241. }
  242. default:
  243. {
  244. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  245. lua_error(state);
  246. break;
  247. }
  248. }
  249. return 0;
  250. }
  251. int lua_Platform_static_getDisplayHeight(lua_State* state)
  252. {
  253. // Get the number of parameters.
  254. int paramCount = lua_gettop(state);
  255. // Attempt to match the parameters to a valid binding.
  256. switch (paramCount)
  257. {
  258. case 0:
  259. {
  260. unsigned int result = Platform::getDisplayHeight();
  261. // Push the return value onto the stack.
  262. lua_pushunsigned(state, result);
  263. return 1;
  264. break;
  265. }
  266. default:
  267. {
  268. lua_pushstring(state, "Invalid number of parameters (expected 0).");
  269. lua_error(state);
  270. break;
  271. }
  272. }
  273. return 0;
  274. }
  275. int lua_Platform_static_getDisplayWidth(lua_State* state)
  276. {
  277. // Get the number of parameters.
  278. int paramCount = lua_gettop(state);
  279. // Attempt to match the parameters to a valid binding.
  280. switch (paramCount)
  281. {
  282. case 0:
  283. {
  284. unsigned int result = Platform::getDisplayWidth();
  285. // Push the return value onto the stack.
  286. lua_pushunsigned(state, result);
  287. return 1;
  288. break;
  289. }
  290. default:
  291. {
  292. lua_pushstring(state, "Invalid number of parameters (expected 0).");
  293. lua_error(state);
  294. break;
  295. }
  296. }
  297. return 0;
  298. }
  299. int lua_Platform_static_isMultiTouch(lua_State* state)
  300. {
  301. // Get the number of parameters.
  302. int paramCount = lua_gettop(state);
  303. // Attempt to match the parameters to a valid binding.
  304. switch (paramCount)
  305. {
  306. case 0:
  307. {
  308. bool result = Platform::isMultiTouch();
  309. // Push the return value onto the stack.
  310. lua_pushboolean(state, result);
  311. return 1;
  312. break;
  313. }
  314. default:
  315. {
  316. lua_pushstring(state, "Invalid number of parameters (expected 0).");
  317. lua_error(state);
  318. break;
  319. }
  320. }
  321. return 0;
  322. }
  323. int lua_Platform_static_isVsync(lua_State* state)
  324. {
  325. // Get the number of parameters.
  326. int paramCount = lua_gettop(state);
  327. // Attempt to match the parameters to a valid binding.
  328. switch (paramCount)
  329. {
  330. case 0:
  331. {
  332. bool result = Platform::isVsync();
  333. // Push the return value onto the stack.
  334. lua_pushboolean(state, result);
  335. return 1;
  336. break;
  337. }
  338. default:
  339. {
  340. lua_pushstring(state, "Invalid number of parameters (expected 0).");
  341. lua_error(state);
  342. break;
  343. }
  344. }
  345. return 0;
  346. }
  347. int lua_Platform_static_keyEventInternal(lua_State* state)
  348. {
  349. // Get the number of parameters.
  350. int paramCount = lua_gettop(state);
  351. // Attempt to match the parameters to a valid binding.
  352. switch (paramCount)
  353. {
  354. case 2:
  355. {
  356. if ((lua_type(state, 1) == LUA_TSTRING || lua_type(state, 1) == LUA_TNIL) &&
  357. lua_type(state, 2) == LUA_TNUMBER)
  358. {
  359. // Get parameter 1 off the stack.
  360. Keyboard::KeyEvent param1 = (Keyboard::KeyEvent)lua_enumFromString_KeyboardKeyEvent(luaL_checkstring(state, 1));
  361. // Get parameter 2 off the stack.
  362. int param2 = (int)luaL_checkint(state, 2);
  363. Platform::keyEventInternal(param1, param2);
  364. return 0;
  365. }
  366. else
  367. {
  368. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  369. lua_error(state);
  370. }
  371. break;
  372. }
  373. default:
  374. {
  375. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  376. lua_error(state);
  377. break;
  378. }
  379. }
  380. return 0;
  381. }
  382. int lua_Platform_static_mouseEventInternal(lua_State* state)
  383. {
  384. // Get the number of parameters.
  385. int paramCount = lua_gettop(state);
  386. // Attempt to match the parameters to a valid binding.
  387. switch (paramCount)
  388. {
  389. case 4:
  390. {
  391. if ((lua_type(state, 1) == LUA_TSTRING || lua_type(state, 1) == LUA_TNIL) &&
  392. lua_type(state, 2) == LUA_TNUMBER &&
  393. lua_type(state, 3) == LUA_TNUMBER &&
  394. lua_type(state, 4) == LUA_TNUMBER)
  395. {
  396. // Get parameter 1 off the stack.
  397. Mouse::MouseEvent param1 = (Mouse::MouseEvent)lua_enumFromString_MouseMouseEvent(luaL_checkstring(state, 1));
  398. // Get parameter 2 off the stack.
  399. int param2 = (int)luaL_checkint(state, 2);
  400. // Get parameter 3 off the stack.
  401. int param3 = (int)luaL_checkint(state, 3);
  402. // Get parameter 4 off the stack.
  403. int param4 = (int)luaL_checkint(state, 4);
  404. bool result = Platform::mouseEventInternal(param1, param2, param3, param4);
  405. // Push the return value onto the stack.
  406. lua_pushboolean(state, result);
  407. return 1;
  408. }
  409. else
  410. {
  411. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  412. lua_error(state);
  413. }
  414. break;
  415. }
  416. default:
  417. {
  418. lua_pushstring(state, "Invalid number of parameters (expected 4).");
  419. lua_error(state);
  420. break;
  421. }
  422. }
  423. return 0;
  424. }
  425. int lua_Platform_static_setAbsoluteTime(lua_State* state)
  426. {
  427. // Get the number of parameters.
  428. int paramCount = lua_gettop(state);
  429. // Attempt to match the parameters to a valid binding.
  430. switch (paramCount)
  431. {
  432. case 1:
  433. {
  434. if (lua_type(state, 1) == LUA_TNUMBER)
  435. {
  436. // Get parameter 1 off the stack.
  437. double param1 = (double)luaL_checknumber(state, 1);
  438. Platform::setAbsoluteTime(param1);
  439. return 0;
  440. }
  441. else
  442. {
  443. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  444. lua_error(state);
  445. }
  446. break;
  447. }
  448. default:
  449. {
  450. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  451. lua_error(state);
  452. break;
  453. }
  454. }
  455. return 0;
  456. }
  457. int lua_Platform_static_setMultiTouch(lua_State* state)
  458. {
  459. // Get the number of parameters.
  460. int paramCount = lua_gettop(state);
  461. // Attempt to match the parameters to a valid binding.
  462. switch (paramCount)
  463. {
  464. case 1:
  465. {
  466. if (lua_type(state, 1) == LUA_TBOOLEAN)
  467. {
  468. // Get parameter 1 off the stack.
  469. bool param1 = ScriptController::luaCheckBool(state, 1);
  470. Platform::setMultiTouch(param1);
  471. return 0;
  472. }
  473. else
  474. {
  475. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  476. lua_error(state);
  477. }
  478. break;
  479. }
  480. default:
  481. {
  482. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  483. lua_error(state);
  484. break;
  485. }
  486. }
  487. return 0;
  488. }
  489. int lua_Platform_static_setVsync(lua_State* state)
  490. {
  491. // Get the number of parameters.
  492. int paramCount = lua_gettop(state);
  493. // Attempt to match the parameters to a valid binding.
  494. switch (paramCount)
  495. {
  496. case 1:
  497. {
  498. if (lua_type(state, 1) == LUA_TBOOLEAN)
  499. {
  500. // Get parameter 1 off the stack.
  501. bool param1 = ScriptController::luaCheckBool(state, 1);
  502. Platform::setVsync(param1);
  503. return 0;
  504. }
  505. else
  506. {
  507. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  508. lua_error(state);
  509. }
  510. break;
  511. }
  512. default:
  513. {
  514. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  515. lua_error(state);
  516. break;
  517. }
  518. }
  519. return 0;
  520. }
  521. int lua_Platform_static_signalShutdown(lua_State* state)
  522. {
  523. // Get the number of parameters.
  524. int paramCount = lua_gettop(state);
  525. // Attempt to match the parameters to a valid binding.
  526. switch (paramCount)
  527. {
  528. case 0:
  529. {
  530. Platform::signalShutdown();
  531. return 0;
  532. break;
  533. }
  534. default:
  535. {
  536. lua_pushstring(state, "Invalid number of parameters (expected 0).");
  537. lua_error(state);
  538. break;
  539. }
  540. }
  541. return 0;
  542. }
  543. int lua_Platform_static_sleep(lua_State* state)
  544. {
  545. // Get the number of parameters.
  546. int paramCount = lua_gettop(state);
  547. // Attempt to match the parameters to a valid binding.
  548. switch (paramCount)
  549. {
  550. case 1:
  551. {
  552. if (lua_type(state, 1) == LUA_TNUMBER)
  553. {
  554. // Get parameter 1 off the stack.
  555. long param1 = (long)luaL_checklong(state, 1);
  556. Platform::sleep(param1);
  557. return 0;
  558. }
  559. else
  560. {
  561. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  562. lua_error(state);
  563. }
  564. break;
  565. }
  566. default:
  567. {
  568. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  569. lua_error(state);
  570. break;
  571. }
  572. }
  573. return 0;
  574. }
  575. int lua_Platform_static_swapBuffers(lua_State* state)
  576. {
  577. // Get the number of parameters.
  578. int paramCount = lua_gettop(state);
  579. // Attempt to match the parameters to a valid binding.
  580. switch (paramCount)
  581. {
  582. case 0:
  583. {
  584. Platform::swapBuffers();
  585. return 0;
  586. break;
  587. }
  588. default:
  589. {
  590. lua_pushstring(state, "Invalid number of parameters (expected 0).");
  591. lua_error(state);
  592. break;
  593. }
  594. }
  595. return 0;
  596. }
  597. int lua_Platform_static_touchEventInternal(lua_State* state)
  598. {
  599. // Get the number of parameters.
  600. int paramCount = lua_gettop(state);
  601. // Attempt to match the parameters to a valid binding.
  602. switch (paramCount)
  603. {
  604. case 4:
  605. {
  606. if ((lua_type(state, 1) == LUA_TSTRING || lua_type(state, 1) == LUA_TNIL) &&
  607. lua_type(state, 2) == LUA_TNUMBER &&
  608. lua_type(state, 3) == LUA_TNUMBER &&
  609. lua_type(state, 4) == LUA_TNUMBER)
  610. {
  611. // Get parameter 1 off the stack.
  612. Touch::TouchEvent param1 = (Touch::TouchEvent)lua_enumFromString_TouchTouchEvent(luaL_checkstring(state, 1));
  613. // Get parameter 2 off the stack.
  614. int param2 = (int)luaL_checkint(state, 2);
  615. // Get parameter 3 off the stack.
  616. int param3 = (int)luaL_checkint(state, 3);
  617. // Get parameter 4 off the stack.
  618. unsigned int param4 = (unsigned int)luaL_checkunsigned(state, 4);
  619. Platform::touchEventInternal(param1, param2, param3, param4);
  620. return 0;
  621. }
  622. else
  623. {
  624. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  625. lua_error(state);
  626. }
  627. break;
  628. }
  629. default:
  630. {
  631. lua_pushstring(state, "Invalid number of parameters (expected 4).");
  632. lua_error(state);
  633. break;
  634. }
  635. }
  636. return 0;
  637. }
  638. }