GLScreen.cs 11 KB

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