GLScreen.cs 11 KB

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