PageRenderTime 28ms CodeModel.GetById 16ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

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