GLScreen.cs 7.5 KB

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