Browse Source

Merge branch 'world' of https://github.com/taylor001/crown into world

mikymod 12 years ago
parent
commit
0755f13ffa

+ 1 - 0
CMakeLists.txt

@@ -65,6 +65,7 @@ endif (CROWN_BUILD MATCHES "linux-debug-32")
 
 # executable name
 set (CROWN_EXECUTABLE_NAME crown-${CROWN_BUILD})
+set (CROWN_LIBRARY_NAME crown-${CROWN_BUILD})
 
 # always build the engine
 add_subdirectory(engine)

+ 5 - 6
engine/CMakeLists.txt

@@ -400,7 +400,6 @@ set (PHYSICS_SRC
 set (PHYSICS_HEADERS
 	physics/Actor.h
 	physics/Controller.cpp
-	physics/Physics.h
 	physics/PhysicsTypes.h
 	physics/PhysicsWorld.h
 	physics/Trigger.h
@@ -694,22 +693,22 @@ link_libraries(${CROWN_LIBRARIES})
 
 add_definitions(${COMPILER_FLAGS})
 
-add_library(crown SHARED ${CROWN_SOURCES} ${CROWN_HEADERS})
+add_library(crown-lib SHARED ${CROWN_SOURCES} ${CROWN_HEADERS})
+set_target_properties (crown-lib PROPERTIES OUTPUT_NAME ${CROWN_LIBRARY_NAME})
 
 set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_RPATH}:$ORIGIN")
 add_executable(${CROWN_EXECUTABLE_NAME} ${CROWN_MAIN_SRC})
-target_link_libraries(${CROWN_EXECUTABLE_NAME} crown)
+target_link_libraries(${CROWN_EXECUTABLE_NAME} crown-lib)
 
 if (CROWN_BUILD_TESTS)
 	add_subdirectory(tests)
 endif (CROWN_BUILD_TESTS)
 
 
-install (TARGETS crown DESTINATION bin)
+install (TARGETS crown-lib DESTINATION bin)
+install (TARGETS crown-lib DESTINATION test)
 install (TARGETS ${CROWN_EXECUTABLE_NAME} DESTINATION bin)
 
-install (TARGETS crown DESTINATION test)
-
 #install (FILES ${HEADERS} DESTINATION include/${CMAKE_PROJECT_NAME})
 
 #install (FILES ${CORE_HEADERS} DESTINATION include/${CMAKE_PROJECT_NAME}/core)

+ 15 - 12
engine/Device.cpp

@@ -100,7 +100,6 @@ Device::Device()
 	, m_resource_manager(NULL)
 	, m_resource_bundle(NULL)
 
-	, m_physx(NULL)
 	, m_world_manager(NULL)
 
 	, m_renderer_init_request(false)
@@ -128,6 +127,7 @@ void Device::init()
 		m_console->init(false);
 	#endif
 
+	Log::d("Creating filesystem...");
 	// Default bundle filesystem
 	#if defined (LINUX) || defined(WINDOWS)
 		if (m_fileserver == 1)
@@ -148,20 +148,20 @@ void Device::init()
 			m_filesystem = CE_NEW(m_allocator, ApkFilesystem)();
 		}
 	#endif
-	Log::d("Filesystem created.");
 
 	m_resource_bundle = Bundle::create(m_allocator, *m_filesystem);
 
 	// Create resource manager
+	Log::d("Creating resource manager...");
 	m_resource_manager = CE_NEW(m_allocator, ResourceManager)(*m_resource_bundle, 0);
-	Log::d("Resource manager created.");
 	Log::d("Resource seed: %d", m_resource_manager->seed());
 
 	// Create world manager
+	Log::d("Creating world manager...");
 	m_world_manager = CE_NEW(m_allocator, WorldManager)();
-	Log::d("World manager created.");
 
 	// Create window
+	Log::d("Creating main window...");
 	m_window = CE_NEW(m_allocator, OsWindow);
 
 	// Create input devices
@@ -170,16 +170,19 @@ void Device::init()
 	m_touch = CE_NEW(m_allocator, Touch);
 
 	// Create renderer
+	Log::d("Creating renderer...");
 	m_renderer = CE_NEW(m_allocator, Renderer)(m_allocator);
 	m_renderer->init();
-	Log::d("Renderer created.");
 
+	Log::d("Creating lua environment...");
 	m_lua_environment = CE_NEW(m_allocator, LuaEnvironment)();
 	m_lua_environment->init();
-	Log::d("Lua environment created.");
 
-	m_physx = CE_NEW(m_allocator, Physics)();
-	Log::d("Physics created.");
+	Log::d("Creating physics...");
+	physics_system::init();
+
+	Log::d("Creating audio...");
+	audio_system::init();
 
 	Log::d("Crown Engine initialized.");
 	Log::d("Initializing Game...");
@@ -211,11 +214,11 @@ void Device::shutdown()
 	// Shutdowns the game
 	m_lua_environment->call_global("shutdown", 0);
 
+	Log::d("Releasing audio...");
+	audio_system::shutdown();
+
 	Log::d("Releasing Physics...");
-	if (m_physx)
-	{
-		CE_DELETE(m_allocator, m_physx);
-	}
+	physics_system::shutdown();
 
 	Log::d("Releasing LuaEnvironment...");
 	if (m_lua_environment)

+ 0 - 3
engine/Device.h

@@ -31,7 +31,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "OS.h"
 #include "LinearAllocator.h"
 #include "Resource.h"
-#include "Physics.h"
 #include "WorldTypes.h"
 
 namespace crown
@@ -149,7 +148,6 @@ public:
 	Touch*					touch();
 	Accelerometer*			accelerometer();
 	ConsoleServer*			console() { return m_console; }
-	physx::PxPhysics*		physx() { return m_physx->m_physics; };
 	WorldManager*			world_manager() { return m_world_manager; }
 
 protected:
@@ -198,7 +196,6 @@ protected:
 	ResourceManager*		m_resource_manager;
 	Bundle*					m_resource_bundle;
 
-	Physics*				m_physx;
 	WorldManager*			m_world_manager;
 
 	bool 					m_renderer_init_request;

+ 12 - 0
engine/audio/SoundWorld.h

@@ -33,6 +33,17 @@ OTHER DEALINGS IN THE SOFTWARE.
 namespace crown
 {
 
+/// Global audio-related functions
+namespace audio_system
+{
+	/// Initializes the audio system.
+	/// This is the place where to create and initialize per-application objects.
+	void init();
+
+	/// It should reverse the actions performed by audio_system::init().
+	void shutdown();
+} // namespace audio_system
+
 class Allocator;
 struct Vector3;
 struct Matrix4x4;
@@ -82,4 +93,5 @@ public:
 	static void destroy(Allocator& a, SoundWorld* sw);
 };
 
+
 } // namespace crown 

+ 41 - 22
engine/audio/backend/ALSoundWorld.cpp

@@ -56,7 +56,7 @@ static const char* al_error_to_string(ALenum error)
 }
 
 //-----------------------------------------------------------------------------
-#ifdef CROWN_DEBUG
+#if defined(CROWN_DEBUG) || defined(CROWN_DEVELOPMENT)
 	#define AL_CHECK(function)\
 		function;\
 		do { ALenum error; CE_ASSERT((error = alGetError()) == AL_NO_ERROR,\
@@ -65,6 +65,38 @@ static const char* al_error_to_string(ALenum error)
 	#define AL_CHECK(function) function;
 #endif
 
+/// Global audio-related functions
+namespace audio_system
+{
+	static ALCdevice* s_al_device;
+	static ALCcontext* s_al_context;
+
+	void init()
+	{
+		s_al_device = alcOpenDevice(NULL);
+		CE_ASSERT(s_al_device, "Cannot open OpenAL audio device");
+
+		s_al_context = alcCreateContext(s_al_device, NULL);
+		CE_ASSERT(s_al_context, "Cannot create OpenAL context");
+
+		AL_CHECK(alcMakeContextCurrent(s_al_context));
+
+		Log::d("OpenAL Vendor   : %s", alGetString(AL_VENDOR));
+		Log::d("OpenAL Version  : %s", alGetString(AL_VERSION));
+		Log::d("OpenAL Renderer : %s", alGetString(AL_RENDERER));
+
+		AL_CHECK(alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED));
+		AL_CHECK(alDopplerFactor(1.0f));
+		AL_CHECK(alDopplerVelocity(343.0f));
+	}
+
+	void shutdown()
+	{
+		alcDestroyContext(s_al_context);
+	    alcCloseDevice(s_al_device);
+	}
+}
+
 //-----------------------------------------------------------------------------
 struct SoundInstance
 {
@@ -105,7 +137,7 @@ struct SoundInstance
 	void reload(SoundResource* new_sr)
 	{
 		destroy();
-		create(new_sr, m_position);
+		create(new_sr, position());
 	}
 
 	void play(bool loop, float volume)
@@ -147,10 +179,16 @@ struct SoundInstance
 		return (state != AL_PLAYING && state != AL_PAUSED);
 	}
 
+	Vector3 position()
+	{
+		ALfloat pos[3];
+		AL_CHECK(alGetSourcefv(m_source, AL_POSITION, pos));
+		return Vector3(pos[0], pos[1], pos[2]);
+	}
+
 	void set_position(const Vector3& pos)
 	{
 		AL_CHECK(alSourcefv(m_source, AL_POSITION, pos.to_float_ptr()));
-		m_position = pos;
 	}
 
 	void set_range(float range)
@@ -172,7 +210,6 @@ public:
 
 	SoundInstanceId m_id;
 	SoundResource* m_resource;
-	Vector3 m_position;
 	ALuint m_buffer;
 	ALuint m_source;
 };
@@ -183,26 +220,11 @@ public:
 
 	ALSoundWorld()
 	{
-		m_device = alcOpenDevice(NULL);
-		CE_ASSERT(m_device, "Cannot open OpenAL audio device");
-
-		m_context = alcCreateContext(m_device, NULL);
-		CE_ASSERT(m_context, "Cannot create OpenAL context");
-
-		AL_CHECK(alcMakeContextCurrent(m_context));
-
-		AL_CHECK(alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED));
-
-		AL_CHECK(alDopplerFactor(1.0f));
-		AL_CHECK(alDopplerVelocity(343.0f));
-
 		set_listener_pose(Matrix4x4::IDENTITY);
 	}
 
 	virtual ~ALSoundWorld()
 	{
-		alcDestroyContext(m_context);
-	    alcCloseDevice(m_device);
 	}
 
 	virtual SoundInstanceId play(const char* name, bool loop, float volume, const Vector3& pos)
@@ -326,9 +348,6 @@ private:
 
 	IdArray<MAX_SOUND_INSTANCES, SoundInstance> m_playing_sounds;
 	Matrix4x4 m_listener_pose;
-
-	ALCdevice* m_device;
-	ALCcontext* m_context;
 };
 
 SoundWorld* SoundWorld::create(Allocator& a)

+ 79 - 76
engine/audio/backend/SLESSoundWorld.cpp

@@ -43,31 +43,69 @@ OTHER DEALINGS IN THE SOFTWARE.
 namespace crown
 {
 
+//-----------------------------------------------------------------------------
 static const char* sles_error_to_string(SLresult result)
 {
 	switch (result)
 	{
-	case SL_RESULT_SUCCESS: return "SL_RESULT_SUCCESS";
-	case SL_RESULT_PARAMETER_INVALID: return "SL_RESULT_PARAMETER_INVALID";
-	case SL_RESULT_MEMORY_FAILURE: return "SL_RESULT_MEMORY_FAILURE";
-	case SL_RESULT_FEATURE_UNSUPPORTED: return "SL_RESULT_FEATURE_UNSUPPORTED";
-	case SL_RESULT_RESOURCE_ERROR: return "SL_RESULT_RESOURCE_ERROR";
-	case SL_RESULT_IO_ERROR: return "SL_RESULT_IO_ERROR";
-	case SL_RESULT_PRECONDITIONS_VIOLATED: return "SL_RESULT_PRECONDITIONS_VIOLATED";
-	case SL_RESULT_CONTENT_CORRUPTED: return "SL_RESULT_CONTENT_CORRUPTED";
-	case SL_RESULT_CONTENT_UNSUPPORTED: return "SL_RESULT_CONTENT_UNSUPPORTED";
-	case SL_RESULT_CONTENT_NOT_FOUND: return "SL_RESULT_CONTENT_NOT_FOUND";
-	case SL_RESULT_PERMISSION_DENIED: return "SL_RESULT_PERMISSION_DENIED";
-	case SL_RESULT_BUFFER_INSUFFICIENT: return "SL_RESULT_BUFFER_INSUFFICIENT";
-	default: return "SL_RESULT_UNKNOWN";
+		case SL_RESULT_SUCCESS: return "SL_RESULT_SUCCESS";
+		case SL_RESULT_PARAMETER_INVALID: return "SL_RESULT_PARAMETER_INVALID";
+		case SL_RESULT_MEMORY_FAILURE: return "SL_RESULT_MEMORY_FAILURE";
+		case SL_RESULT_FEATURE_UNSUPPORTED: return "SL_RESULT_FEATURE_UNSUPPORTED";
+		case SL_RESULT_RESOURCE_ERROR: return "SL_RESULT_RESOURCE_ERROR";
+		case SL_RESULT_IO_ERROR: return "SL_RESULT_IO_ERROR";
+		case SL_RESULT_PRECONDITIONS_VIOLATED: return "SL_RESULT_PRECONDITIONS_VIOLATED";
+		case SL_RESULT_CONTENT_CORRUPTED: return "SL_RESULT_CONTENT_CORRUPTED";
+		case SL_RESULT_CONTENT_UNSUPPORTED: return "SL_RESULT_CONTENT_UNSUPPORTED";
+		case SL_RESULT_CONTENT_NOT_FOUND: return "SL_RESULT_CONTENT_NOT_FOUND";
+		case SL_RESULT_PERMISSION_DENIED: return "SL_RESULT_PERMISSION_DENIED";
+		case SL_RESULT_BUFFER_INSUFFICIENT: return "SL_RESULT_BUFFER_INSUFFICIENT";
+		default: return "UNKNOWN_SL_ERROR";
 	}
 }
 
 //-----------------------------------------------------------------------------
-void check_sles_errors(SLresult result)
+#if defined(CROWN_DEBUG) || defined(CROWN_DEVELOPMENT)
+	#define SL_CHECK(function)\
+		do { SLresult result = function;\
+				CE_ASSERT(result == SL_RESULT_SUCCESS, "OpenSL|ES error: %s", sles_error_to_string(result)); } while (0)
+#else
+	#define SL_CHECK(function) function;
+#endif
+
+namespace audio_system
 {
-	CE_ASSERT(result == SL_RESULT_SUCCESS, "SL_ERROR_CODE: %s", sles_error_to_string(result));
-}
+	static SLObjectItf s_sl_engine;
+	static SLEngineItf s_sl_engine_itf;
+	static SLObjectItf s_sl_output_mix;
+
+	void init()
+	{
+		const SLInterfaceID ids[] = {SL_IID_ENGINE};
+		const SLboolean reqs[] = {SL_BOOLEAN_TRUE};
+		const SLEngineOption opts[] = { (SLuint32) SL_ENGINEOPTION_THREADSAFE, (SLuint32) SL_BOOLEAN_TRUE };
+
+		// Create OpenSL engine
+		SL_CHECK(slCreateEngine(&s_sl_engine, 1, opts, 1, ids, reqs));
+		SL_CHECK((*s_sl_engine)->Realize(s_sl_engine, SL_BOOLEAN_FALSE));
+
+		// Obtain OpenSL engine interface
+		SL_CHECK((*s_sl_engine)->GetInterface(s_sl_engine, SL_IID_ENGINE, &s_sl_engine_itf));
+
+		// Create global output mix
+		const SLInterfaceID ids1[] = {SL_IID_VOLUME};
+		const SLboolean reqs1[] = {SL_BOOLEAN_FALSE};
+
+		SL_CHECK((*s_sl_engine_itf)->CreateOutputMix(s_sl_engine_itf, &s_sl_output_mix, 1, ids1, reqs1)); 
+		SL_CHECK((*s_sl_output_mix)->Realize(s_sl_output_mix, SL_BOOLEAN_FALSE));
+	}
+
+	void shutdown()
+	{
+		(*s_sl_output_mix)->Destroy(s_sl_output_mix);
+		(*s_sl_engine)->Destroy(s_sl_engine);
+	}
+} // namespace audio_system
 
 namespace sles_sound_world
 {
@@ -111,10 +149,8 @@ namespace sles_sound_world
 //-----------------------------------------------------------------------------
 struct SoundInstance
 {
-	void create(SLEngineItf engine, SLObjectItf out_mix_obj, SoundInstanceId id, SoundResource* sr)
+	void create(SLEngineItf engine, SLObjectItf output_mix, SoundInstanceId id, SoundResource* sr)
 	{
-		m_sl_engine = engine;
-		m_out_mix_obj = out_mix_obj;
 		m_resource = sr;
 		m_finished = false;
 		m_id = id;
@@ -178,7 +214,7 @@ struct SoundInstance
 		// Configures audio output mix
 		SLDataLocator_OutputMix out_mix;
 		out_mix.locatorType = SL_DATALOCATOR_OUTPUTMIX;
-		out_mix.outputMix = m_out_mix_obj;
+		out_mix.outputMix = output_mix;
 
 		// Configures audio sink
 		SLDataSink audio_sink;
@@ -189,25 +225,14 @@ struct SoundInstance
 		const SLInterfaceID ids[] = {SL_IID_PLAY, SL_IID_BUFFERQUEUE, SL_IID_VOLUME};
 		const SLboolean reqs[] = {SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE, SL_BOOLEAN_TRUE};
 
-		SLresult result = (*m_sl_engine)->CreateAudioPlayer(m_sl_engine, &m_player_obj, &audio_source, &audio_sink, 3, ids, reqs);
-		check_sles_errors(result);
-
-		result = (*m_player_obj)->Realize(m_player_obj, SL_BOOLEAN_FALSE);
-		check_sles_errors(result);
-
-		// Gets interfaces
-		result = (*m_player_obj)->GetInterface(m_player_obj, SL_IID_PLAY, &m_player_play);
-		check_sles_errors(result);
+		SL_CHECK((*engine)->CreateAudioPlayer(engine, &m_player, &audio_source, &audio_sink, 3, ids, reqs));
+		SL_CHECK((*m_player)->Realize(m_player, SL_BOOLEAN_FALSE));
 
-		result = (*m_player_obj)->GetInterface(m_player_obj, SL_IID_BUFFERQUEUE, &m_player_bufferqueue);
-		check_sles_errors(result);
-
-		result = (*m_player_obj)->GetInterface(m_player_obj, SL_IID_VOLUME, &m_player_volume);
-		check_sles_errors(result);
+		SL_CHECK((*m_player)->GetInterface(m_player, SL_IID_BUFFERQUEUE, &m_player_bufferqueue));
 
 		//(*m_player_bufferqueue)->RegisterCallback(m_player_bufferqueue, SoundInstance::buffer_callback, this);
-		(*m_player_play)->SetCallbackEventsMask(m_player_play, SL_PLAYEVENT_HEADATEND);
-		(*m_player_play)->RegisterCallback(m_player_play, sles_sound_world::player_callback, (void*) id.encode());
+		(*play_itf())->SetCallbackEventsMask(play_itf(), SL_PLAYEVENT_HEADATEND);
+		(*play_itf())->RegisterCallback(play_itf(), sles_sound_world::player_callback, (void*) id.encode());
 
 		// Manage simple sound or stream
 		// m_streaming = sr->sound_type() == SoundType::OGG;
@@ -233,8 +258,8 @@ struct SoundInstance
 		// }
 		stop();
 		(*m_player_bufferqueue)->Clear(m_player_bufferqueue);
-		(*m_player_obj)->AbortAsyncOperation(m_player_obj);
-		(*m_player_obj)->Destroy(m_player_obj);
+		(*m_player)->AbortAsyncOperation(m_player);
+		(*m_player)->Destroy(m_player);
 	}
 
 	void reload(SoundResource* new_sr)
@@ -244,26 +269,22 @@ struct SoundInstance
 	void play(bool loop, float volume)
 	{
 		set_volume(volume);
-		SLresult result = (*m_player_play)->SetPlayState(m_player_play, SL_PLAYSTATE_PLAYING);
-		check_sles_errors(result);
+		SL_CHECK((*play_itf())->SetPlayState(play_itf(), SL_PLAYSTATE_PLAYING));
 	}
 
 	void pause()
 	{
-		SLresult result = (*m_player_play)->SetPlayState(m_player_play, SL_PLAYSTATE_PAUSED);
-		check_sles_errors(result);
+		SL_CHECK((*play_itf())->SetPlayState(play_itf(), SL_PLAYSTATE_PAUSED));
 	}
 
 	void resume()
 	{
-		SLresult res = (*m_player_play)->SetPlayState(m_player_play, SL_PLAYSTATE_PLAYING);
-		check_sles_errors(res);
+		SL_CHECK((*play_itf())->SetPlayState(play_itf(), SL_PLAYSTATE_PLAYING));
 	}
 
 	void stop()
 	{
-		SLresult res = (*m_player_play)->SetPlayState(m_player_play, SL_PLAYSTATE_STOPPED);
-		check_sles_errors(res);
+		SL_CHECK((*play_itf())->SetPlayState(play_itf(), SL_PLAYSTATE_STOPPED));
 	}
 
 	bool finished()
@@ -273,8 +294,9 @@ struct SoundInstance
 
 	void set_volume(float volume)
 	{
-		SLresult res = (*m_player_volume)->SetVolumeLevel(m_player_volume, sles_sound_world::gain_to_attenuation(m_player_volume, volume));
-		check_sles_errors(res);
+		SLVolumeItf vol;
+		SL_CHECK((*m_player)->GetInterface(m_player, SL_IID_VOLUME, &vol));
+		SL_CHECK((*vol)->SetVolumeLevel(vol, sles_sound_world::gain_to_attenuation(vol, volume)));
 	}
 
 	void set_range(float range)
@@ -315,17 +337,20 @@ struct SoundInstance
 		return m_resource;
 	}
 
+	SLPlayItf play_itf()
+	{
+		SLPlayItf play;
+		SL_CHECK((*m_player)->GetInterface(m_player, SL_IID_PLAY, &play));
+		return play;
+	}
+
 public:
 
 	SoundInstanceId m_id;
 	SoundResource* m_resource;
 
-	SLEngineItf m_sl_engine;
-	SLObjectItf m_out_mix_obj;
-	SLObjectItf m_player_obj;
-	SLPlayItf m_player_play;
+	SLObjectItf m_player;
 	SLAndroidSimpleBufferQueueItf m_player_bufferqueue;
-	SLVolumeItf m_player_volume;
 
 	uint32_t m_processed_buffers;
 	bool m_finished;
@@ -339,28 +364,10 @@ public:
 	SLESSoundWorld()
 	{
 		sles_sound_world::init();
-		SLresult result;
-
-		const SLInterfaceID ids[] = {SL_IID_ENGINE};
-		const SLboolean reqs[] = {SL_BOOLEAN_TRUE};
-		const SLEngineOption opts[] = { (SLuint32) SL_ENGINEOPTION_THREADSAFE, (SLuint32) SL_BOOLEAN_TRUE };
-
-		result = slCreateEngine(&m_sl_object, 1, opts, 1, ids, reqs);
-		result = (*m_sl_object)->Realize(m_sl_object, SL_BOOLEAN_FALSE);
-
-		result = (*m_sl_object)->GetInterface(m_sl_object, SL_IID_ENGINE, &m_sl_engine);
-
-		const SLInterfaceID ids1[] = {SL_IID_VOLUME};
-		const SLboolean reqs1[] = {SL_BOOLEAN_FALSE};
-
-		result = (*m_sl_engine)->CreateOutputMix(m_sl_engine, &m_out_mix_obj, 1, ids1, reqs1); 
-		result = (*m_out_mix_obj)->Realize(m_out_mix_obj, SL_BOOLEAN_FALSE);
 	}
 
 	virtual ~SLESSoundWorld()
 	{
-		(*m_out_mix_obj)->Destroy(m_out_mix_obj);
-		(*m_sl_object)->Destroy(m_sl_object);
 		sles_sound_world::shutdown();
 	}
 
@@ -375,7 +382,7 @@ public:
 		SoundInstanceId id = m_playing_sounds.create(dummy);
 
 		SoundInstance& instance = m_playing_sounds.lookup(id);
-		instance.create(m_sl_engine, m_out_mix_obj, id, sr);
+		instance.create(audio_system::s_sl_engine_itf, audio_system::s_sl_output_mix, id, sr);
 		instance.play(loop, volume);
 	}
 
@@ -467,10 +474,6 @@ private:
 
 	IdArray<MAX_SOUND_INSTANCES, SoundInstance> m_playing_sounds;
 	Matrix4x4 m_listener_pose;
-
-	SLObjectItf m_sl_object;
-	SLEngineItf m_sl_engine;
-	SLObjectItf m_out_mix_obj;
 };
 
 SoundWorld* SoundWorld::create(Allocator& a)

+ 8 - 9
engine/physics/Actor.cpp

@@ -29,8 +29,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Quaternion.h"
 #include "Matrix4x4.h"
 #include "Unit.h"
-#include "Device.h"
-#include "Physics.h"
 #include "Log.h"
 #include "PhysicsResource.h"
 #include "SceneGraph.h"
@@ -64,7 +62,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-Actor::Actor(const PhysicsResource* res, uint32_t i, PxScene* scene, SceneGraph& sg, int32_t node, const Vector3& pos, const Quaternion& rot)
+Actor::Actor(const PhysicsResource* res, uint32_t i, PxPhysics* physics, PxScene* scene, SceneGraph& sg, int32_t node, const Vector3& pos, const Quaternion& rot)
 	: m_resource(res)
 	, m_index(i)
 	, m_scene(scene)
@@ -81,28 +79,29 @@ Actor::Actor(const PhysicsResource* res, uint32_t i, PxScene* scene, SceneGraph&
 	{
 		case ActorType::STATIC:
 		{
-			m_actor = device()->physx()->createRigidStatic(PxTransform(pose));
+			m_actor = physics->createRigidStatic(PxTransform(pose));
 			break;
 		}
 		case ActorType::DYNAMIC_PHYSICAL:
 		case ActorType::DYNAMIC_KINEMATIC:
 		{
-			m_actor = device()->physx()->createRigidDynamic(PxTransform(pose));
+			m_actor = physics->createRigidDynamic(PxTransform(pose));
 
 			if (a.type == ActorType::DYNAMIC_KINEMATIC)
 			{
 				static_cast<PxRigidDynamic*>(m_actor)->setRigidDynamicFlag(PxRigidDynamicFlag::eKINEMATIC, true);
 			}
-			break;
+			//break;
 
-			PxRigidBodyExt::setMassAndUpdateInertia(*static_cast<PxRigidDynamic*>(m_actor), 500.0f);
+			//PxRigidBodyExt::setMassAndUpdateInertia(*static_cast<PxRigidDynamic*>(m_actor), 500.0f);
 
-			PxD6Joint* joint = PxD6JointCreate(*device()->physx(), m_actor, PxTransform(pose), NULL, PxTransform(pose));
+			PxD6Joint* joint = PxD6JointCreate(*physics, m_actor, PxTransform(pose), NULL, PxTransform(pose));
 			joint->setMotion(PxD6Axis::eX, PxD6Motion::eFREE);
 			joint->setMotion(PxD6Axis::eY, PxD6Motion::eFREE);
 			//joint->setMotion(PxD6Axis::eZ, PxD6Motion::eFREE);
 			//joint->setMotion(PxD6Axis::eSWING1, PxD6Motion::eFREE);
 			joint->setMotion(PxD6Axis::eSWING2, PxD6Motion::eFREE);
+			break;
 		}
 		default:
 		{
@@ -114,7 +113,7 @@ Actor::Actor(const PhysicsResource* res, uint32_t i, PxScene* scene, SceneGraph&
 	m_actor->userData = this;
 
 	// Creates material
-	m_mat = device()->physx()->createMaterial(a.static_friction, a.dynamic_friction, a.restitution);
+	m_mat = physics->createMaterial(a.static_friction, a.dynamic_friction, a.restitution);
 
 	// Creates shapes
 	uint32_t index = m_resource->shape_index(m_index);

+ 2 - 2
engine/physics/Actor.h

@@ -27,7 +27,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 #pragma once
 
 #include "Types.h"
-#include "IdTable.h"
 #include "IdArray.h"
 #include "PhysicsTypes.h"
 #include "Vector3.h"
@@ -41,6 +40,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 using physx::PxRigidActor;
 using physx::PxMaterial;
 using physx::PxScene;
+using physx::PxPhysics;
 
 namespace crown
 {
@@ -53,7 +53,7 @@ class SceneGraph;
 
 struct Actor
 {
-						Actor(const PhysicsResource* res, uint32_t i, PxScene* scene, SceneGraph& sg, int32_t node, const Vector3& pos, const Quaternion& rot);
+						Actor(const PhysicsResource* res, uint32_t i, PxPhysics* physics, PxScene* scene, SceneGraph& sg, int32_t node, const Vector3& pos, const Quaternion& rot);
 						~Actor();
 
 	void				enable_gravity();

+ 4 - 4
engine/physics/Controller.cpp

@@ -25,7 +25,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 */
 
 #include "Controller.h"
-#include "Device.h"
 #include "MathUtils.h"
 #include "PhysicsResource.h"
 #include "SceneGraph.h"
@@ -33,6 +32,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "PhysicsCallback.h"
 
 #include "PxCapsuleController.h"
+#include "PxPhysicsAPI.h"
 using physx::PxCapsuleClimbingMode;
 using physx::PxCapsuleController;
 using physx::PxCapsuleControllerDesc;
@@ -46,7 +46,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-Controller::Controller(const PhysicsResource* pr, SceneGraph& sg, int32_t node, PxScene* scene, PxControllerManager* manager)
+Controller::Controller(const PhysicsResource* pr, SceneGraph& sg, int32_t node, PxPhysics* physics, PxScene* scene, PxControllerManager* manager)
 	: m_resource(pr)
 	, m_scene_graph(sg)
 	, m_node(node)
@@ -65,14 +65,14 @@ Controller::Controller(const PhysicsResource* pr, SceneGraph& sg, int32_t node,
 	desc.stepOffset = contr.step_offset;
 	desc.contactOffset = contr.contact_offset;
 	desc.upDirection = PxVec3(0.0, 1.0, 0.0);
-	desc.material = device()->physx()->createMaterial(0.5f, 0.5f, 0.5f);
+	desc.material = physics->createMaterial(0.5f, 0.5f, 0.5f);
 	desc.position = PxExtendedVec3(0, 0, 0);
 
 	CE_ASSERT(desc.isValid(), "Capsule is not valid");
 	m_callback = CE_NEW(default_allocator(), PhysicsControllerCallback)();
 	desc.callback = m_callback;
 
-	m_controller = manager->createController(*device()->physx(), scene, desc);
+	m_controller = manager->createController(*physics, scene, desc);
 	CE_ASSERT(m_controller, "Failed to create controller");
 }
 

+ 2 - 1
engine/physics/Controller.h

@@ -31,6 +31,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 using physx::PxController;
 using physx::PxControllerManager;
+using physx::PxPhysics;
 using physx::PxScene;
 using physx::PxU32;
 
@@ -44,7 +45,7 @@ class PhysicsControllerCallback;
 
 struct Controller
 {
-							Controller(const PhysicsResource* pr, SceneGraph& sg, int32_t node, PxScene* scene, PxControllerManager* manager);
+							Controller(const PhysicsResource* pr, SceneGraph& sg, int32_t node, PxPhysics* physics, PxScene* scene, PxControllerManager* manager);
 							~Controller();
 
 	void					move(const Vector3& pos);

+ 0 - 146
engine/physics/Physics.h

@@ -1,146 +0,0 @@
-/*
-Copyright (c) 2013 Daniele Bartolini, Michele Rossi
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#pragma once
-
-#include "ProxyAllocator.h"
-
-#include "PxFoundation.h"
-#include "PxPhysics.h"
-#include "PxCooking.h"
-#include "PxDefaultAllocator.h"
-#include "PxDefaultErrorCallback.h"
-#include "PxExtensionsAPI.h"
-
-#include "Log.h"
-
-using physx::PxAllocatorCallback;
-using physx::PxErrorCallback;
-using physx::PxErrorCode;
-
-namespace crown
-{
-
-class PhysXAllocator : public PxAllocatorCallback
-{
-public:
-
-	PhysXAllocator(Allocator& a)
-		: m_backing(a)
-	{
-	}
-
-	void* allocate(size_t size, const char*, const char*, int)
-	{
-		return m_backing.allocate(size, 16);
-	}
-
-	void deallocate(void* p)
-	{
-		m_backing.deallocate(p);
-	}
-
-private:
-
-	Allocator& m_backing;
-};
-
-class PhysXError : public PxErrorCallback
-{
-public:
-
-	void reportError(PxErrorCode::Enum code, const char* message, const char* file, int line)
-	{
-		switch (code)
-		{
-			case PxErrorCode::eDEBUG_INFO:
-			{
-				Log::i("In %s:%d: %s", file, line, message);
-				break;
-			}
-			case PxErrorCode::eDEBUG_WARNING: 
-			case PxErrorCode::ePERF_WARNING:
-			{
-				Log::w("In %s:%d: %s", file, line, message);
-				break;
-			}
-			case PxErrorCode::eINVALID_PARAMETER:
-			case PxErrorCode::eINVALID_OPERATION:
-			case PxErrorCode::eOUT_OF_MEMORY:
-			case PxErrorCode::eINTERNAL_ERROR:
-			case PxErrorCode::eABORT:
-			{
-				Log::e("In %s:%d: %s", file, line, message);
-				break;
-			}
-			default:
-			{
-				break;
-			}
-		}
-	}
-};
-
-struct Physics
-{
-	Physics();
-	~Physics();
-
-public:
-
-	ProxyAllocator m_allocator;
-	PhysXAllocator m_px_allocator;
-	PhysXError m_error;
-	physx::PxFoundation* m_foundation;
-	physx::PxPhysics* m_physics;
-};
-
-//-----------------------------------------------------------------------------
-inline Physics::Physics()
-	: m_allocator("physics", default_allocator())
-	, m_px_allocator(m_allocator)
-	, m_foundation(NULL)
-	, m_physics(NULL)
-{
-	m_foundation = PxCreateFoundation(PX_PHYSICS_VERSION, m_px_allocator, m_error);
-	CE_ASSERT(m_foundation, "Unable to create PhysX Foundation");
-
-	m_physics = PxCreatePhysics(PX_PHYSICS_VERSION, *m_foundation, physx::PxTolerancesScale());
-	CE_ASSERT(m_physics, "Unable to create PhysX Physics");
-
-	bool extension = PxInitExtensions(*m_physics);
-	CE_ASSERT(extension, "Unable to initialize PhysX Extensions");
-}
-
-//-----------------------------------------------------------------------------
-inline Physics::~Physics()
-{
-	PxCloseExtensions();
-	m_physics->release();
-	m_foundation->release();
-}
-
-} // namespace crown

+ 118 - 11
engine/physics/PhysicsWorld.cpp

@@ -28,12 +28,12 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Vector3.h"
 #include "Actor.h"
 #include "Device.h"
-#include "Physics.h"
 #include "Quaternion.h"
 #include "SceneGraph.h"
 #include "Controller.h"
 #include "Trigger.h"
 #include "PhysicsCallback.h"
+#include "ProxyAllocator.h"
 
 #include "PxPhysicsAPI.h"
 
@@ -57,10 +57,113 @@ using physx::PxFilterObjectAttributes;
 using physx::PxFilterObjectIsTrigger;
 using physx::PxPairFlag;
 using physx::PxFilterFlag;
+using physx::PxSceneLimits;
 
 namespace crown
 {
 
+namespace physics_system
+{
+	using physx::PxFoundation;
+	using physx::PxPhysics;
+	using physx::PxAllocatorCallback;
+	using physx::PxErrorCallback;
+	using physx::PxErrorCode;
+
+	//-----------------------------------------------------------------------------
+	class PhysXAllocator : public PxAllocatorCallback
+	{
+	public:
+
+		PhysXAllocator()
+			: m_backing("physics", default_allocator())
+		{
+		}
+
+		void* allocate(size_t size, const char*, const char*, int)
+		{
+			return m_backing.allocate(size, 16);
+		}
+
+		void deallocate(void* p)
+		{
+			m_backing.deallocate(p);
+		}
+
+	private:
+
+		ProxyAllocator m_backing;
+	};
+
+	//-----------------------------------------------------------------------------
+	class PhysXError : public PxErrorCallback
+	{
+	public:
+
+		void reportError(PxErrorCode::Enum code, const char* message, const char* file, int line)
+		{
+			switch (code)
+			{
+				case PxErrorCode::eDEBUG_INFO:
+				{
+					Log::i("In %s:%d: %s", file, line, message);
+					break;
+				}
+				case PxErrorCode::eDEBUG_WARNING: 
+				case PxErrorCode::ePERF_WARNING:
+				{
+					Log::w("In %s:%d: %s", file, line, message);
+					break;
+				}
+				case PxErrorCode::eINVALID_PARAMETER:
+				case PxErrorCode::eINVALID_OPERATION:
+				case PxErrorCode::eOUT_OF_MEMORY:
+				case PxErrorCode::eINTERNAL_ERROR:
+				case PxErrorCode::eABORT:
+				{
+					CE_ASSERT(false, "In %s:%d: %s", file, line, message);
+					break;
+				}
+				default:
+				{
+					CE_FATAL("Oops, unknown physx error");
+					break;
+				}
+			}
+		}
+	};
+
+	static PhysXAllocator* s_px_allocator;
+	static PhysXError* s_px_error;
+	static PxFoundation* s_foundation;
+	static PxPhysics* s_physics;
+
+	void init()
+	{
+		s_px_allocator = CE_NEW(default_allocator(), PhysXAllocator)();
+		s_px_error = CE_NEW(default_allocator(), PhysXError)();
+
+		s_foundation = PxCreateFoundation(PX_PHYSICS_VERSION, *s_px_allocator, *s_px_error);
+		CE_ASSERT(s_foundation, "Unable to create PhysX Foundation");
+
+		s_physics = PxCreatePhysics(PX_PHYSICS_VERSION, *s_foundation, physx::PxTolerancesScale());
+		CE_ASSERT(s_physics, "Unable to create PhysX Physics");
+
+		bool extension = PxInitExtensions(*s_physics);
+		CE_ASSERT(extension, "Unable to initialize PhysX Extensions");
+	}
+
+	void shutdown()
+	{
+		PxCloseExtensions();
+		s_physics->release();
+		s_foundation->release();
+
+		CE_DELETE(default_allocator(), s_px_error);
+		CE_DELETE(default_allocator(), s_px_allocator);
+	}
+} // namespace physics_system
+
 PxFilterFlags PhysicsFilterShader(PxFilterObjectAttributes attributes0, PxFilterData filterData0, 
 								PxFilterObjectAttributes attributes1, PxFilterData filterData1,
 								PxPairFlags& pairFlags, const void* constantBlock, PxU32 constantBlockSize)
@@ -94,8 +197,13 @@ PhysicsWorld::PhysicsWorld()
 	, m_controllers_pool(default_allocator(), MAX_CONTROLLERS, sizeof(Controller), CE_ALIGNOF(Controller))
 	, m_triggers_pool(default_allocator(), MAX_TRIGGERS, sizeof(Trigger), CE_ALIGNOF(Trigger))
 {
-	PxSceneDesc scene_desc(device()->physx()->getTolerancesScale());
+	PxSceneLimits scene_limits;
+	scene_limits.maxNbActors = MAX_ACTORS;
+	CE_ASSERT(scene_limits.isValid(), "Scene limits is not valid");
+
+	PxSceneDesc scene_desc(physics_system::s_physics->getTolerancesScale());
 	scene_desc.gravity = PxVec3(0.0f, -9.81f, 0.0f);
+	scene_desc.limits = scene_limits;
 
 	if(!scene_desc.cpuDispatcher)
 	{
@@ -107,20 +215,19 @@ PhysicsWorld::PhysicsWorld()
 
 	// Set filter shader
 	scene_desc.filterShader = g_default_filter_shader;
-	// Enable active transformation
-	scene_desc.flags = PxSceneFlag::eENABLE_ACTIVETRANSFORMS;
+	scene_desc.flags = PxSceneFlag::eENABLE_ACTIVETRANSFORMS | PxSceneFlag::eENABLE_KINEMATIC_STATIC_PAIRS;
 
 	// Set simulation event callback
 	m_callback = CE_NEW(default_allocator(), PhysicsSimulationCallback)();
 	scene_desc.simulationEventCallback = m_callback;
 
-	// Create scene
-	m_scene = device()->physx()->createScene(scene_desc);
+	CE_ASSERT(scene_desc.isValid(), "Scene is not valid");
 
-	m_scene->setFlag(PxSceneFlag::eENABLE_KINEMATIC_STATIC_PAIRS, true);
+	// Create scene
+	m_scene = physics_system::s_physics->createScene(scene_desc);
 
 	// Create controller manager
-	m_controller_manager = PxCreateControllerManager(device()->physx()->getFoundation());
+	m_controller_manager = PxCreateControllerManager(*physics_system::s_foundation);
 	CE_ASSERT(m_controller_manager != NULL, "Failed to create PhysX controller manager");
 }
 
@@ -136,7 +243,7 @@ PhysicsWorld::~PhysicsWorld()
 //-----------------------------------------------------------------------------
 ActorId	PhysicsWorld::create_actor(const PhysicsResource* res, const uint32_t index, SceneGraph& sg, int32_t node)
 {
-	Actor* actor = CE_NEW(m_actors_pool, Actor)(res, index, m_scene, sg, node, Vector3::ZERO, Quaternion::IDENTITY);
+	Actor* actor = CE_NEW(m_actors_pool, Actor)(res, index, physics_system::s_physics, m_scene, sg, node, Vector3::ZERO, Quaternion::IDENTITY);
 	return m_actors.create(actor);
 }
 
@@ -152,7 +259,7 @@ void PhysicsWorld::destroy_actor(ActorId id)
 //-----------------------------------------------------------------------------
 ControllerId PhysicsWorld::create_controller(const PhysicsResource* pr, SceneGraph& sg, int32_t node)
 {
-	Controller* controller = CE_NEW(m_controllers_pool, Controller)(pr, sg, node, m_scene, m_controller_manager);
+	Controller* controller = CE_NEW(m_controllers_pool, Controller)(pr, sg, node, physics_system::s_physics, m_scene, m_controller_manager);
 	return m_controllers.create(controller);
 }
 
@@ -168,7 +275,7 @@ void PhysicsWorld::destroy_controller(ControllerId id)
 //-----------------------------------------------------------------------------
 TriggerId PhysicsWorld::create_trigger(const Vector3& half_extents, const Vector3& pos, const Quaternion& rot)
 {
-	Trigger* trigger = CE_NEW(m_triggers_pool, Trigger)(m_scene, half_extents, pos, rot);
+	Trigger* trigger = CE_NEW(m_triggers_pool, Trigger)(physics_system::s_physics, m_scene, half_extents, pos, rot);
 	return m_triggers.create(trigger);
 }
 

+ 11 - 0
engine/physics/PhysicsWorld.h

@@ -48,6 +48,17 @@ using physx::PxActor;
 namespace crown
 {
 
+/// Global physics-related functions
+namespace physics_system
+{
+	/// Initializes the physics system.
+	/// This is the place where to create and initialize per-application objects.
+	void init();
+
+	/// It should reverse the actions performed by audio_system::init().
+	void shutdown();
+} // namespace physics_system
+
 //-----------------------------------------------------------------------------
 struct PhysicsResource;
 struct PhysicsActor;

+ 3 - 5
engine/physics/Trigger.cpp

@@ -24,9 +24,7 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 */
 
-#include "Device.h"
 #include "Matrix4x4.h"
-#include "Physics.h"
 #include "PxPhysicsAPI.h"
 #include "Quaternion.h"
 #include "Trigger.h"
@@ -51,17 +49,17 @@ namespace crown
 {
 	
 //-----------------------------------------------------------------------------
-Trigger::Trigger(PxScene* scene, const Vector3& half_extents, const Vector3& pos, const Quaternion& rot)
+Trigger::Trigger(PxPhysics* physics, PxScene* scene, const Vector3& half_extents, const Vector3& pos, const Quaternion& rot)
 	: m_scene(scene)
 {
 	Matrix4x4 m(rot, pos);
 	m.transpose();
 	PxMat44 pose((PxReal*)(m.to_float_ptr()));
 
-	m_actor = device()->physx()->createRigidStatic(PxTransform(pose));
+	m_actor = physics->createRigidStatic(PxTransform(pose));
 	m_actor->userData = NULL;
 
-	m_mat = device()->physx()->createMaterial(0.5f, 0.5f, 0.5f);
+	m_mat = physics->createMaterial(0.5f, 0.5f, 0.5f);
 	PxShape* shape = m_actor->createShape(PxBoxGeometry(half_extents.x, half_extents.y, half_extents.z), *m_mat);
 	shape->setFlag(PxShapeFlag::eSIMULATION_SHAPE, false);
 	shape->setFlag(PxShapeFlag::eTRIGGER_SHAPE, true);

+ 2 - 1
engine/physics/Trigger.h

@@ -39,6 +39,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 using physx::PxRigidActor;
 using physx::PxMaterial;
 using physx::PxScene;
+using physx::PxPhysics;
 
 namespace crown
 {
@@ -50,7 +51,7 @@ class SceneGraph;
 
 struct Trigger
 {
-						Trigger(PxScene* scene, const Vector3& half_extents, const Vector3& pos, const Quaternion& rot);
+						Trigger(PxPhysics* physics, PxScene* scene, const Vector3& half_extents, const Vector3& pos, const Quaternion& rot);
 						~Trigger();
 
 public: