LuaStack.cpp 4.9 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 "Vector2.h"
  26. #include "Vector3.h"
  27. #include "Matrix4x4.h"
  28. #include "Quaternion.h"
  29. namespace crown
  30. {
  31. static const uint32_t LUA_VEC2_BUFFER_SIZE = 4096;
  32. static Vector2 g_vec2_buffer[LUA_VEC2_BUFFER_SIZE];
  33. static uint32_t g_vec2_used = 0;
  34. static const uint32_t LUA_VEC3_BUFFER_SIZE = 4096;
  35. static Vector3 g_vec3_buffer[LUA_VEC3_BUFFER_SIZE];
  36. static uint32_t g_vec3_used = 0;
  37. static const uint32_t LUA_MAT4_BUFFER_SIZE = 4096;
  38. static Matrix4x4 g_mat4_buffer[LUA_MAT4_BUFFER_SIZE];
  39. static uint32_t g_mat4_used = 0;
  40. static const uint32_t LUA_QUAT_BUFFER_SIZE = 4096;
  41. static Quaternion g_quat_buffer[LUA_QUAT_BUFFER_SIZE];
  42. static uint32_t g_quat_used = 0;
  43. //-----------------------------------------------------------------------------
  44. static Vector2* next_vec2(const Vector2& v)
  45. {
  46. CE_ASSERT(g_vec2_used < LUA_VEC2_BUFFER_SIZE, "Maximum number of Vector2 reached");
  47. return &(g_vec2_buffer[g_vec2_used++] = v);
  48. }
  49. //-----------------------------------------------------------------------------
  50. static Vector3* next_vec3(const Vector3& v)
  51. {
  52. CE_ASSERT(g_vec3_used < LUA_VEC3_BUFFER_SIZE, "Maximum number of Vector3 reached");
  53. return &(g_vec3_buffer[g_vec3_used++] = v);
  54. }
  55. //-----------------------------------------------------------------------------
  56. static Matrix4x4* next_mat4(const Matrix4x4& m)
  57. {
  58. CE_ASSERT(g_mat4_used < LUA_MAT4_BUFFER_SIZE, "Maximum number of Matrix4x4 reached");
  59. return &(g_mat4_buffer[g_mat4_used++] = m);
  60. }
  61. //-----------------------------------------------------------------------------
  62. static Quaternion* next_quat(const Quaternion& q)
  63. {
  64. CE_ASSERT(g_quat_used < LUA_QUAT_BUFFER_SIZE, "Maximum number of Quaternion reached");
  65. return &(g_quat_buffer[g_quat_used++] = q);
  66. }
  67. //-----------------------------------------------------------------------------
  68. Vector2& LuaStack::get_vector2(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, "Vector2");
  74. }
  75. return *(Vector2*)v;
  76. }
  77. //-----------------------------------------------------------------------------
  78. Vector3& LuaStack::get_vector3(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, "Vector3");
  84. }
  85. return *(Vector3*)v;
  86. }
  87. //-----------------------------------------------------------------------------
  88. Matrix4x4& LuaStack::get_matrix4x4(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, "Matrix4x4");
  94. }
  95. return *(Matrix4x4*)m;
  96. }
  97. //-----------------------------------------------------------------------------
  98. Quaternion& LuaStack::get_quaternion(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, "Quaternion");
  104. }
  105. return *(Quaternion*)q;
  106. }
  107. //-----------------------------------------------------------------------------
  108. void LuaStack::push_vector2(const Vector2& v)
  109. {
  110. lua_pushlightuserdata(m_state, next_vec2(v));
  111. }
  112. //-----------------------------------------------------------------------------
  113. void LuaStack::push_vector3(const Vector3& v)
  114. {
  115. lua_pushlightuserdata(m_state, next_vec3(v));
  116. }
  117. //-----------------------------------------------------------------------------
  118. void LuaStack::push_matrix4x4(const Matrix4x4& m)
  119. {
  120. lua_pushlightuserdata(m_state, next_mat4(m));
  121. }
  122. //-----------------------------------------------------------------------------
  123. void LuaStack::push_quaternion(const Quaternion& q)
  124. {
  125. lua_pushlightuserdata(m_state, next_quat(q));
  126. }
  127. } // namespace crown