cleanup ds video options code

This commit is contained in:
adelikat 2020-03-28 10:31:25 -05:00
parent eea7b2136f
commit e2ac7d7a8f
5 changed files with 52 additions and 72 deletions

View File

@ -398,13 +398,15 @@ namespace BizHawk.Client.EmuHawk
// otherwise, have the filter program untransform it
Vector2 v = new Vector2(p.X, p.Y);
v = _currentFilterProgram.UntransformPoint("default", v);
if (Global.Emulator.SystemId == "NDS")
// Poop
if (Global.Emulator is MelonDS ds)
{
MelonDS core = Global.Emulator as MelonDS;
Point touchLocation = core.GetSettings().screenOptions.locations[1];
v.Y = (int)((double)core.BufferHeight / MelonDS.NATIVE_HEIGHT * (v.Y - touchLocation.Y));
v.X = (int)((double)core.BufferWidth / MelonDS.NATIVE_WIDTH * (v.X - touchLocation.X));
Point touchLocation = ds.GetSettings().ScreenOptions.Locations[1];
v.Y = (int)((double)ds.BufferHeight / MelonDS.NativeHeight * (v.Y - touchLocation.Y));
v.X = (int)((double)ds.BufferWidth / MelonDS.NativeWidth * (v.X - touchLocation.X));
}
return new Point((int)v.X, (int)v.Y);
}

View File

@ -1,41 +1,40 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.InteropServices;
/// <summary>
/// Provides a way to arrange displays inside a frame buffer.
/// </summary>
namespace BizHawk.Emulation.Common
{
/// <summary>
/// Provides a way to arrange displays inside a frame buffer.
/// </summary>
public class ScreenArranger
{
public ScreenLayoutSettings layoutSettings;
private Size[] sizes;
private readonly Size[] _sizes;
public ScreenArranger(Size[] sizes)
{
this.sizes = sizes;
_sizes = sizes;
}
public ScreenLayoutSettings LayoutSettings { get; set; }
public unsafe int[] GenerateFramebuffer(int*[] src, int[] srcLength)
{
if (src.Length != layoutSettings.locations.Length)
if (src.Length != LayoutSettings.Locations.Length)
return null;
int[] ret = new int[layoutSettings.finalSize.Width * layoutSettings.finalSize.Height];
var ret = new int[LayoutSettings.FinalSize.Width * LayoutSettings.FinalSize.Height];
for (int iBuf = 0; iBuf < src.Length; iBuf++)
{
int screen = layoutSettings.order[iBuf];
Size size = sizes[screen];
Point position = layoutSettings.locations[screen];
int screen = LayoutSettings.Order[iBuf];
Size size = _sizes[screen];
Point position = LayoutSettings.Locations[screen];
int minSrcX = Math.Max(-position.X, 0);
int maxSrcX = Math.Min(layoutSettings.finalSize.Width - position.X, size.Width);
int maxSrcX = Math.Min(LayoutSettings.FinalSize.Width - position.X, size.Width);
int minDstX = Math.Max(position.X, 0);
int minSrcY = Math.Max(-position.Y, 0);
int maxSrcY = Math.Min(layoutSettings.finalSize.Height - position.Y, size.Height);
int maxSrcY = Math.Min(LayoutSettings.FinalSize.Height - position.Y, size.Height);
int minDstY = Math.Max(position.Y, 0);
if ((maxSrcX - 1) + (maxSrcY - 1) * size.Width > srcLength[iBuf])
@ -43,7 +42,7 @@ namespace BizHawk.Emulation.Common
for (int iY = minSrcY; iY < maxSrcY; iY++)
{
int dstIndex = minDstX + (minDstY + iY - minSrcY) * layoutSettings.finalSize.Width;
int dstIndex = minDstX + (minDstY + iY - minSrcY) * LayoutSettings.FinalSize.Width;
int srcIndex = minSrcX + iY * size.Width;
for (int iX = minSrcX; iX < maxSrcX; iX++)
ret[dstIndex++] = src[screen][srcIndex++];
@ -52,37 +51,12 @@ namespace BizHawk.Emulation.Common
return ret;
}
public unsafe int[] GenerateFramebuffer(int[][] src)
{
GCHandle[] handles = new GCHandle[src.Length];
try
{
for (int i = 0; i < src.Length; i++)
handles[i] = GCHandle.Alloc(src[i], GCHandleType.Pinned);
int*[] srcBuffers = new int*[src.Length];
int[] lengths = new int[src.Length];
for (int i = 0; i < src.Length; i++)
{
srcBuffers[i] = (int*)handles[i].AddrOfPinnedObject();
lengths[i] = src[i].Length;
}
return GenerateFramebuffer(srcBuffers, lengths);
}
finally
{ // unpin the memory
foreach (var h in handles)
if (h.IsAllocated) h.Free();
}
}
}
public class ScreenLayoutSettings
{
public Point[] locations;
public int[] order;
public Size finalSize;
public Point[] Locations { get; set; }
public int[] Order { get; set; }
public Size FinalSize { get; set; }
}
}

View File

@ -43,7 +43,7 @@ namespace BizHawk.Emulation.Cores.Consoles.Nintendo.NDS
| (controller.IsPressed("Touch") ? 0x2000 : 0)
| (controller.IsPressed("LidOpen") ? 0x4000 : 0) | (controller.IsPressed("LidClose") ? 0x8000 : 0);
FrameAdvance((short)buttons, (byte)controller.GetFloat("TouchX"), (byte)controller.GetFloat("TouchY"));
getNewBuffer = true;
_getNewBuffer = true;
return true;
}
@ -108,7 +108,7 @@ namespace BizHawk.Emulation.Cores.Consoles.Nintendo.NDS
throw new Exception("Failed to init NDS.");
InitMemoryDomains();
screenArranger = new ScreenArranger(new Size[] { new Size(NATIVE_WIDTH, NATIVE_HEIGHT), new Size(NATIVE_WIDTH, NATIVE_HEIGHT) });
_screenArranger = new ScreenArranger(new[] { new Size(NativeWidth, NativeHeight), new Size(NativeWidth, NativeHeight) });
PutSettings(settings as MelonSettings);
PutSyncSettings(syncsettings as MelonSyncSettings);

View File

@ -32,17 +32,21 @@ namespace BizHawk.Emulation.Cores.Consoles.Nintendo.NDS
public bool PutSettings(MelonSettings o)
{
if (o == null || o.screenOptions == null)
if (o?.ScreenOptions == null)
{
o = new MelonSettings();
o.screenOptions = new ScreenLayoutSettings();
o.screenOptions.locations = new Point[] { new Point(0, 0), new Point(0, NATIVE_HEIGHT) };
o.screenOptions.order = new int[] { 0, 1 };
o.screenOptions.finalSize = new Size(NATIVE_WIDTH, NATIVE_HEIGHT * 2);
o = new MelonSettings
{
ScreenOptions = new ScreenLayoutSettings
{
Locations = new[] { new Point(0, 0), new Point(0, NativeHeight) },
Order = new[] { 0, 1 },
FinalSize = new Size(NativeWidth, NativeHeight * 2)
}
};
}
_settings = o;
screenArranger.layoutSettings = _settings.screenOptions;
_screenArranger.LayoutSettings = _settings.ScreenOptions;
return false;
}
@ -89,7 +93,7 @@ namespace BizHawk.Emulation.Cores.Consoles.Nintendo.NDS
public class MelonSettings
{
public ScreenLayoutSettings screenOptions;
public ScreenLayoutSettings ScreenOptions { get; set; }
}
public class MelonSyncSettings

View File

@ -6,17 +6,18 @@ namespace BizHawk.Emulation.Cores.Consoles.Nintendo.NDS
{
unsafe partial class MelonDS : IVideoProvider
{
public const int NATIVE_WIDTH = 256;
public const int NativeWidth = 256;
/// <summary>
/// for a single screen
/// </summary>
public const int NATIVE_HEIGHT = 192;
public const int NativeHeight = 192;
public int VirtualWidth => BufferWidth;
public int VirtualHeight => BufferHeight;
public int BufferWidth => _settings.screenOptions.finalSize.Width;
public int BufferHeight => _settings.screenOptions.finalSize.Height;
public int BufferWidth => _settings.ScreenOptions.FinalSize.Width;
public int BufferHeight => _settings.ScreenOptions.FinalSize.Height;
public int VsyncNumerator => 60;
@ -24,8 +25,7 @@ namespace BizHawk.Emulation.Cores.Consoles.Nintendo.NDS
public int BackgroundColor => 0;
ScreenArranger screenArranger;
private readonly ScreenArranger _screenArranger;
[DllImport(dllPath)]
private static extern int* GetTopScreenBuffer();
@ -35,19 +35,19 @@ namespace BizHawk.Emulation.Cores.Consoles.Nintendo.NDS
private static extern int GetScreenBufferSize();
// BizHawk needs to be able to modify the buffer when loading savestates.
private int[] buffer = null;
private bool getNewBuffer = true;
private int[] _buffer;
private bool _getNewBuffer = true;
public int[] GetVideoBuffer()
{
if (getNewBuffer)
if (_getNewBuffer)
{
getNewBuffer = false;
_getNewBuffer = false;
int*[] buffers = new int*[] { GetTopScreenBuffer(), GetBottomScreenBuffer() };
int*[] buffers = { GetTopScreenBuffer(), GetBottomScreenBuffer() };
int bufferSize = GetScreenBufferSize();
buffer = screenArranger.GenerateFramebuffer(buffers, new int[] { bufferSize, bufferSize});
_buffer = _screenArranger.GenerateFramebuffer(buffers, new[] { bufferSize, bufferSize });
}
return buffer;
return _buffer;
}
}
}