entry_winrt.cpp 4.1 KB

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