app.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. //
  2. // This file demonstrates how to initialize EGL in a Windows Store app, using ICoreWindow.
  3. //
  4. #include "app.h"
  5. #include "main/main.h"
  6. #include "core/os/dir_access.h"
  7. #include "core/os/file_access.h"
  8. using namespace Windows::ApplicationModel::Core;
  9. using namespace Windows::ApplicationModel::Activation;
  10. using namespace Windows::UI::Core;
  11. using namespace Windows::UI::Input;
  12. using namespace Windows::Foundation;
  13. using namespace Windows::Graphics::Display;
  14. using namespace Microsoft::WRL;
  15. using namespace Platform;
  16. using namespace $ext_safeprojectname$;
  17. // Helper to convert a length in device-independent pixels (DIPs) to a length in physical pixels.
  18. inline float ConvertDipsToPixels(float dips, float dpi)
  19. {
  20. static const float dipsPerInch = 96.0f;
  21. return floor(dips * dpi / dipsPerInch + 0.5f); // Round to nearest integer.
  22. }
  23. // Implementation of the IFrameworkViewSource interface, necessary to run our app.
  24. ref class HelloTriangleApplicationSource sealed : Windows::ApplicationModel::Core::IFrameworkViewSource
  25. {
  26. public:
  27. virtual Windows::ApplicationModel::Core::IFrameworkView^ CreateView()
  28. {
  29. return ref new App();
  30. }
  31. };
  32. // The main function creates an IFrameworkViewSource for our app, and runs the app.
  33. [Platform::MTAThread]
  34. int main(Platform::Array<Platform::String^>^)
  35. {
  36. auto helloTriangleApplicationSource = ref new HelloTriangleApplicationSource();
  37. CoreApplication::Run(helloTriangleApplicationSource);
  38. return 0;
  39. }
  40. App::App() :
  41. mWindowClosed(false),
  42. mWindowVisible(true),
  43. mWindowWidth(0),
  44. mWindowHeight(0),
  45. mEglDisplay(EGL_NO_DISPLAY),
  46. mEglContext(EGL_NO_CONTEXT),
  47. mEglSurface(EGL_NO_SURFACE)
  48. {
  49. }
  50. // The first method called when the IFrameworkView is being created.
  51. void App::Initialize(CoreApplicationView^ applicationView)
  52. {
  53. // Register event handlers for app lifecycle. This example includes Activated, so that we
  54. // can make the CoreWindow active and start rendering on the window.
  55. applicationView->Activated +=
  56. ref new TypedEventHandler<CoreApplicationView^, IActivatedEventArgs^>(this, &App::OnActivated);
  57. // Logic for other event handlers could go here.
  58. // Information about the Suspending and Resuming event handlers can be found here:
  59. // http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh994930.aspx
  60. os = new OSWinrt;
  61. }
  62. // Called when the CoreWindow object is created (or re-created).
  63. void App::SetWindow(CoreWindow^ p_window)
  64. {
  65. window = p_window;
  66. window->VisibilityChanged +=
  67. ref new TypedEventHandler<CoreWindow^, VisibilityChangedEventArgs^>(this, &App::OnVisibilityChanged);
  68. window->Closed +=
  69. ref new TypedEventHandler<CoreWindow^, CoreWindowEventArgs^>(this, &App::OnWindowClosed);
  70. window->SizeChanged +=
  71. ref new TypedEventHandler<CoreWindow^, WindowSizeChangedEventArgs^>(this, &App::OnWindowSizeChanged);
  72. #if !(WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP)
  73. // Disable all pointer visual feedback for better performance when touching.
  74. // This is not supported on Windows Phone applications.
  75. auto pointerVisualizationSettings = PointerVisualizationSettings::GetForCurrentView();
  76. pointerVisualizationSettings->IsContactFeedbackEnabled = false;
  77. pointerVisualizationSettings->IsBarrelButtonFeedbackEnabled = false;
  78. #endif
  79. window->PointerPressed +=
  80. ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &App::OnPointerPressed);
  81. window->PointerMoved +=
  82. ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &App::OnPointerMoved);
  83. window->PointerReleased +=
  84. ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &App::OnPointerReleased);
  85. //window->PointerWheelChanged +=
  86. // ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &App::OnPointerWheelChanged);
  87. char* args[] = {"-path", "game", NULL};
  88. Main::setup("winrt", 2, args, false);
  89. // The CoreWindow has been created, so EGL can be initialized.
  90. ContextEGL* context = memnew(ContextEGL(window));
  91. os->set_gl_context(context);
  92. UpdateWindowSize(Size(window->Bounds.Width, window->Bounds.Height));
  93. Main::setup2();
  94. }
  95. static int _get_button(Windows::UI::Input::PointerPoint ^pt) {
  96. using namespace Windows::UI::Input;
  97. #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  98. return BUTTON_LEFT;
  99. #else
  100. switch (pt->Properties->PointerUpdateKind)
  101. {
  102. case PointerUpdateKind::LeftButtonPressed:
  103. case PointerUpdateKind::LeftButtonReleased:
  104. return BUTTON_LEFT;
  105. case PointerUpdateKind::RightButtonPressed:
  106. case PointerUpdateKind::RightButtonReleased:
  107. return BUTTON_RIGHT;
  108. case PointerUpdateKind::MiddleButtonPressed:
  109. case PointerUpdateKind::MiddleButtonReleased:
  110. return BUTTON_MIDDLE;
  111. case PointerUpdateKind::XButton1Pressed:
  112. case PointerUpdateKind::XButton1Released:
  113. return BUTTON_WHEEL_UP;
  114. case PointerUpdateKind::XButton2Pressed:
  115. case PointerUpdateKind::XButton2Released:
  116. return BUTTON_WHEEL_DOWN;
  117. default:
  118. break;
  119. }
  120. #endif
  121. return 0;
  122. };
  123. static bool _is_touch(Windows::UI::Input::PointerPoint ^pointerPoint) {
  124. #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  125. return true;
  126. #else
  127. using namespace Windows::Devices::Input;
  128. switch (pointerPoint->PointerDevice->PointerDeviceType) {
  129. case PointerDeviceType::Touch:
  130. case PointerDeviceType::Pen:
  131. return true;
  132. default:
  133. return false;
  134. }
  135. #endif
  136. }
  137. static Windows::Foundation::Point _get_pixel_position(CoreWindow^ window, Windows::Foundation::Point rawPosition, OS* os) {
  138. Windows::Foundation::Point outputPosition;
  139. // Compute coordinates normalized from 0..1.
  140. // If the coordinates need to be sized to the SDL window,
  141. // we'll do that after.
  142. #if 1 || WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
  143. outputPosition.X = rawPosition.X / window->Bounds.Width;
  144. outputPosition.Y = rawPosition.Y / window->Bounds.Height;
  145. #else
  146. switch (DisplayProperties::CurrentOrientation)
  147. {
  148. case DisplayOrientations::Portrait:
  149. outputPosition.X = rawPosition.X / window->Bounds.Width;
  150. outputPosition.Y = rawPosition.Y / window->Bounds.Height;
  151. break;
  152. case DisplayOrientations::PortraitFlipped:
  153. outputPosition.X = 1.0f - (rawPosition.X / window->Bounds.Width);
  154. outputPosition.Y = 1.0f - (rawPosition.Y / window->Bounds.Height);
  155. break;
  156. case DisplayOrientations::Landscape:
  157. outputPosition.X = rawPosition.Y / window->Bounds.Height;
  158. outputPosition.Y = 1.0f - (rawPosition.X / window->Bounds.Width);
  159. break;
  160. case DisplayOrientations::LandscapeFlipped:
  161. outputPosition.X = 1.0f - (rawPosition.Y / window->Bounds.Height);
  162. outputPosition.Y = rawPosition.X / window->Bounds.Width;
  163. break;
  164. default:
  165. break;
  166. }
  167. #endif
  168. OS::VideoMode vm = os->get_video_mode();
  169. outputPosition.X *= vm.width;
  170. outputPosition.Y *= vm.height;
  171. return outputPosition;
  172. };
  173. static int _get_finger(uint32_t p_touch_id) {
  174. return p_touch_id % 31; // for now
  175. };
  176. void App::pointer_event(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::PointerEventArgs^ args, bool p_pressed) {
  177. Windows::UI::Input::PointerPoint ^point = args->CurrentPoint;
  178. Windows::Foundation::Point pos = _get_pixel_position(window, point->Position, os);
  179. int but = _get_button(point);
  180. if (_is_touch(point)) {
  181. InputEvent event;
  182. event.type = InputEvent::SCREEN_TOUCH;
  183. event.device = 0;
  184. event.screen_touch.pressed = p_pressed;
  185. event.screen_touch.x = pos.X;
  186. event.screen_touch.y = pos.Y;
  187. event.screen_touch.index = _get_finger(point->PointerId);
  188. last_touch_x[event.screen_touch.index] = pos.X;
  189. last_touch_y[event.screen_touch.index] = pos.Y;
  190. os->input_event(event);
  191. if (event.screen_touch.index != 0)
  192. return;
  193. }; // fallthrought of sorts
  194. InputEvent event;
  195. event.type = InputEvent::MOUSE_BUTTON;
  196. event.device = 0;
  197. event.mouse_button.pressed = p_pressed;
  198. event.mouse_button.button_index = but;
  199. event.mouse_button.x = pos.X;
  200. event.mouse_button.y = pos.Y;
  201. event.mouse_button.global_x = pos.X;
  202. event.mouse_button.global_y = pos.Y;
  203. last_touch_x[31] = pos.X;
  204. last_touch_y[31] = pos.Y;
  205. os->input_event(event);
  206. };
  207. void App::OnPointerPressed(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::PointerEventArgs^ args) {
  208. pointer_event(sender, args, true);
  209. };
  210. void App::OnPointerReleased(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::PointerEventArgs^ args) {
  211. pointer_event(sender, args, false);
  212. };
  213. void App::OnPointerMoved(Windows::UI::Core::CoreWindow^ sender, Windows::UI::Core::PointerEventArgs^ args) {
  214. Windows::UI::Input::PointerPoint ^point = args->CurrentPoint;
  215. Windows::Foundation::Point pos = _get_pixel_position(window, point->Position, os);
  216. if (_is_touch(point)) {
  217. InputEvent event;
  218. event.type = InputEvent::SCREEN_DRAG;
  219. event.device = 0;
  220. event.screen_drag.x = pos.X;
  221. event.screen_drag.y = pos.Y;
  222. event.screen_drag.index = _get_finger(point->PointerId);
  223. event.screen_drag.relative_x = event.screen_drag.x - last_touch_x[event.screen_drag.index];
  224. event.screen_drag.relative_y = event.screen_drag.y - last_touch_y[event.screen_drag.index];
  225. os->input_event(event);
  226. if (event.screen_drag.index != 0)
  227. return;
  228. }; // fallthrought of sorts
  229. InputEvent event;
  230. event.type = InputEvent::MOUSE_MOTION;
  231. event.device = 0;
  232. event.mouse_motion.x = pos.X;
  233. event.mouse_motion.y = pos.Y;
  234. event.mouse_motion.global_x = pos.X;
  235. event.mouse_motion.global_y = pos.Y;
  236. event.mouse_motion.relative_x = pos.X - last_touch_x[31];
  237. event.mouse_motion.relative_y = pos.Y - last_touch_y[31];
  238. os->input_event(event);
  239. };
  240. // Initializes scene resources
  241. void App::Load(Platform::String^ entryPoint)
  242. {
  243. //char* args[] = {"-test", "render", NULL};
  244. //Main::setup("winrt", 2, args);
  245. }
  246. // This method is called after the window becomes active.
  247. void App::Run()
  248. {
  249. if (Main::start())
  250. os->run();
  251. }
  252. // Terminate events do not cause Uninitialize to be called. It will be called if your IFrameworkView
  253. // class is torn down while the app is in the foreground.
  254. void App::Uninitialize()
  255. {
  256. Main::cleanup();
  257. delete os;
  258. }
  259. // Application lifecycle event handler.
  260. void App::OnActivated(CoreApplicationView^ applicationView, IActivatedEventArgs^ args)
  261. {
  262. // Run() won't start until the CoreWindow is activated.
  263. CoreWindow::GetForCurrentThread()->Activate();
  264. }
  265. // Window event handlers.
  266. void App::OnVisibilityChanged(CoreWindow^ sender, VisibilityChangedEventArgs^ args)
  267. {
  268. mWindowVisible = args->Visible;
  269. }
  270. void App::OnWindowClosed(CoreWindow^ sender, CoreWindowEventArgs^ args)
  271. {
  272. mWindowClosed = true;
  273. }
  274. void App::OnWindowSizeChanged(CoreWindow^ sender, WindowSizeChangedEventArgs^ args)
  275. {
  276. #if (WINAPI_FAMILY == WINAPI_FAMILY_PC_APP)
  277. // On Windows 8.1, apps are resized when they are snapped alongside other apps, or when the device is rotated.
  278. // The default framebuffer will be automatically resized when either of these occur.
  279. // In particular, on a 90 degree rotation, the default framebuffer's width and height will switch.
  280. UpdateWindowSize(args->Size);
  281. #else if (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP)
  282. // On Windows Phone 8.1, the window size changes when the device is rotated.
  283. // The default framebuffer will not be automatically resized when this occurs.
  284. // It is therefore up to the app to handle rotation-specific logic in its rendering code.
  285. //os->screen_size_changed();
  286. UpdateWindowSize(args->Size);
  287. #endif
  288. }
  289. void App::UpdateWindowSize(Size size)
  290. {
  291. float dpi;
  292. #if (WINAPI_FAMILY == WINAPI_FAMILY_PC_APP)
  293. DisplayInformation^ currentDisplayInformation = DisplayInformation::GetForCurrentView();
  294. dpi = currentDisplayInformation->LogicalDpi;
  295. #else if (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP)
  296. dpi = DisplayProperties::LogicalDpi;
  297. #endif
  298. Size pixelSize(ConvertDipsToPixels(size.Width, dpi), ConvertDipsToPixels(size.Height, dpi));
  299. mWindowWidth = static_cast<GLsizei>(pixelSize.Width);
  300. mWindowHeight = static_cast<GLsizei>(pixelSize.Height);
  301. OS::VideoMode vm;
  302. vm.width = mWindowWidth;
  303. vm.height = mWindowHeight;
  304. vm.fullscreen = true;
  305. vm.resizable = false;
  306. os->set_video_mode(vm);
  307. }