AxibugEmuOnline/AxibugEmuOnline.Client/Assets/Script/NesEmulator/VideoProvider.cs

83 lines
2.6 KiB
C#
Raw Normal View History

2024-07-30 18:53:36 +08:00
using AxibugEmuOnline.Client.Assets.Script.NesEmulator;
using Codice.CM.Client.Differences;
using System;
2024-07-30 11:57:09 +08:00
using System.Collections;
using System.Collections.Generic;
using System.Linq;
2024-07-31 17:40:32 +08:00
using System.Runtime.InteropServices;
2024-07-30 18:53:36 +08:00
using System.Text;
2024-07-30 11:57:09 +08:00
using UnityEngine;
2024-07-31 17:40:32 +08:00
using UnityEngine.Playables;
2024-07-30 11:57:09 +08:00
using UnityEngine.UI;
2024-07-30 18:53:36 +08:00
using VirtualNes.Core;
2024-07-30 11:57:09 +08:00
namespace AxibugEmuOnline.Client
{
public class VideoProvider : MonoBehaviour
{
public RawImage Image;
2024-07-31 17:40:32 +08:00
private Color32[] wrapTexBuffer;
private IntPtr wrapTexBufferPointer;
2024-07-30 11:57:09 +08:00
private Texture2D wrapTex;
2024-07-30 18:53:36 +08:00
public void SetDrawData(byte[] screenData, byte[] lineColorMode, int screenWidth, int screenHeight)
2024-07-30 11:57:09 +08:00
{
2024-07-31 17:40:32 +08:00
if (wrapTex == null)
{
wrapTex = new Texture2D(screenWidth, screenHeight, TextureFormat.BGRA32, false);
wrapTexBuffer = new Color32[screenWidth * screenHeight];
// <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();
}
2024-07-30 18:53:36 +08:00
uint[] pPal;
int pScn = 0;
int width;
2024-07-31 17:40:32 +08:00
var Dst = wrapTexBuffer;
2024-07-30 18:53:36 +08:00
var pDst = 0;
for (int line = 0; line < screenHeight; line++)
{
2024-07-31 17:40:32 +08:00
if ((lineColorMode[line] & 0x80) == 0)
2024-07-30 18:53:36 +08:00
{
pPal = PaletteDefine.m_cnPalette[lineColorMode[line] & 0x07];
}
else
{
pPal = PaletteDefine.m_mnPalette[lineColorMode[line] & 0x07];
}
width = screenWidth;
while (width > 0)
{
2024-07-31 17:40:32 +08:00
var edx = screenData[pScn + 8];
2024-07-30 18:53:36 +08:00
byte index = (byte)(edx & 0xFF);
var colorData = pPal[index];
var rawData = BitConverter.GetBytes(colorData);
Dst[pDst] = new Color32(rawData[0], rawData[1], rawData[2], 255);
pScn += 1;
pDst += 1;
width -= 1;
}
pScn += PPU.SCREEN_WIDTH - screenWidth;
}
2024-07-31 17:40:32 +08:00
//wrapTex.SetPixels32(wrapTexBuffer);
wrapTex.LoadRawTextureData(wrapTexBufferPointer, screenWidth * screenHeight * 4);
2024-07-30 11:57:09 +08:00
wrapTex.Apply();
2024-07-30 18:53:36 +08:00
Graphics.Blit(wrapTex, Image.mainTexture as RenderTexture);
2024-07-30 11:57:09 +08:00
}
2024-07-30 18:53:36 +08:00
2024-07-30 11:57:09 +08:00
}
}