GLScreen.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. namespace Ryujinx
  9. {
  10. public class GLScreen : GameWindow
  11. {
  12. private const int TouchScreenWidth = 1280;
  13. private const int TouchScreenHeight = 720;
  14. private const float TouchScreenRatioX = (float)TouchScreenWidth / TouchScreenHeight;
  15. private const float TouchScreenRatioY = (float)TouchScreenHeight / TouchScreenWidth;
  16. private Switch Ns;
  17. private IGalRenderer Renderer;
  18. private KeyboardState? Keyboard = null;
  19. private MouseState? Mouse = null;
  20. public GLScreen(Switch Ns, IGalRenderer Renderer)
  21. : base(1280, 720,
  22. new GraphicsMode(), "Ryujinx", 0,
  23. DisplayDevice.Default, 3, 3,
  24. GraphicsContextFlags.ForwardCompatible)
  25. {
  26. this.Ns = Ns;
  27. this.Renderer = Renderer;
  28. Location = new Point(
  29. (DisplayDevice.Default.Width / 2) - (Width / 2),
  30. (DisplayDevice.Default.Height / 2) - (Height / 2));
  31. }
  32. protected override void OnLoad(EventArgs e)
  33. {
  34. VSync = VSyncMode.On;
  35. Renderer.FrameBuffer.SetWindowSize(Width, Height);
  36. }
  37. protected override void OnUpdateFrame(FrameEventArgs e)
  38. {
  39. HidControllerButtons CurrentButton = 0;
  40. HidJoystickPosition LeftJoystick;
  41. HidJoystickPosition RightJoystick;
  42. int LeftJoystickDX = 0;
  43. int LeftJoystickDY = 0;
  44. int RightJoystickDX = 0;
  45. int RightJoystickDY = 0;
  46. if (Keyboard.HasValue)
  47. {
  48. KeyboardState Keyboard = this.Keyboard.Value;
  49. if (Keyboard[Key.Escape]) this.Exit();
  50. //RightJoystick
  51. if (Keyboard[(Key)Config.FakeJoyCon.Left.StickUp]) LeftJoystickDY = short.MaxValue;
  52. if (Keyboard[(Key)Config.FakeJoyCon.Left.StickDown]) LeftJoystickDY = -short.MaxValue;
  53. if (Keyboard[(Key)Config.FakeJoyCon.Left.StickLeft]) LeftJoystickDX = -short.MaxValue;
  54. if (Keyboard[(Key)Config.FakeJoyCon.Left.StickRight]) LeftJoystickDX = short.MaxValue;
  55. //LeftButtons
  56. if (Keyboard[(Key)Config.FakeJoyCon.Left.StickButton]) CurrentButton |= HidControllerButtons.KEY_LSTICK;
  57. if (Keyboard[(Key)Config.FakeJoyCon.Left.DPadUp]) CurrentButton |= HidControllerButtons.KEY_DUP;
  58. if (Keyboard[(Key)Config.FakeJoyCon.Left.DPadDown]) CurrentButton |= HidControllerButtons.KEY_DDOWN;
  59. if (Keyboard[(Key)Config.FakeJoyCon.Left.DPadLeft]) CurrentButton |= HidControllerButtons.KEY_DLEFT;
  60. if (Keyboard[(Key)Config.FakeJoyCon.Left.DPadRight]) CurrentButton |= HidControllerButtons.KEY_DRIGHT;
  61. if (Keyboard[(Key)Config.FakeJoyCon.Left.ButtonMinus]) CurrentButton |= HidControllerButtons.KEY_MINUS;
  62. if (Keyboard[(Key)Config.FakeJoyCon.Left.ButtonL]) CurrentButton |= HidControllerButtons.KEY_L;
  63. if (Keyboard[(Key)Config.FakeJoyCon.Left.ButtonZL]) CurrentButton |= HidControllerButtons.KEY_ZL;
  64. //RightJoystick
  65. if (Keyboard[(Key)Config.FakeJoyCon.Right.StickUp]) RightJoystickDY = short.MaxValue;
  66. if (Keyboard[(Key)Config.FakeJoyCon.Right.StickDown]) RightJoystickDY = -short.MaxValue;
  67. if (Keyboard[(Key)Config.FakeJoyCon.Right.StickLeft]) RightJoystickDX = -short.MaxValue;
  68. if (Keyboard[(Key)Config.FakeJoyCon.Right.StickRight]) RightJoystickDX = short.MaxValue;
  69. //RightButtons
  70. if (Keyboard[(Key)Config.FakeJoyCon.Right.StickButton]) CurrentButton |= HidControllerButtons.KEY_RSTICK;
  71. if (Keyboard[(Key)Config.FakeJoyCon.Right.ButtonA]) CurrentButton |= HidControllerButtons.KEY_A;
  72. if (Keyboard[(Key)Config.FakeJoyCon.Right.ButtonB]) CurrentButton |= HidControllerButtons.KEY_B;
  73. if (Keyboard[(Key)Config.FakeJoyCon.Right.ButtonX]) CurrentButton |= HidControllerButtons.KEY_X;
  74. if (Keyboard[(Key)Config.FakeJoyCon.Right.ButtonY]) CurrentButton |= HidControllerButtons.KEY_Y;
  75. if (Keyboard[(Key)Config.FakeJoyCon.Right.ButtonPlus]) CurrentButton |= HidControllerButtons.KEY_PLUS;
  76. if (Keyboard[(Key)Config.FakeJoyCon.Right.ButtonR]) CurrentButton |= HidControllerButtons.KEY_R;
  77. if (Keyboard[(Key)Config.FakeJoyCon.Right.ButtonZR]) CurrentButton |= HidControllerButtons.KEY_ZR;
  78. }
  79. LeftJoystick = new HidJoystickPosition
  80. {
  81. DX = LeftJoystickDX,
  82. DY = LeftJoystickDY
  83. };
  84. RightJoystick = new HidJoystickPosition
  85. {
  86. DX = RightJoystickDX,
  87. DY = RightJoystickDY
  88. };
  89. bool HasTouch = false;
  90. //Get screen touch position from left mouse click
  91. //OpenTK always captures mouse events, even if out of focus, so check if window is focused.
  92. if (Focused && Mouse?.LeftButton == ButtonState.Pressed)
  93. {
  94. MouseState Mouse = this.Mouse.Value;
  95. int ScrnWidth = Width;
  96. int ScrnHeight = Height;
  97. if (Width > Height * TouchScreenRatioX)
  98. {
  99. ScrnWidth = (int)(Height * TouchScreenRatioX);
  100. }
  101. else
  102. {
  103. ScrnHeight = (int)(Width * TouchScreenRatioY);
  104. }
  105. int StartX = (Width - ScrnWidth) >> 1;
  106. int StartY = (Height - ScrnHeight) >> 1;
  107. int EndX = StartX + ScrnWidth;
  108. int EndY = StartY + ScrnHeight;
  109. if (Mouse.X >= StartX &&
  110. Mouse.Y >= StartY &&
  111. Mouse.X < EndX &&
  112. Mouse.Y < EndY)
  113. {
  114. int ScrnMouseX = Mouse.X - StartX;
  115. int ScrnMouseY = Mouse.Y - StartY;
  116. int MX = (int)(((float)ScrnMouseX / ScrnWidth) * TouchScreenWidth);
  117. int MY = (int)(((float)ScrnMouseY / ScrnHeight) * TouchScreenHeight);
  118. HidTouchPoint CurrentPoint = new HidTouchPoint
  119. {
  120. X = MX,
  121. Y = MY,
  122. //Placeholder values till more data is acquired
  123. DiameterX = 10,
  124. DiameterY = 10,
  125. Angle = 90
  126. };
  127. HasTouch = true;
  128. Ns.Hid.SetTouchPoints(CurrentPoint);
  129. }
  130. }
  131. if (!HasTouch)
  132. {
  133. Ns.Hid.SetTouchPoints();
  134. }
  135. Ns.Hid.SetJoyconButton(
  136. HidControllerId.CONTROLLER_HANDHELD,
  137. HidControllerLayouts.Handheld_Joined,
  138. CurrentButton,
  139. LeftJoystick,
  140. RightJoystick);
  141. Ns.Hid.SetJoyconButton(
  142. HidControllerId.CONTROLLER_HANDHELD,
  143. HidControllerLayouts.Main,
  144. CurrentButton,
  145. LeftJoystick,
  146. RightJoystick);
  147. Ns.ProcessFrame();
  148. Renderer.RunActions();
  149. }
  150. protected override void OnRenderFrame(FrameEventArgs e)
  151. {
  152. Renderer.FrameBuffer.Render();
  153. Ns.Statistics.RecordSystemFrameTime();
  154. double HostFps = Ns.Statistics.GetSystemFrameRate();
  155. double GameFps = Ns.Statistics.GetGameFrameRate();
  156. Title = $"Ryujinx | Host FPS: {HostFps:0.0} | Game FPS: {GameFps:0.0}";
  157. SwapBuffers();
  158. Ns.Os.SignalVsync();
  159. }
  160. protected override void OnResize(EventArgs e)
  161. {
  162. Renderer.FrameBuffer.SetWindowSize(Width, Height);
  163. }
  164. protected override void OnKeyDown(KeyboardKeyEventArgs e)
  165. {
  166. Keyboard = e.Keyboard;
  167. }
  168. protected override void OnKeyUp(KeyboardKeyEventArgs e)
  169. {
  170. Keyboard = e.Keyboard;
  171. }
  172. protected override void OnMouseDown(MouseButtonEventArgs e)
  173. {
  174. Mouse = e.Mouse;
  175. }
  176. protected override void OnMouseUp(MouseButtonEventArgs e)
  177. {
  178. Mouse = e.Mouse;
  179. }
  180. protected override void OnMouseMove(MouseMoveEventArgs e)
  181. {
  182. Mouse = e.Mouse;
  183. }
  184. }
  185. }