GLScreen.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 = Configuration.Instance.KeyboardControls.GetButtons(keyboard);
  105. (leftJoystickDx, leftJoystickDy) = Configuration.Instance.KeyboardControls.GetLeftStick(keyboard);
  106. (rightJoystickDx, rightJoystickDy) = Configuration.Instance.KeyboardControls.GetRightStick(keyboard);
  107. }
  108. currentButton |= Configuration.Instance.GamepadControls.GetButtons();
  109. //Keyboard has priority stick-wise
  110. if (leftJoystickDx == 0 && leftJoystickDy == 0)
  111. {
  112. (leftJoystickDx, leftJoystickDy) = Configuration.Instance.GamepadControls.GetLeftStick();
  113. }
  114. if (rightJoystickDx == 0 && rightJoystickDy == 0)
  115. {
  116. (rightJoystickDx, rightJoystickDy) = Configuration.Instance.GamepadControls.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. currentButton |= _device.Hid.UpdateStickButtons(leftJoystick, rightJoystick);
  129. bool hasTouch = false;
  130. //Get screen touch position from left mouse click
  131. //OpenTK always captures mouse events, even if out of focus, so check if window is focused.
  132. if (Focused && _mouse?.LeftButton == ButtonState.Pressed)
  133. {
  134. MouseState mouse = _mouse.Value;
  135. int scrnWidth = Width;
  136. int scrnHeight = Height;
  137. if (Width > (Height * TouchScreenWidth) / TouchScreenHeight)
  138. {
  139. scrnWidth = (Height * TouchScreenWidth) / TouchScreenHeight;
  140. }
  141. else
  142. {
  143. scrnHeight = (Width * TouchScreenHeight) / TouchScreenWidth;
  144. }
  145. int startX = (Width - scrnWidth) >> 1;
  146. int startY = (Height - scrnHeight) >> 1;
  147. int endX = startX + scrnWidth;
  148. int endY = startY + scrnHeight;
  149. if (mouse.X >= startX &&
  150. mouse.Y >= startY &&
  151. mouse.X < endX &&
  152. mouse.Y < endY)
  153. {
  154. int scrnMouseX = mouse.X - startX;
  155. int scrnMouseY = mouse.Y - startY;
  156. int mX = (scrnMouseX * TouchScreenWidth) / scrnWidth;
  157. int mY = (scrnMouseY * TouchScreenHeight) / scrnHeight;
  158. HidTouchPoint currentPoint = new HidTouchPoint
  159. {
  160. X = mX,
  161. Y = mY,
  162. //Placeholder values till more data is acquired
  163. DiameterX = 10,
  164. DiameterY = 10,
  165. Angle = 90
  166. };
  167. hasTouch = true;
  168. _device.Hid.SetTouchPoints(currentPoint);
  169. }
  170. }
  171. if (!hasTouch)
  172. {
  173. _device.Hid.SetTouchPoints();
  174. }
  175. HidControllerBase controller = _device.Hid.PrimaryController;
  176. controller.SendInput(currentButton, leftJoystick, rightJoystick);
  177. }
  178. private new void RenderFrame()
  179. {
  180. _renderer.RenderTarget.Render();
  181. _device.Statistics.RecordSystemFrameTime();
  182. double hostFps = _device.Statistics.GetSystemFrameRate();
  183. double gameFps = _device.Statistics.GetGameFrameRate();
  184. string titleSection = string.IsNullOrWhiteSpace(_device.System.CurrentTitle) ? string.Empty
  185. : " | " + _device.System.CurrentTitle;
  186. _newTitle = $"Ryujinx{titleSection} | Host FPS: {hostFps:0.0} | Game FPS: {gameFps:0.0} | " +
  187. $"Game Vsync: {(_device.EnableDeviceVsync ? "On" : "Off")}";
  188. _titleEvent = true;
  189. SwapBuffers();
  190. _device.System.SignalVsync();
  191. _device.VsyncEvent.Set();
  192. }
  193. protected override void OnUnload(EventArgs e)
  194. {
  195. _renderThread.Join();
  196. base.OnUnload(e);
  197. }
  198. protected override void OnResize(EventArgs e)
  199. {
  200. _resizeEvent = true;
  201. }
  202. protected override void OnKeyDown(KeyboardKeyEventArgs e)
  203. {
  204. bool toggleFullscreen = e.Key == Key.F11 ||
  205. (e.Modifiers.HasFlag(KeyModifiers.Alt) && e.Key == Key.Enter);
  206. if (WindowState == WindowState.Fullscreen)
  207. {
  208. if (e.Key == Key.Escape || toggleFullscreen)
  209. {
  210. WindowState = WindowState.Normal;
  211. }
  212. }
  213. else
  214. {
  215. if (e.Key == Key.Escape)
  216. {
  217. Exit();
  218. }
  219. if (toggleFullscreen)
  220. {
  221. WindowState = WindowState.Fullscreen;
  222. }
  223. }
  224. _keyboard = e.Keyboard;
  225. }
  226. protected override void OnKeyUp(KeyboardKeyEventArgs e)
  227. {
  228. _keyboard = e.Keyboard;
  229. }
  230. protected override void OnMouseDown(MouseButtonEventArgs e)
  231. {
  232. _mouse = e.Mouse;
  233. }
  234. protected override void OnMouseUp(MouseButtonEventArgs e)
  235. {
  236. _mouse = e.Mouse;
  237. }
  238. protected override void OnMouseMove(MouseMoveEventArgs e)
  239. {
  240. _mouse = e.Mouse;
  241. }
  242. }
  243. }