entry_winrt.cpp 4.7 KB

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