GLScreen.cs 11 KB

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