lua_vector2.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 "vector2.h"
  24. #include "lua_stack.h"
  25. #include "lua_environment.h"
  26. namespace crown
  27. {
  28. static int vector2_new(lua_State* L)
  29. {
  30. LuaStack stack(L);
  31. stack.push_vector2(Vector2(stack.get_float(1), stack.get_float(2)));
  32. return 1;
  33. }
  34. static int vector2_ctor(lua_State* L)
  35. {
  36. LuaStack stack(L);
  37. stack.remove(1); // Remove table
  38. return vector2_new(L);
  39. }
  40. static int vector2_x(lua_State* L)
  41. {
  42. LuaStack stack(L);
  43. stack.push_float(stack.get_vector2(1).x);
  44. return 1;
  45. }
  46. static int vector2_y(lua_State* L)
  47. {
  48. LuaStack stack(L);
  49. stack.push_float(stack.get_vector2(1).y);
  50. return 1;
  51. }
  52. static int vector2_set_x(lua_State* L)
  53. {
  54. LuaStack stack(L);
  55. stack.get_vector2(1).x = stack.get_float(2);
  56. return 0;
  57. }
  58. static int vector2_set_y(lua_State* L)
  59. {
  60. LuaStack stack(L);
  61. stack.get_vector2(1).y = stack.get_float(2);
  62. return 0;
  63. }
  64. static int vector2_values(lua_State* L)
  65. {
  66. LuaStack stack(L);
  67. Vector2& a = stack.get_vector2(1);
  68. stack.push_float(a.x);
  69. stack.push_float(a.y);
  70. return 2;
  71. }
  72. static int vector2_add(lua_State* L)
  73. {
  74. LuaStack stack(L);
  75. stack.push_vector2(stack.get_vector2(1) + stack.get_vector2(2));
  76. return 1;
  77. }
  78. static int vector2_subtract(lua_State* L)
  79. {
  80. LuaStack stack(L);
  81. stack.push_vector2(stack.get_vector2(1) - stack.get_vector2(2));
  82. return 1;
  83. }
  84. static int vector2_multiply(lua_State* L)
  85. {
  86. LuaStack stack(L);
  87. stack.push_vector2(stack.get_vector2(1) * stack.get_float(2));
  88. return 1;
  89. }
  90. static int vector2_divide(lua_State* L)
  91. {
  92. LuaStack stack(L);
  93. stack.push_vector2(stack.get_vector2(1) / stack.get_float(2));
  94. return 1;
  95. }
  96. static int vector2_dot(lua_State* L)
  97. {
  98. LuaStack stack(L);
  99. stack.push_float(vector2::dot(stack.get_vector2(1), stack.get_vector2(2)));
  100. return 1;
  101. }
  102. static int vector2_equal(lua_State* L)
  103. {
  104. LuaStack stack(L);
  105. stack.push_bool(stack.get_vector2(1) == stack.get_vector2(2));
  106. return 1;
  107. }
  108. static int vector2_length(lua_State* L)
  109. {
  110. LuaStack stack(L);
  111. stack.push_float(vector2::length(stack.get_vector2(1)));
  112. return 1;
  113. }
  114. static int vector2_squared_length(lua_State* L)
  115. {
  116. LuaStack stack(L);
  117. stack.push_float(vector2::squared_length(stack.get_vector2(1)));
  118. return 1;
  119. }
  120. static int vector2_set_length(lua_State* L)
  121. {
  122. LuaStack stack(L);
  123. vector2::set_length(stack.get_vector2(1), stack.get_float(2));
  124. return 0;
  125. }
  126. static int vector2_normalize(lua_State* L)
  127. {
  128. LuaStack stack(L);
  129. stack.push_vector2(vector2::normalize(stack.get_vector2(1)));
  130. return 1;
  131. }
  132. static int vector2_distance(lua_State* L)
  133. {
  134. LuaStack stack(L);
  135. stack.push_float(vector2::distance(stack.get_vector2(1), stack.get_vector2(2)));
  136. return 1;
  137. }
  138. static int vector2_angle(lua_State* L)
  139. {
  140. LuaStack stack(L);
  141. stack.push_float(vector2::angle(stack.get_vector2(1), stack.get_vector2(2)));
  142. return 1;
  143. }
  144. void load_vector2(LuaEnvironment& env)
  145. {
  146. env.load_module_function("Vector2", "new", vector2_new);
  147. env.load_module_function("Vector2", "x", vector2_x);
  148. env.load_module_function("Vector2", "y", vector2_y);
  149. env.load_module_function("Vector2", "set_x", vector2_set_x);
  150. env.load_module_function("Vector2", "set_y", vector2_set_y);
  151. env.load_module_function("Vector2", "values", vector2_values);
  152. env.load_module_function("Vector2", "add", vector2_add);
  153. env.load_module_function("Vector2", "subtract", vector2_subtract);
  154. env.load_module_function("Vector2", "multiply", vector2_multiply);
  155. env.load_module_function("Vector2", "divide", vector2_divide);
  156. env.load_module_function("Vector2", "dot", vector2_dot);
  157. env.load_module_function("Vector2", "equal", vector2_equal);
  158. env.load_module_function("Vector2", "length", vector2_length);
  159. env.load_module_function("Vector2", "squared_length", vector2_squared_length);
  160. env.load_module_function("Vector2", "set_length", vector2_set_length);
  161. env.load_module_function("Vector2", "normalize", vector2_normalize);
  162. env.load_module_function("Vector2", "distance", vector2_distance);
  163. env.load_module_function("Vector2", "angle", vector2_angle);
  164. env.load_module_constructor("Vector2", vector2_ctor);
  165. }
  166. } // namespace crown