lua_Image.cpp 9.7 KB

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