implemented lostDevice support when app loses the directx device

This commit is contained in:
Dale Stammen 2014-11-21 16:24:51 -08:00
parent 32fe5d07ae
commit a607d41454
6 changed files with 36 additions and 11 deletions

View File

@ -83,6 +83,8 @@ void Cocos2dRenderer::Pause()
void Cocos2dRenderer::DeviceLost() void Cocos2dRenderer::DeviceLost()
{ {
Pause();
auto director = cocos2d::Director::getInstance(); auto director = cocos2d::Director::getInstance();
if (director->getOpenGLView()) { if (director->getOpenGLView()) {
cocos2d::GL::invalidateStateCache(); cocos2d::GL::invalidateStateCache();

View File

@ -48,7 +48,8 @@ OpenGLESPage::OpenGLESPage(OpenGLES* openGLES) :
mCustomRenderSurfaceSize(0,0), mCustomRenderSurfaceSize(0,0),
mUseCustomRenderSurfaceSize(false), mUseCustomRenderSurfaceSize(false),
m_coreInput(nullptr), m_coreInput(nullptr),
m_dpi(0.0f) m_dpi(0.0f),
m_deviceLost(false)
{ {
InitializeComponent(); InitializeComponent();
@ -209,7 +210,6 @@ void OpenGLESPage::RecoverFromLostDevice()
{ {
critical_section::scoped_lock lock(mRenderSurfaceCriticalSection); critical_section::scoped_lock lock(mRenderSurfaceCriticalSection);
DestroyRenderSurface(); DestroyRenderSurface();
mOpenGLES->Reset(); mOpenGLES->Reset();
CreateRenderSurface(); CreateRenderSurface();
@ -249,12 +249,19 @@ void OpenGLESPage::StartRenderLoop()
m_renderer = std::make_shared<Cocos2dRenderer>(panelWidth, panelHeight, m_dpi, dispatcher, swapChainPanel); m_renderer = std::make_shared<Cocos2dRenderer>(panelWidth, panelHeight, m_dpi, dispatcher, swapChainPanel);
} }
m_renderer->Resume(); if (m_deviceLost)
while (action->Status == Windows::Foundation::AsyncStatus::Started)
{ {
m_deviceLost = false;
m_renderer->DeviceLost();
}
else
{
m_renderer->Resume();
}
while (action->Status == Windows::Foundation::AsyncStatus::Started && !m_deviceLost)
{
GetSwapChainPanelSize(&panelWidth, &panelHeight); GetSwapChainPanelSize(&panelWidth, &panelHeight);
m_renderer.get()->Draw(panelWidth, panelHeight, m_dpi); m_renderer.get()->Draw(panelWidth, panelHeight, m_dpi);
@ -262,9 +269,10 @@ void OpenGLESPage::StartRenderLoop()
// If the call fails, then we must reinitialize EGL and the GL resources. // If the call fails, then we must reinitialize EGL and the GL resources.
if (mOpenGLES->SwapBuffers(mRenderSurface) != GL_TRUE) if (mOpenGLES->SwapBuffers(mRenderSurface) != GL_TRUE)
{ {
m_deviceLost = true;
// XAML objects like the SwapChainPanel must only be manipulated on the UI thread. // XAML objects like the SwapChainPanel must only be manipulated on the UI thread.
swapChainPanel->Dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::High, ref new Windows::UI::Core::DispatchedHandler([=]() swapChainPanel->Dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::High, ref new Windows::UI::Core::DispatchedHandler([=]()
//swapChainPanel->Dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::High, ref new Windows::UI::Core::DispatchedHandler([=]()
{ {
RecoverFromLostDevice(); RecoverFromLostDevice();
}, CallbackContext::Any)); }, CallbackContext::Any));

View File

@ -69,5 +69,7 @@ namespace cocos2d
void OnPointerReleased(Platform::Object^ sender, Windows::UI::Core::PointerEventArgs^ e); void OnPointerReleased(Platform::Object^ sender, Windows::UI::Core::PointerEventArgs^ e);
float m_dpi; float m_dpi;
bool m_deviceLost;
}; };
} }

View File

@ -83,6 +83,8 @@ void Cocos2dRenderer::Pause()
void Cocos2dRenderer::DeviceLost() void Cocos2dRenderer::DeviceLost()
{ {
Pause();
auto director = cocos2d::Director::getInstance(); auto director = cocos2d::Director::getInstance();
if (director->getOpenGLView()) { if (director->getOpenGLView()) {
cocos2d::GL::invalidateStateCache(); cocos2d::GL::invalidateStateCache();

View File

@ -48,7 +48,8 @@ OpenGLESPage::OpenGLESPage(OpenGLES* openGLES) :
mCustomRenderSurfaceSize(0,0), mCustomRenderSurfaceSize(0,0),
mUseCustomRenderSurfaceSize(false), mUseCustomRenderSurfaceSize(false),
m_coreInput(nullptr), m_coreInput(nullptr),
m_dpi(0.0f) m_dpi(0.0f),
m_deviceLost(false)
{ {
InitializeComponent(); InitializeComponent();
@ -209,7 +210,7 @@ void OpenGLESPage::RecoverFromLostDevice()
{ {
critical_section::scoped_lock lock(mRenderSurfaceCriticalSection); critical_section::scoped_lock lock(mRenderSurfaceCriticalSection);
m_deviceLost = true;
DestroyRenderSurface(); DestroyRenderSurface();
mOpenGLES->Reset(); mOpenGLES->Reset();
CreateRenderSurface(); CreateRenderSurface();
@ -249,7 +250,15 @@ void OpenGLESPage::StartRenderLoop()
m_renderer = std::make_shared<Cocos2dRenderer>(panelWidth, panelHeight, m_dpi, dispatcher, swapChainPanel); m_renderer = std::make_shared<Cocos2dRenderer>(panelWidth, panelHeight, m_dpi, dispatcher, swapChainPanel);
} }
if (m_deviceLost)
{
m_deviceLost = false;
m_renderer->DeviceLost();
}
else
{
m_renderer->Resume(); m_renderer->Resume();
}
while (action->Status == Windows::Foundation::AsyncStatus::Started) while (action->Status == Windows::Foundation::AsyncStatus::Started)

View File

@ -69,5 +69,7 @@ namespace cocos2d
void OnPointerReleased(Platform::Object^ sender, Windows::UI::Core::PointerEventArgs^ e); void OnPointerReleased(Platform::Object^ sender, Windows::UI::Core::PointerEventArgs^ e);
float m_dpi; float m_dpi;
bool m_deviceLost;
}; };
} }