lua_RenderState.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. #include "Base.h"
  2. #include "ScriptController.h"
  3. #include "RenderState.h"
  4. #include "lua_RenderState.h"
  5. #include "lua_Global.h"
  6. namespace gameplay
  7. {
  8. void luaRegister_RenderState()
  9. {
  10. ScriptController* sc = ScriptController::getInstance();
  11. const luaL_Reg lua_members[] =
  12. {
  13. {"addRef", lua_RenderState_addRef},
  14. {"getParameter", lua_RenderState_getParameter},
  15. {"getRefCount", lua_RenderState_getRefCount},
  16. {"getStateBlock", lua_RenderState_getStateBlock},
  17. {"release", lua_RenderState_release},
  18. {"setParameterAutoBinding", lua_RenderState_setParameterAutoBinding},
  19. {"setStateBlock", lua_RenderState_setStateBlock},
  20. {NULL, NULL}
  21. };
  22. const luaL_Reg* lua_statics = NULL;
  23. std::vector<std::string> scopePath;
  24. sc->registerClass("RenderState", lua_members, NULL, lua_RenderState__gc, lua_statics, scopePath);
  25. }
  26. static RenderState* getInstance(lua_State* state)
  27. {
  28. void* userdata = luaL_checkudata(state, 1, "RenderState");
  29. luaL_argcheck(state, userdata != NULL, 1, "'RenderState' expected.");
  30. return (RenderState*)((ScriptController::LuaObject*)userdata)->instance;
  31. }
  32. int lua_RenderState__gc(lua_State* state)
  33. {
  34. // Get the number of parameters.
  35. int paramCount = lua_gettop(state);
  36. // Attempt to match the parameters to a valid binding.
  37. switch (paramCount)
  38. {
  39. case 1:
  40. {
  41. if ((lua_type(state, 1) == LUA_TUSERDATA))
  42. {
  43. void* userdata = luaL_checkudata(state, 1, "RenderState");
  44. luaL_argcheck(state, userdata != NULL, 1, "'RenderState' expected.");
  45. ScriptController::LuaObject* object = (ScriptController::LuaObject*)userdata;
  46. if (object->owns)
  47. {
  48. RenderState* instance = (RenderState*)object->instance;
  49. SAFE_RELEASE(instance);
  50. }
  51. return 0;
  52. }
  53. else
  54. {
  55. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  56. lua_error(state);
  57. }
  58. break;
  59. }
  60. default:
  61. {
  62. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  63. lua_error(state);
  64. break;
  65. }
  66. }
  67. return 0;
  68. }
  69. int lua_RenderState_addRef(lua_State* state)
  70. {
  71. // Get the number of parameters.
  72. int paramCount = lua_gettop(state);
  73. // Attempt to match the parameters to a valid binding.
  74. switch (paramCount)
  75. {
  76. case 1:
  77. {
  78. if ((lua_type(state, 1) == LUA_TUSERDATA))
  79. {
  80. RenderState* instance = getInstance(state);
  81. instance->addRef();
  82. return 0;
  83. }
  84. else
  85. {
  86. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  87. lua_error(state);
  88. }
  89. break;
  90. }
  91. default:
  92. {
  93. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  94. lua_error(state);
  95. break;
  96. }
  97. }
  98. return 0;
  99. }
  100. int lua_RenderState_getParameter(lua_State* state)
  101. {
  102. // Get the number of parameters.
  103. int paramCount = lua_gettop(state);
  104. // Attempt to match the parameters to a valid binding.
  105. switch (paramCount)
  106. {
  107. case 2:
  108. {
  109. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  110. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL))
  111. {
  112. // Get parameter 1 off the stack.
  113. const char* param1 = ScriptController::getInstance()->getString(2, false);
  114. RenderState* instance = getInstance(state);
  115. void* returnPtr = (void*)instance->getParameter(param1);
  116. if (returnPtr)
  117. {
  118. ScriptController::LuaObject* object = (ScriptController::LuaObject*)lua_newuserdata(state, sizeof(ScriptController::LuaObject));
  119. object->instance = returnPtr;
  120. object->owns = false;
  121. luaL_getmetatable(state, "MaterialParameter");
  122. lua_setmetatable(state, -2);
  123. }
  124. else
  125. {
  126. lua_pushnil(state);
  127. }
  128. return 1;
  129. }
  130. else
  131. {
  132. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  133. lua_error(state);
  134. }
  135. break;
  136. }
  137. default:
  138. {
  139. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  140. lua_error(state);
  141. break;
  142. }
  143. }
  144. return 0;
  145. }
  146. int lua_RenderState_getRefCount(lua_State* state)
  147. {
  148. // Get the number of parameters.
  149. int paramCount = lua_gettop(state);
  150. // Attempt to match the parameters to a valid binding.
  151. switch (paramCount)
  152. {
  153. case 1:
  154. {
  155. if ((lua_type(state, 1) == LUA_TUSERDATA))
  156. {
  157. RenderState* instance = getInstance(state);
  158. unsigned int result = instance->getRefCount();
  159. // Push the return value onto the stack.
  160. lua_pushunsigned(state, result);
  161. return 1;
  162. }
  163. else
  164. {
  165. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  166. lua_error(state);
  167. }
  168. break;
  169. }
  170. default:
  171. {
  172. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  173. lua_error(state);
  174. break;
  175. }
  176. }
  177. return 0;
  178. }
  179. int lua_RenderState_getStateBlock(lua_State* state)
  180. {
  181. // Get the number of parameters.
  182. int paramCount = lua_gettop(state);
  183. // Attempt to match the parameters to a valid binding.
  184. switch (paramCount)
  185. {
  186. case 1:
  187. {
  188. if ((lua_type(state, 1) == LUA_TUSERDATA))
  189. {
  190. RenderState* instance = getInstance(state);
  191. void* returnPtr = (void*)instance->getStateBlock();
  192. if (returnPtr)
  193. {
  194. ScriptController::LuaObject* object = (ScriptController::LuaObject*)lua_newuserdata(state, sizeof(ScriptController::LuaObject));
  195. object->instance = returnPtr;
  196. object->owns = false;
  197. luaL_getmetatable(state, "RenderStateStateBlock");
  198. lua_setmetatable(state, -2);
  199. }
  200. else
  201. {
  202. lua_pushnil(state);
  203. }
  204. return 1;
  205. }
  206. else
  207. {
  208. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  209. lua_error(state);
  210. }
  211. break;
  212. }
  213. default:
  214. {
  215. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  216. lua_error(state);
  217. break;
  218. }
  219. }
  220. return 0;
  221. }
  222. int lua_RenderState_release(lua_State* state)
  223. {
  224. // Get the number of parameters.
  225. int paramCount = lua_gettop(state);
  226. // Attempt to match the parameters to a valid binding.
  227. switch (paramCount)
  228. {
  229. case 1:
  230. {
  231. if ((lua_type(state, 1) == LUA_TUSERDATA))
  232. {
  233. RenderState* instance = getInstance(state);
  234. instance->release();
  235. return 0;
  236. }
  237. else
  238. {
  239. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  240. lua_error(state);
  241. }
  242. break;
  243. }
  244. default:
  245. {
  246. lua_pushstring(state, "Invalid number of parameters (expected 1).");
  247. lua_error(state);
  248. break;
  249. }
  250. }
  251. return 0;
  252. }
  253. int lua_RenderState_setParameterAutoBinding(lua_State* state)
  254. {
  255. // Get the number of parameters.
  256. int paramCount = lua_gettop(state);
  257. // Attempt to match the parameters to a valid binding.
  258. switch (paramCount)
  259. {
  260. case 3:
  261. {
  262. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  263. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL) &&
  264. (lua_type(state, 3) == LUA_TSTRING || lua_type(state, 3) == LUA_TNIL))
  265. {
  266. // Get parameter 1 off the stack.
  267. const char* param1 = ScriptController::getInstance()->getString(2, false);
  268. // Get parameter 2 off the stack.
  269. RenderState::AutoBinding param2 = (RenderState::AutoBinding)lua_enumFromString_RenderStateAutoBinding(luaL_checkstring(state, 3));
  270. RenderState* instance = getInstance(state);
  271. instance->setParameterAutoBinding(param1, param2);
  272. return 0;
  273. }
  274. else if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  275. (lua_type(state, 2) == LUA_TSTRING || lua_type(state, 2) == LUA_TNIL) &&
  276. (lua_type(state, 3) == LUA_TSTRING || lua_type(state, 3) == LUA_TNIL))
  277. {
  278. // Get parameter 1 off the stack.
  279. const char* param1 = ScriptController::getInstance()->getString(2, false);
  280. // Get parameter 2 off the stack.
  281. const char* param2 = ScriptController::getInstance()->getString(3, false);
  282. RenderState* instance = getInstance(state);
  283. instance->setParameterAutoBinding(param1, param2);
  284. return 0;
  285. }
  286. else
  287. {
  288. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  289. lua_error(state);
  290. }
  291. break;
  292. }
  293. default:
  294. {
  295. lua_pushstring(state, "Invalid number of parameters (expected 3).");
  296. lua_error(state);
  297. break;
  298. }
  299. }
  300. return 0;
  301. }
  302. int lua_RenderState_setStateBlock(lua_State* state)
  303. {
  304. // Get the number of parameters.
  305. int paramCount = lua_gettop(state);
  306. // Attempt to match the parameters to a valid binding.
  307. switch (paramCount)
  308. {
  309. case 2:
  310. {
  311. if ((lua_type(state, 1) == LUA_TUSERDATA) &&
  312. (lua_type(state, 2) == LUA_TUSERDATA || lua_type(state, 2) == LUA_TTABLE || lua_type(state, 2) == LUA_TNIL))
  313. {
  314. // Get parameter 1 off the stack.
  315. RenderState::StateBlock* param1 = ScriptController::getInstance()->getObjectPointer<RenderState::StateBlock>(2, "RenderStateStateBlock", false);
  316. RenderState* instance = getInstance(state);
  317. instance->setStateBlock(param1);
  318. return 0;
  319. }
  320. else
  321. {
  322. lua_pushstring(state, "Failed to match the given parameters to a valid function signature.");
  323. lua_error(state);
  324. }
  325. break;
  326. }
  327. default:
  328. {
  329. lua_pushstring(state, "Invalid number of parameters (expected 2).");
  330. lua_error(state);
  331. break;
  332. }
  333. }
  334. return 0;
  335. }
  336. }