LuaStack.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "lua.hpp"
  25. #include "Types.h"
  26. #include "IdTable.h"
  27. namespace crown
  28. {
  29. struct Vector2;
  30. struct Vector3;
  31. struct Matrix4x4;
  32. struct Quaternion;
  33. struct Unit;
  34. struct Camera;
  35. class World;
  36. struct Mesh;
  37. struct Sprite;
  38. class PhysicsWorld;
  39. struct Actor;
  40. struct Controller;
  41. class ResourcePackage;
  42. struct Gui;
  43. typedef Id SoundInstanceId;
  44. typedef Id GuiId;
  45. typedef Id GuiComponentId;
  46. void clear_lua_temporaries();
  47. class LuaStack
  48. {
  49. public:
  50. //-----------------------------------------------------------------------------
  51. LuaStack(lua_State* L)
  52. : m_state(L)
  53. {
  54. }
  55. //-----------------------------------------------------------------------------
  56. lua_State* state()
  57. {
  58. return m_state;
  59. }
  60. /// Returns the number of elements in the stack.
  61. /// When called inside a function, it can be used to count
  62. /// the number of arguments passed to the function itself.
  63. int32_t num_args()
  64. {
  65. return lua_gettop(m_state);
  66. }
  67. //-----------------------------------------------------------------------------
  68. void push_nil()
  69. {
  70. lua_pushnil(m_state);
  71. }
  72. //-----------------------------------------------------------------------------
  73. void push_bool(bool value)
  74. {
  75. lua_pushboolean(m_state, value);
  76. }
  77. //-----------------------------------------------------------------------------
  78. void push_int32(int32_t value)
  79. {
  80. lua_pushinteger(m_state, value);
  81. }
  82. //-----------------------------------------------------------------------------
  83. void push_uint32(uint32_t value)
  84. {
  85. lua_pushinteger(m_state, value);
  86. }
  87. //-----------------------------------------------------------------------------
  88. void push_int64(int64_t value)
  89. {
  90. lua_pushinteger(m_state, value);
  91. }
  92. //-----------------------------------------------------------------------------
  93. void push_uint64(uint64_t value)
  94. {
  95. lua_pushinteger(m_state, value);
  96. }
  97. //-----------------------------------------------------------------------------
  98. void push_float(float value)
  99. {
  100. lua_pushnumber(m_state, value);
  101. }
  102. //-----------------------------------------------------------------------------
  103. void push_string(const char* s)
  104. {
  105. lua_pushstring(m_state, s);
  106. }
  107. //-----------------------------------------------------------------------------
  108. void push_literal(const char* s, size_t len)
  109. {
  110. lua_pushlstring(m_state, s, len);
  111. }
  112. //-----------------------------------------------------------------------------
  113. bool get_bool(int32_t index)
  114. {
  115. return (bool) lua_toboolean(m_state, index);
  116. }
  117. //-----------------------------------------------------------------------------
  118. int32_t get_int(int32_t index)
  119. {
  120. return luaL_checkinteger(m_state, index);
  121. }
  122. //-----------------------------------------------------------------------------
  123. float get_float(int32_t index)
  124. {
  125. return luaL_checknumber(m_state, index);
  126. }
  127. //-----------------------------------------------------------------------------
  128. const char* get_string(int32_t index)
  129. {
  130. return luaL_checkstring(m_state, index);
  131. }
  132. /// Pushes an empty table onto the stack.
  133. /// When you want to set keys on the table, you have to use LuaStack::push_key_begin()
  134. /// and LuaStack::push_key_end() as in the following example:
  135. ///
  136. /// LuaStack stack(L)
  137. /// stack.push_table()
  138. /// stack.push_key_begin("foo"); stack.push_foo(); stack.push_key_end()
  139. /// stack.push_key_begin("bar"); stack.push_bar(); stack.push_key_end()
  140. /// return 1;
  141. void push_table()
  142. {
  143. lua_newtable(m_state);
  144. }
  145. /// See Stack::push_table()
  146. void push_key_begin(const char* key)
  147. {
  148. lua_pushstring(m_state, key);
  149. }
  150. /// See Stack::push_table()
  151. void push_key_begin(int32_t i)
  152. {
  153. lua_pushnumber(m_state, i);
  154. }
  155. /// See Stack::push_table()
  156. void push_key_end()
  157. {
  158. lua_settable(m_state, -3);
  159. }
  160. //-----------------------------------------------------------------------------
  161. void push_resource_package(ResourcePackage* package)
  162. {
  163. lua_pushlightuserdata(m_state, package);
  164. }
  165. //-----------------------------------------------------------------------------
  166. ResourcePackage* get_resource_package(int32_t index)
  167. {
  168. return (ResourcePackage*) lua_touserdata(m_state, index);
  169. }
  170. //-----------------------------------------------------------------------------
  171. void push_world(World* world)
  172. {
  173. lua_pushlightuserdata(m_state, world);
  174. }
  175. //-----------------------------------------------------------------------------
  176. World* get_world(int32_t index)
  177. {
  178. return (World*) lua_touserdata(m_state, index);
  179. }
  180. //-----------------------------------------------------------------------------
  181. void push_unit(Unit* unit)
  182. {
  183. lua_pushlightuserdata(m_state, unit);
  184. }
  185. //-----------------------------------------------------------------------------
  186. Unit* get_unit(int32_t index)
  187. {
  188. return (Unit*) lua_touserdata(m_state, index);
  189. }
  190. //-----------------------------------------------------------------------------
  191. void push_camera(Camera* camera)
  192. {
  193. lua_pushlightuserdata(m_state, camera);
  194. }
  195. //-----------------------------------------------------------------------------
  196. Camera* get_camera(int32_t index)
  197. {
  198. return (Camera*) lua_touserdata(m_state, index);
  199. }
  200. //-----------------------------------------------------------------------------
  201. void push_mesh(Mesh* mesh)
  202. {
  203. lua_pushlightuserdata(m_state, mesh);
  204. }
  205. //-----------------------------------------------------------------------------
  206. Mesh* get_mesh(int32_t index)
  207. {
  208. return (Mesh*) lua_touserdata(m_state, index);
  209. }
  210. //-----------------------------------------------------------------------------
  211. void push_sprite(Sprite* sprite)
  212. {
  213. lua_pushlightuserdata(m_state, sprite);
  214. }
  215. //-----------------------------------------------------------------------------
  216. Sprite* get_sprite(int32_t index)
  217. {
  218. return (Sprite*) lua_touserdata(m_state, index);
  219. }
  220. //-----------------------------------------------------------------------------
  221. PhysicsWorld* get_physics_world(int32_t index)
  222. {
  223. return (PhysicsWorld*) lua_touserdata(m_state, index);
  224. }
  225. //-----------------------------------------------------------------------------
  226. void push_physics_world(PhysicsWorld* world)
  227. {
  228. lua_pushlightuserdata(m_state, world);
  229. }
  230. //-----------------------------------------------------------------------------
  231. void push_actor(Actor* actor)
  232. {
  233. lua_pushlightuserdata(m_state, actor);
  234. }
  235. //-----------------------------------------------------------------------------
  236. Actor* get_actor(int32_t index)
  237. {
  238. return (Actor*) lua_touserdata(m_state, index);
  239. }
  240. //-----------------------------------------------------------------------------
  241. void push_controller(Controller* controller)
  242. {
  243. lua_pushlightuserdata(m_state, controller);
  244. }
  245. //-----------------------------------------------------------------------------
  246. Controller* get_controller(int32_t index)
  247. {
  248. return (Controller*) lua_touserdata(m_state, index);
  249. }
  250. //-----------------------------------------------------------------------------
  251. void push_sound_instance_id(const SoundInstanceId id)
  252. {
  253. uintptr_t enc = id.encode();
  254. lua_pushlightuserdata(m_state, (void*)enc);
  255. }
  256. //-----------------------------------------------------------------------------
  257. SoundInstanceId get_sound_instance_id(int32_t index)
  258. {
  259. uint32_t enc = (uintptr_t) lua_touserdata(m_state, index);
  260. SoundInstanceId id;
  261. id.decode(enc);
  262. return id;
  263. }
  264. //-----------------------------------------------------------------------------
  265. void push_gui_id(GuiId id)
  266. {
  267. uintptr_t enc = id.encode();
  268. lua_pushlightuserdata(m_state, (void*)enc);
  269. }
  270. //-----------------------------------------------------------------------------
  271. GuiId get_gui_id(int32_t index)
  272. {
  273. uint32_t enc = (uintptr_t) lua_touserdata(m_state, index);
  274. GuiId id;
  275. id.decode(enc);
  276. return id;
  277. }
  278. //-----------------------------------------------------------------------------
  279. void push_gui(Gui* gui)
  280. {
  281. lua_pushlightuserdata(m_state, gui);
  282. }
  283. //-----------------------------------------------------------------------------
  284. Gui* get_gui(int32_t index)
  285. {
  286. return (Gui*) lua_touserdata(m_state, index);
  287. }
  288. //-----------------------------------------------------------------------------
  289. void push_gui_component_id(GuiComponentId id)
  290. {
  291. uintptr_t enc = id.encode();
  292. lua_pushlightuserdata(m_state, (void*)enc);
  293. }
  294. //-----------------------------------------------------------------------------
  295. GuiComponentId get_gui_component_id(int32_t index)
  296. {
  297. uint32_t enc = (uintptr_t) lua_touserdata(m_state, index);
  298. GuiComponentId id;
  299. id.decode(enc);
  300. return id;
  301. }
  302. bool is_vector2(int32_t index);
  303. bool is_vector3(int32_t index);
  304. bool is_matrix4x4(int32_t index);
  305. bool is_quaternion(int32_t index);
  306. Vector2& get_vector2(int32_t index);
  307. Vector3& get_vector3(int32_t index);
  308. Matrix4x4& get_matrix4x4(int32_t index);
  309. Quaternion& get_quaternion(int32_t index);
  310. void push_vector2(const Vector2& v);
  311. void push_vector3(const Vector3& v);
  312. void push_matrix4x4(const Matrix4x4& m);
  313. void push_quaternion(const Quaternion& q);
  314. private:
  315. lua_State* m_state;
  316. };
  317. } // namespace crown