lua_TextureSampler.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. #include "Base.h"
  2. #include "ScriptController.h"
  3. #include "lua_TextureSampler.h"
  4. #include "Base.h"
  5. #include "FileSystem.h"
  6. #include "Game.h"
  7. #include "Image.h"
  8. #include "Ref.h"
  9. #include "Texture.h"
  10. #include "lua_TextureFilter.h"
  11. #include "lua_TextureFormat.h"
  12. #include "lua_TextureWrap.h"
  13. namespace gameplay
  14. {
  15. void luaRegister_TextureSampler()
  16. {
  17. const luaL_Reg lua_members[] =
  18. {
  19. {"addRef", lua_TextureSampler_addRef},
  20. {"bind", lua_TextureSampler_bind},
  21. {"getRefCount", lua_TextureSampler_getRefCount},
  22. {"getTexture", lua_TextureSampler_getTexture},
  23. {"release", lua_TextureSampler_release},
  24. {"setFilterMode", lua_TextureSampler_setFilterMode},
  25. {"setWrapMode", lua_TextureSampler_setWrapMode},
  26. {NULL, NULL}
  27. };
  28. const luaL_Reg lua_statics[] =
  29. {
  30. {"create", lua_TextureSampler_static_create},
  31. {NULL, NULL}
  32. };
  33. std::vector<std::string> scopePath;
  34. scopePath.push_back("Texture");
  35. ScriptUtil::registerClass("TextureSampler", lua_members, NULL, lua_TextureSampler__gc, lua_statics, scopePath);
  36. }
  37. static Texture::Sampler* getInstance(lua_State* state)
  38. {
  39. void* userdata = luaL_checkudata(state, 1, "TextureSampler");
  40. luaL_argcheck(state, userdata != NULL, 1, "'TextureSampler' expected.");
  41. return (Texture::Sampler*)((ScriptUtil::LuaObject*)userdata)->instance;
  42. }
  43. int lua_TextureSampler__gc(lua_State* state)
  44. {
  45. // Get the number of parameters.
  46. int paramCount = lua_gettop(state);
  47. // Attempt to match the parameters to a valid binding.
  48. switch (paramCount)
  49. {
  50. case 1:
  51. {
  52. if ((lua_type(state, 1) == LUA_TUSERDATA))
  53. {
  54. void* userdata = luaL_checkudata(state, 1, "TextureSampler");
  55. luaL_argcheck(state, userdata != NULL, 1, "'TextureSampler' expected.");
  56. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)userdata;
  57. if (object->owns)
  58. {
  59. Texture::Sampler* instance = (Texture::Sampler*)object->instance;
  60. SAFE_RELEASE(instance);
  61. }
  62. return 0;
  63. }
  64. else
  65. {
  66. lua_pushstring(state, "lua_TextureSampler__gc - Failed to match the given parameters to a valid function signature.");
  67. lua_error(state);
  68. }
  69. break;
  70. }
  71. default:
  72. {
  73. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  74. lua_error(state);
  75. break;
  76. }
  77. }
  78. return 0;
  79. }
  80. int lua_TextureSampler_addRef(lua_State* state)
  81. {
  82. // Get the number of parameters.
  83. int paramCount = lua_gettop(state);
  84. // Attempt to match the parameters to a valid binding.
  85. switch (paramCount)
  86. {
  87. case 1:
  88. {
  89. if ((lua_type(state, 1) == LUA_TUSERDATA))
  90. {
  91. Texture::Sampler* instance = getInstance(state);
  92. instance->addRef();
  93. return 0;
  94. }
  95. else
  96. {
  97. lua_pushstring(state, "lua_TextureSampler_addRef - Failed to match the given parameters to a valid function signature.");
  98. lua_error(state);
  99. }
  100. break;
  101. }
  102. default:
  103. {
  104. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  105. lua_error(state);
  106. break;
  107. }
  108. }
  109. return 0;
  110. }
  111. int lua_TextureSampler_bind(lua_State* state)
  112. {
  113. // Get the number of parameters.
  114. int paramCount = lua_gettop(state);
  115. // Attempt to match the parameters to a valid binding.
  116. switch (paramCount)
  117. {
  118. case 1:
  119. {
  120. if ((lua_type(state, 1) == LUA_TUSERDATA))
  121. {
  122. Texture::Sampler* instance = getInstance(state);
  123. instance->bind();
  124. return 0;
  125. }
  126. else
  127. {
  128. lua_pushstring(state, "lua_TextureSampler_bind - Failed to match the given parameters to a valid function signature.");
  129. lua_error(state);
  130. }
  131. break;
  132. }
  133. default:
  134. {
  135. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  136. lua_error(state);
  137. break;
  138. }
  139. }
  140. return 0;
  141. }
  142. int lua_TextureSampler_getRefCount(lua_State* state)
  143. {
  144. // Get the number of parameters.
  145. int paramCount = lua_gettop(state);
  146. // Attempt to match the parameters to a valid binding.
  147. switch (paramCount)
  148. {
  149. case 1:
  150. {
  151. if ((lua_type(state, 1) == LUA_TUSERDATA))
  152. {
  153. Texture::Sampler* instance = getInstance(state);
  154. unsigned int result = instance->getRefCount();
  155. // Push the return value onto the stack.
  156. lua_pushunsigned(state, result);
  157. return 1;
  158. }
  159. else
  160. {
  161. lua_pushstring(state, "lua_TextureSampler_getRefCount - Failed to match the given parameters to a valid function signature.");
  162. lua_error(state);
  163. }
  164. break;
  165. }
  166. default:
  167. {
  168. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  169. lua_error(state);
  170. break;
  171. }
  172. }
  173. return 0;
  174. }
  175. int lua_TextureSampler_getTexture(lua_State* state)
  176. {
  177. // Get the number of parameters.
  178. int paramCount = lua_gettop(state);
  179. // Attempt to match the parameters to a valid binding.
  180. switch (paramCount)
  181. {
  182. case 1:
  183. {
  184. if ((lua_type(state, 1) == LUA_TUSERDATA))
  185. {
  186. Texture::Sampler* instance = getInstance(state);
  187. void* returnPtr = (void*)instance->getTexture();
  188. if (returnPtr)
  189. {
  190. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
  191. object->instance = returnPtr;
  192. object->owns = false;
  193. luaL_getmetatable(state, "Texture");
  194. lua_setmetatable(state, -2);
  195. }
  196. else
  197. {
  198. lua_pushnil(state);
  199. }
  200. return 1;
  201. }
  202. else
  203. {
  204. lua_pushstring(state, "lua_TextureSampler_getTexture - Failed to match the given parameters to a valid function signature.");
  205. lua_error(state);
  206. }
  207. break;
  208. }
  209. default:
  210. {
  211. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  212. lua_error(state);
  213. break;
  214. }
  215. }
  216. return 0;
  217. }
  218. int lua_TextureSampler_release(lua_State* state)
  219. {
  220. // Get the number of parameters.
  221. int paramCount = lua_gettop(state);
  222. // Attempt to match the parameters to a valid binding.
  223. switch (paramCount)
  224. {
  225. case 1:
  226. {
  227. if ((lua_type(state, 1) == LUA_TUSERDATA))
  228. {
  229. Texture::Sampler* instance = getInstance(state);
  230. instance->release();
  231. return 0;
  232. }
  233. else
  234. {
  235. lua_pushstring(state, "lua_TextureSampler_release - Failed to match the given parameters to a valid function signature.");
  236. lua_error(state);
  237. }
  238. break;
  239. }
  240. default:
  241. {
  242. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  243. lua_error(state);
  244. break;
  245. }
  246. }
  247. return 0;
  248. }
  249. int lua_TextureSampler_setFilterMode(lua_State* state)
  250. {
  251. // Get the number of parameters.
  252. int paramCount = lua_gettop(state);
  253. // Attempt to match the parameters to a valid binding.
  254. switch (paramCount)
  255. {
  256. case 3:
  257. {
  258. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  259. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL) &&
  260. (lua_type(state, 3) == LUA_TSTRING || lua_type(state, 3) == LUA_TNIL))
  261. {
  262. // Get parameter 1 off the stack.
  263. Texture::Filter param1 = (Texture::Filter)lua_enumFromString_TextureFilter(luaL_checkstring(state, 2));
  264. // Get parameter 2 off the stack.
  265. Texture::Filter param2 = (Texture::Filter)lua_enumFromString_TextureFilter(luaL_checkstring(state, 3));
  266. Texture::Sampler* instance = getInstance(state);
  267. instance->setFilterMode(param1, param2);
  268. return 0;
  269. }
  270. else
  271. {
  272. lua_pushstring(state, "lua_TextureSampler_setFilterMode - Failed to match the given parameters to a valid function signature.");
  273. lua_error(state);
  274. }
  275. break;
  276. }
  277. default:
  278. {
  279. lua_pushstring(state, "Invalid number of parameters (expected 3).");
  280. lua_error(state);
  281. break;
  282. }
  283. }
  284. return 0;
  285. }
  286. int lua_TextureSampler_setWrapMode(lua_State* state)
  287. {
  288. // Get the number of parameters.
  289. int paramCount = lua_gettop(state);
  290. // Attempt to match the parameters to a valid binding.
  291. switch (paramCount)
  292. {
  293. case 3:
  294. {
  295. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  296. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL) &&
  297. (lua_type(state, 3) == LUA_TSTRING || lua_type(state, 3) == LUA_TNIL))
  298. {
  299. // Get parameter 1 off the stack.
  300. Texture::Wrap param1 = (Texture::Wrap)lua_enumFromString_TextureWrap(luaL_checkstring(state, 2));
  301. // Get parameter 2 off the stack.
  302. Texture::Wrap param2 = (Texture::Wrap)lua_enumFromString_TextureWrap(luaL_checkstring(state, 3));
  303. Texture::Sampler* instance = getInstance(state);
  304. instance->setWrapMode(param1, param2);
  305. return 0;
  306. }
  307. else
  308. {
  309. lua_pushstring(state, "lua_TextureSampler_setWrapMode - Failed to match the given parameters to a valid function signature.");
  310. lua_error(state);
  311. }
  312. break;
  313. }
  314. default:
  315. {
  316. lua_pushstring(state, "Invalid number of parameters (expected 3).");
  317. lua_error(state);
  318. break;
  319. }
  320. }
  321. return 0;
  322. }
  323. int lua_TextureSampler_static_create(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 1:
  331. {
  332. if ((lua_type(state, 1) == LUA_TUSERDATA || lua_type(state, 1) == LUA_TTABLE || lua_type(state, 1) == LUA_TNIL))
  333. {
  334. // Get parameter 1 off the stack.
  335. ScriptUtil::LuaArray<Texture> param1 = ScriptUtil::getObjectPointer<Texture>(1, "Texture", false);
  336. void* returnPtr = (void*)Texture::Sampler::create(param1);
  337. if (returnPtr)
  338. {
  339. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
  340. object->instance = returnPtr;
  341. object->owns = true;
  342. luaL_getmetatable(state, "TextureSampler");
  343. lua_setmetatable(state, -2);
  344. }
  345. else
  346. {
  347. lua_pushnil(state);
  348. }
  349. return 1;
  350. }
  351. else if ((lua_type(state, 1) == LUA_TSTRING || lua_type(state, 1) == LUA_TNIL))
  352. {
  353. // Get parameter 1 off the stack.
  354. ScriptUtil::LuaArray<const char> param1 = ScriptUtil::getString(1, false);
  355. void* returnPtr = (void*)Texture::Sampler::create(param1);
  356. if (returnPtr)
  357. {
  358. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
  359. object->instance = returnPtr;
  360. object->owns = true;
  361. luaL_getmetatable(state, "TextureSampler");
  362. lua_setmetatable(state, -2);
  363. }
  364. else
  365. {
  366. lua_pushnil(state);
  367. }
  368. return 1;
  369. }
  370. else
  371. {
  372. lua_pushstring(state, "lua_TextureSampler_static_create - Failed to match the given parameters to a valid function signature.");
  373. lua_error(state);
  374. }
  375. break;
  376. }
  377. case 2:
  378. {
  379. if ((lua_type(state, 1) == LUA_TSTRING || lua_type(state, 1) == LUA_TNIL) &&
  380. lua_type(state, 2) == LUA_TBOOLEAN)
  381. {
  382. // Get parameter 1 off the stack.
  383. ScriptUtil::LuaArray<const char> param1 = ScriptUtil::getString(1, false);
  384. // Get parameter 2 off the stack.
  385. bool param2 = ScriptUtil::luaCheckBool(state, 2);
  386. void* returnPtr = (void*)Texture::Sampler::create(param1, param2);
  387. if (returnPtr)
  388. {
  389. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(state, sizeof(ScriptUtil::LuaObject));
  390. object->instance = returnPtr;
  391. object->owns = true;
  392. luaL_getmetatable(state, "TextureSampler");
  393. lua_setmetatable(state, -2);
  394. }
  395. else
  396. {
  397. lua_pushnil(state);
  398. }
  399. return 1;
  400. }
  401. else
  402. {
  403. lua_pushstring(state, "lua_TextureSampler_static_create - Failed to match the given parameters to a valid function signature.");
  404. lua_error(state);
  405. }
  406. break;
  407. }
  408. default:
  409. {
  410. lua_pushstring(state, "Invalid number of parameters (expected 1 or 2).");
  411. lua_error(state);
  412. break;
  413. }
  414. }
  415. return 0;
  416. }
  417. }