Cocos2dRenderer.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * cocos2d-x http://www.cocos2d-x.org
  3. *
  4. * Copyright (c) 2010-2014 - cocos2d-x community
  5. *
  6. * Portions Copyright (c) Microsoft Open Technologies, Inc.
  7. * All Rights Reserved
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and limitations under the License.
  17. */
  18. #include "Cocos2dRenderer.h"
  19. #include "AppDelegate.h"
  20. #include "platform/winrt/CCGLViewImpl-winrt.h"
  21. #include "platform/CCApplication.h"
  22. #include "renderer/CCTextureCache.h"
  23. // These are used by the shader compilation methods.
  24. #include <vector>
  25. #include <iostream>
  26. #include <fstream>
  27. using namespace Platform;
  28. using namespace Windows::UI::Core;
  29. using namespace Windows::UI::Xaml::Controls;
  30. using namespace Windows::Graphics::Display;
  31. using namespace cocos2d;
  32. Cocos2dRenderer::Cocos2dRenderer(int width, int height, float dpi, DisplayOrientations orientation, CoreDispatcher^ dispatcher, Panel^ panel)
  33. : m_app(nullptr)
  34. , m_width(width)
  35. , m_height(height)
  36. , m_dpi(dpi)
  37. , m_dispatcher(dispatcher)
  38. , m_panel(panel)
  39. , m_orientation(orientation)
  40. {
  41. m_app = new AppDelegate();
  42. }
  43. Cocos2dRenderer::~Cocos2dRenderer()
  44. {
  45. delete m_app;
  46. }
  47. void Cocos2dRenderer::Resume()
  48. {
  49. auto director = cocos2d::Director::getInstance();
  50. auto glview = director->getOpenGLView();
  51. if (!glview)
  52. {
  53. GLViewImpl* glview = GLViewImpl::create("Test Cpp");
  54. glview->setDispatcher(m_dispatcher.Get());
  55. glview->setPanel(m_panel.Get());
  56. glview->Create(static_cast<float>(m_width), static_cast<float>(m_height), m_dpi, m_orientation);
  57. director->setOpenGLView(glview);
  58. CCApplication::getInstance()->run();
  59. }
  60. else
  61. {
  62. Application::getInstance()->applicationWillEnterForeground();
  63. cocos2d::EventCustom foregroundEvent(EVENT_COME_TO_FOREGROUND);
  64. cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&foregroundEvent);
  65. }
  66. }
  67. void Cocos2dRenderer::Pause()
  68. {
  69. if (Director::getInstance()->getOpenGLView()) {
  70. Application::getInstance()->applicationDidEnterBackground();
  71. cocos2d::EventCustom backgroundEvent(EVENT_COME_TO_BACKGROUND);
  72. cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&backgroundEvent);
  73. }
  74. }
  75. bool Cocos2dRenderer::AppShouldExit()
  76. {
  77. return GLViewImpl::sharedOpenGLView()->AppShouldExit();
  78. }
  79. void Cocos2dRenderer::DeviceLost()
  80. {
  81. Pause();
  82. auto director = cocos2d::Director::getInstance();
  83. if (director->getOpenGLView()) {
  84. cocos2d::GL::invalidateStateCache();
  85. cocos2d::GLProgramCache::getInstance()->reloadDefaultGLPrograms();
  86. cocos2d::DrawPrimitives::init();
  87. cocos2d::VolatileTextureMgr::reloadAllTextures();
  88. cocos2d::EventCustom recreatedEvent(EVENT_RENDERER_RECREATED);
  89. director->getEventDispatcher()->dispatchEvent(&recreatedEvent);
  90. director->setGLDefaultValues();
  91. Application::getInstance()->applicationWillEnterForeground();
  92. cocos2d::EventCustom foregroundEvent(EVENT_COME_TO_FOREGROUND);
  93. cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&foregroundEvent);
  94. }
  95. }
  96. void Cocos2dRenderer::Draw(GLsizei width, GLsizei height, float dpi, DisplayOrientations orientation)
  97. {
  98. auto glView = GLViewImpl::sharedOpenGLView();
  99. if (orientation != m_orientation)
  100. {
  101. m_orientation = orientation;
  102. glView->UpdateOrientation(orientation);
  103. }
  104. if (width != m_width || height != m_height)
  105. {
  106. m_width = width;
  107. m_height = height;
  108. glView->UpdateForWindowSizeChange(static_cast<float>(width), static_cast<float>(height));
  109. }
  110. if (dpi != m_dpi)
  111. {
  112. m_dpi = dpi;
  113. glView->SetDPI(m_dpi);
  114. }
  115. glView->ProcessEvents();
  116. glView->Render();
  117. }
  118. void Cocos2dRenderer::QueuePointerEvent(cocos2d::PointerEventType type, Windows::UI::Core::PointerEventArgs^ args)
  119. {
  120. GLViewImpl::sharedOpenGLView()->QueuePointerEvent(type, args);
  121. }
  122. void Cocos2dRenderer::QueueBackButtonEvent()
  123. {
  124. GLViewImpl::sharedOpenGLView()->QueueBackKeyPress();
  125. }
  126. void Cocos2dRenderer::QueueKeyboardEvent(WinRTKeyboardEventType type, Windows::UI::Core::KeyEventArgs^ args)
  127. {
  128. GLViewImpl::sharedOpenGLView()->QueueWinRTKeyboardEvent(type, args);
  129. }