entry_winrt.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright 2011-2014 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. window->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
  58. else
  59. window->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending);
  60. }
  61. g_eventQueue.postExitEvent();
  62. thread.shutdown();
  63. }
  64. virtual void Uninitialize()
  65. {
  66. }
  67. private:
  68. bool m_windowVisible;
  69. bool m_windowClosed;
  70. void OnActivated(CoreApplicationView^ applicationView, IActivatedEventArgs^ args)
  71. {
  72. CoreWindow^ window = CoreWindow::GetForCurrentThread();
  73. if (window == nullptr) {
  74. int i = 4;
  75. i++;
  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 setMouseLock(WindowHandle _handle, bool _lock)
  141. {
  142. BX_UNUSED(_handle, _lock);
  143. }
  144. }
  145. [MTAThread]
  146. int main(Array<String^>^)
  147. {
  148. auto appSource = ref new AppSource();
  149. CoreApplication::Run(appSource);
  150. return 0;
  151. }
  152. #endif