GLScreen.cs 7.1 KB

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