2
0

LuaStack.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #include "LuaStack.h"
  24. #include "Assert.h"
  25. #include "Vec2.h"
  26. #include "Vec3.h"
  27. #include "Mat4.h"
  28. #include "Quat.h"
  29. namespace crown
  30. {
  31. static const int32_t LUA_VEC2_BUFFER_SIZE = 4096;
  32. static Vec2 g_vec2_buffer[LUA_VEC2_BUFFER_SIZE];
  33. static uint32_t g_vec2_used = 0;
  34. static const int32_t LUA_VEC3_BUFFER_SIZE = 4096;
  35. static Vec3 g_vec3_buffer[LUA_VEC3_BUFFER_SIZE];
  36. static uint32_t g_vec3_used = 0;
  37. static const int32_t LUA_MAT4_BUFFER_SIZE = 4096;
  38. static Mat4 g_mat4_buffer[LUA_MAT4_BUFFER_SIZE];
  39. static uint32_t g_mat4_used = 0;
  40. static const int32_t LUA_QUAT_BUFFER_SIZE = 4096;
  41. static Quat g_quat_buffer[LUA_QUAT_BUFFER_SIZE];
  42. static uint32_t g_quat_used = 0;
  43. //-----------------------------------------------------------------------------
  44. static Vec2* next_vec2(const Vec2& v)
  45. {
  46. CE_ASSERT(g_vec2_used < LUA_VEC2_BUFFER_SIZE, "Maximum number of Vec2 reached");
  47. return &(g_vec2_buffer[g_vec2_used++] = v);
  48. }
  49. //-----------------------------------------------------------------------------
  50. static Vec3* next_vec3(const Vec3& v)
  51. {
  52. CE_ASSERT(g_vec3_used < LUA_VEC3_BUFFER_SIZE, "Maximum number of Vec3 reached");
  53. return &(g_vec3_buffer[g_vec3_used++] = v);
  54. }
  55. //-----------------------------------------------------------------------------
  56. static Mat4* next_mat4(const Mat4& m)
  57. {
  58. CE_ASSERT(g_mat4_used < LUA_MAT4_BUFFER_SIZE, "Maximum number of Mat4 reached");
  59. return &(g_mat4_buffer[g_mat4_used++] = m);
  60. }
  61. //-----------------------------------------------------------------------------
  62. static Quat* next_quat(const Quat& q)
  63. {
  64. CE_ASSERT(g_quat_used < LUA_QUAT_BUFFER_SIZE, "Maximum number of Quat reached");
  65. return &(g_quat_buffer[g_quat_used++] = q);
  66. }
  67. //-----------------------------------------------------------------------------
  68. Vec2& LuaStack::get_vec2(int32_t index)
  69. {
  70. void* v = lua_touserdata(m_state, index);
  71. if (v < &g_vec2_buffer[0] || v > &g_vec2_buffer[LUA_VEC2_BUFFER_SIZE-1])
  72. {
  73. luaL_typerror(m_state, index, "Vec2");
  74. }
  75. return *(Vec2*)v;
  76. }
  77. //-----------------------------------------------------------------------------
  78. Vec3& LuaStack::get_vec3(int32_t index)
  79. {
  80. void* v = lua_touserdata(m_state, index);
  81. if (v < &g_vec3_buffer[0] || v > &g_vec3_buffer[LUA_VEC3_BUFFER_SIZE-1])
  82. {
  83. luaL_typerror(m_state, index, "Vec3");
  84. }
  85. return *(Vec3*)v;
  86. }
  87. //-----------------------------------------------------------------------------
  88. Mat4& LuaStack::get_mat4(int32_t index)
  89. {
  90. void* m = lua_touserdata(m_state, index);
  91. if (m < &g_mat4_buffer[0] || m > &g_mat4_buffer[LUA_MAT4_BUFFER_SIZE-1])
  92. {
  93. luaL_typerror(m_state, index, "Mat4");
  94. }
  95. return *(Mat4*)m;
  96. }
  97. //-----------------------------------------------------------------------------
  98. Quat& LuaStack::get_quat(int32_t index)
  99. {
  100. void* q = lua_touserdata(m_state, index);
  101. if (q < &g_quat_buffer[0] || q > &g_quat_buffer[LUA_QUAT_BUFFER_SIZE-1])
  102. {
  103. luaL_typerror(m_state, index, "Quat");
  104. }
  105. return *(Quat*)q;
  106. }
  107. //-----------------------------------------------------------------------------
  108. void LuaStack::push_vec2(const Vec2& v)
  109. {
  110. lua_pushlightuserdata(m_state, next_vec2(v));
  111. }
  112. //-----------------------------------------------------------------------------
  113. void LuaStack::push_vec3(const Vec3& v)
  114. {
  115. lua_pushlightuserdata(m_state, next_vec3(v));
  116. }
  117. //-----------------------------------------------------------------------------
  118. void LuaStack::push_mat4(const Mat4& m)
  119. {
  120. lua_pushlightuserdata(m_state, next_mat4(m));
  121. }
  122. //-----------------------------------------------------------------------------
  123. void LuaStack::push_quat(const Quat& q)
  124. {
  125. lua_pushlightuserdata(m_state, next_quat(q));
  126. }
  127. } // namespace crown