/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
- #include "common.h"
- #include "bind_all.h"
- #include "renderer/visual.h"
- #include <d3d9.h>
-
- //-----------------------------------------------------------------------------
- #define TABLE_GET_STRING(name, value) lua_pushstring(L, name); \
- lua_gettable(L, -2); \
- if( lua_isstring(L, -1) ) \
- *value = lua_tostring(L, -1); \
- lua_pop(L, 1);
- //-----------------------------------------------------------------------------
-
- #define TABLE_GET_TABLE_BEGIN(name) lua_pushstring(L, name); \
- lua_gettable(L, -2); \
- if( lua_istable(L, -1) ) \
- {
- //-----------------------------------------------------------------------------
-
- #define TABLE_GET_TABLE_END } \
- lua_pop(L, 1);
- //-----------------------------------------------------------------------------
-
- #define TABLE_GET_ARRAY_BEGIN(start_index) for( uint32 _index = start_index;; _index++ ){ \
- lua_pushinteger(L, _index); \
- lua_gettable(L, -2); \
- if( lua_istable(L, -1) ) \
- { \
- //-----------------------------------------------------------------------------
-
- #define TABLE_GET_ARRAY_END } \
- else { \
- break; } \
- lua_pop(L, 1); \
- } \
- lua_pop(L, 1); \
- //-----------------------------------------------------------------------------
-
- #define TABLE_GET_FLOAT(name, value) lua_pushstring(L, name); \
- lua_gettable(L, -2); \
- if( lua_isnumber(L, -1) ) \
- *value = (float)lua_tonumber(L, -1); \
- lua_pop(L, 1);
- //-----------------------------------------------------------------------------
-
- #define TABLE_GET_COLOR(name, r, g, b, a) TABLE_GET_TABLE_BEGIN(name); \
- lua_pushinteger(L, 1); \
- lua_gettable(L, -2); \
- r = (float)lua_tonumber(L, -1); \
- lua_pop(L, 1); \
- lua_pushinteger(L, 2); \
- lua_gettable(L, -2); \
- g = (float)lua_tonumber(L, -1); \
- lua_pop(L, 1); \
- lua_pushinteger(L, 3); \
- lua_gettable(L, -2); \
- b = (float)lua_tonumber(L, -1); \
- lua_pop(L, 1); \
- lua_pushinteger(L, 4); \
- lua_gettable(L, -2); \
- a = (float)lua_tonumber(L, -1); \
- lua_pop(L, 1); \
- TABLE_GET_TABLE_END
- //-----------------------------------------------------------------------------
-
- namespace luab
- {
- static int bind_visual_create( lua_State * L )
- {
- const char* mesh_name = 0;
- const char* texture0 = 0;
- const char* texture1 = 0;
- D3DMATERIAL9 mat = {0};
- visual_h vis;
-
- TABLE_GET_STRING("Mesh", &mesh_name);
- TABLE_GET_TABLE_BEGIN("Texture");
- lua_pushinteger(L, 1);
- lua_gettable(L, -2);
- texture0 = lua_tostring(L, -1);
- lua_pop(L, 1);
- lua_pushinteger(L, 2);
- lua_gettable(L, -2);
- texture1 = lua_tostring(L, -1);
- lua_pop(L, 1);
- TABLE_GET_TABLE_END
- TABLE_GET_TABLE_BEGIN("Material");
- TABLE_GET_COLOR("Diffuse", mat.Diffuse.r, mat.Diffuse.g, mat.Diffuse.b, mat.Diffuse.a);
- TABLE_GET_COLOR("Ambient", mat.Ambient.r, mat.Ambient.g, mat.Ambient.b, mat.Ambient.a);
- TABLE_GET_COLOR("Specular", mat.Specular.r, mat.Specular.g, mat.Specular.b, mat.Specular.a);
- TABLE_GET_COLOR("Emissive", mat.Emissive.r, mat.Emissive.g, mat.Emissive.b, mat.Emissive.a);
- TABLE_GET_FLOAT("Power", &mat.Power);
- TABLE_GET_TABLE_END
-
- vis = visual_create(mesh_name, texture0, texture1);
- if( !vis )
- return 0;
-
- vis->material = mat;
-
- lua_pushnumber(L, (uint32)vis);
-
- return 1;
- }
-
- static int bind_renderer_tick( lua_State * L )
- {
- renderer::tick();
- return 1;
- }
-
- static int bind_visual_render( lua_State * L )
- {
- uint32 i;
- visual_h vis;
- i = (uint32)lua_tonumber(L, 1);
- vis = (visual_h)i;
- visual_render(vis);
- return 1;
- }
-
- static int bind_visual_set_rotation( lua_State * L )
- {
- uint32 i;
- visual_h vis;
- float pitch, yaw, roll;
- i = (uint32)lua_tonumber(L, 1);
- vis = (visual_h)i;
-
- pitch = (float)lua_tonumber(L, 2);
- yaw = (float)lua_tonumber(L, 3);
- roll = (float)lua_tonumber(L, 4);
-
- visual_set_rotation(vis, pitch, yaw, roll);
- return 1;
- }
-
- static int bind_visual_set_position( lua_State * L )
- {
- uint32 i;
- visual_h vis;
- float x, y, z;
- i = (uint32)lua_tonumber(L, 1);
- vis = (visual_h)i;
-
- x = (float)lua_tonumber(L, 2);
- y = (float)lua_tonumber(L, 3);
- z = (float)lua_tonumber(L, 4);
-
- visual_set_position(vis, D3DXVECTOR3(x, y, z));
- return 1;
- }
-
- static int bind_visual_set_scale( lua_State * L )
- {
- uint32 i;
- visual_h vis;
- float scale;
- i = (uint32)lua_tonumber(L, 1);
- vis = (visual_h)i;
-
- scale = (float)lua_tonumber(L, 2);
-
- visual_set_scaleu(vis, scale);
- return 1;
- }
-
- static const struct luaL_reg funcs[] =
- {
- { "visual_create", bind_visual_create },
- { "tick", bind_renderer_tick },
- { "visual_render", bind_visual_render },
- { "visual_set_rotation", bind_visual_set_rotation },
- { "visual_set_position", bind_visual_set_position },
- { "visual_set_scale", bind_visual_set_scale },
- {0,0}
- };
-
- void bind_renderer( lua_State * L )
- {
- luaL_register( L, "renderer", funcs );
- }
- }