LuaStack.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. class Vector2;
  30. class Vector3;
  31. class Matrix4x4;
  32. class Quaternion;
  33. class Unit;
  34. class Camera;
  35. class World;
  36. class Mesh;
  37. class Sprite;
  38. class PhysicsWorld;
  39. class Actor;
  40. class ResourcePackage;
  41. typedef Id SoundInstanceId;
  42. void clear_lua_temporaries();
  43. class LuaStack
  44. {
  45. public:
  46. //-----------------------------------------------------------------------------
  47. LuaStack(lua_State* L)
  48. : m_state(L)
  49. {
  50. }
  51. //-----------------------------------------------------------------------------
  52. lua_State* state()
  53. {
  54. return m_state;
  55. }
  56. /// Returns the number of elements in the stack.
  57. /// When called inside a function, it can be used to count
  58. /// the number of arguments passed to the function itself.
  59. int32_t num_args()
  60. {
  61. return lua_gettop(m_state);
  62. }
  63. //-----------------------------------------------------------------------------
  64. void push_bool(bool value)
  65. {
  66. lua_pushboolean(m_state, value);
  67. }
  68. //-----------------------------------------------------------------------------
  69. void push_int32(int32_t value)
  70. {
  71. lua_pushinteger(m_state, value);
  72. }
  73. //-----------------------------------------------------------------------------
  74. void push_uint32(uint32_t value)
  75. {
  76. lua_pushinteger(m_state, value);
  77. }
  78. //-----------------------------------------------------------------------------
  79. void push_int64(int64_t value)
  80. {
  81. lua_pushinteger(m_state, value);
  82. }
  83. //-----------------------------------------------------------------------------
  84. void push_uint64(uint64_t value)
  85. {
  86. lua_pushinteger(m_state, value);
  87. }
  88. //-----------------------------------------------------------------------------
  89. void push_float(float value)
  90. {
  91. lua_pushnumber(m_state, value);
  92. }
  93. //-----------------------------------------------------------------------------
  94. void push_string(const char* s)
  95. {
  96. lua_pushstring(m_state, s);
  97. }
  98. //-----------------------------------------------------------------------------
  99. void push_literal(const char* s, size_t len)
  100. {
  101. lua_pushlstring(m_state, s, len);
  102. }
  103. //-----------------------------------------------------------------------------
  104. bool get_bool(int32_t index)
  105. {
  106. return (bool) lua_toboolean(m_state, index);
  107. }
  108. //-----------------------------------------------------------------------------
  109. int32_t get_int(int32_t index)
  110. {
  111. return luaL_checkinteger(m_state, index);
  112. }
  113. //-----------------------------------------------------------------------------
  114. float get_float(int32_t index)
  115. {
  116. return luaL_checknumber(m_state, index);
  117. }
  118. //-----------------------------------------------------------------------------
  119. const char* get_string(int32_t index)
  120. {
  121. return luaL_checkstring(m_state, index);
  122. }
  123. /// Pushes an empty table onto the stack.
  124. /// When you want to set keys on the table, you have to use LuaStack::push_key_begin()
  125. /// and LuaStack::push_key_end() as in the following example:
  126. ///
  127. /// LuaStack stack(L)
  128. /// stack.push_table()
  129. /// stack.push_key_begin("foo"); stack.push_foo(); stack.push_key_end()
  130. /// stack.push_key_begin("bar"); stack.push_bar(); stack.push_key_end()
  131. /// return 1;
  132. void push_table()
  133. {
  134. lua_newtable(m_state);
  135. }
  136. /// See Stack::push_table()
  137. void push_key_begin(const char* key)
  138. {
  139. lua_pushstring(m_state, key);
  140. }
  141. /// See Stack::push_table()
  142. void push_key_begin(int32_t i)
  143. {
  144. lua_pushnumber(m_state, i);
  145. }
  146. /// See Stack::push_table()
  147. void push_key_end()
  148. {
  149. lua_settable(m_state, -3);
  150. }
  151. //-----------------------------------------------------------------------------
  152. void push_resource_package(ResourcePackage* package)
  153. {
  154. lua_pushlightuserdata(m_state, package);
  155. }
  156. //-----------------------------------------------------------------------------
  157. ResourcePackage* get_resource_package(int32_t index)
  158. {
  159. return (ResourcePackage*) lua_touserdata(m_state, index);
  160. }
  161. //-----------------------------------------------------------------------------
  162. void push_world(World* world)
  163. {
  164. lua_pushlightuserdata(m_state, world);
  165. }
  166. //-----------------------------------------------------------------------------
  167. World* get_world(int32_t index)
  168. {
  169. return (World*) lua_touserdata(m_state, index);
  170. }
  171. //-----------------------------------------------------------------------------
  172. void push_unit(Unit* unit)
  173. {
  174. lua_pushlightuserdata(m_state, unit);
  175. }
  176. //-----------------------------------------------------------------------------
  177. Unit* get_unit(int32_t index)
  178. {
  179. return (Unit*) lua_touserdata(m_state, index);
  180. }
  181. //-----------------------------------------------------------------------------
  182. void push_camera(Camera* camera)
  183. {
  184. lua_pushlightuserdata(m_state, camera);
  185. }
  186. //-----------------------------------------------------------------------------
  187. Camera* get_camera(int32_t index)
  188. {
  189. return (Camera*) lua_touserdata(m_state, index);
  190. }
  191. //-----------------------------------------------------------------------------
  192. void push_mesh(Mesh* mesh)
  193. {
  194. lua_pushlightuserdata(m_state, mesh);
  195. }
  196. //-----------------------------------------------------------------------------
  197. Mesh* get_mesh(int32_t index)
  198. {
  199. return (Mesh*) lua_touserdata(m_state, index);
  200. }
  201. //-----------------------------------------------------------------------------
  202. void push_sprite(Sprite* sprite)
  203. {
  204. lua_pushlightuserdata(m_state, sprite);
  205. }
  206. //-----------------------------------------------------------------------------
  207. Sprite* get_sprite(int32_t index)
  208. {
  209. return (Sprite*) lua_touserdata(m_state, index);
  210. }
  211. //-----------------------------------------------------------------------------
  212. PhysicsWorld* get_physics_world(int32_t index)
  213. {
  214. return (PhysicsWorld*) lua_touserdata(m_state, index);
  215. }
  216. //-----------------------------------------------------------------------------
  217. void push_physics_world(PhysicsWorld* world)
  218. {
  219. lua_pushlightuserdata(m_state, world);
  220. }
  221. //-----------------------------------------------------------------------------
  222. void push_actor(Actor* actor)
  223. {
  224. lua_pushlightuserdata(m_state, actor);
  225. }
  226. //-----------------------------------------------------------------------------
  227. Actor* get_actor(int32_t index)
  228. {
  229. return (Actor*) lua_touserdata(m_state, index);
  230. }
  231. //-------------------------------------------------------------------------
  232. void push_sound_instance_id(const SoundInstanceId id)
  233. {
  234. uintptr_t enc = id.encode();
  235. lua_pushlightuserdata(m_state, (void*)enc);
  236. }
  237. //-------------------------------------------------------------------------
  238. SoundInstanceId get_sound_instance_id(int32_t index)
  239. {
  240. uint32_t enc = (uintptr_t) lua_touserdata(m_state, index);
  241. SoundInstanceId id;
  242. id.decode(enc);
  243. return id;
  244. }
  245. bool is_vector2(int32_t index);
  246. bool is_vector3(int32_t index);
  247. bool is_matrix4x4(int32_t index);
  248. bool is_quaternion(int32_t index);
  249. Vector2& get_vector2(int32_t index);
  250. Vector3& get_vector3(int32_t index);
  251. Matrix4x4& get_matrix4x4(int32_t index);
  252. Quaternion& get_quaternion(int32_t index);
  253. void push_vector2(const Vector2& v);
  254. void push_vector3(const Vector3& v);
  255. void push_matrix4x4(const Matrix4x4& m);
  256. void push_quaternion(const Quaternion& q);
  257. private:
  258. lua_State* m_state;
  259. };
  260. } // namespace crown