Преглед изворни кода

src: fix some issues detected by cppcheck

Daniele Bartolini пре 6 година
родитељ
комит
2095e841b0

+ 9 - 8
src/core/error/callstack_windows.cpp

@@ -13,6 +13,7 @@
 #pragma warning(disable:4091) // 'keyword' : ignored on left of 'type' when no variable is declared
 #include <dbghelp.h>
 #pragma warning(pop)
+#include <new>
 
 namespace crown
 {
@@ -53,8 +54,8 @@ namespace error
 		ZeroMemory(&line, sizeof(IMAGEHLP_LINE64));
 		line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
 
-		char buf[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)];
-		PSYMBOL_INFO sym = (PSYMBOL_INFO)buf;
+		char buf[sizeof(SYMBOL_INFO) + (MAX_SYM_NAME - 1) * sizeof(TCHAR)];
+		SYMBOL_INFO* sym = new (buf) SYMBOL_INFO();
 		sym->SizeOfStruct = sizeof(SYMBOL_INFO);
 		sym->MaxNameLen = MAX_SYM_NAME;
 
@@ -82,12 +83,12 @@ namespace error
 						);
 			res = res && SymFromAddr(GetCurrentProcess(), stack.AddrPC.Offset, 0, sym);
 
-			char buf[512];
+			char str[512];
 
 			if (res == TRUE)
 			{
-				snprintf(buf
-					, sizeof(buf)
+				snprintf(str
+					, sizeof(str)
 					, "    [%2i] %s in %s:%d\n"
 					, num
 					, sym->Name
@@ -97,15 +98,15 @@ namespace error
 			}
 			else
 			{
-				snprintf(buf
-					, sizeof(buf)
+				snprintf(str
+					, sizeof(str)
 					, "    [%2i] 0x%p\n"
 					, num
 					, stack.AddrPC.Offset
 					);
 			}
 
-			ss << buf;
+			ss << str;
 		}
 
 		SymCleanup(GetCurrentProcess());

+ 25 - 19
src/core/filesystem/file_monitor_linux.cpp

@@ -34,7 +34,7 @@ struct FileMonitorImpl
 	FileMonitorFunction _function;
 	void* _user_data;
 
-	FileMonitorImpl(Allocator& a)
+	explicit FileMonitorImpl(Allocator& a)
 		: _allocator(&a)
 		, _fd(0)
 		, _watches(a)
@@ -71,31 +71,37 @@ struct FileMonitorImpl
 		hash_map::set(_watches_reverse, str, wd);
 
 		// Add all sub-dirs recursively
-		DIR *dir;
-		struct dirent *entry;
+		if (recursive)
+			add_subdirectories(path);
+	}
 
-		if (!(dir = opendir(path)))
-			return;
+	void add_subdirectories(const char* path)
+	{
+		struct dirent *entry;
 
-		while ((entry = readdir(dir)))
+		DIR *dir = opendir(path);
+		if (dir != NULL)
 		{
-			const char* dname = entry->d_name;
+			while ((entry = readdir(dir)))
+			{
+				const char* dname = entry->d_name;
 
-			if (!strcmp(dname, ".") || !strcmp(dname, ".."))
-				continue;
+				if (!strcmp(dname, ".") || !strcmp(dname, ".."))
+					continue;
 
-			// FIXME: some filesystems do not support DT_DIR.
-			if (entry->d_type != DT_DIR)
-				continue;
+				// FIXME: some filesystems do not support DT_DIR.
+				if (entry->d_type != DT_DIR)
+					continue;
 
-			TempAllocator512 ta;
-			DynamicString str(ta);
-			path::join(str, path, dname);
+				TempAllocator512 ta;
+				DynamicString str(ta);
+				path::join(str, path, dname);
 
-			add_watch(str.c_str(), recursive);
-		}
+				add_watch(str.c_str(), true);
+			}
 
-		closedir(dir);
+			closedir(dir);
+		}
 	}
 
 	void start(u32 num, const char** paths, bool recursive, FileMonitorFunction fmf, void* user_data)
@@ -112,7 +118,7 @@ struct FileMonitorImpl
 		for (u32 i = 0; i < num; ++i)
 			add_watch(paths[i], recursive);
 
-		_thread.start([](void* thiz) { return ((FileMonitorImpl*)thiz)->watch(); }, this);
+		_thread.start([](void* thiz) { return static_cast<FileMonitorImpl*>(thiz)->watch(); }, this);
 	}
 
 	void stop()

+ 1 - 1
src/core/filesystem/filesystem_apk.cpp

@@ -21,7 +21,7 @@ struct FileApk : public File
 	AAssetManager* _asset_manager;
 	AAsset* _asset;
 
-	FileApk(AAssetManager* asset_manager)
+	explicit FileApk(AAssetManager* asset_manager)
 		: _asset_manager(asset_manager)
 		, _asset(NULL)
 	{

+ 3 - 10
src/core/json/json.cpp

@@ -174,13 +174,10 @@ namespace json
 		{
 			while (*++json)
 			{
-				// Empty string
 				if (*json == '"')
-				{
-					++json;
 					return;
-				}
-				else if (*json == '\\')
+
+				if (*json == '\\')
 				{
 					++json;
 
@@ -194,13 +191,9 @@ namespace json
 					case 'n': str += '\n'; break;
 					case 'r': str += '\r'; break;
 					case 't': str += '\t'; break;
-					default:
-					{
-						CE_FATAL("Bad escape character");
-						break;
+					default: CE_FATAL("Bad escape character"); break;
 					}
 				}
-				}
 				else
 				{
 					str += *json;

+ 2 - 5
src/core/json/sjson.cpp

@@ -249,13 +249,10 @@ namespace sjson
 		{
 			while (*++json)
 			{
-				// Empty string
 				if (*json == '"')
-				{
-					++json;
 					return;
-				}
-				else if (*json == '\\')
+
+				if (*json == '\\')
 				{
 					++json;
 

+ 29 - 29
src/core/os.cpp

@@ -231,52 +231,52 @@ namespace os
 	void list_files(const char* path, Vector<DynamicString>& files)
 	{
 #if CROWN_PLATFORM_POSIX
-		DIR *dir;
 		struct dirent *entry;
 
-		if (!(dir = opendir(path)))
-			return;
-
-		while ((entry = readdir(dir)))
+		DIR *dir = opendir(path);
+		if (dir != NULL)
 		{
-			const char* dname = entry->d_name;
+			while ((entry = readdir(dir)))
+			{
+				const char* dname = entry->d_name;
 
-			if (!strcmp(dname, ".") || !strcmp(dname, ".."))
-				continue;
+				if (!strcmp(dname, ".") || !strcmp(dname, ".."))
+					continue;
 
-			TempAllocator512 ta;
-			DynamicString fname(ta);
-			fname.set(dname, strlen32(dname));
-			vector::push_back(files, fname);
-		}
+				TempAllocator256 ta;
+				DynamicString fname(ta);
+				fname.set(dname, strlen32(dname));
+				vector::push_back(files, fname);
+			}
 
-		closedir(dir);
+			closedir(dir);
+		}
 #elif CROWN_PLATFORM_WINDOWS
-		TempAllocator1024 ta;
+		TempAllocator256 ta;
 		DynamicString cur_path(ta);
 		cur_path += path;
 		cur_path += "\\*";
 
 		WIN32_FIND_DATA ffd;
 		HANDLE file = FindFirstFile(cur_path.c_str(), &ffd);
-		if (file == INVALID_HANDLE_VALUE)
-			return;
-
-		do
+		if (file != INVALID_HANDLE_VALUE)
 		{
-			const char* dname = ffd.cFileName;
+			do
+			{
+				const char* dname = ffd.cFileName;
 
-			if (!strcmp(dname, ".") || !strcmp(dname, ".."))
-				continue;
+				if (!strcmp(dname, ".") || !strcmp(dname, ".."))
+					continue;
 
-			TempAllocator512 ta;
-			DynamicString fname(ta);
-			fname.set(dname, strlen32(dname));
-			vector::push_back(files, fname);
-		}
-		while (FindNextFile(file, &ffd) != 0);
+				TempAllocator256 ta;
+				DynamicString fname(ta);
+				fname.set(dname, strlen32(dname));
+				vector::push_back(files, fname);
+			}
+			while (FindNextFile(file, &ffd) != 0);
 
-		FindClose(file);
+			FindClose(file);
+		}
 #endif
 	}
 

+ 1 - 1
src/core/unit_tests.cpp

@@ -1360,12 +1360,12 @@ static void test_process()
 {
 #if CROWN_PLATFORM_POSIX
 	{
-		char buf[128] = {0};
 		const char* argv[] = {"printf", "Hello,\\nworld.\\n", NULL};
 
 		Process pr;
 		if (pr.spawn(argv, ProcessFlags::STDOUT_PIPE) == 0)
 		{
+			char buf[128] = {0};
 			pr.fgets(buf, sizeof(buf));
 			ENSURE(strcmp(buf, "Hello,\n") == 0);
 			pr.fgets(buf, sizeof(buf));

+ 1 - 1
src/device/device.cpp

@@ -146,7 +146,7 @@ struct BgfxAllocator : public bx::AllocatorI
 {
 	ProxyAllocator _allocator;
 
-	BgfxAllocator(Allocator& a)
+	explicit BgfxAllocator(Allocator& a)
 		: _allocator(a, "bgfx")
 	{
 	}

+ 2 - 2
src/device/main_android.cpp

@@ -198,12 +198,12 @@ struct AndroidDevice
 
 	static s32 on_input_event(struct android_app* app, AInputEvent* event)
 	{
-		return ((AndroidDevice*) app->userData)->process_input(app, event);
+		return static_cast<AndroidDevice*>(app->userData)->process_input(app, event);
 	}
 
 	static void on_app_cmd(struct android_app* app, s32 cmd)
 	{
-		((AndroidDevice*) app->userData)->process_command(app, cmd);
+		static_cast<AndroidDevice*>(app->userData)->process_command(app, cmd);
 	}
 };
 

+ 1 - 3
src/resource/data_compiler.cpp

@@ -159,7 +159,7 @@ struct LineReader
 	const u32 _len;
 	u32 _pos;
 
-	LineReader(const char* str)
+	explicit LineReader(const char* str)
 		: _str(str)
 		, _len(strlen32(str))
 		, _pos(0)
@@ -194,7 +194,6 @@ static void console_command_compile(ConsoleServer& cs, TCPSocket& client, const
 	sjson::parse_string(platform, obj["platform"]);
 
 	{
-		TempAllocator512 ta;
 		StringStream ss(ta);
 		ss << "{\"type\":\"compile\",\"id\":\"" << id.c_str() << "\",\"start\":true}";
 		cs.send(client, string_stream::c_str(ss));
@@ -203,7 +202,6 @@ static void console_command_compile(ConsoleServer& cs, TCPSocket& client, const
 	bool succ = ((DataCompiler*)user_data)->compile(data_dir.c_str(), platform.c_str());
 
 	{
-		TempAllocator512 ta;
 		StringStream ss(ta);
 		ss << "{\"type\":\"compile\",\"id\":\"" << id.c_str() << "\",\"success\":" << (succ ? "true" : "false") << "}";
 		cs.send(client, string_stream::c_str(ss));

+ 1 - 1
src/resource/expression_language.cpp

@@ -150,7 +150,7 @@ namespace expression_language
 		enum TokenType {EMPTY, NUMBER, FUNCTION, VARIABLE, LEFT_PARENTHESIS, RIGHT_PARENTHESIS};
 
 		Token() : type(EMPTY), id(0) {}
-		Token(TokenType type) : type(type) {}
+		explicit Token(TokenType type) : type(type) {}
 		Token(TokenType type, unsigned id) : type(type), id(id) {}
 		Token(TokenType type, float value) : type(type), value(value) {}
 

+ 9 - 9
src/resource/mesh_resource.cpp

@@ -188,7 +188,7 @@ namespace mesh_resource_internal
 		bool _has_normal;
 		bool _has_uv;
 
-		MeshCompiler(CompileOptions& opts)
+		explicit MeshCompiler(CompileOptions& opts)
 			: _opts(opts)
 			, _positions(default_allocator())
 			, _normals(default_allocator())
@@ -235,7 +235,7 @@ namespace mesh_resource_internal
 			_has_uv = false;
 		}
 
-		void parse_float_array(const char* array_json, Array<f32>& output)
+		void parse_float_array(Array<f32>& output, const char* array_json)
 		{
 			TempAllocator4096 ta;
 			JsonArray arr(ta);
@@ -248,7 +248,7 @@ namespace mesh_resource_internal
 			}
 		}
 
-		void parse_index_array(const char* array_json, Array<u16>& output)
+		void parse_index_array(Array<u16>& output, const char* array_json)
 		{
 			TempAllocator4096 ta;
 			JsonArray arr(ta);
@@ -270,15 +270,15 @@ namespace mesh_resource_internal
 			JsonArray data_json(ta);
 			sjson::parse_array(data_json, obj["data"]);
 
-			parse_index_array(data_json[0], _position_indices);
+			parse_index_array(_position_indices, data_json[0]);
 
 			if (_has_normal)
 			{
-				parse_index_array(data_json[1], _normal_indices);
+				parse_index_array(_normal_indices, data_json[1]);
 			}
 			if (_has_uv)
 			{
-				parse_index_array(data_json[2], _uv_indices);
+				parse_index_array(_uv_indices, data_json[2]);
 			}
 		}
 
@@ -293,15 +293,15 @@ namespace mesh_resource_internal
 			_has_normal = json_object::has(obj, "normal");
 			_has_uv     = json_object::has(obj, "texcoord");
 
-			parse_float_array(obj["position"], _positions);
+			parse_float_array(_positions, obj["position"]);
 
 			if (_has_normal)
 			{
-				parse_float_array(obj["normal"], _normals);
+				parse_float_array(_normals, obj["normal"]);
 			}
 			if (_has_uv)
 			{
-				parse_float_array(obj["texcoord"], _uvs);
+				parse_float_array(_uvs, obj["texcoord"]);
 			}
 
 			parse_indices(obj["indices"]);

+ 1 - 2
src/resource/physics_resource.cpp

@@ -443,7 +443,7 @@ namespace physics_config_resource_internal
 		Array<PhysicsCollisionFilter> _filters;
 		u32 _filter;
 
-		CollisionFilterCompiler(CompileOptions& opts)
+		explicit CollisionFilterCompiler(CompileOptions& opts)
 			: _opts(opts)
 			, _filter_map(default_allocator())
 			, _filters(default_allocator())
@@ -560,7 +560,6 @@ namespace physics_config_resource_internal
 		offt += sizeof(PhysicsActor) * pcr.num_actors;
 
 		pcr.filters_offset = offt;
-		offt += sizeof(PhysicsCollisionFilter) * pcr.num_filters;
 
 		// Write all
 		opts.write(pcr.version);

+ 4 - 4
src/resource/shader_resource.cpp

@@ -620,7 +620,7 @@ namespace shader_resource_internal
 		DynamicString _fs_input_output;
 		HashMap<DynamicString, DynamicString> _samplers;
 
-		BgfxShader(Allocator& a)
+		explicit BgfxShader(Allocator& a)
 			: _includes(a)
 			, _code(a)
 			, _vs_code(a)
@@ -640,7 +640,7 @@ namespace shader_resource_internal
 		DynamicString _bgfx_shader;
 		DynamicString _render_state;
 
-		ShaderPermutation(Allocator& a)
+		explicit ShaderPermutation(Allocator& a)
 			: _bgfx_shader(a)
 			, _render_state(a)
 		{
@@ -654,7 +654,7 @@ namespace shader_resource_internal
 		DynamicString _shader;
 		Vector<DynamicString> _defines;
 
-		StaticCompile(Allocator& a)
+		explicit StaticCompile(Allocator& a)
 			: _shader(a)
 			, _defines(a)
 		{
@@ -676,7 +676,7 @@ namespace shader_resource_internal
 		DynamicString _vs_out_path;
 		DynamicString _fs_out_path;
 
-		ShaderCompiler(CompileOptions& opts)
+		explicit ShaderCompiler(CompileOptions& opts)
 			: _opts(opts)
 			, _render_states(default_allocator())
 			, _sampler_states(default_allocator())

+ 5 - 5
src/resource/state_machine_resource.cpp

@@ -142,8 +142,8 @@ namespace state_machine_internal
 		u32 _offset;
 
 		OffsetAccumulator()
+			: _offset(sizeof(StateMachineResource))
 		{
-			_offset = sizeof(StateMachineResource);
 		}
 
 		// Returns the offset of
@@ -166,7 +166,7 @@ namespace state_machine_internal
 		DynamicString weight;
 		u32 bytecode_entry;
 
-		AnimationInfo(Allocator& a)
+		explicit AnimationInfo(Allocator& a)
 			: weight(a)
 		{
 		}
@@ -197,7 +197,7 @@ namespace state_machine_internal
 		{
 		}
 
-		StateInfo(Allocator& a)
+		explicit StateInfo(Allocator& a)
 			: animations(a)
 			, transitions(a)
 			, speed(a)
@@ -221,7 +221,7 @@ namespace state_machine_internal
 		{
 		}
 
-		VariableInfo(Allocator& a)
+		explicit VariableInfo(Allocator& a)
 			: name_string(a)
 			, value(0.0f)
 		{
@@ -238,7 +238,7 @@ namespace state_machine_internal
 		Vector<VariableInfo> _variables;
 		Array<u32> _byte_code;
 
-		StateMachineCompiler(CompileOptions& opts)
+		explicit StateMachineCompiler(CompileOptions& opts)
 			: _opts(opts)
 			, _states(default_allocator())
 			, _offsets(default_allocator())

+ 83 - 83
src/world/physics_world_bullet.cpp

@@ -142,7 +142,7 @@ struct MyDebugDrawer : public btIDebugDraw
 {
 	DebugLine* _lines;
 
-	MyDebugDrawer(DebugLine& dl)
+	explicit MyDebugDrawer(DebugLine& dl)
 		: _lines(&dl)
 	{
 	}
@@ -208,7 +208,7 @@ struct PhysicsWorldImpl
 	struct ActorInstanceData
 	{
 		UnitId unit;
-		btRigidBody* actor;
+		btRigidBody* body;
 	};
 
 	Allocator* _allocator;
@@ -268,12 +268,12 @@ struct PhysicsWorldImpl
 
 		for (u32 i = 0; i < array::size(_actor); ++i)
 		{
-			btRigidBody* rb = _actor[i].actor;
+			btRigidBody* body = _actor[i].body;
 
-			_dynamics_world->removeRigidBody(rb);
-			CE_DELETE(*_allocator, rb->getMotionState());
-			CE_DELETE(*_allocator, rb->getCollisionShape());
-			CE_DELETE(*_allocator, rb);
+			_dynamics_world->removeRigidBody(body);
+			CE_DELETE(*_allocator, body->getMotionState());
+			CE_DELETE(*_allocator, body->getCollisionShape());
+			CE_DELETE(*_allocator, body);
 		}
 
 		for (u32 i = 0; i < array::size(_collider); ++i)
@@ -505,39 +505,39 @@ struct PhysicsWorldImpl
 		rbinfo.m_angularSleepingThreshold = 0.7f; // FIXME
 
 		// Create rigid body
-		btRigidBody* actor = CE_NEW(*_allocator, btRigidBody)(rbinfo);
+		btRigidBody* body = CE_NEW(*_allocator, btRigidBody)(rbinfo);
 
-		int cflags = actor->getCollisionFlags();
+		int cflags = body->getCollisionFlags();
 		cflags |= is_kinematic ? btCollisionObject::CF_KINEMATIC_OBJECT    : 0;
 		cflags |= is_static    ? btCollisionObject::CF_STATIC_OBJECT       : 0;
 		cflags |= is_trigger   ? btCollisionObject::CF_NO_CONTACT_RESPONSE : 0;
-		actor->setCollisionFlags(cflags);
+		body->setCollisionFlags(cflags);
 		if (is_kinematic)
-			actor->setActivationState(DISABLE_DEACTIVATION);
+			body->setActivationState(DISABLE_DEACTIVATION);
 
-		actor->setLinearFactor(btVector3(
+		body->setLinearFactor(btVector3(
 			(ar->flags & ActorFlags::LOCK_TRANSLATION_X) ? 0.0f : 1.0f,
 			(ar->flags & ActorFlags::LOCK_TRANSLATION_Y) ? 0.0f : 1.0f,
 			(ar->flags & ActorFlags::LOCK_TRANSLATION_Z) ? 0.0f : 1.0f)
 		);
-		actor->setAngularFactor(btVector3(
+		body->setAngularFactor(btVector3(
 			(ar->flags & ActorFlags::LOCK_ROTATION_X) ? 0.0f : 1.0f,
 			(ar->flags & ActorFlags::LOCK_ROTATION_Y) ? 0.0f : 1.0f,
 			(ar->flags & ActorFlags::LOCK_ROTATION_Z) ? 0.0f : 1.0f)
 		);
 
 		const u32 last = array::size(_actor);
-		actor->setUserPointer((void*)(uintptr_t)last);
+		body->setUserPointer((void*)(uintptr_t)last);
 
 		// Set collision filters
 		const u32 me   = physics_config_resource::filter(_config_resource, ar->collision_filter)->me;
 		const u32 mask = physics_config_resource::filter(_config_resource, ar->collision_filter)->mask;
 
-		_dynamics_world->addRigidBody(actor, me, mask);
+		_dynamics_world->addRigidBody(body, me, mask);
 
 		ActorInstanceData aid;
-		aid.unit  = id;
-		aid.actor = actor;
+		aid.unit = id;
+		aid.body = body;
 
 		array::push_back(_actor, aid);
 		hash_map::set(_actor_map, id, last);
@@ -551,13 +551,13 @@ struct PhysicsWorldImpl
 		const UnitId u      = _actor[i.i].unit;
 		const UnitId last_u = _actor[last].unit;
 
-		_dynamics_world->removeRigidBody(_actor[i.i].actor);
-		CE_DELETE(*_allocator, _actor[i.i].actor->getMotionState());
-		CE_DELETE(*_allocator, _actor[i.i].actor->getCollisionShape());
-		CE_DELETE(*_allocator, _actor[i.i].actor);
+		_dynamics_world->removeRigidBody(_actor[i.i].body);
+		CE_DELETE(*_allocator, _actor[i.i].body->getMotionState());
+		CE_DELETE(*_allocator, _actor[i.i].body->getCollisionShape());
+		CE_DELETE(*_allocator, _actor[i.i].body);
 
 		_actor[i.i] = _actor[last];
-		_actor[i.i].actor->setUserPointer((void*)(uintptr_t)i.i);
+		_actor[i.i].body->setUserPointer((void*)(uintptr_t)i.i);
 
 		array::pop_back(_actor);
 
@@ -572,31 +572,31 @@ struct PhysicsWorldImpl
 
 	Vector3 actor_world_position(ActorInstance i) const
 	{
-		return to_vector3(_actor[i.i].actor->getCenterOfMassPosition());
+		return to_vector3(_actor[i.i].body->getCenterOfMassPosition());
 	}
 
 	Quaternion actor_world_rotation(ActorInstance i) const
 	{
-		return to_quaternion(_actor[i.i].actor->getOrientation());
+		return to_quaternion(_actor[i.i].body->getOrientation());
 	}
 
 	Matrix4x4 actor_world_pose(ActorInstance i) const
 	{
-		return to_matrix4x4(_actor[i.i].actor->getCenterOfMassTransform());
+		return to_matrix4x4(_actor[i.i].body->getCenterOfMassTransform());
 	}
 
 	void actor_teleport_world_position(ActorInstance i, const Vector3& p)
 	{
-		btTransform pose = _actor[i.i].actor->getCenterOfMassTransform();
+		btTransform pose = _actor[i.i].body->getCenterOfMassTransform();
 		pose.setOrigin(to_btVector3(p));
-		_actor[i.i].actor->setCenterOfMassTransform(pose);
+		_actor[i.i].body->setCenterOfMassTransform(pose);
 	}
 
 	void actor_teleport_world_rotation(ActorInstance i, const Quaternion& r)
 	{
-		btTransform pose = _actor[i.i].actor->getCenterOfMassTransform();
+		btTransform pose = _actor[i.i].body->getCenterOfMassTransform();
 		pose.setRotation(to_btQuaternion(r));
-		_actor[i.i].actor->setCenterOfMassTransform(pose);
+		_actor[i.i].body->setCenterOfMassTransform(pose);
 	}
 
 	void actor_teleport_world_pose(ActorInstance i, const Matrix4x4& m)
@@ -604,27 +604,27 @@ struct PhysicsWorldImpl
 		const Quaternion rot = rotation(m);
 		const Vector3 pos = translation(m);
 
-		btTransform pose = _actor[i.i].actor->getCenterOfMassTransform();
+		btTransform pose = _actor[i.i].body->getCenterOfMassTransform();
 		pose.setRotation(to_btQuaternion(rot));
 		pose.setOrigin(to_btVector3(pos));
-		_actor[i.i].actor->setCenterOfMassTransform(pose);
+		_actor[i.i].body->setCenterOfMassTransform(pose);
 	}
 
 	Vector3 actor_center_of_mass(ActorInstance i) const
 	{
-		return to_vector3(_actor[i.i].actor->getCenterOfMassTransform().getOrigin());
+		return to_vector3(_actor[i.i].body->getCenterOfMassTransform().getOrigin());
 	}
 
 	void actor_enable_gravity(ActorInstance i)
 	{
-		btRigidBody* body = _actor[i.i].actor;
+		btRigidBody* body = _actor[i.i].body;
 		body->setFlags(body->getFlags() & ~BT_DISABLE_WORLD_GRAVITY);
 		body->setGravity(_dynamics_world->getGravity());
 	}
 
 	void actor_disable_gravity(ActorInstance i)
 	{
-		btRigidBody* body = _actor[i.i].actor;
+		btRigidBody* body = _actor[i.i].body;
 		body->setFlags(body->getFlags() | BT_DISABLE_WORLD_GRAVITY);
 		body->setGravity(btVector3(0.0f, 0.0f, 0.0f));
 	}
@@ -646,7 +646,7 @@ struct PhysicsWorldImpl
 
 	void actor_set_kinematic(ActorInstance i, bool kinematic)
 	{
-		btRigidBody* body = _actor[i.i].actor;
+		btRigidBody* body = _actor[i.i].body;
 		int flags = body->getCollisionFlags();
 
 		if (kinematic)
@@ -663,12 +663,12 @@ struct PhysicsWorldImpl
 
 	bool actor_is_static(ActorInstance i) const
 	{
-		return _actor[i.i].actor->getCollisionFlags() & btCollisionObject::CF_STATIC_OBJECT;
+		return _actor[i.i].body->getCollisionFlags() & btCollisionObject::CF_STATIC_OBJECT;
 	}
 
 	bool actor_is_dynamic(ActorInstance i) const
 	{
-		const int flags = _actor[i.i].actor->getCollisionFlags();
+		const int flags = _actor[i.i].body->getCollisionFlags();
 		return !(flags & btCollisionObject::CF_STATIC_OBJECT)
 			&& !(flags & btCollisionObject::CF_KINEMATIC_OBJECT)
 			;
@@ -676,7 +676,7 @@ struct PhysicsWorldImpl
 
 	bool actor_is_kinematic(ActorInstance i) const
 	{
-		const int flags = _actor[i.i].actor->getCollisionFlags();
+		const int flags = _actor[i.i].body->getCollisionFlags();
 		return (flags & btCollisionObject::CF_KINEMATIC_OBJECT) != 0;
 	}
 
@@ -687,93 +687,93 @@ struct PhysicsWorldImpl
 
 	f32 actor_linear_damping(ActorInstance i) const
 	{
-		return _actor[i.i].actor->getLinearDamping();
+		return _actor[i.i].body->getLinearDamping();
 	}
 
 	void actor_set_linear_damping(ActorInstance i, f32 rate)
 	{
-		_actor[i.i].actor->setDamping(rate, _actor[i.i].actor->getAngularDamping());
+		_actor[i.i].body->setDamping(rate, _actor[i.i].body->getAngularDamping());
 	}
 
 	f32 actor_angular_damping(ActorInstance i) const
 	{
-		return _actor[i.i].actor->getAngularDamping();
+		return _actor[i.i].body->getAngularDamping();
 	}
 
 	void actor_set_angular_damping(ActorInstance i, f32 rate)
 	{
-		_actor[i.i].actor->setDamping(_actor[i.i].actor->getLinearDamping(), rate);
+		_actor[i.i].body->setDamping(_actor[i.i].body->getLinearDamping(), rate);
 	}
 
 	Vector3 actor_linear_velocity(ActorInstance i) const
 	{
-		btVector3 v = _actor[i.i].actor->getLinearVelocity();
+		btVector3 v = _actor[i.i].body->getLinearVelocity();
 		return to_vector3(v);
 	}
 
 	void actor_set_linear_velocity(ActorInstance i, const Vector3& vel)
 	{
-		_actor[i.i].actor->activate();
-		_actor[i.i].actor->setLinearVelocity(to_btVector3(vel));
+		_actor[i.i].body->activate();
+		_actor[i.i].body->setLinearVelocity(to_btVector3(vel));
 	}
 
 	Vector3 actor_angular_velocity(ActorInstance i) const
 	{
-		btVector3 v = _actor[i.i].actor->getAngularVelocity();
+		btVector3 v = _actor[i.i].body->getAngularVelocity();
 		return to_vector3(v);
 	}
 
 	void actor_set_angular_velocity(ActorInstance i, const Vector3& vel)
 	{
-		_actor[i.i].actor->activate();
-		_actor[i.i].actor->setAngularVelocity(to_btVector3(vel));
+		_actor[i.i].body->activate();
+		_actor[i.i].body->setAngularVelocity(to_btVector3(vel));
 	}
 
 	void actor_add_impulse(ActorInstance i, const Vector3& impulse)
 	{
-		_actor[i.i].actor->activate();
-		_actor[i.i].actor->applyCentralImpulse(to_btVector3(impulse));
+		_actor[i.i].body->activate();
+		_actor[i.i].body->applyCentralImpulse(to_btVector3(impulse));
 	}
 
 	void actor_add_impulse_at(ActorInstance i, const Vector3& impulse, const Vector3& pos)
 	{
-		_actor[i.i].actor->activate();
-		_actor[i.i].actor->applyImpulse(to_btVector3(impulse), to_btVector3(pos));
+		_actor[i.i].body->activate();
+		_actor[i.i].body->applyImpulse(to_btVector3(impulse), to_btVector3(pos));
 	}
 
 	void actor_add_torque_impulse(ActorInstance i, const Vector3& imp)
 	{
-		_actor[i.i].actor->applyTorqueImpulse(to_btVector3(imp));
+		_actor[i.i].body->applyTorqueImpulse(to_btVector3(imp));
 	}
 
 	void actor_push(ActorInstance i, const Vector3& vel, f32 mass)
 	{
 		const Vector3 f = vel * mass;
-		_actor[i.i].actor->applyCentralForce(to_btVector3(f));
+		_actor[i.i].body->applyCentralForce(to_btVector3(f));
 	}
 
 	void actor_push_at(ActorInstance i, const Vector3& vel, f32 mass, const Vector3& pos)
 	{
 		const Vector3 f = vel * mass;
-		_actor[i.i].actor->applyForce(to_btVector3(f), to_btVector3(pos));
+		_actor[i.i].body->applyForce(to_btVector3(f), to_btVector3(pos));
 	}
 
 	bool actor_is_sleeping(ActorInstance i)
 	{
-		return !_actor[i.i].actor->isActive();
+		return !_actor[i.i].body->isActive();
 	}
 
 	void actor_wake_up(ActorInstance i)
 	{
-		_actor[i.i].actor->activate(true);
+		_actor[i.i].body->activate(true);
 	}
 
 	JointInstance joint_create(ActorInstance a0, ActorInstance a1, const JointDesc& jd)
 	{
 		const btVector3 anchor_0 = to_btVector3(jd.anchor_0);
 		const btVector3 anchor_1 = to_btVector3(jd.anchor_1);
-		btRigidBody* actor_0 = _actor[a0.i].actor;
-		btRigidBody* actor_1 = is_valid(a1) ? _actor[a1.i].actor : NULL;
+		btRigidBody* body_0 = _actor[a0.i].body;
+		btRigidBody* body_1 = is_valid(a1) ? _actor[a1.i].body : NULL;
 
 		btTypedConstraint* joint = NULL;
 		switch(jd.type)
@@ -782,17 +782,17 @@ struct PhysicsWorldImpl
 			{
 				const btTransform frame_0 = btTransform(btQuaternion::getIdentity(), anchor_0);
 				const btTransform frame_1 = btTransform(btQuaternion::getIdentity(), anchor_1);
-	 			joint = CE_NEW(*_allocator, btFixedConstraint)(*actor_0
-	 				, *actor_1
-	 				, frame_0
-	 				, frame_1
-	 				);
+				joint = CE_NEW(*_allocator, btFixedConstraint)(*body_0
+					, *body_1
+					, frame_0
+					, frame_1
+					);
 			}
 			break;
 
 		case JointType::SPRING:
-			joint = CE_NEW(*_allocator, btPoint2PointConstraint)(*actor_0
-				, *actor_1
+			joint = CE_NEW(*_allocator, btPoint2PointConstraint)(*body_0
+				, *body_1
 				, anchor_0
 				, anchor_1
 				);
@@ -800,8 +800,8 @@ struct PhysicsWorldImpl
 
 		case JointType::HINGE:
 			{
-				btHingeConstraint* hinge = CE_NEW(*_allocator, btHingeConstraint)(*actor_0
-					, *actor_1
+				btHingeConstraint* hinge = CE_NEW(*_allocator, btHingeConstraint)(*body_0
+					, *body_1
 					, anchor_0
 					, anchor_1
 					, to_btVector3(jd.hinge.axis)
@@ -852,13 +852,13 @@ struct PhysicsWorldImpl
 
 		if (cb.hasHit())
 		{
-			const u32 actor = (u32)(uintptr_t)btRigidBody::upcast(cb.m_collisionObject)->getUserPointer();
+			const u32 actor_i = (u32)(uintptr_t)btRigidBody::upcast(cb.m_collisionObject)->getUserPointer();
 
 			hit.position = to_vector3(cb.m_hitPointWorld);
 			hit.normal   = to_vector3(cb.m_hitNormalWorld);
 			hit.time     = (f32)cb.m_closestHitFraction;
-			hit.unit     = _actor[actor].unit;
-			hit.actor.i  = actor;
+			hit.unit     = _actor[actor_i].unit;
+			hit.actor.i  = actor_i;
 			return true;
 		}
 
@@ -884,13 +884,13 @@ struct PhysicsWorldImpl
 
 			for (int i = 0; i < num; ++i)
 			{
-				const u32 actor = (u32)(uintptr_t)btRigidBody::upcast(cb.m_collisionObjects[i])->getUserPointer();
+				const u32 actor_i = (u32)(uintptr_t)btRigidBody::upcast(cb.m_collisionObjects[i])->getUserPointer();
 
 				hits[i].position = to_vector3(cb.m_hitPointWorld[i]);
 				hits[i].normal   = to_vector3(cb.m_hitNormalWorld[i]);
 				hits[i].time     = (f32)cb.m_closestHitFraction;
-				hits[i].unit     = _actor[actor].unit;
-				hits[i].actor.i  = actor;
+				hits[i].unit     = _actor[actor_i].unit;
+				hits[i].actor.i  = actor_i;
 			}
 
 			return true;
@@ -912,13 +912,13 @@ struct PhysicsWorldImpl
 
 		if (cb.hasHit())
 		{
-			const u32 actor = (u32)(uintptr_t)btRigidBody::upcast(cb.m_hitCollisionObject)->getUserPointer();
+			const u32 actor_i = (u32)(uintptr_t)btRigidBody::upcast(cb.m_hitCollisionObject)->getUserPointer();
 
 			hit.position = to_vector3(cb.m_hitPointWorld);
 			hit.normal   = to_vector3(cb.m_hitNormalWorld);
 			hit.time     = (f32)cb.m_closestHitFraction;
-			hit.unit     = _actor[actor].unit;
-			hit.actor.i  = actor;
+			hit.unit     = _actor[actor_i].unit;
+			hit.actor.i  = actor_i;
 			return true;
 		}
 
@@ -958,7 +958,7 @@ struct PhysicsWorldImpl
 			const Quaternion rot = rotation(*begin_world);
 			const Vector3 pos = translation(*begin_world);
 			// http://www.bulletphysics.org/mediawiki-1.5.8/index.php/MotionStates
-			btMotionState* ms = _actor[ai].actor->getMotionState();
+			btMotionState* ms = _actor[ai].body->getMotionState();
 			if (ms)
 				ms->setWorldTransform(btTransform(to_btQuaternion(rot), to_btVector3(pos)));
 		}
@@ -971,7 +971,7 @@ struct PhysicsWorldImpl
 
 		const int num = _dynamics_world->getNumCollisionObjects();
 		const btCollisionObjectArray& collision_array = _dynamics_world->getCollisionObjectArray();
-	    // Update actors
+		// Update actors
 		for (int i = 0; i < num; ++i)
 		{
 			if ((uintptr_t)collision_array[i]->getUserPointer() == (uintptr_t)UINT32_MAX)
@@ -1024,12 +1024,12 @@ struct PhysicsWorldImpl
 		// Limit bodies velocity
 		for (u32 i = 0; i < array::size(_actor); ++i)
 		{
-			CE_ENSURE(NULL != _actor[i].actor);
-			const btVector3 velocity = _actor[i].actor->getLinearVelocity();
+			CE_ENSURE(NULL != _actor[i].body);
+			const btVector3 velocity = _actor[i].body->getLinearVelocity();
 			const btScalar speed = velocity.length();
 
 			if (speed > 100.0f)
-				_actor[i].actor->setLinearVelocity(velocity * 100.0f / speed);
+				_actor[i].body->setLinearVelocity(velocity * 100.0f / speed);
 		}
 
 		// Check collisions
@@ -1096,7 +1096,7 @@ struct PhysicsWorldImpl
 
 	static void unit_destroyed_callback(UnitId id, void* user_ptr)
 	{
-		((PhysicsWorldImpl*)user_ptr)->unit_destroyed_callback(id);
+		static_cast<PhysicsWorldImpl*>(user_ptr)->unit_destroyed_callback(id);
 	}
 
 	static ColliderInstance make_collider_instance(u32 i) { ColliderInstance inst = { i }; return inst; }

+ 1 - 1
src/world/physics_world_noop.cpp

@@ -30,7 +30,7 @@ struct PhysicsWorldImpl
 {
 	EventStream _events;
 
-	PhysicsWorldImpl(Allocator& a)
+	explicit PhysicsWorldImpl(Allocator& a)
 		: _events(a)
 	{
 	}

+ 2 - 5
src/world/world.cpp

@@ -626,8 +626,7 @@ void spawn_units(World& w, const UnitResource& ur, const Vector3& pos, const Qua
 			for (u32 i = 0, n = component->num_instances; i < n; ++i)
 			{
 				Matrix4x4 tm = scene_graph->world_pose(unit_lookup[unit_index[i]]);
-				Vector3 scl = scale(tm);
-				physics_world->collider_create(unit_lookup[unit_index[i]], cd, scl);
+				physics_world->collider_create(unit_lookup[unit_index[i]], cd, scale(tm));
 				cd = (ColliderDesc*)((char*)(cd + 1) + cd->size);
 			}
 		}
@@ -637,9 +636,7 @@ void spawn_units(World& w, const UnitResource& ur, const Vector3& pos, const Qua
 			for (u32 i = 0, n = component->num_instances; i < n; ++i, ++ar)
 			{
 				Matrix4x4 tm = scene_graph->world_pose(unit_lookup[unit_index[i]]);
-				Vector3 pos = translation(tm);
-				Quaternion rot = rotation(tm);
-				physics_world->actor_create(unit_lookup[unit_index[i]], ar, from_quaternion_translation(rot, pos));
+				physics_world->actor_create(unit_lookup[unit_index[i]], ar, from_quaternion_translation(rotation(tm), translation(tm)));
 			}
 		}
 		else if (component->type == COMPONENT_TYPE_MESH_RENDERER)