Daniele Bartolini %!s(int64=10) %!d(string=hai) anos
pai
achega
21b5e7abf9
Modificáronse 2 ficheiros con 217 adicións e 206 borrados
  1. 4 4
      src/lua/lua_api.cpp
  2. 213 202
      src/lua/lua_stack.h

+ 4 - 4
src/lua/lua_api.cpp

@@ -1008,14 +1008,14 @@ static int input_device_axis_name(lua_State* L, InputDevice& id)
 static int input_device_button_id(lua_State* L, InputDevice& id)
 {
 	LuaStack stack(L);
-	stack.push_int(id.button_id(stack.get_string_id(1)));
+	stack.push_int(id.button_id(stack.get_string_id_32(1)));
 	return 1;
 }
 
 static int input_device_axis_id(lua_State* L, InputDevice& id)
 {
 	LuaStack stack(L);
-	stack.push_int(id.axis_id(stack.get_string_id(1)));
+	stack.push_int(id.axis_id(stack.get_string_id_32(1)));
 	return 1;
 }
 
@@ -1627,7 +1627,7 @@ static int render_world_create_mesh(lua_State* L)
 
 	MeshRendererDesc desc;
 	desc.mesh_resource = stack.get_resource_id(3);
-	desc.mesh_name = stack.get_string_id(4);
+	desc.mesh_name = stack.get_string_id_32(4);
 	desc.material_resource = stack.get_resource_id(5);
 	desc.visible = stack.get_bool(6);
 
@@ -1954,7 +1954,7 @@ static int physics_world_disable_actor_collision(lua_State* L)
 static int physics_world_set_actor_collision_filter(lua_State* L)
 {
 	LuaStack stack(L);
-	stack.get_physics_world(1)->set_actor_collision_filter(stack.get_actor(2), stack.get_string_id(3));
+	stack.get_physics_world(1)->set_actor_collision_filter(stack.get_actor(2), stack.get_string_id_32(3));
 	return 0;
 }
 

+ 213 - 202
src/lua/lua_stack.h

@@ -106,370 +106,405 @@ struct LuaStack
 		return lua_type(L, i);
 	}
 
-	void push_nil()
+	bool get_bool(int i)
 	{
-		lua_pushnil(L);
+		return lua_toboolean(L, i) == 1;
 	}
 
-	void push_bool(bool value)
+	int get_int(int i)
 	{
-		lua_pushboolean(L, value);
+		return (int)lua_tonumber(L, i);
 	}
 
-	void push_int(int value)
+	float get_float(int i)
 	{
-		lua_pushinteger(L, value);
+		return (float)lua_tonumber(L, i);
 	}
 
-	void push_id(uint32_t value)
+	const char* get_string(int i)
 	{
-		lua_pushinteger(L, value);
+		return lua_tostring(L, i);
 	}
 
-	void push_string_id(StringId32 value)
+	void* get_pointer(int i)
 	{
-		lua_pushinteger(L, value.id());
+		if (!lua_isuserdata(L, i))
+			luaL_typerror(L, i, "lightuserdata");
+
+		void* p = lua_touserdata(L, i);
+		CE_ASSERT_NOT_NULL(p);
+		return p;
 	}
 
-	void push_float(float value)
+	uint32_t get_id(int i)
 	{
-		lua_pushnumber(L, value);
+		return (uint32_t)lua_tonumber(L, i);
 	}
 
-	void push_string(const char* s)
+	StringId32 get_string_id_32(int i)
 	{
-		lua_pushstring(L, s);
+		return StringId32(get_string(i));
 	}
 
-	void push_fstring(const char* fmt, ...)
+	StringId64 get_string_id_64(int i)
 	{
-		va_list vl;
-		va_start(vl, fmt);
-		lua_pushvfstring(L, fmt, vl);
-		va_end(vl);
+		return StringId64(get_string(i));
 	}
 
-	void push_lstring(const char* s, uint32_t len)
+	StringId64 get_resource_id(int i)
 	{
-		lua_pushlstring(L, s, len);
+		return StringId64(get_string(i));
 	}
 
-	void push_pointer(void* p)
+	DebugLine* get_debug_line(int i)
 	{
-		CE_ASSERT_NOT_NULL(p);
-		lua_pushlightuserdata(L, p);
+		DebugLine* p = (DebugLine*)get_pointer(i);
+		check_type(i, p);
+		return p;
 	}
 
-	bool get_bool(int i)
+	ResourcePackage* get_resource_package(int i)
 	{
-		return lua_toboolean(L, i) == 1;
+		ResourcePackage* p = (ResourcePackage*)get_pointer(i);
+		check_type(i, p);
+		return p;
 	}
 
-	int get_int(int i)
+	World* get_world(int i)
 	{
-		return (int)lua_tonumber(L, i);
+		World* p = (World*)get_pointer(i);
+		check_type(i, p);
+		return p;
 	}
 
-	uint32_t get_id(int i)
+	SceneGraph* get_scene_graph(int i)
 	{
-		return (uint32_t)lua_tonumber(L, i);
+		SceneGraph* p = (SceneGraph*)get_pointer(i);
+		check_type(i, p);
+		return p;
 	}
 
-	StringId32 get_string_id(int i)
+	Level* get_level(int i)
 	{
-		return StringId32(get_string(i));
+		Level* p = (Level*)get_pointer(i);
+		check_type(i, p);
+		return p;
 	}
 
-	float get_float(int i)
+	RenderWorld* get_render_world(int i)
 	{
-		return (float)lua_tonumber(L, i);
+		RenderWorld* p = (RenderWorld*)get_pointer(i);
+		check_type(i, p);
+		return p;
 	}
 
-	const char* get_string(int i)
+	PhysicsWorld* get_physics_world(int i)
 	{
-		return lua_tostring(L, i);
+		PhysicsWorld* p = (PhysicsWorld*)get_pointer(i);
+//		if (*(uint32_t*)p != PhysicsWorld::MARKER)
+//			luaL_typerror(L, i, "PhysicsWorld");
+		return p;
 	}
 
-	void* get_pointer(int i)
+	SoundWorld* get_sound_world(int i)
 	{
-		if (!lua_isuserdata(L, i))
-			luaL_typerror(L, i, "lightuserdata");
-
-		void* p = lua_touserdata(L, i);
-		CE_ASSERT_NOT_NULL(p);
+		SoundWorld* p = (SoundWorld*)get_pointer(i);
+//		if (*(uint32_t*)p != SoundWorld::MARKER)
+//			luaL_typerror(L, i, "SoundWorld");
 		return p;
 	}
 
-	/// Pushes an empty table onto the stack.
-	/// When you want to set keys on the table, you have to use LuaStack::push_key_begin()
-	/// and LuaStack::push_key_end() as in the following example:
-	///
-	/// LuaStack stack(L)
-	/// stack.push_table()
-	/// stack.push_key_begin("foo"); stack.push_foo(); stack.push_key_end()
-	/// stack.push_key_begin("bar"); stack.push_bar(); stack.push_key_end()
-	/// return 1;
-	void push_table(int narr = 0, int nrec = 0)
+	UnitId get_unit(int i)
 	{
-		lua_createtable(L, narr, nrec);
+		uint32_t enc = (uint32_t)(uintptr_t)get_pointer(i);
+
+		if ((enc & LIGHTDATA_TYPE_MASK) != UNIT_MARKER)
+			luaL_typerror(L, i, "UnitId");
+
+		UnitId id;
+		id.decode(enc >> 2);
+		return id;
 	}
 
-	/// See Stack::push_table()
-	void push_key_begin(const char* key)
+	CameraInstance get_camera(int i)
 	{
-		lua_pushstring(L, key);
+		CameraInstance inst = { get_id(i) };
+		return inst;
 	}
 
-	/// See Stack::push_table()
-	void push_key_begin(int i)
+	TransformInstance get_transform(int i)
 	{
-		lua_pushnumber(L, i);
+		TransformInstance inst = { get_id(i) };
+		return inst;
 	}
 
-	/// See Stack::push_table()
-	void push_key_end()
+	MeshInstance get_mesh_instance(int i)
 	{
-		lua_settable(L, -3);
+		MeshInstance inst = { get_id(i) };
+		return inst;
 	}
 
-	int next(int i)
+	SpriteInstance get_sprite_instance(int i)
 	{
-		return lua_next(L, i);
+		SpriteInstance inst = { get_id(i) };
+		return inst;
 	}
 
-	StringId64 get_resource_id(int i)
+	LightInstance get_light_instance(int i)
 	{
-		return StringId64(get_string(i));
+		LightInstance inst = { get_id(i) };
+		return inst;
 	}
 
-	void push_debug_line(DebugLine* line)
+	Material* get_material(int i)
 	{
-		push_pointer(line);
+		return (Material*)get_pointer(i);
 	}
 
-	DebugLine* get_debug_line(int i)
+	ActorInstance get_actor(int i)
 	{
-		DebugLine* p = (DebugLine*)get_pointer(i);
-		check_type(i, p);
-		return p;
+		ActorInstance inst = { get_id(i) };
+		return inst;
 	}
 
-	void push_resource_package(ResourcePackage* package)
+	ControllerInstance get_controller(int i)
 	{
-		push_pointer(package);
+		ControllerInstance inst = { get_id(i) };
+		return inst;
 	}
 
-	ResourcePackage* get_resource_package(int i)
+	SoundInstanceId get_sound_instance_id(int i)
 	{
-		ResourcePackage* p = (ResourcePackage*)get_pointer(i);
-		check_type(i, p);
-		return p;
+		return get_id(i);
 	}
 
-	void push_world(World* world)
+	Gui* get_gui(int i)
 	{
-		push_pointer(world);
-	};
+		return (Gui*)get_pointer(i);
+	}
 
-	World* get_world(int i)
+	Vector2 get_vector2(int i);
+	Vector3& get_vector3(int i);
+	Matrix4x4& get_matrix4x4(int i);
+	Quaternion& get_quaternion(int i);
+	Color4 get_color4(int i);
+
+	Vector2& get_vector2box(int i)
 	{
-		World* p = (World*)get_pointer(i);
-		check_type(i, p);
-		return p;
-	};
+		Vector2* v = (Vector2*)luaL_checkudata(L, i, "Vector2Box");
+		return *v;
+	}
 
-	void push_scene_graph(SceneGraph* sg)
+	Vector3& get_vector3box(int i)
 	{
-		push_pointer(sg);
+		Vector3* v = (Vector3*)luaL_checkudata(L, i, "Vector3Box");
+		return *v;
 	}
 
-	SceneGraph* get_scene_graph(int i)
+	Quaternion& get_quaternionbox(int i)
 	{
-		SceneGraph* p = (SceneGraph*)get_pointer(i);
-		check_type(i, p);
-		return p;
+		Quaternion* q = (Quaternion*)luaL_checkudata(L, i, "QuaternionBox");
+		return *q;
 	}
 
-	void push_level(Level* level)
+	Matrix4x4& get_matrix4x4box(int i)
 	{
-		push_pointer(level);
+		Matrix4x4* m = (Matrix4x4*)luaL_checkudata(L, i, "Matrix4x4Box");
+		return *m;
 	}
 
-	Level* get_level(int i)
+	void push_nil()
 	{
-		Level* p = (Level*)get_pointer(i);
-		check_type(i, p);
-		return p;
+		lua_pushnil(L);
 	}
 
-	void push_render_world(RenderWorld* world)
+	void push_bool(bool value)
 	{
-		push_pointer(world);
+		lua_pushboolean(L, value);
 	}
 
-	RenderWorld* get_render_world(int i)
+	void push_int(int value)
 	{
-		RenderWorld* p = (RenderWorld*)get_pointer(i);
-		check_type(i, p);
-		return p;
+		lua_pushnumber(L, value);
 	}
 
-	void push_physics_world(PhysicsWorld* world)
+	void push_float(float value)
 	{
-		push_pointer(world);
+		lua_pushnumber(L, value);
 	}
 
-	PhysicsWorld* get_physics_world(int i)
+	void push_string(const char* s)
 	{
-		PhysicsWorld* p = (PhysicsWorld*)get_pointer(i);
-//		if (*(uint32_t*)p != PhysicsWorld::MARKER)
-//			luaL_typerror(L, i, "PhysicsWorld");
-		return p;
+		lua_pushstring(L, s);
 	}
 
-	void push_sound_world(SoundWorld* world)
+	void push_fstring(const char* fmt, ...)
 	{
-		push_pointer(world);
+		va_list vl;
+		va_start(vl, fmt);
+		lua_pushvfstring(L, fmt, vl);
+		va_end(vl);
 	}
 
-	SoundWorld* get_sound_world(int i)
+	void push_lstring(const char* s, uint32_t len)
 	{
-		SoundWorld* p = (SoundWorld*)get_pointer(i);
-//		if (*(uint32_t*)p != SoundWorld::MARKER)
-//			luaL_typerror(L, i, "SoundWorld");
-		return p;
+		lua_pushlstring(L, s, len);
 	}
 
-	void push_unit(UnitId id)
+	void push_string_id(StringId32 value)
 	{
-		uint32_t encoded = (id.encode() << 2) | UNIT_MARKER;
-		push_pointer((void*)(uintptr_t)encoded);
+		lua_pushnumber(L, value.id());
 	}
 
-	UnitId get_unit(int i)
+	void push_pointer(void* p)
 	{
-		uint32_t enc = (uint32_t)(uintptr_t)get_pointer(i);
+		CE_ASSERT_NOT_NULL(p);
+		lua_pushlightuserdata(L, p);
+	}
 
-		if ((enc & LIGHTDATA_TYPE_MASK) != UNIT_MARKER)
-			luaL_typerror(L, i, "UnitId");
+	void push_function(lua_CFunction f)
+	{
+		lua_pushcfunction(L, f);
+	}
 
-		UnitId id;
-		id.decode(enc >> 2);
-		return id;
+	void push_id(uint32_t value)
+	{
+		lua_pushnumber(L, value);
 	}
 
-	void push_camera(CameraInstance i)
+	/// Pushes an empty table onto the stack.
+	/// When you want to set keys on the table, you have to use LuaStack::push_key_begin()
+	/// and LuaStack::push_key_end() as in the following example:
+	///
+	/// LuaStack stack(L)
+	/// stack.push_table()
+	/// stack.push_key_begin("foo"); stack.push_foo(); stack.push_key_end()
+	/// stack.push_key_begin("bar"); stack.push_bar(); stack.push_key_end()
+	/// return 1;
+	void push_table(int narr = 0, int nrec = 0)
 	{
-		push_id(i.i);
+		lua_createtable(L, narr, nrec);
 	}
 
-	CameraInstance get_camera(int i)
+	/// See Stack::push_table()
+	void push_key_begin(const char* key)
 	{
-		CameraInstance inst = { get_id(i) };
-		return inst;
+		lua_pushstring(L, key);
 	}
 
-	void push_transform(TransformInstance i)
+	/// See Stack::push_table()
+	void push_key_begin(int i)
 	{
-		push_id(i.i);
+		lua_pushnumber(L, i);
 	}
 
-	TransformInstance get_transform(int i)
+	/// See Stack::push_table()
+	void push_key_end()
 	{
-		TransformInstance inst = { get_id(i) };
-		return inst;
+		lua_settable(L, -3);
 	}
 
-	void push_mesh_instance(MeshInstance i)
+	int next(int i)
 	{
-		push_id(i.i);
+		return lua_next(L, i);
 	}
 
-	MeshInstance get_mesh_instance(int i)
+	void push_debug_line(DebugLine* line)
 	{
-		MeshInstance inst = { get_id(i) };
-		return inst;
+		push_pointer(line);
 	}
 
-	void push_sprite_instance(SpriteInstance i)
+	void push_resource_package(ResourcePackage* package)
 	{
-		push_id(i.i);
+		push_pointer(package);
 	}
 
-	SpriteInstance get_sprite_instance(int i)
+	void push_world(World* world)
 	{
-		SpriteInstance inst = { get_id(i) };
-		return inst;
+		push_pointer(world);
+	};
+
+	void push_scene_graph(SceneGraph* sg)
+	{
+		push_pointer(sg);
 	}
 
-	void push_light_instance(LightInstance i)
+	void push_level(Level* level)
 	{
-		push_id(i.i);
+		push_pointer(level);
 	}
 
-	LightInstance get_light_instance(int i)
+	void push_render_world(RenderWorld* world)
 	{
-		LightInstance inst = { get_id(i) };
-		return inst;
+		push_pointer(world);
 	}
 
-	void push_material(Material* material)
+	void push_physics_world(PhysicsWorld* world)
 	{
-		push_pointer(material);
+		push_pointer(world);
 	}
 
-	Material* get_material(int i)
+	void push_sound_world(SoundWorld* world)
 	{
-		return (Material*)get_pointer(i);
+		push_pointer(world);
 	}
 
-	void push_actor(ActorInstance i)
+	void push_unit(UnitId id)
+	{
+		uint32_t encoded = (id.encode() << 2) | UNIT_MARKER;
+		push_pointer((void*)(uintptr_t)encoded);
+	}
+
+	void push_camera(CameraInstance i)
 	{
 		push_id(i.i);
 	}
 
-	ActorInstance get_actor(int i)
+	void push_transform(TransformInstance i)
 	{
-		ActorInstance inst = { get_id(i) };
-		return inst;
+		push_id(i.i);
 	}
 
-	void push_controller(ControllerInstance i)
+	void push_mesh_instance(MeshInstance i)
 	{
 		push_id(i.i);
 	}
 
-	ControllerInstance get_controller(int i)
+	void push_sprite_instance(SpriteInstance i)
 	{
-		ControllerInstance inst = { get_id(i) };
-		return inst;
+		push_id(i.i);
 	}
 
-	void push_sound_instance_id(const SoundInstanceId id)
+	void push_light_instance(LightInstance i)
 	{
-		push_id(id);
+		push_id(i.i);
 	}
 
-	SoundInstanceId get_sound_instance_id(int i)
+	void push_material(Material* material)
 	{
-		return get_id(i);
+		push_pointer(material);
 	}
 
-	void push_gui(Gui* gui)
+	void push_actor(ActorInstance i)
 	{
-		push_pointer(gui);
+		push_id(i.i);
 	}
 
-	Gui* get_gui(int i)
+	void push_controller(ControllerInstance i)
 	{
-		return (Gui*)get_pointer(i);
+		push_id(i.i);
+	}
+
+	void push_sound_instance_id(const SoundInstanceId id)
+	{
+		push_id(id);
+	}
+
+	void push_gui(Gui* gui)
+	{
+		push_pointer(gui);
 	}
 
-	Vector2 get_vector2(int i);
-	Vector3& get_vector3(int i);
-	Matrix4x4& get_matrix4x4(int i);
-	Quaternion& get_quaternion(int i);
-	Color4 get_color4(int i);
 	void push_vector2(const Vector2& v);
 	void push_vector3(const Vector3& v);
 	void push_matrix4x4(const Matrix4x4& m);
@@ -484,12 +519,6 @@ struct LuaStack
 		*vec = v;
 	}
 
-	Vector2& get_vector2box(int i)
-	{
-		Vector2* v = (Vector2*)luaL_checkudata(L, i, "Vector2Box");
-		return *v;
-	}
-
 	void push_vector3box(const Vector3& v)
 	{
 		Vector3* vec = (Vector3*)lua_newuserdata(L, sizeof(Vector3));
@@ -498,12 +527,6 @@ struct LuaStack
 		*vec = v;
 	}
 
-	Vector3& get_vector3box(int i)
-	{
-		Vector3* v = (Vector3*)luaL_checkudata(L, i, "Vector3Box");
-		return *v;
-	}
-
 	void push_quaternionbox(const Quaternion& q)
 	{
 		Quaternion* quat = (Quaternion*)lua_newuserdata(L, sizeof(Quaternion));
@@ -512,12 +535,6 @@ struct LuaStack
 		*quat = q;
 	}
 
-	Quaternion& get_quaternionbox(int i)
-	{
-		Quaternion* q = (Quaternion*)luaL_checkudata(L, i, "QuaternionBox");
-		return *q;
-	}
-
 	void push_matrix4x4box(const Matrix4x4& m)
 	{
 		Matrix4x4* mat = (Matrix4x4*)lua_newuserdata(L, sizeof(Matrix4x4));
@@ -526,12 +543,6 @@ struct LuaStack
 		*mat = m;
 	}
 
-	Matrix4x4& get_matrix4x4box(int i)
-	{
-		Matrix4x4* m = (Matrix4x4*)luaL_checkudata(L, i, "Matrix4x4Box");
-		return *m;
-	}
-
 	void check_temporary(int i, const Vector3* p);
 	void check_temporary(int i, const Quaternion* p);
 	void check_temporary(int i, const Matrix4x4* p);