2023-03-11 22:10:18 +08:00
|
|
|
/*
|
2023-03-12 01:44:55 +08:00
|
|
|
* cocos2d-x https://axmolengine.github.io/
|
2023-03-11 22:10:18 +08:00
|
|
|
*
|
|
|
|
* Copyright (c) 2010-2014 - cocos2d-x community
|
|
|
|
* Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "AxmolRenderer.h"
|
|
|
|
#include "AppDelegate.h"
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "platform/winrt/GLViewImpl-winrt.h"
|
|
|
|
#include "platform/Application.h"
|
|
|
|
#include "renderer/TextureCache.h"
|
2023-03-11 22:10:18 +08:00
|
|
|
|
|
|
|
// These are used by the shader compilation methods.
|
|
|
|
#include <vector>
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
using namespace Windows::UI::Core;
|
|
|
|
using namespace Windows::UI::Xaml::Controls;
|
|
|
|
using namespace Windows::Graphics::Display;
|
|
|
|
using namespace ax;
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
std::unique_ptr<AppDelegate> appDelegate;
|
|
|
|
}
|
|
|
|
|
|
|
|
AxmolRenderer::AxmolRenderer(int width,
|
|
|
|
int height,
|
|
|
|
float dpi,
|
|
|
|
DisplayOrientations orientation,
|
2023-09-26 00:11:33 +08:00
|
|
|
CoreDispatcher const& dispatcher,
|
|
|
|
Panel const& panel)
|
2023-03-11 22:10:18 +08:00
|
|
|
: m_width(width), m_height(height), m_dpi(dpi), m_dispatcher(dispatcher), m_panel(panel), m_orientation(orientation)
|
|
|
|
{
|
|
|
|
appDelegate.reset(new AppDelegate());
|
|
|
|
}
|
|
|
|
|
|
|
|
AxmolRenderer::~AxmolRenderer() {}
|
|
|
|
|
|
|
|
void AxmolRenderer::Resume()
|
|
|
|
{
|
|
|
|
auto director = ax::Director::getInstance();
|
2023-12-01 09:04:51 +08:00
|
|
|
auto glview = director->getGLView();
|
2023-03-11 22:10:18 +08:00
|
|
|
|
|
|
|
if (!glview)
|
|
|
|
{
|
2023-03-11 22:53:55 +08:00
|
|
|
GLViewImpl* glview = GLViewImpl::createWithRect(
|
2023-12-13 01:56:53 +08:00
|
|
|
"axmol2", ax::Rect{0, 0, static_cast<float>(m_width), static_cast<float>(m_height)});
|
2023-03-11 22:53:55 +08:00
|
|
|
glview->UpdateOrientation(m_orientation);
|
|
|
|
glview->SetDPI(m_dpi);
|
2023-09-26 00:11:33 +08:00
|
|
|
glview->setDispatcher(m_dispatcher);
|
|
|
|
glview->setPanel(m_panel);
|
2023-12-01 09:04:51 +08:00
|
|
|
director->setGLView(glview);
|
2023-03-11 22:10:18 +08:00
|
|
|
Application::getInstance()->run();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Application::getInstance()->applicationWillEnterForeground();
|
|
|
|
ax::EventCustom foregroundEvent(EVENT_COME_TO_FOREGROUND);
|
|
|
|
ax::Director::getInstance()->getEventDispatcher()->dispatchEvent(&foregroundEvent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AxmolRenderer::Pause()
|
|
|
|
{
|
2023-12-01 09:04:51 +08:00
|
|
|
if (Director::getInstance()->getGLView())
|
2023-03-11 22:10:18 +08:00
|
|
|
{
|
|
|
|
Application::getInstance()->applicationDidEnterBackground();
|
|
|
|
ax::EventCustom backgroundEvent(EVENT_COME_TO_BACKGROUND);
|
|
|
|
ax::Director::getInstance()->getEventDispatcher()->dispatchEvent(&backgroundEvent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AxmolRenderer::AppShouldExit()
|
|
|
|
{
|
2023-12-01 09:04:51 +08:00
|
|
|
return GLViewImpl::sharedGLView()->AppShouldExit();
|
2023-03-11 22:10:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AxmolRenderer::DeviceLost()
|
|
|
|
{
|
|
|
|
Pause();
|
|
|
|
|
|
|
|
auto director = ax::Director::getInstance();
|
2023-12-01 09:04:51 +08:00
|
|
|
if (director->getGLView())
|
2023-03-11 22:10:18 +08:00
|
|
|
{
|
|
|
|
// TODO:
|
|
|
|
// ax::GL::invalidateStateCache();
|
|
|
|
// ax::GLProgramCache::getInstance()->reloadDefaultGLPrograms();
|
|
|
|
// ax::DrawPrimitives::init();
|
|
|
|
// ax::VolatileTextureMgr::reloadAllTextures();
|
|
|
|
|
|
|
|
ax::EventCustom recreatedEvent(EVENT_RENDERER_RECREATED);
|
|
|
|
director->getEventDispatcher()->dispatchEvent(&recreatedEvent);
|
|
|
|
director->setGLDefaultValues();
|
|
|
|
|
|
|
|
Application::getInstance()->applicationWillEnterForeground();
|
|
|
|
ax::EventCustom foregroundEvent(EVENT_COME_TO_FOREGROUND);
|
|
|
|
ax::Director::getInstance()->getEventDispatcher()->dispatchEvent(&foregroundEvent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-31 20:42:39 +08:00
|
|
|
void AxmolRenderer::SetQueueOperationCb(std::function<void(AsyncOperation, void*)> cb)
|
|
|
|
{
|
2023-12-01 09:04:51 +08:00
|
|
|
GLViewImpl::sharedGLView()->SetQueueOperationCb(std::move(cb));
|
2023-05-31 20:42:39 +08:00
|
|
|
}
|
|
|
|
|
2023-03-11 22:10:18 +08:00
|
|
|
void AxmolRenderer::Draw(size_t width, size_t height, float dpi, DisplayOrientations orientation)
|
|
|
|
{
|
2023-12-01 09:04:51 +08:00
|
|
|
auto glView = GLViewImpl::sharedGLView();
|
2023-03-11 22:10:18 +08:00
|
|
|
|
|
|
|
if (orientation != m_orientation)
|
|
|
|
{
|
|
|
|
m_orientation = orientation;
|
|
|
|
glView->UpdateOrientation(orientation);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (width != m_width || height != m_height)
|
|
|
|
{
|
|
|
|
m_width = width;
|
|
|
|
m_height = height;
|
|
|
|
glView->UpdateForWindowSizeChange(static_cast<float>(width), static_cast<float>(height));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dpi != m_dpi)
|
|
|
|
{
|
|
|
|
m_dpi = dpi;
|
|
|
|
glView->SetDPI(m_dpi);
|
|
|
|
}
|
|
|
|
|
|
|
|
glView->ProcessEvents();
|
|
|
|
glView->Render();
|
|
|
|
}
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
void AxmolRenderer::QueuePointerEvent(ax::PointerEventType type, Windows::UI::Core::PointerEventArgs const& args)
|
2023-03-11 22:10:18 +08:00
|
|
|
{
|
2023-12-01 09:04:51 +08:00
|
|
|
GLViewImpl::sharedGLView()->QueuePointerEvent(type, args);
|
2023-03-11 22:10:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void AxmolRenderer::QueueBackButtonEvent()
|
|
|
|
{
|
2023-12-01 09:04:51 +08:00
|
|
|
GLViewImpl::sharedGLView()->QueueBackKeyPress();
|
2023-03-11 22:10:18 +08:00
|
|
|
}
|
|
|
|
|
2023-09-26 00:11:33 +08:00
|
|
|
void AxmolRenderer::QueueKeyboardEvent(WinRTKeyboardEventType type, Windows::UI::Core::KeyEventArgs const& args)
|
2023-03-11 22:10:18 +08:00
|
|
|
{
|
2023-12-01 09:04:51 +08:00
|
|
|
GLViewImpl::sharedGLView()->QueueWinRTKeyboardEvent(type, args);
|
2023-03-11 22:10:18 +08:00
|
|
|
}
|