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.
|
|
|
|
*/
|
|
|
|
|
2016-03-24 03:14:12 +08:00
|
|
|
#include "Cocos2dRenderer.h"
|
2014-10-23 22:38:04 +08:00
|
|
|
#include "AppDelegate.h"
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "platform/winrt/CCGLViewImpl-winrt.h"
|
2016-03-22 16:41:40 +08:00
|
|
|
#include "platform/CCApplication.h"
|
2014-10-23 22:38:04 +08:00
|
|
|
#include "cocos2d.h"
|
2014-11-22 02:31:57 +08:00
|
|
|
#include "renderer/CCTextureCache.h"
|
2014-10-23 22:38:04 +08:00
|
|
|
|
|
|
|
// These are used by the shader compilation methods.
|
|
|
|
#include <vector>
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
using namespace Platform;
|
|
|
|
using namespace Windows::UI::Core;
|
|
|
|
using namespace Windows::UI::Xaml::Controls;
|
|
|
|
using namespace Windows::Graphics::Display;
|
2015-08-19 02:06:47 +08:00
|
|
|
using namespace cocos2d;
|
2014-10-23 22:38:04 +08:00
|
|
|
|
2015-01-20 01:38:49 +08:00
|
|
|
Cocos2dRenderer::Cocos2dRenderer(int width, int height, float dpi, DisplayOrientations orientation, CoreDispatcher^ dispatcher, Panel^ panel)
|
2014-10-28 02:07:00 +08:00
|
|
|
: m_app(nullptr)
|
|
|
|
, m_width(width)
|
|
|
|
, m_height(height)
|
|
|
|
, m_dpi(dpi)
|
2014-10-23 22:38:04 +08:00
|
|
|
, m_dispatcher(dispatcher)
|
|
|
|
, m_panel(panel)
|
2015-01-20 01:38:49 +08:00
|
|
|
, m_orientation(orientation)
|
2014-10-23 22:38:04 +08:00
|
|
|
{
|
2014-10-28 02:07:00 +08:00
|
|
|
m_app = new AppDelegate();
|
2014-10-23 22:38:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Cocos2dRenderer::~Cocos2dRenderer()
|
|
|
|
{
|
2014-10-28 02:07:00 +08:00
|
|
|
delete m_app;
|
2014-10-23 22:38:04 +08:00
|
|
|
}
|
|
|
|
|
2014-11-22 02:31:57 +08:00
|
|
|
void Cocos2dRenderer::Resume()
|
|
|
|
{
|
|
|
|
auto director = cocos2d::Director::getInstance();
|
|
|
|
auto glview = director->getOpenGLView();
|
|
|
|
|
2016-03-22 16:41:40 +08:00
|
|
|
if (!glview)
|
2014-11-22 02:31:57 +08:00
|
|
|
{
|
|
|
|
GLViewImpl* glview = GLViewImpl::create("Test Cpp");
|
|
|
|
glview->setDispatcher(m_dispatcher.Get());
|
|
|
|
glview->setPanel(m_panel.Get());
|
2015-01-20 01:38:49 +08:00
|
|
|
glview->Create(static_cast<float>(m_width), static_cast<float>(m_height), m_dpi, m_orientation);
|
2014-11-22 02:31:57 +08:00
|
|
|
director->setOpenGLView(glview);
|
|
|
|
CCApplication::getInstance()->run();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Application::getInstance()->applicationWillEnterForeground();
|
2015-04-03 00:07:44 +08:00
|
|
|
cocos2d::EventCustom foregroundEvent(EVENT_COME_TO_FOREGROUND);
|
|
|
|
cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&foregroundEvent);
|
2014-11-22 02:31:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Cocos2dRenderer::Pause()
|
|
|
|
{
|
|
|
|
if (Director::getInstance()->getOpenGLView()) {
|
|
|
|
Application::getInstance()->applicationDidEnterBackground();
|
2015-04-03 00:07:44 +08:00
|
|
|
cocos2d::EventCustom backgroundEvent(EVENT_COME_TO_BACKGROUND);
|
|
|
|
cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&backgroundEvent);
|
2014-11-22 02:31:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-21 01:43:34 +08:00
|
|
|
bool Cocos2dRenderer::AppShouldExit()
|
|
|
|
{
|
|
|
|
return GLViewImpl::sharedOpenGLView()->AppShouldExit();
|
|
|
|
}
|
|
|
|
|
2014-11-22 02:31:57 +08:00
|
|
|
void Cocos2dRenderer::DeviceLost()
|
|
|
|
{
|
2014-11-22 08:24:51 +08:00
|
|
|
Pause();
|
|
|
|
|
2014-11-22 02:31:57 +08:00
|
|
|
auto director = cocos2d::Director::getInstance();
|
|
|
|
if (director->getOpenGLView()) {
|
|
|
|
cocos2d::GL::invalidateStateCache();
|
|
|
|
cocos2d::GLProgramCache::getInstance()->reloadDefaultGLPrograms();
|
|
|
|
cocos2d::DrawPrimitives::init();
|
|
|
|
cocos2d::VolatileTextureMgr::reloadAllTextures();
|
|
|
|
|
|
|
|
cocos2d::EventCustom recreatedEvent(EVENT_RENDERER_RECREATED);
|
|
|
|
director->getEventDispatcher()->dispatchEvent(&recreatedEvent);
|
|
|
|
director->setGLDefaultValues();
|
|
|
|
|
|
|
|
Application::getInstance()->applicationWillEnterForeground();
|
|
|
|
cocos2d::EventCustom foregroundEvent(EVENT_COME_TO_FOREGROUND);
|
|
|
|
cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&foregroundEvent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-20 01:38:49 +08:00
|
|
|
void Cocos2dRenderer::Draw(GLsizei width, GLsizei height, float dpi, DisplayOrientations orientation)
|
2014-10-23 22:38:04 +08:00
|
|
|
{
|
2015-04-23 10:11:29 +08:00
|
|
|
auto glView = GLViewImpl::sharedOpenGLView();
|
|
|
|
|
2015-01-20 01:38:49 +08:00
|
|
|
if (orientation != m_orientation)
|
|
|
|
{
|
|
|
|
m_orientation = orientation;
|
2015-04-23 10:11:29 +08:00
|
|
|
glView->UpdateOrientation(orientation);
|
2015-01-20 01:38:49 +08:00
|
|
|
}
|
|
|
|
|
2014-10-28 02:07:00 +08:00
|
|
|
if (width != m_width || height != m_height)
|
2014-10-23 22:38:04 +08:00
|
|
|
{
|
2014-10-28 02:07:00 +08:00
|
|
|
m_width = width;
|
|
|
|
m_height = height;
|
2015-04-23 10:11:29 +08:00
|
|
|
glView->UpdateForWindowSizeChange(static_cast<float>(width), static_cast<float>(height));
|
2014-10-23 22:38:04 +08:00
|
|
|
}
|
|
|
|
|
2014-10-28 02:07:00 +08:00
|
|
|
if (dpi != m_dpi)
|
|
|
|
{
|
|
|
|
m_dpi = dpi;
|
2015-04-23 10:11:29 +08:00
|
|
|
glView->SetDPI(m_dpi);
|
2014-10-28 02:07:00 +08:00
|
|
|
}
|
|
|
|
|
2015-04-23 10:11:29 +08:00
|
|
|
glView->ProcessEvents();
|
2015-04-23 10:11:42 +08:00
|
|
|
glView->Render();
|
2014-10-23 22:38:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Cocos2dRenderer::QueuePointerEvent(cocos2d::PointerEventType type, Windows::UI::Core::PointerEventArgs^ args)
|
|
|
|
{
|
|
|
|
GLViewImpl::sharedOpenGLView()->QueuePointerEvent(type, args);
|
|
|
|
}
|
|
|
|
|
2015-04-20 07:31:30 +08:00
|
|
|
void Cocos2dRenderer::QueueBackButtonEvent()
|
|
|
|
{
|
|
|
|
GLViewImpl::sharedOpenGLView()->QueueBackKeyPress();
|
|
|
|
}
|
|
|
|
|
2015-05-13 00:20:10 +08:00
|
|
|
void Cocos2dRenderer::QueueKeyboardEvent(WinRTKeyboardEventType type, Windows::UI::Core::KeyEventArgs^ args)
|
|
|
|
{
|
2015-08-19 02:06:47 +08:00
|
|
|
GLViewImpl::sharedOpenGLView()->QueueWinRTKeyboardEvent(type, args);
|
2015-05-13 00:20:10 +08:00
|
|
|
}
|