GLScreen.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. using OpenTK;
  2. using OpenTK.Graphics;
  3. using OpenTK.Input;
  4. using Ryujinx.Graphics.Gal;
  5. using Ryujinx.HLE;
  6. using Ryujinx.HLE.Input;
  7. using Ryujinx.Profiler.UI;
  8. using System;
  9. using System.Threading;
  10. using Stopwatch = System.Diagnostics.Stopwatch;
  11. namespace Ryujinx.UI
  12. {
  13. public class GlScreen : GameWindow
  14. {
  15. private const int TouchScreenWidth = 1280;
  16. private const int TouchScreenHeight = 720;
  17. private const int TargetFps = 60;
  18. private Switch _device;
  19. private IGalRenderer _renderer;
  20. private HotkeyButtons _prevHotkeyButtons = 0;
  21. private KeyboardState? _keyboard = null;
  22. private MouseState? _mouse = null;
  23. private Thread _renderThread;
  24. private bool _resizeEvent;
  25. private bool _titleEvent;
  26. private string _newTitle;
  27. #if USE_PROFILING
  28. private ProfileWindowManager _profileWindow;
  29. #endif
  30. public GlScreen(Switch device, IGalRenderer renderer)
  31. : base(1280, 720,
  32. new GraphicsMode(), "Ryujinx", 0,
  33. DisplayDevice.Default, 3, 3,
  34. GraphicsContextFlags.ForwardCompatible)
  35. {
  36. _device = device;
  37. _renderer = renderer;
  38. Location = new Point(
  39. (DisplayDevice.Default.Width / 2) - (Width / 2),
  40. (DisplayDevice.Default.Height / 2) - (Height / 2));
  41. #if USE_PROFILING
  42. // Start profile window, it will handle itself from there
  43. _profileWindow = new ProfileWindowManager();
  44. #endif
  45. }
  46. private void RenderLoop()
  47. {
  48. MakeCurrent();
  49. Stopwatch chrono = new Stopwatch();
  50. chrono.Start();
  51. long ticksPerFrame = Stopwatch.Frequency / TargetFps;
  52. long ticks = 0;
  53. while (Exists && !IsExiting)
  54. {
  55. if (_device.WaitFifo())
  56. {
  57. _device.ProcessFrame();
  58. }
  59. _renderer.RunActions();
  60. if (_resizeEvent)
  61. {
  62. _resizeEvent = false;
  63. _renderer.RenderTarget.SetWindowSize(Width, Height);
  64. }
  65. ticks += chrono.ElapsedTicks;
  66. chrono.Restart();
  67. if (ticks >= ticksPerFrame)
  68. {
  69. RenderFrame();
  70. // Queue max. 1 vsync
  71. ticks = Math.Min(ticks - ticksPerFrame, ticksPerFrame);
  72. }
  73. }
  74. }
  75. public void MainLoop()
  76. {
  77. VSync = VSyncMode.Off;
  78. Visible = true;
  79. _renderer.RenderTarget.SetWindowSize(Width, Height);
  80. Context.MakeCurrent(null);
  81. // OpenTK doesn't like sleeps in its thread, to avoid this a renderer thread is created
  82. _renderThread = new Thread(RenderLoop);
  83. _renderThread.Start();
  84. while (Exists && !IsExiting)
  85. {
  86. ProcessEvents();
  87. if (!IsExiting)
  88. {
  89. UpdateFrame();
  90. if (_titleEvent)
  91. {
  92. _titleEvent = false;
  93. Title = _newTitle;
  94. }
  95. }
  96. // Polling becomes expensive if it's not slept
  97. Thread.Sleep(1);
  98. }
  99. }
  100. private new void UpdateFrame()
  101. {
  102. HotkeyButtons currentHotkeyButtons = 0;
  103. ControllerButtons currentButton = 0;
  104. JoystickPosition leftJoystick;
  105. JoystickPosition rightJoystick;
  106. HLE.Input.Keyboard? hidKeyboard = null;
  107. int leftJoystickDx = 0;
  108. int leftJoystickDy = 0;
  109. int rightJoystickDx = 0;
  110. int rightJoystickDy = 0;
  111. // Keyboard Input
  112. if (_keyboard.HasValue)
  113. {
  114. KeyboardState keyboard = _keyboard.Value;
  115. #if USE_PROFILING
  116. // Profiler input, lets the profiler get access to the main windows keyboard state
  117. _profileWindow.UpdateKeyInput(keyboard);
  118. #endif
  119. // Normal Input
  120. currentHotkeyButtons = Configuration.Instance.KeyboardControls.GetHotkeyButtons(keyboard);
  121. currentButton = Configuration.Instance.KeyboardControls.GetButtons(keyboard);
  122. if (Configuration.Instance.EnableKeyboard)
  123. {
  124. hidKeyboard = Configuration.Instance.KeyboardControls.GetKeysDown(keyboard);
  125. }
  126. (leftJoystickDx, leftJoystickDy) = Configuration.Instance.KeyboardControls.GetLeftStick(keyboard);
  127. (rightJoystickDx, rightJoystickDy) = Configuration.Instance.KeyboardControls.GetRightStick(keyboard);
  128. }
  129. if (!hidKeyboard.HasValue)
  130. {
  131. hidKeyboard = new HLE.Input.Keyboard
  132. {
  133. Modifier = 0,
  134. Keys = new int[0x8]
  135. };
  136. }
  137. currentButton |= Configuration.Instance.JoystickControls.GetButtons();
  138. // Keyboard has priority stick-wise
  139. if (leftJoystickDx == 0 && leftJoystickDy == 0)
  140. {
  141. (leftJoystickDx, leftJoystickDy) = Configuration.Instance.JoystickControls.GetLeftStick();
  142. }
  143. if (rightJoystickDx == 0 && rightJoystickDy == 0)
  144. {
  145. (rightJoystickDx, rightJoystickDy) = Configuration.Instance.JoystickControls.GetRightStick();
  146. }
  147. leftJoystick = new JoystickPosition
  148. {
  149. Dx = leftJoystickDx,
  150. Dy = leftJoystickDy
  151. };
  152. rightJoystick = new JoystickPosition
  153. {
  154. Dx = rightJoystickDx,
  155. Dy = rightJoystickDy
  156. };
  157. currentButton |= _device.Hid.UpdateStickButtons(leftJoystick, rightJoystick);
  158. bool hasTouch = false;
  159. // Get screen touch position from left mouse click
  160. // OpenTK always captures mouse events, even if out of focus, so check if window is focused.
  161. if (Focused && _mouse?.LeftButton == ButtonState.Pressed)
  162. {
  163. MouseState mouse = _mouse.Value;
  164. int scrnWidth = Width;
  165. int scrnHeight = Height;
  166. if (Width > (Height * TouchScreenWidth) / TouchScreenHeight)
  167. {
  168. scrnWidth = (Height * TouchScreenWidth) / TouchScreenHeight;
  169. }
  170. else
  171. {
  172. scrnHeight = (Width * TouchScreenHeight) / TouchScreenWidth;
  173. }
  174. int startX = (Width - scrnWidth) >> 1;
  175. int startY = (Height - scrnHeight) >> 1;
  176. int endX = startX + scrnWidth;
  177. int endY = startY + scrnHeight;
  178. if (mouse.X >= startX &&
  179. mouse.Y >= startY &&
  180. mouse.X < endX &&
  181. mouse.Y < endY)
  182. {
  183. int scrnMouseX = mouse.X - startX;
  184. int scrnMouseY = mouse.Y - startY;
  185. int mX = (scrnMouseX * TouchScreenWidth) / scrnWidth;
  186. int mY = (scrnMouseY * TouchScreenHeight) / scrnHeight;
  187. TouchPoint currentPoint = new TouchPoint
  188. {
  189. X = mX,
  190. Y = mY,
  191. // Placeholder values till more data is acquired
  192. DiameterX = 10,
  193. DiameterY = 10,
  194. Angle = 90
  195. };
  196. hasTouch = true;
  197. _device.Hid.SetTouchPoints(currentPoint);
  198. }
  199. }
  200. if (!hasTouch)
  201. {
  202. _device.Hid.SetTouchPoints();
  203. }
  204. if (Configuration.Instance.EnableKeyboard && hidKeyboard.HasValue)
  205. {
  206. _device.Hid.WriteKeyboard(hidKeyboard.Value);
  207. }
  208. BaseController controller = _device.Hid.PrimaryController;
  209. controller.SendInput(currentButton, leftJoystick, rightJoystick);
  210. // Toggle vsync
  211. if (currentHotkeyButtons.HasFlag(HotkeyButtons.ToggleVSync) &&
  212. !_prevHotkeyButtons.HasFlag(HotkeyButtons.ToggleVSync))
  213. {
  214. _device.EnableDeviceVsync = !_device.EnableDeviceVsync;
  215. }
  216. _prevHotkeyButtons = currentHotkeyButtons;
  217. }
  218. private new void RenderFrame()
  219. {
  220. _renderer.RenderTarget.Render();
  221. _device.Statistics.RecordSystemFrameTime();
  222. double hostFps = _device.Statistics.GetSystemFrameRate();
  223. double gameFps = _device.Statistics.GetGameFrameRate();
  224. string titleSection = string.IsNullOrWhiteSpace(_device.System.CurrentTitle) ? string.Empty
  225. : " | " + _device.System.CurrentTitle;
  226. _newTitle = $"Ryujinx{titleSection} | Host FPS: {hostFps:0.0} | Game FPS: {gameFps:0.0} | " +
  227. $"Game Vsync: {(_device.EnableDeviceVsync ? "On" : "Off")}";
  228. _titleEvent = true;
  229. SwapBuffers();
  230. _device.System.SignalVsync();
  231. _device.VsyncEvent.Set();
  232. }
  233. protected override void OnUnload(EventArgs e)
  234. {
  235. #if USE_PROFILING
  236. _profileWindow.Close();
  237. #endif
  238. _renderThread.Join();
  239. base.OnUnload(e);
  240. }
  241. protected override void OnResize(EventArgs e)
  242. {
  243. _resizeEvent = true;
  244. }
  245. protected override void OnKeyDown(KeyboardKeyEventArgs e)
  246. {
  247. bool toggleFullscreen = e.Key == Key.F11 ||
  248. (e.Modifiers.HasFlag(KeyModifiers.Alt) && e.Key == Key.Enter);
  249. if (WindowState == WindowState.Fullscreen)
  250. {
  251. if (e.Key == Key.Escape || toggleFullscreen)
  252. {
  253. WindowState = WindowState.Normal;
  254. }
  255. }
  256. else
  257. {
  258. if (e.Key == Key.Escape)
  259. {
  260. Exit();
  261. }
  262. if (toggleFullscreen)
  263. {
  264. WindowState = WindowState.Fullscreen;
  265. }
  266. }
  267. _keyboard = e.Keyboard;
  268. }
  269. protected override void OnKeyUp(KeyboardKeyEventArgs e)
  270. {
  271. _keyboard = e.Keyboard;
  272. }
  273. protected override void OnMouseDown(MouseButtonEventArgs e)
  274. {
  275. _mouse = e.Mouse;
  276. }
  277. protected override void OnMouseUp(MouseButtonEventArgs e)
  278. {
  279. _mouse = e.Mouse;
  280. }
  281. protected override void OnMouseMove(MouseMoveEventArgs e)
  282. {
  283. _mouse = e.Mouse;
  284. }
  285. }
  286. }