LuaStack.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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_int(int32_t value)
  53. {
  54. lua_pushinteger(m_state, value);
  55. }
  56. //-----------------------------------------------------------------------------
  57. void LuaStack::push_float(float value)
  58. {
  59. lua_pushnumber(m_state, value);
  60. }
  61. //-----------------------------------------------------------------------------
  62. void LuaStack::push_string(const char* str, size_t len)
  63. {
  64. lua_pushlstring(m_state, str, len);
  65. }
  66. //-----------------------------------------------------------------------------
  67. void LuaStack::push_vec2(Vec2* v)
  68. {
  69. ce_assert(v >= &vec2_buffer[0] && v <= &vec2_buffer[LUA_VEC2_BUFFER_SIZE-1], "Vec2 type error");
  70. lua_pushlightuserdata(m_state, v);
  71. }
  72. //-----------------------------------------------------------------------------
  73. void LuaStack::push_vec3(Vec3* v)
  74. {
  75. ce_assert(v >= &vec3_buffer[0] && v <= &vec3_buffer[LUA_VEC3_BUFFER_SIZE-1], "Vec3 type error");
  76. lua_pushlightuserdata(m_state, v);
  77. }
  78. //-----------------------------------------------------------------------------
  79. void LuaStack::push_mat4(Mat4* m)
  80. {
  81. ce_assert(m >= &mat4_buffer[0] && m <= &mat4_buffer[LUA_MAT4_BUFFER_SIZE-1], "Mat4 type error");
  82. lua_pushlightuserdata(m_state, m);
  83. }
  84. //-----------------------------------------------------------------------------
  85. void LuaStack::push_quat(Quat* q)
  86. {
  87. ce_assert(q >= &quat_buffer[0] && q <= &quat_buffer[LUA_MAT4_BUFFER_SIZE-1], "Quat type error");
  88. lua_pushlightuserdata(m_state, q);
  89. }
  90. //-----------------------------------------------------------------------------
  91. bool LuaStack::get_bool(int32_t index)
  92. {
  93. return (bool) luaL_checkinteger(m_state, index);
  94. }
  95. //-----------------------------------------------------------------------------
  96. int32_t LuaStack::get_int(int32_t index)
  97. {
  98. return luaL_checkinteger(m_state, index);
  99. }
  100. //-----------------------------------------------------------------------------
  101. float LuaStack::get_float(int32_t index)
  102. {
  103. return luaL_checknumber(m_state, index);
  104. }
  105. //-----------------------------------------------------------------------------
  106. const char* LuaStack::get_string(int32_t index)
  107. {
  108. return luaL_checkstring(m_state, index);
  109. }
  110. //-----------------------------------------------------------------------------
  111. Vec2* LuaStack::get_vec2(int32_t index)
  112. {
  113. ce_assert(lua_islightuserdata(m_state, index), "Not a lightuserdata object");
  114. Vec2* v = (Vec2*)lua_touserdata(m_state, index);
  115. ce_assert(v >= &vec2_buffer[0] && v <= &vec2_buffer[LUA_VEC2_BUFFER_SIZE-1], "Vec2 type error");
  116. return v;
  117. }
  118. //-----------------------------------------------------------------------------
  119. Vec3* LuaStack::get_vec3(int32_t index)
  120. {
  121. ce_assert(lua_islightuserdata(m_state, index), "Not a lightuserdata object");
  122. Vec3* v = (Vec3*)lua_touserdata(m_state, index);
  123. ce_assert(v >= &vec3_buffer[0] && v <= &vec3_buffer[LUA_VEC3_BUFFER_SIZE-1], "Vec3 type error");
  124. return v;
  125. }
  126. //-----------------------------------------------------------------------------
  127. Mat4* LuaStack::get_mat4(int32_t index)
  128. {
  129. ce_assert(lua_islightuserdata(m_state, index), "Not a lightuserdata object");
  130. Mat4* m = (Mat4*)lua_touserdata(m_state, index);
  131. ce_assert(m >= &mat4_buffer[0] && m <= &mat4_buffer[LUA_MAT4_BUFFER_SIZE-1], "Mat4 type error");
  132. return m;
  133. }
  134. //-----------------------------------------------------------------------------
  135. Quat* LuaStack::get_quat(int32_t index)
  136. {
  137. ce_assert(lua_islightuserdata(m_state, index), "Not a lightuserdata object");
  138. Quat* q = (Quat*)lua_touserdata(m_state, index);
  139. ce_assert(q >= &quat_buffer[0] && q <= &quat_buffer[LUA_QUAT_BUFFER_SIZE-1], "Quat type error");
  140. return q;
  141. }
  142. } // namespace crown