2018-02-04 18:08:20 -05:00
|
|
|
using OpenTK;
|
|
|
|
using OpenTK.Graphics;
|
2018-03-02 20:49:17 -05:00
|
|
|
using OpenTK.Input;
|
2018-02-20 15:09:23 -05:00
|
|
|
using Ryujinx.Graphics.Gal;
|
2018-06-10 20:46:42 -04:00
|
|
|
using Ryujinx.HLE;
|
|
|
|
using Ryujinx.HLE.Input;
|
2018-02-04 18:08:20 -05:00
|
|
|
using System;
|
2018-07-12 13:03:52 -04:00
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
using Stopwatch = System.Diagnostics.Stopwatch;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
namespace Ryujinx
|
|
|
|
{
|
|
|
|
public class GLScreen : GameWindow
|
|
|
|
{
|
2018-03-02 20:49:17 -05:00
|
|
|
private const int TouchScreenWidth = 1280;
|
|
|
|
private const int TouchScreenHeight = 720;
|
|
|
|
|
2018-07-12 13:03:52 -04:00
|
|
|
private const int TargetFPS = 60;
|
|
|
|
|
2018-08-16 19:47:36 -04:00
|
|
|
private Switch Device;
|
2018-02-04 18:08:20 -05:00
|
|
|
|
|
|
|
private IGalRenderer Renderer;
|
|
|
|
|
2018-06-21 17:10:19 -04:00
|
|
|
private KeyboardState? Keyboard = null;
|
|
|
|
|
|
|
|
private MouseState? Mouse = null;
|
2018-06-23 20:39:25 -04:00
|
|
|
|
2018-07-12 13:03:52 -04:00
|
|
|
private Thread RenderThread;
|
|
|
|
|
|
|
|
private bool ResizeEvent;
|
|
|
|
|
|
|
|
private bool TitleEvent;
|
|
|
|
|
|
|
|
private string NewTitle;
|
|
|
|
|
2018-08-16 19:47:36 -04:00
|
|
|
public GLScreen(Switch Device, IGalRenderer Renderer)
|
2018-02-04 18:08:20 -05:00
|
|
|
: base(1280, 720,
|
|
|
|
new GraphicsMode(), "Ryujinx", 0,
|
|
|
|
DisplayDevice.Default, 3, 3,
|
|
|
|
GraphicsContextFlags.ForwardCompatible)
|
|
|
|
{
|
2018-08-16 19:47:36 -04:00
|
|
|
this.Device = Device;
|
2018-02-04 18:08:20 -05:00
|
|
|
this.Renderer = Renderer;
|
2018-03-04 18:32:18 -05:00
|
|
|
|
|
|
|
Location = new Point(
|
|
|
|
(DisplayDevice.Default.Width / 2) - (Width / 2),
|
|
|
|
(DisplayDevice.Default.Height / 2) - (Height / 2));
|
2018-07-12 13:03:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private void RenderLoop()
|
|
|
|
{
|
|
|
|
MakeCurrent();
|
|
|
|
|
|
|
|
Stopwatch Chrono = new Stopwatch();
|
|
|
|
|
|
|
|
Chrono.Start();
|
|
|
|
|
|
|
|
long TicksPerFrame = Stopwatch.Frequency / TargetFPS;
|
|
|
|
|
|
|
|
long Ticks = 0;
|
|
|
|
|
|
|
|
while (Exists && !IsExiting)
|
|
|
|
{
|
2018-08-16 19:47:36 -04:00
|
|
|
if (Device.WaitFifo())
|
2018-07-12 13:03:52 -04:00
|
|
|
{
|
2018-08-16 19:47:36 -04:00
|
|
|
Device.ProcessFrame();
|
2018-07-12 13:03:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Renderer.RunActions();
|
|
|
|
|
|
|
|
if (ResizeEvent)
|
|
|
|
{
|
|
|
|
ResizeEvent = false;
|
|
|
|
|
|
|
|
Renderer.FrameBuffer.SetWindowSize(Width, Height);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ticks += Chrono.ElapsedTicks;
|
|
|
|
|
|
|
|
Chrono.Restart();
|
|
|
|
|
|
|
|
if (Ticks >= TicksPerFrame)
|
|
|
|
{
|
|
|
|
RenderFrame();
|
|
|
|
|
|
|
|
//Queue max. 1 vsync
|
|
|
|
Ticks = Math.Min(Ticks - TicksPerFrame, TicksPerFrame);
|
|
|
|
}
|
|
|
|
}
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-07-12 13:03:52 -04:00
|
|
|
public void MainLoop()
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-07-12 13:03:52 -04:00
|
|
|
VSync = VSyncMode.Off;
|
|
|
|
|
|
|
|
Visible = true;
|
2018-04-14 00:31:27 -04:00
|
|
|
|
2018-06-23 20:39:25 -04:00
|
|
|
Renderer.FrameBuffer.SetWindowSize(Width, Height);
|
2018-07-12 13:03:52 -04:00
|
|
|
|
|
|
|
Context.MakeCurrent(null);
|
|
|
|
|
|
|
|
//OpenTK doesn't like sleeps in its thread, to avoid this a renderer thread is created
|
|
|
|
RenderThread = new Thread(RenderLoop);
|
|
|
|
|
|
|
|
RenderThread.Start();
|
|
|
|
|
|
|
|
while (Exists && !IsExiting)
|
|
|
|
{
|
|
|
|
ProcessEvents();
|
|
|
|
|
|
|
|
if (!IsExiting)
|
|
|
|
{
|
|
|
|
UpdateFrame();
|
|
|
|
|
|
|
|
if (TitleEvent)
|
|
|
|
{
|
|
|
|
TitleEvent = false;
|
|
|
|
|
|
|
|
Title = NewTitle;
|
|
|
|
}
|
|
|
|
}
|
2018-07-02 18:08:54 -04:00
|
|
|
|
2018-07-29 00:35:36 -04:00
|
|
|
//Polling becomes expensive if it's not slept
|
|
|
|
Thread.Sleep(1);
|
2018-07-02 18:08:54 -04:00
|
|
|
}
|
|
|
|
}
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-07-12 13:03:52 -04:00
|
|
|
private new void UpdateFrame()
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-03-02 20:49:17 -05:00
|
|
|
HidControllerButtons CurrentButton = 0;
|
2018-07-02 18:08:54 -04:00
|
|
|
HidJoystickPosition LeftJoystick;
|
|
|
|
HidJoystickPosition RightJoystick;
|
2018-02-22 08:28:27 -05:00
|
|
|
|
2018-07-29 00:35:36 -04:00
|
|
|
int LeftJoystickDX = 0;
|
|
|
|
int LeftJoystickDY = 0;
|
|
|
|
int RightJoystickDX = 0;
|
|
|
|
int RightJoystickDY = 0;
|
2018-06-23 20:39:25 -04:00
|
|
|
|
2018-07-02 18:08:54 -04:00
|
|
|
//Keyboard Input
|
2018-06-21 17:10:19 -04:00
|
|
|
if (Keyboard.HasValue)
|
|
|
|
{
|
|
|
|
KeyboardState Keyboard = this.Keyboard.Value;
|
|
|
|
|
2018-07-29 00:35:36 -04:00
|
|
|
CurrentButton = Config.JoyConKeyboard.GetButtons(Keyboard);
|
|
|
|
|
|
|
|
(LeftJoystickDX, LeftJoystickDY) = Config.JoyConKeyboard.GetLeftStick(Keyboard);
|
|
|
|
|
|
|
|
(RightJoystickDX, RightJoystickDY) = Config.JoyConKeyboard.GetRightStick(Keyboard);
|
2018-07-02 18:08:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
//Controller Input
|
2018-07-29 00:35:36 -04:00
|
|
|
CurrentButton |= Config.JoyConController.GetButtons();
|
2018-08-16 19:47:36 -04:00
|
|
|
|
2018-07-29 00:35:36 -04:00
|
|
|
//Keyboard has priority stick-wise
|
|
|
|
if (LeftJoystickDX == 0 && LeftJoystickDY == 0)
|
2018-07-02 18:08:54 -04:00
|
|
|
{
|
2018-07-29 00:35:36 -04:00
|
|
|
(LeftJoystickDX, LeftJoystickDY) = Config.JoyConController.GetLeftStick();
|
2018-06-21 17:10:19 -04:00
|
|
|
}
|
2018-03-02 20:49:17 -05:00
|
|
|
|
2018-07-29 00:35:36 -04:00
|
|
|
if (RightJoystickDX == 0 && RightJoystickDY == 0)
|
|
|
|
{
|
|
|
|
(RightJoystickDX, RightJoystickDY) = Config.JoyConController.GetRightStick();
|
|
|
|
}
|
2018-08-16 19:47:36 -04:00
|
|
|
|
2018-03-02 20:49:17 -05:00
|
|
|
LeftJoystick = new HidJoystickPosition
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-02-17 18:54:19 -05:00
|
|
|
DX = LeftJoystickDX,
|
|
|
|
DY = LeftJoystickDY
|
|
|
|
};
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-03-02 20:49:17 -05:00
|
|
|
RightJoystick = new HidJoystickPosition
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-02-17 18:54:19 -05:00
|
|
|
DX = RightJoystickDX,
|
|
|
|
DY = RightJoystickDY
|
|
|
|
};
|
|
|
|
|
2018-03-02 20:49:17 -05:00
|
|
|
bool HasTouch = false;
|
|
|
|
|
2018-02-22 08:28:27 -05:00
|
|
|
//Get screen touch position from left mouse click
|
2018-03-02 20:49:17 -05:00
|
|
|
//OpenTK always captures mouse events, even if out of focus, so check if window is focused.
|
2018-06-21 17:10:19 -04:00
|
|
|
if (Focused && Mouse?.LeftButton == ButtonState.Pressed)
|
2018-03-02 20:49:17 -05:00
|
|
|
{
|
2018-06-21 17:10:19 -04:00
|
|
|
MouseState Mouse = this.Mouse.Value;
|
|
|
|
|
2018-03-02 20:49:17 -05:00
|
|
|
int ScrnWidth = Width;
|
|
|
|
int ScrnHeight = Height;
|
|
|
|
|
2018-07-29 00:35:36 -04:00
|
|
|
if (Width > (Height * TouchScreenWidth) / TouchScreenHeight)
|
2018-02-22 08:28:27 -05:00
|
|
|
{
|
2018-07-29 00:35:36 -04:00
|
|
|
ScrnWidth = (Height * TouchScreenWidth) / TouchScreenHeight;
|
2018-03-02 20:49:17 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-07-29 00:35:36 -04:00
|
|
|
ScrnHeight = (Width * TouchScreenHeight) / TouchScreenWidth;
|
2018-03-02 20:49:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int StartX = (Width - ScrnWidth) >> 1;
|
|
|
|
int StartY = (Height - ScrnHeight) >> 1;
|
|
|
|
|
|
|
|
int EndX = StartX + ScrnWidth;
|
|
|
|
int EndY = StartY + ScrnHeight;
|
|
|
|
|
|
|
|
if (Mouse.X >= StartX &&
|
|
|
|
Mouse.Y >= StartY &&
|
|
|
|
Mouse.X < EndX &&
|
|
|
|
Mouse.Y < EndY)
|
|
|
|
{
|
|
|
|
int ScrnMouseX = Mouse.X - StartX;
|
|
|
|
int ScrnMouseY = Mouse.Y - StartY;
|
|
|
|
|
2018-07-29 00:35:36 -04:00
|
|
|
int MX = (ScrnMouseX * TouchScreenWidth) / ScrnWidth;
|
|
|
|
int MY = (ScrnMouseY * TouchScreenHeight) / ScrnHeight;
|
2018-03-02 20:49:17 -05:00
|
|
|
|
|
|
|
HidTouchPoint CurrentPoint = new HidTouchPoint
|
2018-02-22 08:28:27 -05:00
|
|
|
{
|
2018-03-02 20:49:17 -05:00
|
|
|
X = MX,
|
|
|
|
Y = MY,
|
2018-02-22 08:28:27 -05:00
|
|
|
|
|
|
|
//Placeholder values till more data is acquired
|
|
|
|
DiameterX = 10,
|
|
|
|
DiameterY = 10,
|
2018-03-02 20:49:17 -05:00
|
|
|
Angle = 90
|
2018-02-22 08:28:27 -05:00
|
|
|
};
|
2018-03-02 20:49:17 -05:00
|
|
|
|
|
|
|
HasTouch = true;
|
|
|
|
|
2018-08-16 19:47:36 -04:00
|
|
|
Device.Hid.SetTouchPoints(CurrentPoint);
|
2018-02-22 08:28:27 -05:00
|
|
|
}
|
2018-03-02 20:49:17 -05:00
|
|
|
}
|
2018-02-22 08:28:27 -05:00
|
|
|
|
2018-03-02 20:49:17 -05:00
|
|
|
if (!HasTouch)
|
|
|
|
{
|
2018-08-16 19:47:36 -04:00
|
|
|
Device.Hid.SetTouchPoints();
|
2018-03-02 20:49:17 -05:00
|
|
|
}
|
|
|
|
|
2018-08-16 19:47:36 -04:00
|
|
|
Device.Hid.SetJoyconButton(
|
2018-04-13 16:39:45 -04:00
|
|
|
HidControllerId.CONTROLLER_HANDHELD,
|
|
|
|
HidControllerLayouts.Handheld_Joined,
|
|
|
|
CurrentButton,
|
|
|
|
LeftJoystick,
|
|
|
|
RightJoystick);
|
|
|
|
|
2018-08-16 19:47:36 -04:00
|
|
|
Device.Hid.SetJoyconButton(
|
2018-03-02 20:49:17 -05:00
|
|
|
HidControllerId.CONTROLLER_HANDHELD,
|
|
|
|
HidControllerLayouts.Main,
|
|
|
|
CurrentButton,
|
|
|
|
LeftJoystick,
|
|
|
|
RightJoystick);
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-07-12 13:03:52 -04:00
|
|
|
private new void RenderFrame()
|
2018-02-04 18:08:20 -05:00
|
|
|
{
|
2018-06-23 20:39:25 -04:00
|
|
|
Renderer.FrameBuffer.Render();
|
2018-03-06 15:18:49 -05:00
|
|
|
|
2018-08-16 19:47:36 -04:00
|
|
|
Device.Statistics.RecordSystemFrameTime();
|
2018-02-08 19:43:22 -05:00
|
|
|
|
2018-08-16 19:47:36 -04:00
|
|
|
double HostFps = Device.Statistics.GetSystemFrameRate();
|
|
|
|
double GameFps = Device.Statistics.GetGameFrameRate();
|
2018-02-04 18:08:20 -05:00
|
|
|
|
2018-07-12 13:03:52 -04:00
|
|
|
NewTitle = $"Ryujinx | Host FPS: {HostFps:0.0} | Game FPS: {GameFps:0.0}";
|
|
|
|
|
|
|
|
TitleEvent = true;
|
2018-03-06 15:18:49 -05:00
|
|
|
|
2018-06-23 20:39:25 -04:00
|
|
|
SwapBuffers();
|
2018-03-19 14:58:46 -04:00
|
|
|
|
2018-08-16 19:47:36 -04:00
|
|
|
Device.System.SignalVsync();
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
|
2018-07-12 13:03:52 -04:00
|
|
|
protected override void OnUnload(EventArgs e)
|
|
|
|
{
|
|
|
|
RenderThread.Join();
|
|
|
|
|
|
|
|
base.OnUnload(e);
|
|
|
|
}
|
|
|
|
|
2018-02-20 05:52:35 -05:00
|
|
|
protected override void OnResize(EventArgs e)
|
|
|
|
{
|
2018-07-12 13:03:52 -04:00
|
|
|
ResizeEvent = true;
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
2018-06-21 17:10:19 -04:00
|
|
|
|
|
|
|
protected override void OnKeyDown(KeyboardKeyEventArgs e)
|
|
|
|
{
|
2018-07-29 00:35:36 -04:00
|
|
|
bool ToggleFullscreen = e.Key == Key.F11 ||
|
|
|
|
(e.Modifiers.HasFlag(KeyModifiers.Alt) && e.Key == Key.Enter);
|
|
|
|
|
|
|
|
if (WindowState == WindowState.Fullscreen)
|
|
|
|
{
|
|
|
|
if (e.Key == Key.Escape || ToggleFullscreen)
|
|
|
|
{
|
|
|
|
WindowState = WindowState.Normal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (e.Key == Key.Escape)
|
|
|
|
{
|
|
|
|
Exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ToggleFullscreen)
|
|
|
|
{
|
|
|
|
WindowState = WindowState.Fullscreen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-21 17:10:19 -04:00
|
|
|
Keyboard = e.Keyboard;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnKeyUp(KeyboardKeyEventArgs e)
|
|
|
|
{
|
|
|
|
Keyboard = e.Keyboard;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnMouseDown(MouseButtonEventArgs e)
|
|
|
|
{
|
|
|
|
Mouse = e.Mouse;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnMouseUp(MouseButtonEventArgs e)
|
|
|
|
{
|
|
|
|
Mouse = e.Mouse;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnMouseMove(MouseMoveEventArgs e)
|
|
|
|
{
|
|
|
|
Mouse = e.Mouse;
|
|
|
|
}
|
2018-02-04 18:08:20 -05:00
|
|
|
}
|
|
|
|
}
|