替换托管 DirectX 的方法
本文关键字:方法 DirectX 替换 | 更新日期: 2023-09-27 18:34:28
我的旧项目有一些问题...如果简短 - "使用 Microsoft.DirectX"。他们弃用了,现在,我在新PC上运行我的游戏,遇到了各种故障和麻烦,这些故障和麻烦以前是不存在的。由于它们已弃用且不再受支持,我正在寻找解决方案来替换 gfx 引擎而不会丢失所有应用程序代码。例如,旧的XNA项目可以移植到MonoGame或FNA,而无需对其架构进行常规更改,我需要以同样的方式进行操作。在这里,任何接近 API 托管 directX 的框架都没有很多移植它们的痛苦?夏普DX?瘦身?还有其他吗?(我以前没有使用SharpDX或SlimDX过期(
这里有一些关于我的GFX代码的片段,只是为了想象它是怎么回事。它们基于托管 DirectX。
public bool InitD3D(ref Form frmForm)
{
PresentParameters uParms = null;
bool bRes = false;
mfrmMain = frmForm;
Constants.gSettings.LoadSettings();
//Debug.WriteLine("InitD3D in thread: " & Threading.Thread.CurrentThread.ManagedThreadId.ToString)
try {
CreateFlags lCreateFlags = default(CreateFlags);
bool bReduced = false;
uParms = CreatePresentationParams(mfrmMain);
if (uParms == null)
return false;
lCreateFlags = (CreateFlags)Constants.gSettings.VertexProcessing;
if (lCreateFlags != CreateFlags.HardwareVertexProcessing && lCreateFlags != CreateFlags.MixedVertexProcessing && lCreateFlags != CreateFlags.SoftwareVertexProcessing) {
lCreateFlags = CreateFlags.HardwareVertexProcessing;
}
Caps uDevCaps = Manager.GetDeviceCaps(Manager.Adapters.Default.Adapter, DeviceType.Hardware);
if (uDevCaps.DeviceCaps.SupportsHardwareTransformAndLight == false) {
if (lCreateFlags == CreateFlags.HardwareVertexProcessing) {
lCreateFlags = CreateFlags.SoftwareVertexProcessing;
bReduced = true;
}
}
try {
gfxDevice = new Device(0, DeviceType.Hardware, frmForm.Handle, lCreateFlags, uParms);
} catch {
bReduced = true;
if ((lCreateFlags & CreateFlags.HardwareVertexProcessing) == CreateFlags.HardwareVertexProcessing) {
lCreateFlags = (lCreateFlags ^ CreateFlags.HardwareVertexProcessing) | CreateFlags.MixedVertexProcessing;
try {
gfxDevice = new Device(0, DeviceType.Hardware, frmForm.Handle, lCreateFlags, uParms);
} catch {
lCreateFlags = (lCreateFlags ^ CreateFlags.MixedVertexProcessing) | CreateFlags.SoftwareVertexProcessing;
try {
gfxDevice = new Device(0, DeviceType.Hardware, frmForm.Handle, lCreateFlags, uParms);
} catch (Exception ex) {
Interaction.MsgBox("Game will not run on this computer because the video hardware is not sufficient.", MsgBoxStyle.OkOnly, "Error");
System.Environment.Exit(0);
}
}
} else if ((lCreateFlags & CreateFlags.MixedVertexProcessing) == CreateFlags.MixedVertexProcessing) {
lCreateFlags = (lCreateFlags ^ CreateFlags.MixedVertexProcessing) | CreateFlags.SoftwareVertexProcessing;
try {
gfxDevice = new Device(0, DeviceType.Hardware, frmForm.Handle, lCreateFlags, uParms);
} catch (Exception ex) {
Interaction.MsgBox("Game will not run on this computer because the video hardware is not sufficient.", MsgBoxStyle.OkOnly, "Error");
System.Environment.Exit(0);
}
}
}
if (bReduced == true)
Interaction.MsgBox("Game was required to alter settings that will affect performance in order to run the game.", MsgBoxStyle.OkOnly, "Error");
Constants.gSettings.VertexProcessing = Convert.ToInt32(lCreateFlags);
bRes = (gfxDevice != null);
if (Constants.gSettings.MiniMapLocX > uParms.BackBufferWidth)
Constants.gSettings.MiniMapLocX = uParms.BackBufferWidth - Constants.gSettings.MiniMapWidthHeight;
if (Constants.gSettings.MiniMapLocY > uParms.BackBufferHeight)
Constants.gSettings.MiniMapLocY = uParms.BackBufferHeight - Constants.gSettings.MiniMapWidthHeight;
} catch {
Interaction.MsgBox(Err().Description, MsgBoxStyle.Critical | MsgBoxStyle.OkOnly, "Error in initialize Direct3D");
bRes = false;
} finally {
uParms = null;
}
if (bRes == false) {
Interaction.MsgBox("Unable to initialize Direct3D 9.0c. Your video card may not fully support DirectX 9.0c. Download the latest drivers for your video card and try again.", MsgBoxStyle.OkOnly | MsgBoxStyle.Critical, "Unable to Initialize");
return false;
}
var _with8 = gfxDevice.DeviceCaps.VertexShaderVersion;
if (_with8.Major == 1) {
Constants.gSettings.PostGlowAmt = 0f;
Constants.gSettings.LightQuality = EngineSettings.LightQualitySetting.VSPS1;
}
mbSupportsNewModelMethod = gfxDevice.DeviceCaps.TextureOperationCaps.SupportsBlendTextureAlpha == true && gfxDevice.DeviceCaps.MaxTextureBlendStages > 2 && gfxDevice.DeviceCaps.MaxSimultaneousTextures > 1;
if (Constants.gWMgr == null)
Constants.gWMgr = new WpnFXManager();
if (Constants.gShldMgr == null)
Constants.gShldMgr = new ShieldFXManager();
if (Constants.gExplMgr == null)
Constants.gExplMgr = new ExplosionManager();
// New ExplosionFXManager(gfxDevice)
if (Constants.gPFXEngine32 == null)
Constants.gPFXEngine32 = new BurnFX.ParticleEngine(32);
//32 for size of points
if (Constants.gEntityDeath == null)
Constants.gEntityDeath = new DeathSequenceMgr();
//If goRewards Is Nothing Then goRewards = New WarpointRewards()
if (Constants.gMissileMgr == null)
Constants.gMissileMgr = new MissileMgr();
if (Constants.gBurnMarkMgr == null)
Constants.gBurnMarkMgr = new EntityBurnMarkManager();
Constants.InitializeFXColors();
var _with9 = matIronCurtain;
_with9.Emissive = System.Drawing.Color.FromArgb(255, 255, 0, 0);
_with9.Diffuse = System.Drawing.Color.FromArgb(255, 255, 0, 0);
_with9.Ambient = System.Drawing.Color.FromArgb(255, 255, 0, 0);
_with9.Specular = System.Drawing.Color.FromArgb(255, 255, 0, 0);
_with9.SpecularSharpness = 10;
gfxDevice.DeviceLost += gfxDevice_DeviceLost;
gfxDevice.DeviceReset += gfxDevice_DeviceReset;
gfxDevice.Disposing += gfxDevice_Disposing;
mbInitialized = bRes;
return bRes;
}
对于现有旧版托管 DirectX 1.1 C# 代码的直接移植,SlimDX 可能是最好的选择,因为它旨在成为直接替代品。您也可以移植到使用SharpDX,MonoGame,Unity3D或其他框架。
请参阅 DirectX 和 .NET