lua_Image.cpp 9.7 KB

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