GLScreen.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using OpenTK;
  2. using OpenTK.Graphics;
  3. using OpenTK.Graphics.OpenGL;
  4. using Ryujinx.Core;
  5. using Ryujinx.Graphics.Gal;
  6. using System;
  7. namespace Ryujinx
  8. {
  9. public class GLScreen : GameWindow
  10. {
  11. private Switch Ns;
  12. private IGalRenderer Renderer;
  13. public GLScreen(Switch Ns, IGalRenderer Renderer)
  14. : base(1280, 720,
  15. new GraphicsMode(), "Ryujinx", 0,
  16. DisplayDevice.Default, 3, 3,
  17. GraphicsContextFlags.ForwardCompatible)
  18. {
  19. this.Ns = Ns;
  20. this.Renderer = Renderer;
  21. }
  22. protected override void OnLoad(EventArgs e)
  23. {
  24. VSync = VSyncMode.On;
  25. Renderer.InitializeFrameBuffer();
  26. }
  27. protected override void OnUpdateFrame(FrameEventArgs e)
  28. {
  29. HidControllerKeys CurrentButton = 0;
  30. JoystickPosition LeftJoystick;
  31. JoystickPosition RightJoystick;
  32. if (Keyboard[OpenTK.Input.Key.Escape]) this.Exit();
  33. //RightJoystick
  34. int LeftJoystickDX = 0;
  35. int LeftJoystickDY = 0;
  36. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.StickUp]) LeftJoystickDY = short.MaxValue;
  37. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.StickDown]) LeftJoystickDY = -short.MaxValue;
  38. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.StickLeft]) LeftJoystickDX = -short.MaxValue;
  39. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.StickRight]) LeftJoystickDX = short.MaxValue;
  40. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.StickButton]) CurrentButton |= HidControllerKeys.KEY_LSTICK;
  41. //LeftButtons
  42. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.DPadUp]) CurrentButton |= HidControllerKeys.KEY_DUP;
  43. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.DPadDown]) CurrentButton |= HidControllerKeys.KEY_DDOWN;
  44. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.DPadLeft]) CurrentButton |= HidControllerKeys.KEY_DLEFT;
  45. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.DPadRight]) CurrentButton |= HidControllerKeys.KEY_DRIGHT;
  46. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.ButtonMinus]) CurrentButton |= HidControllerKeys.KEY_MINUS;
  47. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.ButtonL]) CurrentButton |= HidControllerKeys.KEY_L;
  48. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Left.ButtonZL]) CurrentButton |= HidControllerKeys.KEY_ZL;
  49. //RightJoystick
  50. int RightJoystickDX = 0;
  51. int RightJoystickDY = 0;
  52. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.StickUp]) RightJoystickDY = short.MaxValue;
  53. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.StickDown]) RightJoystickDY = -short.MaxValue;
  54. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.StickLeft]) RightJoystickDX = -short.MaxValue;
  55. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.StickRight]) RightJoystickDX = short.MaxValue;
  56. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.StickButton]) CurrentButton |= HidControllerKeys.KEY_RSTICK;
  57. //RightButtons
  58. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonA]) CurrentButton |= HidControllerKeys.KEY_A;
  59. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonB]) CurrentButton |= HidControllerKeys.KEY_B;
  60. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonX]) CurrentButton |= HidControllerKeys.KEY_X;
  61. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonY]) CurrentButton |= HidControllerKeys.KEY_Y;
  62. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonPlus]) CurrentButton |= HidControllerKeys.KEY_PLUS;
  63. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonR]) CurrentButton |= HidControllerKeys.KEY_R;
  64. if (Keyboard[(OpenTK.Input.Key)Config.FakeJoyCon.Right.ButtonZR]) CurrentButton |= HidControllerKeys.KEY_ZR;
  65. LeftJoystick = new JoystickPosition
  66. {
  67. DX = LeftJoystickDX,
  68. DY = LeftJoystickDY
  69. };
  70. RightJoystick = new JoystickPosition
  71. {
  72. DX = RightJoystickDX,
  73. DY = RightJoystickDY
  74. };
  75. //Get screen touch position from left mouse click
  76. //Opentk always captures mouse events, even if out of focus, so check if window is focused.
  77. if (Mouse != null && Focused)
  78. if (Mouse.GetState().LeftButton == OpenTK.Input.ButtonState.Pressed)
  79. {
  80. HidTouchScreenEntryTouch CurrentPoint = new HidTouchScreenEntryTouch
  81. {
  82. Timestamp = (uint)Environment.TickCount,
  83. X = (uint)Mouse.X,
  84. Y = (uint)Mouse.Y,
  85. //Placeholder values till more data is acquired
  86. DiameterX = 10,
  87. DiameterY = 10,
  88. Angle = 90,
  89. //Only support single touch
  90. TouchIndex = 0,
  91. };
  92. if (Mouse.X > -1 && Mouse.Y > -1)
  93. Ns.SendTouchScreenEntry(CurrentPoint);
  94. }
  95. //We just need one pair of JoyCon because it's emulate by the keyboard.
  96. Ns.SendControllerButtons(HidControllerID.CONTROLLER_HANDHELD, HidControllerLayouts.Main, CurrentButton, LeftJoystick, RightJoystick);
  97. }
  98. protected override void OnRenderFrame(FrameEventArgs e)
  99. {
  100. GL.Viewport(0, 0, Width, Height);
  101. Title = $"Ryujinx Screen - (Vsync: {VSync} - FPS: {1f / e.Time:0})";
  102. GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  103. Renderer.RunActions();
  104. Renderer.Render();
  105. SwapBuffers();
  106. }
  107. protected override void OnResize(EventArgs e)
  108. {
  109. Renderer.SetWindowSize(Width, Height);
  110. }
  111. }
  112. }