2014-10-23 22:38:04 +08:00
|
|
|
|
/*
|
|
|
|
|
* cocos2d-x http://www.cocos2d-x.org
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2010-2014 - cocos2d-x community
|
|
|
|
|
*
|
|
|
|
|
* Portions Copyright (c) Microsoft Open Technologies, Inc.
|
|
|
|
|
* All Rights Reserved
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
|
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2014-10-24 10:28:53 +08:00
|
|
|
|
#include "App.xaml.h"
|
2014-10-23 22:38:04 +08:00
|
|
|
|
#include "OpenGLESPage.xaml.h"
|
|
|
|
|
|
|
|
|
|
using namespace cocos2d;
|
|
|
|
|
using namespace Platform;
|
|
|
|
|
using namespace Concurrency;
|
|
|
|
|
using namespace Windows::Foundation;
|
|
|
|
|
using namespace Windows::Foundation::Collections;
|
|
|
|
|
using namespace Windows::Graphics::Display;
|
|
|
|
|
using namespace Windows::System::Threading;
|
|
|
|
|
using namespace Windows::UI::Core;
|
|
|
|
|
using namespace Windows::UI::Input;
|
|
|
|
|
using namespace Windows::UI::Xaml;
|
|
|
|
|
using namespace Windows::UI::Xaml::Controls;
|
|
|
|
|
using namespace Windows::UI::Xaml::Controls::Primitives;
|
|
|
|
|
using namespace Windows::UI::Xaml::Data;
|
|
|
|
|
using namespace Windows::UI::Xaml::Input;
|
|
|
|
|
using namespace Windows::UI::Xaml::Media;
|
|
|
|
|
using namespace Windows::UI::Xaml::Navigation;
|
|
|
|
|
|
2015-04-18 06:47:52 +08:00
|
|
|
|
#if (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP)
|
|
|
|
|
using namespace Windows::Phone::UI::Input;
|
|
|
|
|
#endif
|
|
|
|
|
|
2014-10-23 22:38:04 +08:00
|
|
|
|
OpenGLESPage::OpenGLESPage() :
|
|
|
|
|
OpenGLESPage(nullptr)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OpenGLESPage::OpenGLESPage(OpenGLES* openGLES) :
|
|
|
|
|
mOpenGLES(openGLES),
|
|
|
|
|
mRenderSurface(EGL_NO_SURFACE),
|
|
|
|
|
mCustomRenderSurfaceSize(0,0),
|
|
|
|
|
mUseCustomRenderSurfaceSize(false),
|
2014-10-28 02:07:00 +08:00
|
|
|
|
m_coreInput(nullptr),
|
2014-11-22 08:24:51 +08:00
|
|
|
|
m_dpi(0.0f),
|
2015-01-20 01:38:49 +08:00
|
|
|
|
m_deviceLost(false),
|
|
|
|
|
m_orientation(DisplayOrientations::Landscape)
|
2014-10-23 22:38:04 +08:00
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
|
|
|
|
|
Windows::UI::Core::CoreWindow^ window = Windows::UI::Xaml::Window::Current->CoreWindow;
|
|
|
|
|
|
|
|
|
|
window->VisibilityChanged +=
|
|
|
|
|
ref new Windows::Foundation::TypedEventHandler<Windows::UI::Core::CoreWindow^, Windows::UI::Core::VisibilityChangedEventArgs^>(this, &OpenGLESPage::OnVisibilityChanged);
|
|
|
|
|
|
|
|
|
|
swapChainPanel->SizeChanged +=
|
|
|
|
|
ref new Windows::UI::Xaml::SizeChangedEventHandler(this, &OpenGLESPage::OnSwapChainPanelSizeChanged);
|
|
|
|
|
|
2015-01-20 01:38:49 +08:00
|
|
|
|
DisplayInformation^ currentDisplayInformation = DisplayInformation::GetForCurrentView();
|
|
|
|
|
|
|
|
|
|
currentDisplayInformation->OrientationChanged +=
|
|
|
|
|
ref new TypedEventHandler<DisplayInformation^, Object^>(this, &OpenGLESPage::OnOrientationChanged);
|
|
|
|
|
|
|
|
|
|
m_orientation = currentDisplayInformation->CurrentOrientation;
|
|
|
|
|
|
2014-10-23 22:38:04 +08:00
|
|
|
|
this->Loaded +=
|
|
|
|
|
ref new Windows::UI::Xaml::RoutedEventHandler(this, &OpenGLESPage::OnPageLoaded);
|
|
|
|
|
|
|
|
|
|
mSwapChainPanelSize = { swapChainPanel->RenderSize.Width, swapChainPanel->RenderSize.Height };
|
|
|
|
|
|
|
|
|
|
#if (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP)
|
|
|
|
|
Windows::UI::ViewManagement::StatusBar::GetForCurrentView()->HideAsync();
|
2015-04-18 06:47:52 +08:00
|
|
|
|
HardwareButtons::BackPressed += ref new EventHandler<BackPressedEventArgs^>(this, &OpenGLESPage::OnBackButtonPressed);
|
2014-10-23 22:38:04 +08:00
|
|
|
|
#else
|
|
|
|
|
// Disable all pointer visual feedback for better performance when touching.
|
|
|
|
|
// This is not supported on Windows Phone applications.
|
|
|
|
|
auto pointerVisualizationSettings = Windows::UI::Input::PointerVisualizationSettings::GetForCurrentView();
|
|
|
|
|
pointerVisualizationSettings->IsContactFeedbackEnabled = false;
|
|
|
|
|
pointerVisualizationSettings->IsBarrelButtonFeedbackEnabled = false;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// Register our SwapChainPanel to get independent input pointer events
|
|
|
|
|
auto workItemHandler = ref new WorkItemHandler([this](IAsyncAction ^)
|
|
|
|
|
{
|
|
|
|
|
// The CoreIndependentInputSource will raise pointer events for the specified device types on whichever thread it's created on.
|
|
|
|
|
m_coreInput = swapChainPanel->CreateCoreIndependentInputSource(
|
|
|
|
|
Windows::UI::Core::CoreInputDeviceTypes::Mouse |
|
|
|
|
|
Windows::UI::Core::CoreInputDeviceTypes::Touch |
|
|
|
|
|
Windows::UI::Core::CoreInputDeviceTypes::Pen
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Register for pointer events, which will be raised on the background thread.
|
|
|
|
|
m_coreInput->PointerPressed += ref new TypedEventHandler<Object^, PointerEventArgs^>(this, &OpenGLESPage::OnPointerPressed);
|
|
|
|
|
m_coreInput->PointerMoved += ref new TypedEventHandler<Object^, PointerEventArgs^>(this, &OpenGLESPage::OnPointerMoved);
|
|
|
|
|
m_coreInput->PointerReleased += ref new TypedEventHandler<Object^, PointerEventArgs^>(this, &OpenGLESPage::OnPointerReleased);
|
|
|
|
|
|
|
|
|
|
// Begin processing input messages as they're delivered.
|
|
|
|
|
m_coreInput->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessUntilQuit);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Run task on a dedicated high priority background thread.
|
|
|
|
|
m_inputLoopWorker = ThreadPool::RunAsync(workItemHandler, WorkItemPriority::High, WorkItemOptions::TimeSliced);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OpenGLESPage::~OpenGLESPage()
|
|
|
|
|
{
|
|
|
|
|
StopRenderLoop();
|
|
|
|
|
DestroyRenderSurface();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenGLESPage::OnPageLoaded(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
|
|
|
|
|
{
|
|
|
|
|
// The SwapChainPanel has been created and arranged in the page layout, so EGL can be initialized.
|
|
|
|
|
CreateRenderSurface();
|
|
|
|
|
StartRenderLoop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenGLESPage::OnPointerPressed(Object^ sender, PointerEventArgs^ e)
|
|
|
|
|
{
|
|
|
|
|
if (m_renderer)
|
|
|
|
|
{
|
|
|
|
|
m_renderer->QueuePointerEvent(PointerEventType::PointerPressed, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenGLESPage::OnPointerMoved(Object^ sender, PointerEventArgs^ e)
|
|
|
|
|
{
|
|
|
|
|
if (m_renderer)
|
|
|
|
|
{
|
|
|
|
|
m_renderer->QueuePointerEvent(PointerEventType::PointerMoved, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenGLESPage::OnPointerReleased(Object^ sender, PointerEventArgs^ e)
|
|
|
|
|
{
|
|
|
|
|
if (m_renderer)
|
|
|
|
|
{
|
|
|
|
|
m_renderer->QueuePointerEvent(PointerEventType::PointerReleased, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-20 01:38:49 +08:00
|
|
|
|
void OpenGLESPage::OnOrientationChanged(DisplayInformation^ sender, Object^ args)
|
|
|
|
|
{
|
|
|
|
|
critical_section::scoped_lock lock(mSwapChainPanelSizeCriticalSection);
|
|
|
|
|
m_orientation = sender->CurrentOrientation;
|
|
|
|
|
}
|
2014-10-23 22:38:04 +08:00
|
|
|
|
|
|
|
|
|
void OpenGLESPage::OnVisibilityChanged(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::VisibilityChangedEventArgs^ args)
|
|
|
|
|
{
|
|
|
|
|
if (args->Visible && mRenderSurface != EGL_NO_SURFACE)
|
|
|
|
|
{
|
|
|
|
|
StartRenderLoop();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
StopRenderLoop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-18 06:47:52 +08:00
|
|
|
|
#if (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP)
|
2015-04-21 09:36:58 +08:00
|
|
|
|
/*
|
|
|
|
|
We set args->Handled = true to prevent the app from quitting when the back button is pressed.
|
|
|
|
|
This is because this back button event happens on the XAML UI thread and not the cocos2d-x UI thread.
|
|
|
|
|
We need to give the game developer a chance to decide to exit the app depending on where they
|
|
|
|
|
are in their game. They can receive the back button event by listening for the
|
2015-04-21 21:21:32 +08:00
|
|
|
|
EventKeyboard::KeyCode::KEY_ESCAPE event.
|
2015-04-21 09:36:58 +08:00
|
|
|
|
|
2015-04-21 21:21:32 +08:00
|
|
|
|
The default behavior is to exit the app if the EventKeyboard::KeyCode::KEY_ESCAPE event
|
2015-04-21 09:36:58 +08:00
|
|
|
|
is not handled by the game.
|
|
|
|
|
*/
|
2015-04-18 06:47:52 +08:00
|
|
|
|
void OpenGLESPage::OnBackButtonPressed(Object^ sender, BackPressedEventArgs^ args)
|
|
|
|
|
{
|
2015-04-20 07:31:30 +08:00
|
|
|
|
if (m_renderer)
|
2015-04-18 06:47:52 +08:00
|
|
|
|
{
|
2015-04-20 07:31:30 +08:00
|
|
|
|
m_renderer->QueueBackButtonEvent();
|
2015-04-18 06:47:52 +08:00
|
|
|
|
args->Handled = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
2014-10-23 22:38:04 +08:00
|
|
|
|
void OpenGLESPage::OnSwapChainPanelSizeChanged(Object^ sender, Windows::UI::Xaml::SizeChangedEventArgs^ e)
|
|
|
|
|
{
|
|
|
|
|
// Size change events occur outside of the render thread. A lock is required when updating
|
|
|
|
|
// the swapchainpanel size
|
|
|
|
|
critical_section::scoped_lock lock(mSwapChainPanelSizeCriticalSection);
|
|
|
|
|
mSwapChainPanelSize = { e->NewSize.Width, e->NewSize.Height };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenGLESPage::GetSwapChainPanelSize(GLsizei* width, GLsizei* height)
|
|
|
|
|
{
|
|
|
|
|
critical_section::scoped_lock lock(mSwapChainPanelSizeCriticalSection);
|
|
|
|
|
// If a custom render surface size is specified, return its size instead of
|
|
|
|
|
// the swapchain panel size.
|
|
|
|
|
if (mUseCustomRenderSurfaceSize)
|
|
|
|
|
{
|
|
|
|
|
*width = static_cast<GLsizei>(mCustomRenderSurfaceSize.Width);
|
|
|
|
|
*height = static_cast<GLsizei>(mCustomRenderSurfaceSize.Height);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
*width = static_cast<GLsizei>(mSwapChainPanelSize.Width);
|
|
|
|
|
*height = static_cast<GLsizei>(mSwapChainPanelSize.Height);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenGLESPage::CreateRenderSurface()
|
|
|
|
|
{
|
|
|
|
|
if (mOpenGLES)
|
|
|
|
|
{
|
|
|
|
|
//
|
|
|
|
|
// A Custom render surface size can be specified by uncommenting the following lines.
|
|
|
|
|
// The render surface will be automatically scaled to fit the entire window. Using a
|
|
|
|
|
// smaller sized render surface can result in a performance gain.
|
|
|
|
|
//
|
|
|
|
|
//mCustomRenderSurfaceSize = Size(800, 600);
|
|
|
|
|
//mUseCustomRenderSurfaceSize = true;
|
|
|
|
|
|
|
|
|
|
mRenderSurface = mOpenGLES->CreateSurface(swapChainPanel, mUseCustomRenderSurfaceSize ? &mCustomRenderSurfaceSize : nullptr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenGLESPage::DestroyRenderSurface()
|
|
|
|
|
{
|
|
|
|
|
if (mOpenGLES)
|
|
|
|
|
{
|
|
|
|
|
mOpenGLES->DestroySurface(mRenderSurface);
|
|
|
|
|
}
|
|
|
|
|
mRenderSurface = EGL_NO_SURFACE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenGLESPage::RecoverFromLostDevice()
|
|
|
|
|
{
|
|
|
|
|
// Stop the render loop, reset OpenGLES, recreate the render surface
|
|
|
|
|
// and start the render loop again to recover from a lost device.
|
|
|
|
|
|
|
|
|
|
StopRenderLoop();
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
critical_section::scoped_lock lock(mRenderSurfaceCriticalSection);
|
|
|
|
|
DestroyRenderSurface();
|
|
|
|
|
mOpenGLES->Reset();
|
|
|
|
|
CreateRenderSurface();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StartRenderLoop();
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-21 01:43:34 +08:00
|
|
|
|
void OpenGLESPage::TerminateApp()
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
critical_section::scoped_lock lock(mRenderSurfaceCriticalSection);
|
2015-04-23 10:13:12 +08:00
|
|
|
|
|
|
|
|
|
if (mOpenGLES)
|
|
|
|
|
{
|
|
|
|
|
mOpenGLES->DestroySurface(mRenderSurface);
|
|
|
|
|
mOpenGLES->Cleanup();
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-21 01:43:34 +08:00
|
|
|
|
}
|
|
|
|
|
Windows::UI::Xaml::Application::Current->Exit();
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-23 22:38:04 +08:00
|
|
|
|
void OpenGLESPage::StartRenderLoop()
|
|
|
|
|
{
|
|
|
|
|
// If the render loop is already running then do not start another thread.
|
|
|
|
|
if (mRenderLoopWorker != nullptr && mRenderLoopWorker->Status == Windows::Foundation::AsyncStatus::Started)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-28 04:40:19 +08:00
|
|
|
|
DisplayInformation^ currentDisplayInformation = DisplayInformation::GetForCurrentView();
|
|
|
|
|
m_dpi = currentDisplayInformation->LogicalDpi;
|
|
|
|
|
|
2014-10-23 22:38:04 +08:00
|
|
|
|
auto dispatcher = Windows::UI::Xaml::Window::Current->CoreWindow->Dispatcher;
|
|
|
|
|
|
|
|
|
|
// Create a task for rendering that will be run on a background thread.
|
|
|
|
|
auto workItemHandler = ref new Windows::System::Threading::WorkItemHandler([this, dispatcher](Windows::Foundation::IAsyncAction ^ action)
|
|
|
|
|
{
|
|
|
|
|
critical_section::scoped_lock lock(mRenderSurfaceCriticalSection);
|
|
|
|
|
|
|
|
|
|
mOpenGLES->MakeCurrent(mRenderSurface);
|
|
|
|
|
|
|
|
|
|
GLsizei panelWidth = 0;
|
|
|
|
|
GLsizei panelHeight = 0;
|
|
|
|
|
GetSwapChainPanelSize(&panelWidth, &panelHeight);
|
2014-10-28 02:07:00 +08:00
|
|
|
|
|
2014-10-23 22:38:04 +08:00
|
|
|
|
if (m_renderer.get() == nullptr)
|
|
|
|
|
{
|
2015-01-20 01:38:49 +08:00
|
|
|
|
m_renderer = std::make_shared<Cocos2dRenderer>(panelWidth, panelHeight, m_dpi, m_orientation, dispatcher, swapChainPanel);
|
2014-10-23 22:38:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
2014-11-22 08:24:51 +08:00
|
|
|
|
if (m_deviceLost)
|
|
|
|
|
{
|
|
|
|
|
m_deviceLost = false;
|
|
|
|
|
m_renderer->DeviceLost();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
m_renderer->Resume();
|
|
|
|
|
}
|
2014-11-22 02:31:57 +08:00
|
|
|
|
|
|
|
|
|
|
2014-11-22 08:24:51 +08:00
|
|
|
|
while (action->Status == Windows::Foundation::AsyncStatus::Started && !m_deviceLost)
|
2014-10-23 22:38:04 +08:00
|
|
|
|
{
|
|
|
|
|
GetSwapChainPanelSize(&panelWidth, &panelHeight);
|
2015-01-20 01:38:49 +08:00
|
|
|
|
m_renderer.get()->Draw(panelWidth, panelHeight, m_dpi, m_orientation);
|
2014-10-23 22:38:04 +08:00
|
|
|
|
|
2015-04-23 10:13:12 +08:00
|
|
|
|
// run on main UI thread
|
|
|
|
|
if (m_renderer->AppShouldExit())
|
2014-10-23 22:38:04 +08:00
|
|
|
|
{
|
2015-04-23 10:13:12 +08:00
|
|
|
|
swapChainPanel->Dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new DispatchedHandler([this]()
|
|
|
|
|
{
|
|
|
|
|
TerminateApp();
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else if (mOpenGLES->SwapBuffers(mRenderSurface) != GL_TRUE)
|
|
|
|
|
{
|
|
|
|
|
// The call to eglSwapBuffers might not be successful (i.e. due to Device Lost)
|
|
|
|
|
// If the call fails, then we must reinitialize EGL and the GL resources.
|
|
|
|
|
|
2014-11-22 08:24:51 +08:00
|
|
|
|
m_deviceLost = true;
|
|
|
|
|
|
2015-04-18 00:31:02 +08:00
|
|
|
|
if (m_renderer)
|
|
|
|
|
{
|
|
|
|
|
m_renderer->Pause();
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-23 22:38:04 +08:00
|
|
|
|
// 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([=]()
|
|
|
|
|
{
|
|
|
|
|
RecoverFromLostDevice();
|
|
|
|
|
}, CallbackContext::Any));
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-04-18 00:31:02 +08:00
|
|
|
|
|
|
|
|
|
if (m_renderer)
|
|
|
|
|
{
|
|
|
|
|
m_renderer->Pause();
|
|
|
|
|
}
|
2014-10-23 22:38:04 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Run task on a dedicated high priority background thread.
|
|
|
|
|
mRenderLoopWorker = Windows::System::Threading::ThreadPool::RunAsync(workItemHandler, Windows::System::Threading::WorkItemPriority::High, Windows::System::Threading::WorkItemOptions::TimeSliced);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenGLESPage::StopRenderLoop()
|
|
|
|
|
{
|
|
|
|
|
if (mRenderLoopWorker)
|
|
|
|
|
{
|
|
|
|
|
mRenderLoopWorker->Cancel();
|
|
|
|
|
mRenderLoopWorker = nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|