entry_winrt.cx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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/math.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[] = { "app.exe", "--d3d12", "01" };
  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. bgfx::renderFrame();
  68. bx::Thread thread;
  69. thread.init(MainThreadFunc, nullptr);
  70. CoreWindow^ window = CoreWindow::GetForCurrentThread();
  71. auto bounds = window->Bounds;
  72. #if BX_PLATFORM_WINRT
  73. auto dpi = DisplayInformation::GetForCurrentView()->LogicalDpi;
  74. static const float dipsPerInch = 96.0f;
  75. g_eventQueue.postSizeEvent(g_defaultWindow
  76. , lround(bx::ffloor(bounds.Width * dpi / dipsPerInch + 0.5f) )
  77. , lround(bx::ffloor(bounds.Height * dpi / dipsPerInch + 0.5f) )
  78. );
  79. #endif // BX_PLATFORM_WINRT
  80. while (!m_windowClosed)
  81. {
  82. if (m_windowVisible)
  83. {
  84. window->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
  85. }
  86. else
  87. {
  88. window->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending);
  89. }
  90. bgfx::renderFrame();
  91. }
  92. g_eventQueue.postExitEvent();
  93. while (bgfx::RenderFrame::NoContext != bgfx::renderFrame() ) {};
  94. thread.shutdown();
  95. }
  96. virtual void Uninitialize()
  97. {
  98. }
  99. private:
  100. bool m_windowVisible;
  101. bool m_windowClosed;
  102. void OnActivated(CoreApplicationView^ applicationView, IActivatedEventArgs^ args)
  103. {
  104. CoreWindow::GetForCurrentThread()->Activate();
  105. }
  106. void OnVisibilityChanged(CoreWindow^ sender, VisibilityChangedEventArgs^ args)
  107. {
  108. m_windowVisible = args->Visible;
  109. }
  110. void OnSuspending(Platform::Object^ sender, SuspendingEventArgs^ args)
  111. {
  112. SuspendingDeferral^ deferral = args->SuspendingOperation->GetDeferral();
  113. BX_UNUSED(deferral);
  114. }
  115. void OnResuming(Platform::Object^ sender, Platform::Object^ args)
  116. {
  117. }
  118. void OnWindowClosed(CoreWindow^ sender, CoreWindowEventArgs^ args)
  119. {
  120. m_windowClosed = true;
  121. }
  122. static int32_t MainThreadFunc(bx::Thread*, void*)
  123. {
  124. return entry::main(BX_COUNTOF(g_emptyArgs), g_emptyArgs);
  125. }
  126. };
  127. ref class AppSource sealed : IFrameworkViewSource
  128. {
  129. public:
  130. virtual IFrameworkView^ CreateView()
  131. {
  132. return ref new App();
  133. }
  134. };
  135. namespace entry
  136. {
  137. const Event* poll()
  138. {
  139. return g_eventQueue.poll();
  140. }
  141. const Event* poll(WindowHandle _handle)
  142. {
  143. return g_eventQueue.poll(_handle);
  144. }
  145. void release(const Event* _event)
  146. {
  147. g_eventQueue.release(_event);
  148. }
  149. WindowHandle createWindow(int32_t _x, int32_t _y, uint32_t _width, uint32_t _height, uint32_t _flags, const char* _title)
  150. {
  151. BX_UNUSED(_x, _y, _width, _height, _flags, _title);
  152. WindowHandle handle = { UINT16_MAX };
  153. return handle;
  154. }
  155. void destroyWindow(WindowHandle _handle)
  156. {
  157. BX_UNUSED(_handle);
  158. }
  159. void setWindowPos(WindowHandle _handle, int32_t _x, int32_t _y)
  160. {
  161. BX_UNUSED(_handle, _x, _y);
  162. }
  163. void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height)
  164. {
  165. BX_UNUSED(_handle, _width, _height);
  166. }
  167. void setWindowTitle(WindowHandle _handle, const char* _title)
  168. {
  169. BX_UNUSED(_handle, _title);
  170. }
  171. void setWindowFlags(WindowHandle _handle, uint32_t _flags, bool _enabled)
  172. {
  173. BX_UNUSED(_handle, _flags, _enabled);
  174. }
  175. void toggleFullscreen(WindowHandle _handle)
  176. {
  177. BX_UNUSED(_handle);
  178. }
  179. void setMouseLock(WindowHandle _handle, bool _lock)
  180. {
  181. BX_UNUSED(_handle, _lock);
  182. }
  183. }
  184. [MTAThread]
  185. int main(Array<String^>^)
  186. {
  187. auto appSource = ref new AppSource();
  188. CoreApplication::Run(appSource);
  189. return 0;
  190. }
  191. #endif // BX_PLATFORM_WINRT || BX_PLATFORM_XBOXONE