entry_winrt.cpp 4.0 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 <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. if (window == nullptr) {
  49. int i = 4;
  50. i++;
  51. }
  52. //auto bounds = window->Bounds;
  53. //g_eventQueue.postSizeEvent(g_defaultWindow, bounds.Width, bounds.Height);
  54. while (!m_windowClosed)
  55. {
  56. if (m_windowVisible)
  57. {
  58. window->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
  59. }
  60. else
  61. {
  62. window->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending);
  63. }
  64. }
  65. g_eventQueue.postExitEvent();
  66. thread.shutdown();
  67. }
  68. virtual void Uninitialize()
  69. {
  70. }
  71. private:
  72. bool m_windowVisible;
  73. bool m_windowClosed;
  74. void OnActivated(CoreApplicationView^ applicationView, IActivatedEventArgs^ args)
  75. {
  76. CoreWindow^ window = CoreWindow::GetForCurrentThread();
  77. if (window == nullptr) {
  78. int i = 4;
  79. i++;
  80. }
  81. CoreWindow::GetForCurrentThread()->Activate();
  82. }
  83. void OnVisibilityChanged(CoreWindow^ sender, VisibilityChangedEventArgs^ args)
  84. {
  85. m_windowVisible = args->Visible;
  86. }
  87. void OnWindowClosed(CoreWindow^ sender, CoreWindowEventArgs^ args)
  88. {
  89. m_windowClosed = true;
  90. }
  91. static int32_t MainThreadFunc(void*)
  92. {
  93. return entry::main(0, g_emptyArgs);
  94. }
  95. };
  96. ref class AppSource sealed : IFrameworkViewSource
  97. {
  98. public:
  99. virtual IFrameworkView^ CreateView()
  100. {
  101. return ref new App();
  102. }
  103. };
  104. namespace entry
  105. {
  106. const Event* poll()
  107. {
  108. return g_eventQueue.poll();
  109. }
  110. const Event* poll(WindowHandle _handle)
  111. {
  112. return g_eventQueue.poll(_handle);
  113. }
  114. void release(const Event* _event)
  115. {
  116. g_eventQueue.release(_event);
  117. }
  118. WindowHandle createWindow(int32_t _x, int32_t _y, uint32_t _width, uint32_t _height, uint32_t _flags, const char* _title)
  119. {
  120. BX_UNUSED(_x, _y, _width, _height, _flags, _title);
  121. WindowHandle handle = { UINT16_MAX };
  122. return handle;
  123. }
  124. void destroyWindow(WindowHandle _handle)
  125. {
  126. BX_UNUSED(_handle);
  127. }
  128. void setWindowPos(WindowHandle _handle, int32_t _x, int32_t _y)
  129. {
  130. BX_UNUSED(_handle, _x, _y);
  131. }
  132. void setWindowSize(WindowHandle _handle, uint32_t _width, uint32_t _height)
  133. {
  134. BX_UNUSED(_handle, _width, _height);
  135. }
  136. void setWindowTitle(WindowHandle _handle, const char* _title)
  137. {
  138. BX_UNUSED(_handle, _title);
  139. }
  140. void toggleWindowFrame(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