GLScreen.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. _renderThread.Start();
  89. while (Exists && !IsExiting)
  90. {
  91. ProcessEvents();
  92. if (!IsExiting)
  93. {
  94. UpdateFrame();
  95. if (_titleEvent)
  96. {
  97. _titleEvent = false;
  98. Title = _newTitle;
  99. }
  100. }
  101. // Polling becomes expensive if it's not slept
  102. Thread.Sleep(1);
  103. }
  104. }
  105. private new void UpdateFrame()
  106. {
  107. HotkeyButtons currentHotkeyButtons = 0;
  108. ControllerButtons currentButton = 0;
  109. JoystickPosition leftJoystick;
  110. JoystickPosition rightJoystick;
  111. HLE.Input.Keyboard? hidKeyboard = null;
  112. int leftJoystickDx = 0;
  113. int leftJoystickDy = 0;
  114. int rightJoystickDx = 0;
  115. int rightJoystickDy = 0;
  116. // Keyboard Input
  117. if (_keyboard.HasValue)
  118. {
  119. KeyboardState keyboard = _keyboard.Value;
  120. #if USE_PROFILING
  121. // Profiler input, lets the profiler get access to the main windows keyboard state
  122. _profileWindow.UpdateKeyInput(keyboard);
  123. #endif
  124. // Normal Input
  125. currentHotkeyButtons = KeyboardControls.GetHotkeyButtons(ConfigurationState.Instance.Hid.KeyboardControls, keyboard);
  126. currentButton = KeyboardControls.GetButtons(ConfigurationState.Instance.Hid.KeyboardControls, keyboard);
  127. if (ConfigurationState.Instance.Hid.EnableKeyboard)
  128. {
  129. hidKeyboard = KeyboardControls.GetKeysDown(ConfigurationState.Instance.Hid.KeyboardControls, keyboard);
  130. }
  131. (leftJoystickDx, leftJoystickDy) = KeyboardControls.GetLeftStick(ConfigurationState.Instance.Hid.KeyboardControls, keyboard);
  132. (rightJoystickDx, rightJoystickDy) = KeyboardControls.GetRightStick(ConfigurationState.Instance.Hid.KeyboardControls, keyboard);
  133. }
  134. if (!hidKeyboard.HasValue)
  135. {
  136. hidKeyboard = new HLE.Input.Keyboard
  137. {
  138. Modifier = 0,
  139. Keys = new int[0x8]
  140. };
  141. }
  142. currentButton |= _primaryController.GetButtons();
  143. // Keyboard has priority stick-wise
  144. if (leftJoystickDx == 0 && leftJoystickDy == 0)
  145. {
  146. (leftJoystickDx, leftJoystickDy) = _primaryController.GetLeftStick();
  147. }
  148. if (rightJoystickDx == 0 && rightJoystickDy == 0)
  149. {
  150. (rightJoystickDx, rightJoystickDy) = _primaryController.GetRightStick();
  151. }
  152. leftJoystick = new JoystickPosition
  153. {
  154. Dx = leftJoystickDx,
  155. Dy = leftJoystickDy
  156. };
  157. rightJoystick = new JoystickPosition
  158. {
  159. Dx = rightJoystickDx,
  160. Dy = rightJoystickDy
  161. };
  162. currentButton |= _device.Hid.UpdateStickButtons(leftJoystick, rightJoystick);
  163. bool hasTouch = false;
  164. // Get screen touch position from left mouse click
  165. // OpenTK always captures mouse events, even if out of focus, so check if window is focused.
  166. if (Focused && _mouse?.LeftButton == ButtonState.Pressed)
  167. {
  168. MouseState mouse = _mouse.Value;
  169. int scrnWidth = Width;
  170. int scrnHeight = Height;
  171. if (Width > (Height * TouchScreenWidth) / TouchScreenHeight)
  172. {
  173. scrnWidth = (Height * TouchScreenWidth) / TouchScreenHeight;
  174. }
  175. else
  176. {
  177. scrnHeight = (Width * TouchScreenHeight) / TouchScreenWidth;
  178. }
  179. int startX = (Width - scrnWidth) >> 1;
  180. int startY = (Height - scrnHeight) >> 1;
  181. int endX = startX + scrnWidth;
  182. int endY = startY + scrnHeight;
  183. if (mouse.X >= startX &&
  184. mouse.Y >= startY &&
  185. mouse.X < endX &&
  186. mouse.Y < endY)
  187. {
  188. int scrnMouseX = mouse.X - startX;
  189. int scrnMouseY = mouse.Y - startY;
  190. int mX = (scrnMouseX * TouchScreenWidth) / scrnWidth;
  191. int mY = (scrnMouseY * TouchScreenHeight) / scrnHeight;
  192. TouchPoint currentPoint = new TouchPoint
  193. {
  194. X = mX,
  195. Y = mY,
  196. // Placeholder values till more data is acquired
  197. DiameterX = 10,
  198. DiameterY = 10,
  199. Angle = 90
  200. };
  201. hasTouch = true;
  202. _device.Hid.SetTouchPoints(currentPoint);
  203. }
  204. }
  205. if (!hasTouch)
  206. {
  207. _device.Hid.SetTouchPoints();
  208. }
  209. if (ConfigurationState.Instance.Hid.EnableKeyboard && hidKeyboard.HasValue)
  210. {
  211. _device.Hid.WriteKeyboard(hidKeyboard.Value);
  212. }
  213. BaseController controller = _device.Hid.PrimaryController;
  214. controller.SendInput(currentButton, leftJoystick, rightJoystick);
  215. // Toggle vsync
  216. if (currentHotkeyButtons.HasFlag(HotkeyButtons.ToggleVSync) &&
  217. !_prevHotkeyButtons.HasFlag(HotkeyButtons.ToggleVSync))
  218. {
  219. _device.EnableDeviceVsync = !_device.EnableDeviceVsync;
  220. }
  221. _prevHotkeyButtons = currentHotkeyButtons;
  222. }
  223. private new void RenderFrame()
  224. {
  225. _device.PresentFrame(SwapBuffers);
  226. _device.Statistics.RecordSystemFrameTime();
  227. double hostFps = _device.Statistics.GetSystemFrameRate();
  228. double gameFps = _device.Statistics.GetGameFrameRate();
  229. string titleNameSection = string.IsNullOrWhiteSpace(_device.System.TitleName) ? string.Empty
  230. : " | " + _device.System.TitleName;
  231. string titleIDSection = string.IsNullOrWhiteSpace(_device.System.TitleId) ? string.Empty
  232. : " | " + _device.System.TitleId.ToUpper();
  233. _newTitle = $"Ryujinx{titleNameSection}{titleIDSection} | Host FPS: {hostFps:0.0} | Game FPS: {gameFps:0.0} | " +
  234. $"Game Vsync: {(_device.EnableDeviceVsync ? "On" : "Off")}";
  235. _titleEvent = true;
  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. }