GLScreen.cs 8.9 KB

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