about summary refs log tree commit diff
path: root/src/engine/client
diff options
context:
space:
mode:
authorMagnus Auvinen <magnus.auvinen@gmail.com>2011-12-30 16:02:22 +0100
committerMagnus Auvinen <magnus.auvinen@gmail.com>2011-12-30 16:02:22 +0100
commit8ffe5826156d07b1feb7fc58bf59a1431e01160f (patch)
treea2a0a536538e725b68741b516e80f3e0a73999c5 /src/engine/client
parent6e20c32859ee4518f398a12b65586463ddeecaab (diff)
downloadzcatch-8ffe5826156d07b1feb7fc58bf59a1431e01160f.tar.gz
zcatch-8ffe5826156d07b1feb7fc58bf59a1431e01160f.zip
ugly incomplete hack to put the rendering into another thread so we don't have to wait for the flip
Diffstat (limited to 'src/engine/client')
-rw-r--r--src/engine/client/client.cpp98
-rw-r--r--src/engine/client/client.h6
2 files changed, 96 insertions, 8 deletions
diff --git a/src/engine/client/client.cpp b/src/engine/client/client.cpp
index 9f43b9ce..4107cd85 100644
--- a/src/engine/client/client.cpp
+++ b/src/engine/client/client.cpp
@@ -7,6 +7,7 @@
 
 #include <base/math.h>
 #include <base/system.h>
+#include <base/tl/threading.h>
 
 #include <engine/client.h>
 #include <engine/config.h>
@@ -1688,17 +1689,27 @@ void CClient::InitInterfaces()
 	m_Friends.Init();
 }
 
-void CClient::Run()
-{
-	int64 ReportTime = time_get();
-	int64 ReportInterval = time_freq()*1;
 
-	m_LocalStartTime = time_get();
-	m_SnapshotParts = 0;
+enum
+{
+	GFXSTATE_ERROR = -1,
+	GFXSTATE_INIT = 0,
+	GFXSTATE_IDLE,
+	GFXSTATE_RENDERING,
+	GFXSTATE_SWAPPING,
+};
 
+void CClient::GraphicsThread()
+{
 	// init graphics
 	if(m_pGraphics->Init() != 0)
+	{
+		m_GfxState = GFXSTATE_ERROR;
+		m_GfxStateSemaphore.signal();
+		m_GfxState = GFXSTATE_ERROR;
 		return;
+	}
+
 
 	// open socket
 	{
@@ -1708,6 +1719,7 @@ void CClient::Run()
 		if(!m_NetClient.Open(BindAddr, 0))
 		{
 			dbg_msg("client", "couldn't start network");
+			m_GfxState = GFXSTATE_ERROR;
 			return;
 		}
 	}
@@ -1722,16 +1734,66 @@ void CClient::Run()
 	MasterServer()->RefreshAddresses(m_NetClient.NetType());
 
 	// init the editor
-	m_pEditor->Init();
+	//m_pEditor->Init();
 
 	// init sound, allowed to fail
 	m_SoundInitFailed = Sound()->Init() != 0;
 
 	// load data
 	if(!LoadData())
+	{
+		m_GfxState = GFXSTATE_ERROR;
 		return;
+	}
 
 	GameClient()->OnInit();
+
+
+	while(1)
+	{
+		// do idle
+		sync_barrier();
+		m_GfxState = GFXSTATE_IDLE;
+		m_GfxStateSemaphore.signal();
+		m_GfxRenderSemaphore.wait();
+		
+		// do render
+		m_GfxState = GFXSTATE_RENDERING;
+		m_GfxStateSemaphore.signal();
+		Render();
+		sync_barrier();
+
+		// do swap
+		m_GfxState = GFXSTATE_SWAPPING;
+		m_GfxStateSemaphore.signal();
+		m_pGraphics->Swap();
+	}
+
+	// do shutdown
+}
+
+void CClient::Run()
+{
+	int64 ReportTime = time_get();
+	int64 ReportInterval = time_freq()*1;
+
+	m_LocalStartTime = time_get();
+	m_SnapshotParts = 0;
+
+	m_GfxState = GFXSTATE_INIT;
+	thread_create(GraphicsThreadProxy, this);
+
+	// wait for gfx to init
+	while(1)
+	{
+		m_GfxStateSemaphore.wait();
+		if(m_GfxState == GFXSTATE_ERROR)
+			return;
+		if(m_GfxState != GFXSTATE_INIT)
+			break;
+	}
+
+
 	char aBuf[256];
 	str_format(aBuf, sizeof(aBuf), "version %s", GameClient()->NetVersion());
 	m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, "client", aBuf);
@@ -1847,7 +1909,27 @@ void CClient::Run()
 				m_EditorActive = false;
 
 			Update();
+			
+
+			if(m_GfxState == GFXSTATE_IDLE)
+			{
+				// issue new rendering
+				m_GfxRenderSemaphore.signal();
+
+				// wait for gfx to finish rendering
+				while(m_GfxState != GFXSTATE_SWAPPING)
+					m_GfxStateSemaphore.wait();
 
+			}
+
+			/*
+			if(m_pGraphics->AsyncSwapIsDone())
+			{
+				m_pGraphics->BeginScene();
+				Render();
+				m_pGraphics->EndScene();
+			}*/
+			/*
 			if(g_Config.m_DbgStress)
 			{
 				if((m_Frames%10) == 0)
@@ -1860,7 +1942,7 @@ void CClient::Run()
 			{
 				Render();
 				m_pGraphics->Swap();
-			}
+			}*/
 		}
 
 		AutoScreenshot_Cleanup();
diff --git a/src/engine/client/client.h b/src/engine/client/client.h
index 1504a4e4..83553eb4 100644
--- a/src/engine/client/client.h
+++ b/src/engine/client/client.h
@@ -172,6 +172,12 @@ class CClient : public IClient, public CDemoPlayer::IListner
 		class CHostLookup m_VersionServeraddr;
 	} m_VersionInfo;
 
+	semaphore m_GfxRenderSemaphore;
+	semaphore m_GfxStateSemaphore;
+	volatile int m_GfxState;
+	static void GraphicsThreadProxy(void *pThis) { ((CClient*)pThis)->GraphicsThread(); }
+	void GraphicsThread();
+
 public:
 	IEngine *Engine() { return m_pEngine; }
 	IEngineGraphics *Graphics() { return m_pGraphics; }