GLScreen.cs 10 KB

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