GLScreen.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. using OpenTK;
  2. using OpenTK.Graphics;
  3. using OpenTK.Input;
  4. using Ryujinx.Graphics.Gal;
  5. using Ryujinx.HLE;
  6. using Ryujinx.HLE.Input;
  7. using Ryujinx.UI.Input;
  8. using System;
  9. using System.Threading;
  10. using Stopwatch = System.Diagnostics.Stopwatch;
  11. namespace Ryujinx
  12. {
  13. public class GLScreen : GameWindow
  14. {
  15. private const int TouchScreenWidth = 1280;
  16. private const int TouchScreenHeight = 720;
  17. private const int TargetFPS = 60;
  18. private Switch Ns;
  19. private IGalRenderer Renderer;
  20. private KeyboardState? Keyboard = null;
  21. private MouseState? Mouse = null;
  22. private Thread RenderThread;
  23. private bool ResizeEvent;
  24. private bool TitleEvent;
  25. private string NewTitle;
  26. public GLScreen(Switch Ns, IGalRenderer Renderer)
  27. : base(1280, 720,
  28. new GraphicsMode(), "Ryujinx", 0,
  29. DisplayDevice.Default, 3, 3,
  30. GraphicsContextFlags.ForwardCompatible)
  31. {
  32. this.Ns = Ns;
  33. this.Renderer = Renderer;
  34. Location = new Point(
  35. (DisplayDevice.Default.Width / 2) - (Width / 2),
  36. (DisplayDevice.Default.Height / 2) - (Height / 2));
  37. }
  38. private void RenderLoop()
  39. {
  40. MakeCurrent();
  41. Stopwatch Chrono = new Stopwatch();
  42. Chrono.Start();
  43. long TicksPerFrame = Stopwatch.Frequency / TargetFPS;
  44. long Ticks = 0;
  45. while (Exists && !IsExiting)
  46. {
  47. if (Ns.WaitFifo())
  48. {
  49. Ns.ProcessFrame();
  50. }
  51. Renderer.RunActions();
  52. if (ResizeEvent)
  53. {
  54. ResizeEvent = false;
  55. Renderer.FrameBuffer.SetWindowSize(Width, Height);
  56. }
  57. Ticks += Chrono.ElapsedTicks;
  58. Chrono.Restart();
  59. if (Ticks >= TicksPerFrame)
  60. {
  61. RenderFrame();
  62. //Queue max. 1 vsync
  63. Ticks = Math.Min(Ticks - TicksPerFrame, TicksPerFrame);
  64. }
  65. }
  66. }
  67. public void MainLoop()
  68. {
  69. VSync = VSyncMode.Off;
  70. Visible = true;
  71. Renderer.FrameBuffer.SetWindowSize(Width, Height);
  72. Context.MakeCurrent(null);
  73. //OpenTK doesn't like sleeps in its thread, to avoid this a renderer thread is created
  74. RenderThread = new Thread(RenderLoop);
  75. RenderThread.Start();
  76. while (Exists && !IsExiting)
  77. {
  78. ProcessEvents();
  79. if (!IsExiting)
  80. {
  81. UpdateFrame();
  82. if (TitleEvent)
  83. {
  84. TitleEvent = false;
  85. Title = NewTitle;
  86. }
  87. }
  88. //Polling becomes expensive if it's not slept
  89. Thread.Sleep(1);
  90. }
  91. }
  92. private new void UpdateFrame()
  93. {
  94. HidControllerButtons CurrentButton = 0;
  95. HidJoystickPosition LeftJoystick;
  96. HidJoystickPosition RightJoystick;
  97. int LeftJoystickDX = 0;
  98. int LeftJoystickDY = 0;
  99. int RightJoystickDX = 0;
  100. int RightJoystickDY = 0;
  101. //Keyboard Input
  102. if (Keyboard.HasValue)
  103. {
  104. KeyboardState Keyboard = this.Keyboard.Value;
  105. CurrentButton = Config.JoyConKeyboard.GetButtons(Keyboard);
  106. (LeftJoystickDX, LeftJoystickDY) = Config.JoyConKeyboard.GetLeftStick(Keyboard);
  107. (RightJoystickDX, RightJoystickDY) = Config.JoyConKeyboard.GetRightStick(Keyboard);
  108. }
  109. //Controller Input
  110. CurrentButton |= Config.JoyConController.GetButtons();
  111. //Keyboard has priority stick-wise
  112. if (LeftJoystickDX == 0 && LeftJoystickDY == 0)
  113. {
  114. (LeftJoystickDX, LeftJoystickDY) = Config.JoyConController.GetLeftStick();
  115. }
  116. if (RightJoystickDX == 0 && RightJoystickDY == 0)
  117. {
  118. (RightJoystickDX, RightJoystickDY) = Config.JoyConController.GetRightStick();
  119. }
  120. LeftJoystick = new HidJoystickPosition
  121. {
  122. DX = LeftJoystickDX,
  123. DY = LeftJoystickDY
  124. };
  125. RightJoystick = new HidJoystickPosition
  126. {
  127. DX = RightJoystickDX,
  128. DY = RightJoystickDY
  129. };
  130. bool HasTouch = false;
  131. //Get screen touch position from left mouse click
  132. //OpenTK always captures mouse events, even if out of focus, so check if window is focused.
  133. if (Focused && Mouse?.LeftButton == ButtonState.Pressed)
  134. {
  135. MouseState Mouse = this.Mouse.Value;
  136. int ScrnWidth = Width;
  137. int ScrnHeight = Height;
  138. if (Width > (Height * TouchScreenWidth) / TouchScreenHeight)
  139. {
  140. ScrnWidth = (Height * TouchScreenWidth) / TouchScreenHeight;
  141. }
  142. else
  143. {
  144. ScrnHeight = (Width * TouchScreenHeight) / TouchScreenWidth;
  145. }
  146. int StartX = (Width - ScrnWidth) >> 1;
  147. int StartY = (Height - ScrnHeight) >> 1;
  148. int EndX = StartX + ScrnWidth;
  149. int EndY = StartY + ScrnHeight;
  150. if (Mouse.X >= StartX &&
  151. Mouse.Y >= StartY &&
  152. Mouse.X < EndX &&
  153. Mouse.Y < EndY)
  154. {
  155. int ScrnMouseX = Mouse.X - StartX;
  156. int ScrnMouseY = Mouse.Y - StartY;
  157. int MX = (ScrnMouseX * TouchScreenWidth) / ScrnWidth;
  158. int MY = (ScrnMouseY * TouchScreenHeight) / ScrnHeight;
  159. HidTouchPoint CurrentPoint = new HidTouchPoint
  160. {
  161. X = MX,
  162. Y = MY,
  163. //Placeholder values till more data is acquired
  164. DiameterX = 10,
  165. DiameterY = 10,
  166. Angle = 90
  167. };
  168. HasTouch = true;
  169. Ns.Hid.SetTouchPoints(CurrentPoint);
  170. }
  171. }
  172. if (!HasTouch)
  173. {
  174. Ns.Hid.SetTouchPoints();
  175. }
  176. Ns.Hid.SetJoyconButton(
  177. HidControllerId.CONTROLLER_HANDHELD,
  178. HidControllerLayouts.Handheld_Joined,
  179. CurrentButton,
  180. LeftJoystick,
  181. RightJoystick);
  182. Ns.Hid.SetJoyconButton(
  183. HidControllerId.CONTROLLER_HANDHELD,
  184. HidControllerLayouts.Main,
  185. CurrentButton,
  186. LeftJoystick,
  187. RightJoystick);
  188. }
  189. private new void RenderFrame()
  190. {
  191. Renderer.FrameBuffer.Render();
  192. Ns.Statistics.RecordSystemFrameTime();
  193. double HostFps = Ns.Statistics.GetSystemFrameRate();
  194. double GameFps = Ns.Statistics.GetGameFrameRate();
  195. NewTitle = $"Ryujinx | Host FPS: {HostFps:0.0} | Game FPS: {GameFps:0.0}";
  196. TitleEvent = true;
  197. SwapBuffers();
  198. Ns.Os.SignalVsync();
  199. }
  200. protected override void OnUnload(EventArgs e)
  201. {
  202. RenderThread.Join();
  203. base.OnUnload(e);
  204. }
  205. protected override void OnResize(EventArgs e)
  206. {
  207. ResizeEvent = true;
  208. }
  209. protected override void OnKeyDown(KeyboardKeyEventArgs e)
  210. {
  211. bool ToggleFullscreen = e.Key == Key.F11 ||
  212. (e.Modifiers.HasFlag(KeyModifiers.Alt) && e.Key == Key.Enter);
  213. if (WindowState == WindowState.Fullscreen)
  214. {
  215. if (e.Key == Key.Escape || ToggleFullscreen)
  216. {
  217. WindowState = WindowState.Normal;
  218. }
  219. }
  220. else
  221. {
  222. if (e.Key == Key.Escape)
  223. {
  224. Exit();
  225. }
  226. if (ToggleFullscreen)
  227. {
  228. WindowState = WindowState.Fullscreen;
  229. }
  230. }
  231. Keyboard = e.Keyboard;
  232. }
  233. protected override void OnKeyUp(KeyboardKeyEventArgs e)
  234. {
  235. Keyboard = e.Keyboard;
  236. }
  237. protected override void OnMouseDown(MouseButtonEventArgs e)
  238. {
  239. Mouse = e.Mouse;
  240. }
  241. protected override void OnMouseUp(MouseButtonEventArgs e)
  242. {
  243. Mouse = e.Mouse;
  244. }
  245. protected override void OnMouseMove(MouseMoveEventArgs e)
  246. {
  247. Mouse = e.Mouse;
  248. }
  249. }
  250. }