GLScreen.cs 11 KB

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