GLScreen.cs 8.9 KB

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