GLScreen.cs 9.7 KB

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