Simple c# directX example
Asked Answered
D

3

6

I try to start c# directX programming with this tutorial: http://www.riemers.net/eng/Tutorials/DirectX/Csharp/Series1/tut2.php I'm using visual Studion Community 2015, my code is like this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace DirecxTest01
{
    public partial class Form1 : Form
    {
        private Device device;
        public Form1()
        {
            InitializeComponent();
            InitializeDevice();
        }
        private void InitializeDevice()
        {
            PresentParameters presentParams = new PresentParameters();
            presentParams.Windowed = true;
            presentParams.SwapEffect = SwapEffect.Discard;
            device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
        }
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            device.Clear(ClearFlags.Target, Color.DarkSlateBlue, 1.0f, 0);
            device.Present();
        }
    }
}

I also addesd References to all the DirectX dll's. Whe I run the Programm nothing happened, not even the Form Window appers. No error message, just nothing. I tried to comment out the DirectX Stuff line by line. Even with this code the Programm hangs up:

private void InitializeDevice()
    {
        PresentParameters presentParams = new PresentParameters();
        //presentParams.Windowed = true;
        //presentParams.SwapEffect = SwapEffect.Discard;
        //device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
        }

When I out comment

//PresentParameters presentParams = new PresentParameters();

to, at least the Windowas Form apperears.

Whats wrong with my code?

Doublethink answered 1/4, 2017 at 18:0 Comment(1)
the link here is dEadKaete
S
8

The tutorial you are using is for the deprecated Managed DirectX 1.1 assemblies. They were written for .NET 1.1, and are not compatible with .NET 4.0 or later--although there are some hacks out there to try to make them work.

  • Since the last version of D3DX9 supported by Managed DirectX 1.1 is April 2006, it makes use of a very outdated version of the HLSL compiler.
  • The Managed DirectX 1.1 assemblies are 32-bit only, so you cannot use them from an x64 native .NET application (/platform:anycpu on a Windows x64 system). You must build with /platform:x86 and stay within the limits of the 2 GB memory space of 32-bit applications.
  • The assemblies only support the legacy DirectX API set, with no support for Direct3D9Ex, Direct3D 10.x, Direct3D 11, Direct2D, DirectWrite, DXGI, D3DX10, D3DX11, XAUDIO2, XACT, XINPUT, etc.
  • Since Managed DirectX 2.0 was never released in production form, the Managed DirectX 1.1 assemblies still reflect .NET 1.1 design principles and does not support or make use of .NET 2.0 constructs.
  • Managed DirectX 1.1 while compatible with .NET 2.0 (and the .NET 3.0 and .NET 3.5 extensions of the 2.0 runtime), is not compatible with .NET 4.0
  • The Managed DirectX 1.1 assemblies are only deployed by the legacy DirectSetup and the legacy DirectX SDK.

In short: Don't use them. Use something more modern like SharpDX, SlimDX, or Unity3D.

See DirectX and .NET and Where is the DirectX SDK?

Sardius answered 2/4, 2017 at 4:43 Comment(3)
According to sharpdx.org, "as of 29 Mar 2019, SharpDX is no longer being under development or maintenance." It looks like SlimDX is also not being actively maintained any longer. (I realize that this answer is several years old).Smoky
SharpDX code is available here so you can still use it. It's a shame they couldn't keep it going, but at least it's a better option than legacy Managed DirectX 1.1 which hasn't been updated in nearly two decades -and- is closed source. SlimDX Is also retired as the maintainer stopped working on it around 2008, but SlimDX is more a 'direct replacement' for legacy Managed DX 1.1.Sardius
@WilliamJohnHolden so at this moment there are no solutions to write Direct3D app with .Net Core, right?Hebraic
J
1

I know this is an old thread but I ran into the exact problem and the solution of "don't use DirectX" is not an option because I am working on an existing system without the option of upgrading all the relevant parts using the DirectX dlls.

Fortunately I found a solution to work with the old DirectX assemblies on a .NET Framework version 4.6.1.

Simply add this in the App.config file (right below the </appSettings>):

<startup useLegacyV2RuntimeActivationPolicy="true" >
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.61"/>
</startup>
Joktan answered 13/4, 2021 at 6:56 Comment(0)
H
1

It seems that there is one library that continue the way of SharpDX, this is Vortice.Windows.

Vortice.Windows is a collection of Win32 and UWP libraries with bindings support for DXGI, WIC, DirectWrite, Direct2D, Direct3D9, Direct3D11, Direct3D12, XInput, XAudio2, X3DAudio, DirectInput, DirectStorage, DirectML, UIAnimation and DirectSound.

This library targets .net7.0 and .net8.0 and uses modern C# 12, see CHANGELOG for list of changes between commits.

Hebraic answered 2/12, 2023 at 22:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.