entry_winrt.cx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright 2011-2016 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include "entry_p.h"
  6. #if BX_PLATFORM_WINRT || BX_PLATFORM_XBOXONE
  7. #include <bgfx/bgfxplatform.h>
  8. #include <bx/thread.h>
  9. #include <Unknwn.h>
  10. using namespace Windows::ApplicationModel;
  11. using namespace Windows::ApplicationModel::Core;
  12. using namespace Windows::ApplicationModel::Activation;
  13. using namespace Windows::UI::Core;
  14. using namespace Windows::UI::Input;
  15. using namespace Windows::System;
  16. using namespace Windows::Foundation;
  17. #if BX_PLATFORM_WINRT
  18. using namespace Windows::Graphics::Display;
  19. #endif // BX_PLATFORM_WINRT
  20. using namespace Platform;
  21. static char* g_emptyArgs[] = { "" };
  22. static entry::WindowHandle g_defaultWindow = { 0 };
  23. static entry::EventQueue g_eventQueue;
  24. ///
  25. inline void winrtSetWindow(::IUnknown* _window)
  26. {
  27. bgfx::PlatformData pd;
  28. pd.ndt = NULL;
  29. pd.nwh = _window;
  30. pd.context = NULL;
  31. pd.backBuffer = NULL;
  32. pd.backBufferDS = NULL;
  33. bgfx::setPlatformData(pd);
  34. }
  35. ref class App sealed : public IFrameworkView
  36. {
  37. public:
  38. App()
  39. : m_windowVisible(true)
  40. , m_windowClosed(false)
  41. {
  42. }
  43. // IFrameworkView Methods.
  44. virtual void Initialize(CoreApplicationView^ applicationView)
  45. {
  46. applicationView->Activated += ref new
  47. TypedEventHandler<CoreApplicationView^, IActivatedEventArgs^>(this, &App::OnActivated);
  48. CoreApplication::Suspending += ref new
  49. EventHandler<SuspendingEventArgs^>(this, &App::OnSuspending);
  50. CoreApplication::Resuming += ref new
  51. EventHandler<Platform::Object^>(this, &App::OnResuming);
  52. }
  53. virtual void SetWindow(CoreWindow^ window)
  54. {
  55. window->VisibilityChanged += ref new
  56. TypedEventHandler<CoreWindow^, VisibilityChangedEventArgs^>(this, &App::OnVisibilityChanged);
  57. window->Closed += ref new
  58. TypedEventHandler<CoreWindow^, CoreWindowEventArgs^>(this, &App::OnWindowClosed);
  59. winrtSetWindow(reinterpret_cast<IUnknown*>(window) );
  60. }
  61. virtual void Load(String^ entryPoint)
  62. {
  63. }
  64. virtual void Run()
  65. {
  66. bx::Thread thread;
  67. thread.init(MainThreadFunc, nullptr);
  68. CoreWindow^ window = CoreWindow::GetForCurrentThread();
  69. auto bounds = window->Bounds;
  70. #if BX_PLATFORM_WINRT
  71. auto dpi = DisplayInformation::GetForCurrentView()->LogicalDpi;
  72. static const float dipsPerInch = 96.0f;
  73. g_eventQueue.postSizeEvent(g_defaultWindow
  74. , lround(bx::ffloor(bounds.Width * dpi / dipsPerInch + 0.5f) )
  75. , lround(bx::ffloor(bounds.Height * dpi / dipsPerInch + 0.5f) )
  76. );
  77. #endif // BX_PLATFORM_WINRT
  78. while (!m_windowClosed)
  79. {
  80. if (m_windowVisible)
  81. {
  82. window->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
  83. }
  84. else
  85. {
  86. window->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending);
  87. }
  88. }
  89. g_eventQueue.postExitEvent();
  90. thread.shutdown();
  91. }
  92. virtual void Uninitialize()
  93. {
  94. }
  95. private:
  96. bool m_windowVisible;
  97. bool m_windowClosed;
  98. void OnActivated(CoreApplicationView^ applicationView, IActivatedEventArgs^ args)
  99. {
  100. CoreWindow::GetForCurrentThread()->Activate();
  101. }
  102. void OnVisibilityChanged(CoreWindow^ sender, VisibilityChangedEventArgs^ args)
  103. {
  104. m_windowVisible = args->Visible;
  105. }
  106. void OnSuspending(Platform::Object^ sender, SuspendingEventArgs^ args)
  107. {
  108. SuspendingDeferral^ deferral = args->SuspendingOperation->GetDeferral();
  109. BX_UNUSED(deferral);
  110. }
  111. void OnResuming(Platform::Object^ sender, Platform::Object^ args)
  112. {
  113. }
  114. void OnWindowClosed(CoreWindow^ sender, CoreWindowEventArgs^ args)
  115. {
  116. m_windowClosed = true;
  117. }
  118. static int32_t MainThreadFunc(void*)
  119. {
  120. return entry::main(0, g_emptyArgs);
  121. }
  122. };
  123. ref class AppSource sealed : IFrameworkViewSource
  124. {
  125. public:
  126. virtual IFrameworkView^ CreateView()
  127. {
  128. return ref new App();
  129. }
  130. };
  131. namespace entry
  132. {
  133. const Event* poll()
  134. {
  135. return g_eventQueue.poll();
  136. }
  137. const Event* poll(WindowHandle _handle)
  138. {
  139. return g_eventQueue.poll(_handle);
  140. }
  141. void release(const Event* _event)
  142. {
  143. g_eventQueue.release(_event);
  144. }
  145. WindowHandle createWindow(int32_t _x, int32_t _y, uint32_t _width, uint32_t _height, uint32_t _flags, const char* _title)
  146. {
  147. BX_UNUSED(_x, _y, _width, _height, _flags, _title);
  148. WindowHandle handle = { UINT16_MAX };
  149. return handle;
  150. }
  151. void destroyWindow(WindowHandle _handle)
  152. {
  153. BX_UNUSED(_handle);
  154. }
  155. void setWindowPos(WindowHandle _handle, int32_t _x, int32_t _y)
  156. {
  157. BX_UNUSED(_handle, _x, _y);
  158. }
  159. void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height)
  160. {
  161. BX_UNUSED(_handle, _width, _height);
  162. }
  163. void setWindowTitle(WindowHandle _handle, const char* _title)
  164. {
  165. BX_UNUSED(_handle, _title);
  166. }
  167. void toggleWindowFrame(WindowHandle _handle)
  168. {
  169. BX_UNUSED(_handle);
  170. }
  171. void toggleFullscreen(WindowHandle _handle)
  172. {
  173. BX_UNUSED(_handle);
  174. }
  175. void setMouseLock(WindowHandle _handle, bool _lock)
  176. {
  177. BX_UNUSED(_handle, _lock);
  178. }
  179. }
  180. [MTAThread]
  181. int main(Array<String^>^)
  182. {
  183. auto appSource = ref new AppSource();
  184. CoreApplication::Run(appSource);
  185. return 0;
  186. }
  187. #endif // BX_PLATFORM_WINRT || BX_PLATFORM_XBOXONE