GLScreen.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. HidKeyboard? hidKeyboard = null;
  108. int leftJoystickDx = 0;
  109. int leftJoystickDy = 0;
  110. int rightJoystickDx = 0;
  111. int rightJoystickDy = 0;
  112. //Keyboard Input
  113. if (_keyboard.HasValue)
  114. {
  115. KeyboardState keyboard = _keyboard.Value;
  116. #if USE_PROFILING
  117. // Profiler input, lets the profiler get access to the main windows keyboard state
  118. _profileWindow.UpdateKeyInput(keyboard);
  119. #endif
  120. // Normal Input
  121. currentHotkeyButtons = Configuration.Instance.KeyboardControls.GetHotkeyButtons(keyboard);
  122. currentButton = Configuration.Instance.KeyboardControls.GetButtons(keyboard);
  123. if (Configuration.Instance.EnableKeyboard)
  124. {
  125. hidKeyboard = Configuration.Instance.KeyboardControls.GetKeysDown(keyboard);
  126. }
  127. (leftJoystickDx, leftJoystickDy) = Configuration.Instance.KeyboardControls.GetLeftStick(keyboard);
  128. (rightJoystickDx, rightJoystickDy) = Configuration.Instance.KeyboardControls.GetRightStick(keyboard);
  129. }
  130. if (!hidKeyboard.HasValue)
  131. {
  132. hidKeyboard = new HidKeyboard
  133. {
  134. Modifier = 0,
  135. Keys = new int[0x8]
  136. };
  137. }
  138. currentButton |= Configuration.Instance.GamepadControls.GetButtons();
  139. //Keyboard has priority stick-wise
  140. if (leftJoystickDx == 0 && leftJoystickDy == 0)
  141. {
  142. (leftJoystickDx, leftJoystickDy) = Configuration.Instance.GamepadControls.GetLeftStick();
  143. }
  144. if (rightJoystickDx == 0 && rightJoystickDy == 0)
  145. {
  146. (rightJoystickDx, rightJoystickDy) = Configuration.Instance.GamepadControls.GetRightStick();
  147. }
  148. leftJoystick = new HidJoystickPosition
  149. {
  150. Dx = leftJoystickDx,
  151. Dy = leftJoystickDy
  152. };
  153. rightJoystick = new HidJoystickPosition
  154. {
  155. Dx = rightJoystickDx,
  156. Dy = rightJoystickDy
  157. };
  158. currentButton |= _device.Hid.UpdateStickButtons(leftJoystick, rightJoystick);
  159. bool hasTouch = false;
  160. //Get screen touch position from left mouse click
  161. //OpenTK always captures mouse events, even if out of focus, so check if window is focused.
  162. if (Focused && _mouse?.LeftButton == ButtonState.Pressed)
  163. {
  164. MouseState mouse = _mouse.Value;
  165. int scrnWidth = Width;
  166. int scrnHeight = Height;
  167. if (Width > (Height * TouchScreenWidth) / TouchScreenHeight)
  168. {
  169. scrnWidth = (Height * TouchScreenWidth) / TouchScreenHeight;
  170. }
  171. else
  172. {
  173. scrnHeight = (Width * TouchScreenHeight) / TouchScreenWidth;
  174. }
  175. int startX = (Width - scrnWidth) >> 1;
  176. int startY = (Height - scrnHeight) >> 1;
  177. int endX = startX + scrnWidth;
  178. int endY = startY + scrnHeight;
  179. if (mouse.X >= startX &&
  180. mouse.Y >= startY &&
  181. mouse.X < endX &&
  182. mouse.Y < endY)
  183. {
  184. int scrnMouseX = mouse.X - startX;
  185. int scrnMouseY = mouse.Y - startY;
  186. int mX = (scrnMouseX * TouchScreenWidth) / scrnWidth;
  187. int mY = (scrnMouseY * TouchScreenHeight) / scrnHeight;
  188. HidTouchPoint currentPoint = new HidTouchPoint
  189. {
  190. X = mX,
  191. Y = mY,
  192. //Placeholder values till more data is acquired
  193. DiameterX = 10,
  194. DiameterY = 10,
  195. Angle = 90
  196. };
  197. hasTouch = true;
  198. _device.Hid.SetTouchPoints(currentPoint);
  199. }
  200. }
  201. if (!hasTouch)
  202. {
  203. _device.Hid.SetTouchPoints();
  204. }
  205. if (Configuration.Instance.EnableKeyboard && hidKeyboard.HasValue)
  206. {
  207. _device.Hid.WriteKeyboard(hidKeyboard.Value);
  208. }
  209. HidControllerBase controller = _device.Hid.PrimaryController;
  210. controller.SendInput(currentButton, leftJoystick, rightJoystick);
  211. // Toggle vsync
  212. if (currentHotkeyButtons.HasFlag(HidHotkeyButtons.ToggleVSync) &&
  213. !_prevHotkeyButtons.HasFlag(HidHotkeyButtons.ToggleVSync))
  214. {
  215. _device.EnableDeviceVsync = !_device.EnableDeviceVsync;
  216. }
  217. _prevHotkeyButtons = currentHotkeyButtons;
  218. }
  219. private new void RenderFrame()
  220. {
  221. _renderer.RenderTarget.Render();
  222. _device.Statistics.RecordSystemFrameTime();
  223. double hostFps = _device.Statistics.GetSystemFrameRate();
  224. double gameFps = _device.Statistics.GetGameFrameRate();
  225. string titleSection = string.IsNullOrWhiteSpace(_device.System.CurrentTitle) ? string.Empty
  226. : " | " + _device.System.CurrentTitle;
  227. _newTitle = $"Ryujinx{titleSection} | Host FPS: {hostFps:0.0} | Game FPS: {gameFps:0.0} | " +
  228. $"Game Vsync: {(_device.EnableDeviceVsync ? "On" : "Off")}";
  229. _titleEvent = true;
  230. SwapBuffers();
  231. _device.System.SignalVsync();
  232. _device.VsyncEvent.Set();
  233. }
  234. protected override void OnUnload(EventArgs e)
  235. {
  236. #if USE_PROFILING
  237. _profileWindow.Close();
  238. #endif
  239. _renderThread.Join();
  240. base.OnUnload(e);
  241. }
  242. protected override void OnResize(EventArgs e)
  243. {
  244. _resizeEvent = true;
  245. }
  246. protected override void OnKeyDown(KeyboardKeyEventArgs e)
  247. {
  248. bool toggleFullscreen = e.Key == Key.F11 ||
  249. (e.Modifiers.HasFlag(KeyModifiers.Alt) && e.Key == Key.Enter);
  250. if (WindowState == WindowState.Fullscreen)
  251. {
  252. if (e.Key == Key.Escape || toggleFullscreen)
  253. {
  254. WindowState = WindowState.Normal;
  255. }
  256. }
  257. else
  258. {
  259. if (e.Key == Key.Escape)
  260. {
  261. Exit();
  262. }
  263. if (toggleFullscreen)
  264. {
  265. WindowState = WindowState.Fullscreen;
  266. }
  267. }
  268. _keyboard = e.Keyboard;
  269. }
  270. protected override void OnKeyUp(KeyboardKeyEventArgs e)
  271. {
  272. _keyboard = e.Keyboard;
  273. }
  274. protected override void OnMouseDown(MouseButtonEventArgs e)
  275. {
  276. _mouse = e.Mouse;
  277. }
  278. protected override void OnMouseUp(MouseButtonEventArgs e)
  279. {
  280. _mouse = e.Mouse;
  281. }
  282. protected override void OnMouseMove(MouseMoveEventArgs e)
  283. {
  284. _mouse = e.Mouse;
  285. }
  286. }
  287. }