GLScreen.cs 11 KB

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