lua_stack.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * Copyright (c) 2012-2023 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: MIT
  4. */
  5. #pragma once
  6. #include "config.h"
  7. #include "core/math/types.h"
  8. #include "core/strings/string_id.h"
  9. #include "core/types.h"
  10. #include "resource/types.h"
  11. #include "world/types.h"
  12. struct lua_State;
  13. typedef int (*lua_CFunction)(lua_State *L);
  14. #ifndef LUA_OK
  15. #define LUA_OK 0
  16. #endif
  17. #define LIGHTDATA_TYPE_MASK uintptr_t(0x3)
  18. #define LIGHTDATA_TYPE_SHIFT uintptr_t(0)
  19. #define LIGHTDATA_POINTER_MARKER uintptr_t(0)
  20. #define LIGHTDATA_UNIT_MARKER uintptr_t(1)
  21. #define LIGHTDATA_UNIT_ID_MASK uintptr_t(0xfffffffc)
  22. #define LIGHTDATA_UNIT_ID_SHIFT uintptr_t(2)
  23. #define LUA_VECTOR3_MARKER_MASK uintptr_t(0x03)
  24. #define LUA_VECTOR3_MARKER_SHIFT uintptr_t(0)
  25. #define LUA_QUATERNION_MARKER_MASK uintptr_t(0x0f)
  26. #define LUA_QUATERNION_MARKER_SHIFT uintptr_t(0)
  27. #define LUA_MATRIX4X4_MARKER_MASK uintptr_t(0x3f)
  28. #define LUA_MATRIX4X4_MARKER_SHIFT uintptr_t(0)
  29. namespace crown
  30. {
  31. /// Wrapper to manipulate Lua stack.
  32. ///
  33. /// @ingroup Lua
  34. struct LuaStack
  35. {
  36. lua_State *L;
  37. ///
  38. explicit LuaStack(lua_State *L);
  39. /// Returns the number of elements in the stack.
  40. /// When called inside a function, it can be used to count
  41. /// the number of arguments passed to the function itself.
  42. int num_args();
  43. /// Removes the element at the given valid index, shifting down the elements
  44. /// above this index to fill the gap. Cannot be called with a pseudo-index,
  45. /// because a pseudo-index is not an actual stack position.
  46. void remove(int i);
  47. /// Pops @a n elements from the stack.
  48. void pop(int n);
  49. ///
  50. bool is_nil(int i);
  51. ///
  52. bool is_bool(int i);
  53. ///
  54. bool is_number(int i);
  55. ///
  56. bool is_string(int i);
  57. ///
  58. bool is_pointer(int i);
  59. ///
  60. bool is_function(int i);
  61. ///
  62. bool is_table(int i);
  63. ///
  64. bool is_vector3(int i);
  65. ///
  66. bool is_quaternion(int i);
  67. ///
  68. bool is_matrix4x4(int i);
  69. /// Wraps lua_type.
  70. int value_type(int i);
  71. ///
  72. bool get_bool(int i);
  73. ///
  74. int get_int(int i);
  75. ///
  76. f32 get_float(int i);
  77. ///
  78. const char *get_string(int i);
  79. ///
  80. void *get_pointer(int i);
  81. ///
  82. u32 get_id(int i);
  83. ///
  84. StringId32 get_string_id_32(int i);
  85. ///
  86. StringId64 get_string_id_64(int i);
  87. ///
  88. StringId64 get_resource_name(int i);
  89. ///
  90. Gui *get_gui(int i);
  91. ///
  92. DebugLine *get_debug_line(int i);
  93. ///
  94. ResourcePackage *get_resource_package(int i);
  95. ///
  96. World *get_world(int i);
  97. ///
  98. SceneGraph *get_scene_graph(int i);
  99. ///
  100. Level *get_level(int i);
  101. ///
  102. RenderWorld *get_render_world(int i);
  103. ///
  104. PhysicsWorld *get_physics_world(int i);
  105. ///
  106. SoundWorld *get_sound_world(int i);
  107. ///
  108. ScriptWorld *get_script_world(int i);
  109. ///
  110. AnimationStateMachine *get_animation_state_machine(int i);
  111. ///
  112. bool is_unit(int i);
  113. ///
  114. UnitId get_unit(int i);
  115. ///
  116. CameraInstance get_camera_instance(int i);
  117. ///
  118. TransformInstance get_transform_instance(int i);
  119. ///
  120. MeshInstance get_mesh_instance(int i);
  121. ///
  122. SpriteInstance get_sprite_instance(int i);
  123. ///
  124. LightInstance get_light_instance(int i);
  125. ///
  126. StateMachineInstance get_state_machine_instance(int i);
  127. ///
  128. Material *get_material(int i);
  129. ///
  130. ActorInstance get_actor_instance(int i);
  131. ///
  132. SoundInstanceId get_sound_instance_id(int i);
  133. ///
  134. ScriptInstance get_script_instance(int i);
  135. ///
  136. Vector2 get_vector2(int i);
  137. ///
  138. Vector3 &get_vector3(int i);
  139. ///
  140. Vector4 get_vector4(int i);
  141. ///
  142. Quaternion &get_quaternion(int i);
  143. ///
  144. Matrix4x4 &get_matrix4x4(int i);
  145. ///
  146. Color4 get_color4(int i);
  147. ///
  148. Vector2 &get_vector2box(int i);
  149. ///
  150. Vector3 &get_vector3box(int i);
  151. ///
  152. Quaternion &get_quaternionbox(int i);
  153. ///
  154. Matrix4x4 &get_matrix4x4box(int i);
  155. ///
  156. void push_nil();
  157. ///
  158. void push_bool(bool value);
  159. ///
  160. void push_int(int value);
  161. ///
  162. void push_float(f32 value);
  163. ///
  164. void push_string(const char *s);
  165. ///
  166. void push_fstring(const char *fmt, ...);
  167. ///
  168. void push_lstring(const char *s, u32 len);
  169. ///
  170. void push_string_id(StringId32 value);
  171. ///
  172. void push_pointer(void *p);
  173. ///
  174. void push_function(lua_CFunction f);
  175. ///
  176. void push_id(u32 value);
  177. /// Pushes an empty table onto the stack.
  178. /// When you want to set keys on the table, you have to use LuaStack::push_key_begin()
  179. /// and LuaStack::push_key_end() as in the following example:
  180. ///
  181. /// LuaStack stack(L)
  182. /// stack.push_table()
  183. /// stack.push_key_begin("foo"); stack.push_foo(); stack.push_key_end()
  184. /// stack.push_key_begin("bar"); stack.push_bar(); stack.push_key_end()
  185. /// return 1;
  186. void push_table(int narr = 0, int nrec = 0);
  187. /// See Stack::push_table()
  188. void push_key_begin(const char *key);
  189. /// See Stack::push_table()
  190. void push_key_begin(int i);
  191. /// See Stack::push_table()
  192. void push_key_end();
  193. ///
  194. int next(int i);
  195. ///
  196. void push_gui(Gui *dg);
  197. ///
  198. void push_debug_line(DebugLine *line);
  199. ///
  200. void push_resource_package(ResourcePackage *package);
  201. ///
  202. void push_world(World *world);
  203. ///
  204. void push_scene_graph(SceneGraph *sg);
  205. ///
  206. void push_level(Level *level);
  207. ///
  208. void push_render_world(RenderWorld *world);
  209. ///
  210. void push_physics_world(PhysicsWorld *world);
  211. ///
  212. void push_sound_world(SoundWorld *world);
  213. ///
  214. void push_script_world(ScriptWorld *world);
  215. ///
  216. void push_animation_state_machine(AnimationStateMachine *sm);
  217. ///
  218. void push_unit(UnitId unit);
  219. ///
  220. void push_camera(CameraInstance i);
  221. ///
  222. void push_transform(TransformInstance i);
  223. ///
  224. void push_mesh_instance(MeshInstance i);
  225. ///
  226. void push_sprite_instance(SpriteInstance i);
  227. ///
  228. void push_light_instance(LightInstance i);
  229. ///
  230. void push_state_machine_instance(StateMachineInstance i);
  231. ///
  232. void push_material(Material *material);
  233. ///
  234. void push_actor(ActorInstance i);
  235. ///
  236. void push_sound_instance_id(SoundInstanceId id);
  237. ///
  238. void push_script_instance(ScriptInstance i);
  239. ///
  240. void push_vector2(const Vector2 &v);
  241. ///
  242. void push_vector3(const Vector3 &v);
  243. ///
  244. void push_matrix4x4(const Matrix4x4 &m);
  245. ///
  246. void push_quaternion(const Quaternion &q);
  247. ///
  248. void push_color4(const Color4 &c);
  249. ///
  250. void push_vector2box(const Vector2 &v);
  251. ///
  252. void push_vector3box(const Vector3 &v);
  253. ///
  254. void push_quaternionbox(const Quaternion &q);
  255. ///
  256. void push_matrix4x4box(const Matrix4x4 &m);
  257. ///
  258. void push_value(int i);
  259. ///
  260. void call(int nresults);
  261. ///
  262. Vector3 *check_temporary(int i, const Vector3 *p);
  263. ///
  264. Quaternion *check_temporary(int i, const Quaternion *p);
  265. ///
  266. Matrix4x4 *check_temporary(int i, const Matrix4x4 *p);
  267. ///
  268. void check_marker(int i, const void *p, u32 type_marker, const char *type_name);
  269. };
  270. } // namespace crown