GLScreen.cs 7.5 KB

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