|
|
@@ -33,280 +33,15 @@ OTHER DEALINGS IN THE SOFTWARE.
|
|
|
#include "Device.h"
|
|
|
#include "LuaResource.h"
|
|
|
#include "ResourceManager.h"
|
|
|
+#include "Config.h"
|
|
|
|
|
|
namespace crown
|
|
|
{
|
|
|
|
|
|
-extern int vector2_add(lua_State* L);
|
|
|
-extern int vector2_subtract(lua_State* L);
|
|
|
-extern int vector2_multiply(lua_State* L);
|
|
|
-extern int vector2_divide(lua_State* L);
|
|
|
-extern int vector2_negate(lua_State* L);
|
|
|
-extern int vector3_add(lua_State* L);
|
|
|
-extern int vector3_subtract(lua_State* L);
|
|
|
-extern int vector3_multiply(lua_State* L);
|
|
|
-extern int vector3_divide(lua_State* L);
|
|
|
-extern int vector3_negate(lua_State* L);
|
|
|
-extern int vector2(lua_State* L);
|
|
|
-extern int vector3(lua_State* L);
|
|
|
-extern int matrix4x4(lua_State* L);
|
|
|
-extern int quaternion(lua_State* L);
|
|
|
-
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-CE_EXPORT int luaopen_libcrown(lua_State* /*L*/)
|
|
|
-{
|
|
|
- LuaEnvironment* env = device()->lua_environment();
|
|
|
-
|
|
|
- load_int_setting(*env);
|
|
|
- load_float_setting(*env);
|
|
|
- load_string_setting(*env);
|
|
|
-
|
|
|
- load_vector2(*env);
|
|
|
- load_vector3(*env);
|
|
|
- load_matrix4x4(*env);
|
|
|
- load_quaternion(*env);
|
|
|
- load_math(*env);
|
|
|
- load_window(*env);
|
|
|
- load_mouse(*env);
|
|
|
- load_keyboard(*env);
|
|
|
- load_touch(*env);
|
|
|
- load_accelerometer(*env);
|
|
|
- load_device(*env);
|
|
|
- load_resource_package(*env);
|
|
|
-
|
|
|
- load_unit(*env);
|
|
|
- load_camera(*env);
|
|
|
- load_world(*env);
|
|
|
- load_mesh(*env);
|
|
|
- load_sprite(*env);
|
|
|
- load_actor(*env);
|
|
|
- load_controller(*env);
|
|
|
- load_physics_world(*env);
|
|
|
- load_sound_world(*env);
|
|
|
-
|
|
|
- load_gui(*env);
|
|
|
-
|
|
|
- load_debug_line(*env);
|
|
|
-
|
|
|
- return 1;
|
|
|
-}
|
|
|
-
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-static int crown_lua_vector2_call(lua_State* L)
|
|
|
-{
|
|
|
- lua_remove(L, 1);
|
|
|
- return vector2(L);
|
|
|
-}
|
|
|
-
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-static int crown_lua_vector3_call(lua_State* L)
|
|
|
-{
|
|
|
- lua_remove(L, 1);
|
|
|
- return vector3(L);
|
|
|
-}
|
|
|
-
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-static int crown_lua_matrix4x4_call(lua_State* L)
|
|
|
-{
|
|
|
- lua_remove(L, 1);
|
|
|
- return matrix4x4(L);
|
|
|
-}
|
|
|
-
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-static int crown_lua_quaternion_call(lua_State* L)
|
|
|
-{
|
|
|
- lua_remove(L, 1);
|
|
|
- return quaternion(L);
|
|
|
-}
|
|
|
-
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-static int crown_lua_lightuserdata_add(lua_State* L)
|
|
|
-{
|
|
|
- LuaStack stack(L);
|
|
|
-
|
|
|
- if (stack.is_vector3(1))
|
|
|
- {
|
|
|
- return vector3_add(L);
|
|
|
- }
|
|
|
- return vector2_add(L);
|
|
|
-}
|
|
|
-
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-static int crown_lua_lightuserdata_sub(lua_State* L)
|
|
|
-{
|
|
|
- LuaStack stack(L);
|
|
|
-
|
|
|
- if (stack.is_vector3(1))
|
|
|
- {
|
|
|
- return vector3_subtract(L);
|
|
|
- }
|
|
|
- return vector2_subtract(L);
|
|
|
-}
|
|
|
-
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-static int crown_lua_lightuserdata_mul(lua_State* L)
|
|
|
-{
|
|
|
- LuaStack stack(L);
|
|
|
-
|
|
|
- if (stack.is_vector3(1))
|
|
|
- {
|
|
|
- return vector3_multiply(L);
|
|
|
- }
|
|
|
- return vector2_multiply(L);
|
|
|
-}
|
|
|
-
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-static int crown_lua_lightuserdata_div(lua_State* L)
|
|
|
-{
|
|
|
- LuaStack stack(L);
|
|
|
-
|
|
|
- if (stack.is_vector3(1))
|
|
|
- {
|
|
|
- return vector3_divide(L);
|
|
|
- }
|
|
|
- return vector2_divide(L);
|
|
|
-}
|
|
|
-
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-static int crown_lua_lightuserdata_unm(lua_State* L)
|
|
|
-{
|
|
|
- LuaStack stack(L);
|
|
|
-
|
|
|
- if (stack.is_vector3(1))
|
|
|
- {
|
|
|
- return vector3_negate(L);
|
|
|
- }
|
|
|
- return vector2_negate(L);
|
|
|
-}
|
|
|
-
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-static int crown_lua_require(lua_State* L)
|
|
|
-{
|
|
|
- LuaStack stack(L);
|
|
|
-
|
|
|
- const char* filename = stack.get_string(1);
|
|
|
-
|
|
|
- const ResourceId lua_res = device()->resource_manager()->load("lua", filename);
|
|
|
- device()->resource_manager()->flush();
|
|
|
-
|
|
|
- const LuaResource* lr = (LuaResource*) device()->resource_manager()->data(lua_res);
|
|
|
- luaL_loadbuffer(L, (const char*) lr->program(), lr->size(), "");
|
|
|
-
|
|
|
- device()->resource_manager()->unload(lua_res);
|
|
|
-
|
|
|
- return 1;
|
|
|
-}
|
|
|
-
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-LuaEnvironment::LuaEnvironment()
|
|
|
- : m_state(luaL_newstate())
|
|
|
-{
|
|
|
-}
|
|
|
-
|
|
|
-//-----------------------------------------------------------------------------
|
|
|
-void LuaEnvironment::init()
|
|
|
-{
|
|
|
- // Open default libraries
|
|
|
- luaL_openlibs(m_state);
|
|
|
- // Open Crown library
|
|
|
- lua_cpcall(m_state, luaopen_libcrown, NULL);
|
|
|
-
|
|
|
- // Register custom loader
|
|
|
- lua_getfield(m_state, LUA_GLOBALSINDEX, "package");
|
|
|
- lua_getfield(m_state, -1, "loaders");
|
|
|
- lua_remove(m_state, -2);
|
|
|
-
|
|
|
- int num_loaders = 0;
|
|
|
- lua_pushnil(m_state);
|
|
|
- while (lua_next(m_state, -2) != 0)
|
|
|
- {
|
|
|
- lua_pop(m_state, 1);
|
|
|
- num_loaders++;
|
|
|
- }
|
|
|
-
|
|
|
- lua_pushinteger(m_state, num_loaders + 1);
|
|
|
- lua_pushcfunction(m_state, crown_lua_require);
|
|
|
- lua_rawset(m_state, -3);
|
|
|
-
|
|
|
- lua_pop(m_state, 1);
|
|
|
-
|
|
|
- // Create metatable for lightuserdata
|
|
|
- lua_pushlightuserdata(m_state, (void*)0x0); // Just dummy userdata
|
|
|
- luaL_newmetatable(m_state, "Lightuserdata_mt");
|
|
|
- lua_setmetatable(m_state, -2);
|
|
|
- lua_pop(m_state, 1);
|
|
|
-
|
|
|
- luaL_newmetatable(m_state, "Lightuserdata_mt");
|
|
|
-
|
|
|
- lua_pushstring(m_state, "__add");
|
|
|
- lua_pushcfunction(m_state, crown_lua_lightuserdata_add);
|
|
|
- lua_settable(m_state, 1);
|
|
|
-
|
|
|
- lua_pushstring(m_state, "__sub");
|
|
|
- lua_pushcfunction(m_state, crown_lua_lightuserdata_sub);
|
|
|
- lua_settable(m_state, 1);
|
|
|
-
|
|
|
- lua_pushstring(m_state, "__mul");
|
|
|
- lua_pushcfunction(m_state, crown_lua_lightuserdata_mul);
|
|
|
- lua_settable(m_state, 1);
|
|
|
-
|
|
|
- lua_pushstring(m_state, "__div");
|
|
|
- lua_pushcfunction(m_state, crown_lua_lightuserdata_div);
|
|
|
- lua_settable(m_state, 1);
|
|
|
-
|
|
|
- lua_pushstring(m_state, "__unm");
|
|
|
- lua_pushcfunction(m_state, crown_lua_lightuserdata_unm);
|
|
|
- lua_settable(m_state, 1);
|
|
|
-
|
|
|
- // Vector2 metatable
|
|
|
- luaL_newmetatable(m_state, "Vector2_mt");
|
|
|
- lua_pushvalue(m_state, -1);
|
|
|
- lua_setfield(m_state, -2, "__index");
|
|
|
- lua_pushcfunction(m_state, crown_lua_vector2_call);
|
|
|
- lua_setfield(m_state, -2, "__call");
|
|
|
-
|
|
|
- lua_getglobal(m_state, "Vector2");
|
|
|
- lua_pushvalue(m_state, -2); // Duplicate metatable
|
|
|
- lua_setmetatable(m_state, -2); // setmetatable(Vector2, Vector2_mt)
|
|
|
-
|
|
|
- // Vector3 metatable
|
|
|
- luaL_newmetatable(m_state, "Vector3_mt");
|
|
|
- lua_pushvalue(m_state, -1);
|
|
|
- lua_setfield(m_state, -2, "__index");
|
|
|
- lua_pushcfunction(m_state, crown_lua_vector3_call);
|
|
|
- lua_setfield(m_state, -2, "__call");
|
|
|
-
|
|
|
- lua_getglobal(m_state, "Vector3");
|
|
|
- lua_pushvalue(m_state, -2); // Duplicate metatable
|
|
|
- lua_setmetatable(m_state, -2); // setmetatable(Vector3, Vector3_mt)
|
|
|
-
|
|
|
- // Matrix4x4 metatable
|
|
|
- luaL_newmetatable(m_state, "Matrix4x4_mt");
|
|
|
- lua_pushvalue(m_state, -1);
|
|
|
- lua_setfield(m_state, -2, "__index");
|
|
|
- lua_pushcfunction(m_state, crown_lua_matrix4x4_call);
|
|
|
- lua_setfield(m_state, -2, "__call");
|
|
|
-
|
|
|
- lua_getglobal(m_state, "Matrix4x4");
|
|
|
- lua_pushvalue(m_state, -2); // Duplicate metatable
|
|
|
- lua_setmetatable(m_state, -2); // setmetatable(Matrix4x4, Matrix4x4_mt)
|
|
|
-
|
|
|
- // Quaternion metatable
|
|
|
- luaL_newmetatable(m_state, "Quaternion_mt");
|
|
|
- lua_pushvalue(m_state, -1);
|
|
|
- lua_setfield(m_state, -2, "__index");
|
|
|
- lua_pushcfunction(m_state, crown_lua_quaternion_call);
|
|
|
- lua_setfield(m_state, -2, "__call");
|
|
|
-
|
|
|
- lua_getglobal(m_state, "Quaternion");
|
|
|
- lua_pushvalue(m_state, -2); // Duplicate metatable
|
|
|
- lua_setmetatable(m_state, -2); // setmetatable(Quaternion, Quaternion_mt)
|
|
|
-}
|
|
|
-
|
|
|
//-----------------------------------------------------------------------------
|
|
|
-void LuaEnvironment::shutdown()
|
|
|
+LuaEnvironment::LuaEnvironment(lua_State* L)
|
|
|
+ : m_state(L)
|
|
|
{
|
|
|
- lua_close(m_state);
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|