GBA.Unity/Assets/emulator/VideoProvider.cs

62 lines
2.0 KiB
C#
Raw Permalink Normal View History

2024-08-16 14:51:15 +08:00
using OptimeGBA;
using System;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.UI;
public class VideoProvider : MonoBehaviour
{
public RawImage m_drawCanvas;
private RectTransform m_drawCanvasrect;
private IntPtr wrapTexBufferPointer;
private Texture2D wrapTex;
private int TexBufferSize;
uint[] wrapTexBuffer = new uint[240 * 160];
2024-08-16 21:50:32 +08:00
//Color32[] DisplayColorBuffer = new Color32[240 * 160];
2024-08-16 14:51:15 +08:00
2024-08-16 21:50:32 +08:00
private void Awake()
2024-08-16 14:51:15 +08:00
{
if (wrapTex == null)
{
wrapTex = new Texture2D(240, 160, TextureFormat.RGBA32, false);
wrapTex.filterMode = FilterMode.Point;
//wrapTexBuffer = screenData;
// <20>̶<EFBFBD><CCB6><EFBFBD><EFBFBD><EFBFBD><E9A3AC>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD>
GCHandle handle = GCHandle.Alloc(wrapTexBuffer, GCHandleType.Pinned);
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>
wrapTexBufferPointer = handle.AddrOfPinnedObject();
m_drawCanvas.texture = wrapTex;
TexBufferSize = wrapTexBuffer.Length * 4;
m_drawCanvasrect = m_drawCanvas.GetComponent<RectTransform>();
float targetWidth = ((float)240 / 160) * m_drawCanvasrect.rect.height;
m_drawCanvasrect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, targetWidth);
}
}
2024-08-16 21:50:32 +08:00
public void OnRenderFrame()
2024-08-16 14:51:15 +08:00
{
var buf = Emulator.instance.ShowBackBuf ? Emulator.instance.gba.Ppu.Renderer.ScreenBack : Emulator.instance.gba.Ppu.Renderer.ScreenFront;
unsafe
{
for (uint i = 0; i < 240 * 160; i++)
{
wrapTexBuffer[i] = PpuRenderer.ColorLutCorrected[buf[i] & 0x7FFF];
2024-08-16 21:50:32 +08:00
//fixed (uint* p = &wrapTexBuffer[i])
//{
// byte* bp = (byte*)p;
// DisplayColorBuffer[i] = new Color32(*(bp++), *(bp++), *(bp++), *(bp++));
//}
2024-08-16 14:51:15 +08:00
}
}
2024-08-16 21:50:32 +08:00
wrapTex.LoadRawTextureData(wrapTexBufferPointer, TexBufferSize);
//wrapTex.SetPixels32(DisplayColorBuffer, 0);
2024-08-16 14:51:15 +08:00
wrapTex.Apply();
}
}