WinPhone8.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #include "pch.h"
  11. #include "WinPhone8.h"
  12. #include "BasicTimer.h"
  13. using namespace Windows::ApplicationModel;
  14. using namespace Windows::ApplicationModel::Core;
  15. using namespace Windows::ApplicationModel::Activation;
  16. using namespace Windows::UI::Core;
  17. using namespace Windows::System;
  18. using namespace Windows::Foundation;
  19. using namespace Windows::Graphics::Display;
  20. using namespace concurrency;
  21. WinPhone8::WinPhone8() :
  22. m_windowClosed(false),
  23. m_windowVisible(true)
  24. {
  25. }
  26. void WinPhone8::Initialize(CoreApplicationView^ applicationView)
  27. {
  28. applicationView->Activated +=
  29. ref new TypedEventHandler<CoreApplicationView^, IActivatedEventArgs^>(this, &WinPhone8::OnActivated);
  30. CoreApplication::Suspending +=
  31. ref new EventHandler<SuspendingEventArgs^>(this, &WinPhone8::OnSuspending);
  32. CoreApplication::Resuming +=
  33. ref new EventHandler<Platform::Object^>(this, &WinPhone8::OnResuming);
  34. m_renderer = ref new CubeRenderer();
  35. }
  36. void WinPhone8::SetWindow(CoreWindow^ window)
  37. {
  38. window->VisibilityChanged +=
  39. ref new TypedEventHandler<CoreWindow^, VisibilityChangedEventArgs^>(this, &WinPhone8::OnVisibilityChanged);
  40. window->Closed +=
  41. ref new TypedEventHandler<CoreWindow^, CoreWindowEventArgs^>(this, &WinPhone8::OnWindowClosed);
  42. window->PointerPressed +=
  43. ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &WinPhone8::OnPointerPressed);
  44. window->PointerMoved +=
  45. ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &WinPhone8::OnPointerMoved);
  46. window->PointerReleased +=
  47. ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &WinPhone8::OnPointerReleased);
  48. m_renderer->Initialize(CoreWindow::GetForCurrentThread());
  49. }
  50. void WinPhone8::Load(Platform::String^ entryPoint)
  51. {
  52. }
  53. void WinPhone8::Run()
  54. {
  55. BasicTimer^ timer = ref new BasicTimer();
  56. while (!m_windowClosed)
  57. {
  58. if (m_windowVisible)
  59. {
  60. timer->Update();
  61. CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent);
  62. m_renderer->Update(timer->Total, timer->Delta);
  63. m_renderer->Render();
  64. m_renderer->Present(); // This call is synchronized to the display frame rate.
  65. }
  66. else
  67. {
  68. CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending);
  69. }
  70. }
  71. }
  72. void WinPhone8::Uninitialize()
  73. {
  74. }
  75. void WinPhone8::OnVisibilityChanged(CoreWindow^ sender, VisibilityChangedEventArgs^ args)
  76. {
  77. m_windowVisible = args->Visible;
  78. }
  79. void WinPhone8::OnWindowClosed(CoreWindow^ sender, CoreWindowEventArgs^ args)
  80. {
  81. m_windowClosed = true;
  82. }
  83. #include "RakPeerInterface.h"
  84. using namespace RakNet;
  85. #define DEFAULT_SERVER_PORT 61111
  86. #define DEFAULT_SERVER_ADDRESS "natpunch.jenkinssoftware.com"
  87. void WinPhone8::OnPointerPressed(CoreWindow^ sender, PointerEventArgs^ args)
  88. {
  89. // Insert your code here.
  90. RakPeerInterface *rakPeer = RakPeerInterface::GetInstance();
  91. SocketDescriptor sd;
  92. StartupResult sr = rakPeer->Startup(1, &sd, 1);
  93. assert(sr==RAKNET_STARTED);
  94. ConnectionAttemptResult car = rakPeer->Connect(DEFAULT_SERVER_ADDRESS, DEFAULT_SERVER_PORT, 0, 0);
  95. assert(car==CONNECTION_ATTEMPT_STARTED);
  96. }
  97. void WinPhone8::OnPointerMoved(CoreWindow^ sender, PointerEventArgs^ args)
  98. {
  99. // Insert your code here.
  100. }
  101. void WinPhone8::OnPointerReleased(CoreWindow^ sender, PointerEventArgs^ args)
  102. {
  103. // Insert your code here.
  104. }
  105. void WinPhone8::OnActivated(CoreApplicationView^ applicationView, IActivatedEventArgs^ args)
  106. {
  107. CoreWindow::GetForCurrentThread()->Activate();
  108. }
  109. void WinPhone8::OnSuspending(Platform::Object^ sender, SuspendingEventArgs^ args)
  110. {
  111. // Save app state asynchronously after requesting a deferral. Holding a deferral
  112. // indicates that the application is busy performing suspending operations. Be
  113. // aware that a deferral may not be held indefinitely. After about five seconds,
  114. // the app will be forced to exit.
  115. SuspendingDeferral^ deferral = args->SuspendingOperation->GetDeferral();
  116. m_renderer->ReleaseResourcesForSuspending();
  117. create_task([this, deferral]()
  118. {
  119. // Insert your code here.
  120. deferral->Complete();
  121. });
  122. }
  123. void WinPhone8::OnResuming(Platform::Object^ sender, Platform::Object^ args)
  124. {
  125. // Restore any data or state that was unloaded on suspend. By default, data
  126. // and state are persisted when resuming from suspend. Note that this event
  127. // does not occur if the app was previously terminated.
  128. m_renderer->CreateWindowSizeDependentResources();
  129. }
  130. IFrameworkView^ Direct3DApplicationSource::CreateView()
  131. {
  132. return ref new WinPhone8();
  133. }
  134. [Platform::MTAThread]
  135. int main(Platform::Array<Platform::String^>^)
  136. {
  137. auto direct3DApplicationSource = ref new Direct3DApplicationSource();
  138. CoreApplication::Run(direct3DApplicationSource);
  139. return 0;
  140. }
粤ICP备19079148号