Parcourir la source

Fix main thread (~85%)

Daniele Bartolini il y a 12 ans
Parent
commit
8568202d38

+ 1 - 34
engine/Device.cpp

@@ -67,33 +67,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 namespace crown
 {
 
-class MainThread : public Thread
-{
-public:
-
-	MainThread()
-		: Thread("main-thread")
-	{
-	}
-
-	int32_t run()
-	{
-		Device* engine = device();
-		engine->init();
-
-		while (!is_terminating() && engine->is_running())
-		{
-			engine->frame();
-		}
-
-		engine->shutdown();
-
-		return 0;
-	}
-};
-
-static MainThread g_main_thread;
-
 //-----------------------------------------------------------------------------
 Device::Device() : 
 	m_allocator(m_subsystems_heap, MAX_SUBSYSTEMS_HEAP),
@@ -172,11 +145,7 @@ bool Device::init(int argc, char** argv)
 		}
 	#endif
 
-	g_main_thread.start();
-
-	#ifndef ANDROID
-	g_main_thread.join();
-	#endif
+	init();
 
 	return true;
 }
@@ -277,8 +246,6 @@ void Device::shutdown()
 {
 	CE_ASSERT(is_init(), "Engine is not initialized");
 
-	g_main_thread.stop();
-
 	// Shutdowns the game
 	m_lua_environment->call_global("shutdown", 0);
 

+ 41 - 3
engine/os/linux/main.cpp

@@ -26,17 +26,55 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #include "Crown.h"
 
-int main(int argc, char** argv)
+namespace crown
+{
+
+struct MainArgs
+{
+	int argc;
+	char** argv;
+};
+
+static OsThread thread("main-thread");
+
+int32_t main_thread(void* data)
 {
 	crown::os::init_os();
 
 	crown::Device* engine = crown::device();
 
-	if (!engine->init(argc, argv))
+	MainArgs* args = (MainArgs*)data;
+	if (!engine->init(args->argc, args->argv))
 	{
-		exit(-1);
+		return -1;
 	}
 
+	while (engine->is_running())
+	{
+		engine->frame();
+	}
+
+	engine->shutdown();
+
+	thread.stop();
+
 	return 0;
 }
 
+int ce_main(void* args)
+{
+	thread.start(main_thread, args);
+
+	while (thread.is_running()) ;
+	return 0;
+}
+
+}
+
+int main(int argc, char** argv)
+{
+	crown::MainArgs args;
+	args.argc = argc;
+	args.argv = argv;
+	return crown::ce_main(&args);
+}

+ 2 - 2
engine/resource/ResourceLoader.cpp

@@ -33,7 +33,7 @@ namespace crown
 
 //-----------------------------------------------------------------------------
 ResourceLoader::ResourceLoader(Bundle& bundle, Allocator& resource_heap) :
-	Thread("resource-loader"),
+	m_thread("resource-loader"),
 	m_bundle(bundle),
 	m_resource_heap(resource_heap),
 	m_num_requests(0),
@@ -94,7 +94,7 @@ void* ResourceLoader::load_resource_data(LoadResourceId id) const
 //-----------------------------------------------------------------------------
 int32_t ResourceLoader::run()
 {
-	while (!is_terminating())
+	while (m_thread.is_running())
 	{
 		m_requests_mutex.lock();
 		while (m_requests.empty())

+ 20 - 1
engine/resource/ResourceLoader.h

@@ -66,7 +66,7 @@ struct LoadResourceData
 };
 
 /// Loads resources in a background thread.
-class ResourceLoader : public Thread
+class ResourceLoader
 {
 public:
 
@@ -86,8 +86,27 @@ public:
 	// Loads resources in the loading queue.
 	int32_t					run();
 
+	void start()
+	{
+		m_thread.start(background_run, this);
+	}
+
+	void stop()
+	{
+		m_thread.stop();
+	}
+
+private:
+
+	static int32_t background_run(void* thiz)
+	{
+		return ((ResourceLoader*)thiz)->run();
+	}
+
 private:
 
+	OsThread				m_thread;
+
 	// Whether to look for resources
 	Bundle&					m_bundle;