entry_winrt.cx 5.0 KB

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