LuaStack.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #include "LuaStack.h"
  2. #include "Assert.h"
  3. namespace crown
  4. {
  5. static const int32_t LUA_VEC2_BUFFER_SIZE = 4096;
  6. static Vec2 vec2_buffer[LUA_VEC2_BUFFER_SIZE];
  7. static uint32_t vec2_used = 0;
  8. static const int32_t LUA_VEC3_BUFFER_SIZE = 4096;
  9. static Vec3 vec3_buffer[LUA_VEC3_BUFFER_SIZE];
  10. static uint32_t vec3_used = 0;
  11. static const int32_t LUA_MAT4_BUFFER_SIZE = 4096;
  12. static Mat4 mat4_buffer[LUA_MAT4_BUFFER_SIZE];
  13. static uint32_t mat4_used = 0;
  14. static const int32_t LUA_QUAT_BUFFER_SIZE = 4096;
  15. static Quat quat_buffer[LUA_QUAT_BUFFER_SIZE];
  16. static uint32_t quat_used = 0;
  17. //-----------------------------------------------------------------------------
  18. Vec2* next_vec2()
  19. {
  20. CE_ASSERT(vec2_used < LUA_VEC2_BUFFER_SIZE, "Maximum number of Vec2 reached");
  21. return &vec2_buffer[vec2_used++];
  22. }
  23. //-----------------------------------------------------------------------------
  24. Vec3* next_vec3()
  25. {
  26. CE_ASSERT(vec3_used < LUA_VEC3_BUFFER_SIZE, "Maximum number of Vec3 reached");
  27. return &vec3_buffer[vec3_used++];
  28. }
  29. //-----------------------------------------------------------------------------
  30. Mat4* next_mat4()
  31. {
  32. CE_ASSERT(mat4_used < LUA_MAT4_BUFFER_SIZE, "Maximum number of Mat4 reached");
  33. return &mat4_buffer[mat4_used++];
  34. }
  35. //-----------------------------------------------------------------------------
  36. Quat* next_quat()
  37. {
  38. CE_ASSERT(quat_used < LUA_QUAT_BUFFER_SIZE, "Maximum number of Quat reached");
  39. return &quat_buffer[quat_used++];
  40. }
  41. //-----------------------------------------------------------------------------
  42. LuaStack::LuaStack(lua_State* L)
  43. {
  44. m_state = L;
  45. }
  46. //-----------------------------------------------------------------------------
  47. void LuaStack::push_bool(bool value)
  48. {
  49. lua_pushboolean(m_state, value);
  50. }
  51. //-----------------------------------------------------------------------------
  52. void LuaStack::push_int32(int32_t value)
  53. {
  54. lua_pushinteger(m_state, value);
  55. }
  56. //-----------------------------------------------------------------------------
  57. void LuaStack::push_uint32(uint32_t value)
  58. {
  59. lua_pushinteger(m_state, value);
  60. }
  61. //-----------------------------------------------------------------------------
  62. void LuaStack::push_int64(int64_t value)
  63. {
  64. lua_pushinteger(m_state, value);
  65. }
  66. //-----------------------------------------------------------------------------
  67. void LuaStack::push_uint64(uint64_t value)
  68. {
  69. lua_pushinteger(m_state, value);
  70. }
  71. //-----------------------------------------------------------------------------
  72. void LuaStack::push_float(float value)
  73. {
  74. lua_pushnumber(m_state, value);
  75. }
  76. //-----------------------------------------------------------------------------
  77. void LuaStack::push_string(const char* str, size_t len)
  78. {
  79. lua_pushlstring(m_state, str, len);
  80. }
  81. //-----------------------------------------------------------------------------
  82. void LuaStack::push_lightdata(void* data, size_t len)
  83. {
  84. lua_pushlightuserdata(m_state, data);
  85. }
  86. //-----------------------------------------------------------------------------
  87. void LuaStack::push_vec2(Vec2* v)
  88. {
  89. CE_ASSERT(v >= &vec2_buffer[0] && v <= &vec2_buffer[LUA_VEC2_BUFFER_SIZE-1], "Vec2 type error");
  90. lua_pushlightuserdata(m_state, v);
  91. }
  92. //-----------------------------------------------------------------------------
  93. void LuaStack::push_vec3(Vec3* v)
  94. {
  95. CE_ASSERT(v >= &vec3_buffer[0] && v <= &vec3_buffer[LUA_VEC3_BUFFER_SIZE-1], "Vec3 type error");
  96. lua_pushlightuserdata(m_state, v);
  97. }
  98. //-----------------------------------------------------------------------------
  99. void LuaStack::push_mat4(Mat4* m)
  100. {
  101. CE_ASSERT(m >= &mat4_buffer[0] && m <= &mat4_buffer[LUA_MAT4_BUFFER_SIZE-1], "Mat4 type error");
  102. lua_pushlightuserdata(m_state, m);
  103. }
  104. //-----------------------------------------------------------------------------
  105. void LuaStack::push_quat(Quat* q)
  106. {
  107. CE_ASSERT(q >= &quat_buffer[0] && q <= &quat_buffer[LUA_MAT4_BUFFER_SIZE-1], "Quat type error");
  108. lua_pushlightuserdata(m_state, q);
  109. }
  110. //-----------------------------------------------------------------------------
  111. bool LuaStack::get_bool(int32_t index)
  112. {
  113. return (bool) luaL_checkinteger(m_state, index);
  114. }
  115. //-----------------------------------------------------------------------------
  116. int32_t LuaStack::get_int(int32_t index)
  117. {
  118. return luaL_checkinteger(m_state, index);
  119. }
  120. //-----------------------------------------------------------------------------
  121. float LuaStack::get_float(int32_t index)
  122. {
  123. return luaL_checknumber(m_state, index);
  124. }
  125. //-----------------------------------------------------------------------------
  126. const char* LuaStack::get_string(int32_t index)
  127. {
  128. return luaL_checkstring(m_state, index);
  129. }
  130. //-----------------------------------------------------------------------------
  131. void* LuaStack::get_lightdata(int32_t index)
  132. {
  133. CE_ASSERT(lua_islightuserdata(m_state, index), "Not a lightuserdata object");
  134. void* data = lua_touserdata(m_state, index);
  135. return data;
  136. }
  137. //-----------------------------------------------------------------------------
  138. Vec2* LuaStack::get_vec2(int32_t index)
  139. {
  140. CE_ASSERT(lua_islightuserdata(m_state, index), "Not a lightuserdata object");
  141. Vec2* v = (Vec2*)lua_touserdata(m_state, index);
  142. CE_ASSERT(v >= &vec2_buffer[0] && v <= &vec2_buffer[LUA_VEC2_BUFFER_SIZE-1], "Vec2 type error");
  143. return v;
  144. }
  145. //-----------------------------------------------------------------------------
  146. Vec3* LuaStack::get_vec3(int32_t index)
  147. {
  148. CE_ASSERT(lua_islightuserdata(m_state, index), "Not a lightuserdata object");
  149. Vec3* v = (Vec3*)lua_touserdata(m_state, index);
  150. CE_ASSERT(v >= &vec3_buffer[0] && v <= &vec3_buffer[LUA_VEC3_BUFFER_SIZE-1], "Vec3 type error");
  151. return v;
  152. }
  153. //-----------------------------------------------------------------------------
  154. Mat4* LuaStack::get_mat4(int32_t index)
  155. {
  156. CE_ASSERT(lua_islightuserdata(m_state, index), "Not a lightuserdata object");
  157. Mat4* m = (Mat4*)lua_touserdata(m_state, index);
  158. CE_ASSERT(m >= &mat4_buffer[0] && m <= &mat4_buffer[LUA_MAT4_BUFFER_SIZE-1], "Mat4 type error");
  159. return m;
  160. }
  161. //-----------------------------------------------------------------------------
  162. Quat* LuaStack::get_quat(int32_t index)
  163. {
  164. CE_ASSERT(lua_islightuserdata(m_state, index), "Not a lightuserdata object");
  165. Quat* q = (Quat*)lua_touserdata(m_state, index);
  166. CE_ASSERT(q >= &quat_buffer[0] && q <= &quat_buffer[LUA_QUAT_BUFFER_SIZE-1], "Quat type error");
  167. return q;
  168. }
  169. } // namespace crown