GLScreen.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 float TouchScreenRatioX = (float)TouchScreenWidth / TouchScreenHeight;
  17. private const float TouchScreenRatioY = (float)TouchScreenHeight / TouchScreenWidth;
  18. private const int TargetFPS = 60;
  19. private Switch Ns;
  20. private IGalRenderer Renderer;
  21. private KeyboardState? Keyboard = null;
  22. private MouseState? Mouse = null;
  23. private Thread RenderThread;
  24. private bool ResizeEvent;
  25. private bool TitleEvent;
  26. private string NewTitle;
  27. public GLScreen(Switch Ns, IGalRenderer Renderer)
  28. : base(1280, 720,
  29. new GraphicsMode(), "Ryujinx", 0,
  30. DisplayDevice.Default, 3, 3,
  31. GraphicsContextFlags.ForwardCompatible)
  32. {
  33. this.Ns = Ns;
  34. this.Renderer = Renderer;
  35. Location = new Point(
  36. (DisplayDevice.Default.Width / 2) - (Width / 2),
  37. (DisplayDevice.Default.Height / 2) - (Height / 2));
  38. ResizeEvent = false;
  39. TitleEvent = false;
  40. }
  41. private void RenderLoop()
  42. {
  43. MakeCurrent();
  44. Stopwatch Chrono = new Stopwatch();
  45. Chrono.Start();
  46. long TicksPerFrame = Stopwatch.Frequency / TargetFPS;
  47. long Ticks = 0;
  48. while (Exists && !IsExiting)
  49. {
  50. if (Ns.WaitFifo())
  51. {
  52. Ns.ProcessFrame();
  53. }
  54. Renderer.RunActions();
  55. if (ResizeEvent)
  56. {
  57. ResizeEvent = false;
  58. Renderer.FrameBuffer.SetWindowSize(Width, Height);
  59. }
  60. Ticks += Chrono.ElapsedTicks;
  61. Chrono.Restart();
  62. if (Ticks >= TicksPerFrame)
  63. {
  64. RenderFrame();
  65. //Queue max. 1 vsync
  66. Ticks = Math.Min(Ticks - TicksPerFrame, TicksPerFrame);
  67. }
  68. }
  69. }
  70. public void MainLoop()
  71. {
  72. VSync = VSyncMode.Off;
  73. Visible = true;
  74. Renderer.FrameBuffer.SetWindowSize(Width, Height);
  75. Context.MakeCurrent(null);
  76. //OpenTK doesn't like sleeps in its thread, to avoid this a renderer thread is created
  77. RenderThread = new Thread(RenderLoop);
  78. RenderThread.Start();
  79. while (Exists && !IsExiting)
  80. {
  81. ProcessEvents();
  82. if (!IsExiting)
  83. {
  84. UpdateFrame();
  85. if (TitleEvent)
  86. {
  87. TitleEvent = false;
  88. Title = NewTitle;
  89. }
  90. }
  91. }
  92. }
  93. private bool IsGamePadButtonPressedFromString(GamePadState GamePad, string Button)
  94. {
  95. if (Button.ToUpper() == "LTRIGGER" || Button.ToUpper() == "RTRIGGER")
  96. {
  97. return GetGamePadTriggerFromString(GamePad, Button) >= Config.GamePadTriggerThreshold;
  98. }
  99. else
  100. {
  101. return (GetGamePadButtonFromString(GamePad, Button) == ButtonState.Pressed);
  102. }
  103. }
  104. private ButtonState GetGamePadButtonFromString(GamePadState GamePad, string Button)
  105. {
  106. switch (Button.ToUpper())
  107. {
  108. case "A": return GamePad.Buttons.A;
  109. case "B": return GamePad.Buttons.B;
  110. case "X": return GamePad.Buttons.X;
  111. case "Y": return GamePad.Buttons.Y;
  112. case "LSTICK": return GamePad.Buttons.LeftStick;
  113. case "RSTICK": return GamePad.Buttons.RightStick;
  114. case "LSHOULDER": return GamePad.Buttons.LeftShoulder;
  115. case "RSHOULDER": return GamePad.Buttons.RightShoulder;
  116. case "DPADUP": return GamePad.DPad.Up;
  117. case "DPADDOWN": return GamePad.DPad.Down;
  118. case "DPADLEFT": return GamePad.DPad.Left;
  119. case "DPADRIGHT": return GamePad.DPad.Right;
  120. case "START": return GamePad.Buttons.Start;
  121. case "BACK": return GamePad.Buttons.Back;
  122. default: throw new ArgumentException();
  123. }
  124. }
  125. private float GetGamePadTriggerFromString(GamePadState GamePad, string Trigger)
  126. {
  127. switch (Trigger.ToUpper())
  128. {
  129. case "LTRIGGER": return GamePad.Triggers.Left;
  130. case "RTRIGGER": return GamePad.Triggers.Right;
  131. default: throw new ArgumentException();
  132. }
  133. }
  134. private Vector2 GetJoystickAxisFromString(GamePadState GamePad, string Joystick)
  135. {
  136. switch (Joystick.ToUpper())
  137. {
  138. case "LJOYSTICK": return GamePad.ThumbSticks.Left;
  139. case "RJOYSTICK": return new Vector2(-GamePad.ThumbSticks.Right.Y, -GamePad.ThumbSticks.Right.X);
  140. default: throw new ArgumentException();
  141. }
  142. }
  143. private new void UpdateFrame()
  144. {
  145. HidControllerButtons CurrentButton = 0;
  146. HidJoystickPosition LeftJoystick;
  147. HidJoystickPosition RightJoystick;
  148. int LeftJoystickDX = 0;
  149. int LeftJoystickDY = 0;
  150. int RightJoystickDX = 0;
  151. int RightJoystickDY = 0;
  152. float AnalogStickDeadzone = Config.GamePadDeadzone;
  153. //Keyboard Input
  154. if (Keyboard.HasValue)
  155. {
  156. KeyboardState Keyboard = this.Keyboard.Value;
  157. if (Keyboard[Key.Escape]) this.Exit();
  158. //LeftJoystick
  159. if (Keyboard[(Key)Config.JoyConKeyboard.Left.StickUp]) LeftJoystickDY = short.MaxValue;
  160. if (Keyboard[(Key)Config.JoyConKeyboard.Left.StickDown]) LeftJoystickDY = -short.MaxValue;
  161. if (Keyboard[(Key)Config.JoyConKeyboard.Left.StickLeft]) LeftJoystickDX = -short.MaxValue;
  162. if (Keyboard[(Key)Config.JoyConKeyboard.Left.StickRight]) LeftJoystickDX = short.MaxValue;
  163. //LeftButtons
  164. if (Keyboard[(Key)Config.JoyConKeyboard.Left.StickButton]) CurrentButton |= HidControllerButtons.KEY_LSTICK;
  165. if (Keyboard[(Key)Config.JoyConKeyboard.Left.DPadUp]) CurrentButton |= HidControllerButtons.KEY_DUP;
  166. if (Keyboard[(Key)Config.JoyConKeyboard.Left.DPadDown]) CurrentButton |= HidControllerButtons.KEY_DDOWN;
  167. if (Keyboard[(Key)Config.JoyConKeyboard.Left.DPadLeft]) CurrentButton |= HidControllerButtons.KEY_DLEFT;
  168. if (Keyboard[(Key)Config.JoyConKeyboard.Left.DPadRight]) CurrentButton |= HidControllerButtons.KEY_DRIGHT;
  169. if (Keyboard[(Key)Config.JoyConKeyboard.Left.ButtonMinus]) CurrentButton |= HidControllerButtons.KEY_MINUS;
  170. if (Keyboard[(Key)Config.JoyConKeyboard.Left.ButtonL]) CurrentButton |= HidControllerButtons.KEY_L;
  171. if (Keyboard[(Key)Config.JoyConKeyboard.Left.ButtonZL]) CurrentButton |= HidControllerButtons.KEY_ZL;
  172. //RightJoystick
  173. if (Keyboard[(Key)Config.JoyConKeyboard.Right.StickUp]) RightJoystickDY = short.MaxValue;
  174. if (Keyboard[(Key)Config.JoyConKeyboard.Right.StickDown]) RightJoystickDY = -short.MaxValue;
  175. if (Keyboard[(Key)Config.JoyConKeyboard.Right.StickLeft]) RightJoystickDX = -short.MaxValue;
  176. if (Keyboard[(Key)Config.JoyConKeyboard.Right.StickRight]) RightJoystickDX = short.MaxValue;
  177. //RightButtons
  178. if (Keyboard[(Key)Config.JoyConKeyboard.Right.StickButton]) CurrentButton |= HidControllerButtons.KEY_RSTICK;
  179. if (Keyboard[(Key)Config.JoyConKeyboard.Right.ButtonA]) CurrentButton |= HidControllerButtons.KEY_A;
  180. if (Keyboard[(Key)Config.JoyConKeyboard.Right.ButtonB]) CurrentButton |= HidControllerButtons.KEY_B;
  181. if (Keyboard[(Key)Config.JoyConKeyboard.Right.ButtonX]) CurrentButton |= HidControllerButtons.KEY_X;
  182. if (Keyboard[(Key)Config.JoyConKeyboard.Right.ButtonY]) CurrentButton |= HidControllerButtons.KEY_Y;
  183. if (Keyboard[(Key)Config.JoyConKeyboard.Right.ButtonPlus]) CurrentButton |= HidControllerButtons.KEY_PLUS;
  184. if (Keyboard[(Key)Config.JoyConKeyboard.Right.ButtonR]) CurrentButton |= HidControllerButtons.KEY_R;
  185. if (Keyboard[(Key)Config.JoyConKeyboard.Right.ButtonZR]) CurrentButton |= HidControllerButtons.KEY_ZR;
  186. }
  187. //Controller Input
  188. if (Config.GamePadEnable)
  189. {
  190. GamePadState GamePad = OpenTK.Input.GamePad.GetState(Config.GamePadIndex);
  191. //LeftButtons
  192. if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Left.DPadUp)) CurrentButton |= HidControllerButtons.KEY_DUP;
  193. if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Left.DPadDown)) CurrentButton |= HidControllerButtons.KEY_DDOWN;
  194. if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Left.DPadLeft)) CurrentButton |= HidControllerButtons.KEY_DLEFT;
  195. if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Left.DPadRight)) CurrentButton |= HidControllerButtons.KEY_DRIGHT;
  196. if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Left.StickButton)) CurrentButton |= HidControllerButtons.KEY_LSTICK;
  197. if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Left.ButtonMinus)) CurrentButton |= HidControllerButtons.KEY_MINUS;
  198. if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Left.ButtonL)) CurrentButton |= HidControllerButtons.KEY_L;
  199. if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Left.ButtonZL)) CurrentButton |= HidControllerButtons.KEY_ZL;
  200. //RightButtons
  201. if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Right.ButtonA)) CurrentButton |= HidControllerButtons.KEY_A;
  202. if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Right.ButtonB)) CurrentButton |= HidControllerButtons.KEY_B;
  203. if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Right.ButtonX)) CurrentButton |= HidControllerButtons.KEY_X;
  204. if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Right.ButtonY)) CurrentButton |= HidControllerButtons.KEY_Y;
  205. if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Right.StickButton)) CurrentButton |= HidControllerButtons.KEY_RSTICK;
  206. if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Right.ButtonPlus)) CurrentButton |= HidControllerButtons.KEY_PLUS;
  207. if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Right.ButtonR)) CurrentButton |= HidControllerButtons.KEY_R;
  208. if (IsGamePadButtonPressedFromString(GamePad, Config.JoyConController.Right.ButtonZR)) CurrentButton |= HidControllerButtons.KEY_ZR;
  209. //LeftJoystick
  210. if (GetJoystickAxisFromString(GamePad, Config.JoyConController.Left.Stick).X >= AnalogStickDeadzone
  211. || GetJoystickAxisFromString(GamePad, Config.JoyConController.Left.Stick).X <= -AnalogStickDeadzone)
  212. LeftJoystickDX = (int)(GetJoystickAxisFromString(GamePad, Config.JoyConController.Left.Stick).X * short.MaxValue);
  213. if (GetJoystickAxisFromString(GamePad, Config.JoyConController.Left.Stick).Y >= AnalogStickDeadzone
  214. || GetJoystickAxisFromString(GamePad, Config.JoyConController.Left.Stick).Y <= -AnalogStickDeadzone)
  215. LeftJoystickDY = (int)(GetJoystickAxisFromString(GamePad, Config.JoyConController.Left.Stick).Y * short.MaxValue);
  216. //RightJoystick
  217. if (GetJoystickAxisFromString(GamePad, Config.JoyConController.Right.Stick).X >= AnalogStickDeadzone
  218. || GetJoystickAxisFromString(GamePad, Config.JoyConController.Right.Stick).X <= -AnalogStickDeadzone)
  219. RightJoystickDX = (int)(GetJoystickAxisFromString(GamePad, Config.JoyConController.Right.Stick).X * short.MaxValue);
  220. if (GetJoystickAxisFromString(GamePad, Config.JoyConController.Right.Stick).Y >= AnalogStickDeadzone
  221. || GetJoystickAxisFromString(GamePad, Config.JoyConController.Right.Stick).Y <= -AnalogStickDeadzone)
  222. RightJoystickDY = (int)(GetJoystickAxisFromString(GamePad, Config.JoyConController.Right.Stick).Y * short.MaxValue);
  223. }
  224. LeftJoystick = new HidJoystickPosition
  225. {
  226. DX = LeftJoystickDX,
  227. DY = LeftJoystickDY
  228. };
  229. RightJoystick = new HidJoystickPosition
  230. {
  231. DX = RightJoystickDX,
  232. DY = RightJoystickDY
  233. };
  234. bool HasTouch = false;
  235. //Get screen touch position from left mouse click
  236. //OpenTK always captures mouse events, even if out of focus, so check if window is focused.
  237. if (Focused && Mouse?.LeftButton == ButtonState.Pressed)
  238. {
  239. MouseState Mouse = this.Mouse.Value;
  240. int ScrnWidth = Width;
  241. int ScrnHeight = Height;
  242. if (Width > Height * TouchScreenRatioX)
  243. {
  244. ScrnWidth = (int)(Height * TouchScreenRatioX);
  245. }
  246. else
  247. {
  248. ScrnHeight = (int)(Width * TouchScreenRatioY);
  249. }
  250. int StartX = (Width - ScrnWidth) >> 1;
  251. int StartY = (Height - ScrnHeight) >> 1;
  252. int EndX = StartX + ScrnWidth;
  253. int EndY = StartY + ScrnHeight;
  254. if (Mouse.X >= StartX &&
  255. Mouse.Y >= StartY &&
  256. Mouse.X < EndX &&
  257. Mouse.Y < EndY)
  258. {
  259. int ScrnMouseX = Mouse.X - StartX;
  260. int ScrnMouseY = Mouse.Y - StartY;
  261. int MX = (int)(((float)ScrnMouseX / ScrnWidth) * TouchScreenWidth);
  262. int MY = (int)(((float)ScrnMouseY / ScrnHeight) * TouchScreenHeight);
  263. HidTouchPoint CurrentPoint = new HidTouchPoint
  264. {
  265. X = MX,
  266. Y = MY,
  267. //Placeholder values till more data is acquired
  268. DiameterX = 10,
  269. DiameterY = 10,
  270. Angle = 90
  271. };
  272. HasTouch = true;
  273. Ns.Hid.SetTouchPoints(CurrentPoint);
  274. }
  275. }
  276. if (!HasTouch)
  277. {
  278. Ns.Hid.SetTouchPoints();
  279. }
  280. Ns.Hid.SetJoyconButton(
  281. HidControllerId.CONTROLLER_HANDHELD,
  282. HidControllerLayouts.Handheld_Joined,
  283. CurrentButton,
  284. LeftJoystick,
  285. RightJoystick);
  286. Ns.Hid.SetJoyconButton(
  287. HidControllerId.CONTROLLER_HANDHELD,
  288. HidControllerLayouts.Main,
  289. CurrentButton,
  290. LeftJoystick,
  291. RightJoystick);
  292. }
  293. private new void RenderFrame()
  294. {
  295. Renderer.FrameBuffer.Render();
  296. Ns.Statistics.RecordSystemFrameTime();
  297. double HostFps = Ns.Statistics.GetSystemFrameRate();
  298. double GameFps = Ns.Statistics.GetGameFrameRate();
  299. NewTitle = $"Ryujinx | Host FPS: {HostFps:0.0} | Game FPS: {GameFps:0.0}";
  300. TitleEvent = true;
  301. SwapBuffers();
  302. Ns.Os.SignalVsync();
  303. }
  304. protected override void OnUnload(EventArgs e)
  305. {
  306. RenderThread.Join();
  307. base.OnUnload(e);
  308. }
  309. protected override void OnResize(EventArgs e)
  310. {
  311. ResizeEvent = true;
  312. }
  313. protected override void OnKeyDown(KeyboardKeyEventArgs e)
  314. {
  315. Keyboard = e.Keyboard;
  316. }
  317. protected override void OnKeyUp(KeyboardKeyEventArgs e)
  318. {
  319. Keyboard = e.Keyboard;
  320. }
  321. protected override void OnMouseDown(MouseButtonEventArgs e)
  322. {
  323. Mouse = e.Mouse;
  324. }
  325. protected override void OnMouseUp(MouseButtonEventArgs e)
  326. {
  327. Mouse = e.Mouse;
  328. }
  329. protected override void OnMouseMove(MouseMoveEventArgs e)
  330. {
  331. Mouse = e.Mouse;
  332. }
  333. }
  334. }