entry_winrt.cx 5.2 KB

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