/src/bindings/bind_renderer.cpp

https://bitbucket.org/vivkin/gam3b00bs/ · C++ · 184 lines · 149 code · 27 blank · 8 comment · 7 complexity · 21750746e8022191d354e38e70dbffd1 MD5 · raw file

  1. #include "common.h"
  2. #include "bind_all.h"
  3. #include "renderer/visual.h"
  4. #include <d3d9.h>
  5. //-----------------------------------------------------------------------------
  6. #define TABLE_GET_STRING(name, value) lua_pushstring(L, name); \
  7. lua_gettable(L, -2); \
  8. if( lua_isstring(L, -1) ) \
  9. *value = lua_tostring(L, -1); \
  10. lua_pop(L, 1);
  11. //-----------------------------------------------------------------------------
  12. #define TABLE_GET_TABLE_BEGIN(name) lua_pushstring(L, name); \
  13. lua_gettable(L, -2); \
  14. if( lua_istable(L, -1) ) \
  15. {
  16. //-----------------------------------------------------------------------------
  17. #define TABLE_GET_TABLE_END } \
  18. lua_pop(L, 1);
  19. //-----------------------------------------------------------------------------
  20. #define TABLE_GET_ARRAY_BEGIN(start_index) for( uint32 _index = start_index;; _index++ ){ \
  21. lua_pushinteger(L, _index); \
  22. lua_gettable(L, -2); \
  23. if( lua_istable(L, -1) ) \
  24. { \
  25. //-----------------------------------------------------------------------------
  26. #define TABLE_GET_ARRAY_END } \
  27. else { \
  28. break; } \
  29. lua_pop(L, 1); \
  30. } \
  31. lua_pop(L, 1); \
  32. //-----------------------------------------------------------------------------
  33. #define TABLE_GET_FLOAT(name, value) lua_pushstring(L, name); \
  34. lua_gettable(L, -2); \
  35. if( lua_isnumber(L, -1) ) \
  36. *value = (float)lua_tonumber(L, -1); \
  37. lua_pop(L, 1);
  38. //-----------------------------------------------------------------------------
  39. #define TABLE_GET_COLOR(name, r, g, b, a) TABLE_GET_TABLE_BEGIN(name); \
  40. lua_pushinteger(L, 1); \
  41. lua_gettable(L, -2); \
  42. r = (float)lua_tonumber(L, -1); \
  43. lua_pop(L, 1); \
  44. lua_pushinteger(L, 2); \
  45. lua_gettable(L, -2); \
  46. g = (float)lua_tonumber(L, -1); \
  47. lua_pop(L, 1); \
  48. lua_pushinteger(L, 3); \
  49. lua_gettable(L, -2); \
  50. b = (float)lua_tonumber(L, -1); \
  51. lua_pop(L, 1); \
  52. lua_pushinteger(L, 4); \
  53. lua_gettable(L, -2); \
  54. a = (float)lua_tonumber(L, -1); \
  55. lua_pop(L, 1); \
  56. TABLE_GET_TABLE_END
  57. //-----------------------------------------------------------------------------
  58. namespace luab
  59. {
  60. static int bind_visual_create( lua_State * L )
  61. {
  62. const char* mesh_name = 0;
  63. const char* texture0 = 0;
  64. const char* texture1 = 0;
  65. D3DMATERIAL9 mat = {0};
  66. visual_h vis;
  67. TABLE_GET_STRING("Mesh", &mesh_name);
  68. TABLE_GET_TABLE_BEGIN("Texture");
  69. lua_pushinteger(L, 1);
  70. lua_gettable(L, -2);
  71. texture0 = lua_tostring(L, -1);
  72. lua_pop(L, 1);
  73. lua_pushinteger(L, 2);
  74. lua_gettable(L, -2);
  75. texture1 = lua_tostring(L, -1);
  76. lua_pop(L, 1);
  77. TABLE_GET_TABLE_END
  78. TABLE_GET_TABLE_BEGIN("Material");
  79. TABLE_GET_COLOR("Diffuse", mat.Diffuse.r, mat.Diffuse.g, mat.Diffuse.b, mat.Diffuse.a);
  80. TABLE_GET_COLOR("Ambient", mat.Ambient.r, mat.Ambient.g, mat.Ambient.b, mat.Ambient.a);
  81. TABLE_GET_COLOR("Specular", mat.Specular.r, mat.Specular.g, mat.Specular.b, mat.Specular.a);
  82. TABLE_GET_COLOR("Emissive", mat.Emissive.r, mat.Emissive.g, mat.Emissive.b, mat.Emissive.a);
  83. TABLE_GET_FLOAT("Power", &mat.Power);
  84. TABLE_GET_TABLE_END
  85. vis = visual_create(mesh_name, texture0, texture1);
  86. if( !vis )
  87. return 0;
  88. vis->material = mat;
  89. lua_pushnumber(L, (uint32)vis);
  90. return 1;
  91. }
  92. static int bind_renderer_tick( lua_State * L )
  93. {
  94. renderer::tick();
  95. return 1;
  96. }
  97. static int bind_visual_render( lua_State * L )
  98. {
  99. uint32 i;
  100. visual_h vis;
  101. i = (uint32)lua_tonumber(L, 1);
  102. vis = (visual_h)i;
  103. visual_render(vis);
  104. return 1;
  105. }
  106. static int bind_visual_set_rotation( lua_State * L )
  107. {
  108. uint32 i;
  109. visual_h vis;
  110. float pitch, yaw, roll;
  111. i = (uint32)lua_tonumber(L, 1);
  112. vis = (visual_h)i;
  113. pitch = (float)lua_tonumber(L, 2);
  114. yaw = (float)lua_tonumber(L, 3);
  115. roll = (float)lua_tonumber(L, 4);
  116. visual_set_rotation(vis, pitch, yaw, roll);
  117. return 1;
  118. }
  119. static int bind_visual_set_position( lua_State * L )
  120. {
  121. uint32 i;
  122. visual_h vis;
  123. float x, y, z;
  124. i = (uint32)lua_tonumber(L, 1);
  125. vis = (visual_h)i;
  126. x = (float)lua_tonumber(L, 2);
  127. y = (float)lua_tonumber(L, 3);
  128. z = (float)lua_tonumber(L, 4);
  129. visual_set_position(vis, D3DXVECTOR3(x, y, z));
  130. return 1;
  131. }
  132. static int bind_visual_set_scale( lua_State * L )
  133. {
  134. uint32 i;
  135. visual_h vis;
  136. float scale;
  137. i = (uint32)lua_tonumber(L, 1);
  138. vis = (visual_h)i;
  139. scale = (float)lua_tonumber(L, 2);
  140. visual_set_scaleu(vis, scale);
  141. return 1;
  142. }
  143. static const struct luaL_reg funcs[] =
  144. {
  145. { "visual_create", bind_visual_create },
  146. { "tick", bind_renderer_tick },
  147. { "visual_render", bind_visual_render },
  148. { "visual_set_rotation", bind_visual_set_rotation },
  149. { "visual_set_position", bind_visual_set_position },
  150. { "visual_set_scale", bind_visual_set_scale },
  151. {0,0}
  152. };
  153. void bind_renderer( lua_State * L )
  154. {
  155. luaL_register( L, "renderer", funcs );
  156. }
  157. }