Direct3D - Creating a Device with SlimDX

From IronPython Cookbook

What is SlimDX?

Adapted from the SlimDX main site:

   SlimDX is a free open source framework that enables
   developers to easily build DirectX applications using
   .NET technologies such as C#, VB.NET, and IronPython.
    Originally, the managed game development niche was 
    filled by Microsoft's Managed DirectX, 
    which is now deprecated and no longer recommended!
   In it's wake, SlimDX has stepped up to provide support
   in a way that is very similar to the original MDX libraries.
   While similar, however, they are not intended to be the same APIs.
   SlimDX exposes a broader range of technologies,
   has a core design that improves upon that provided by MDX,
   and is more in-tune with managed methodologies
   since the advent of .NET 2.0.

No, it doesn't move, and it isn't animated. Amazing!

You must first install SlimDX SDK! Get it here.

This example creates a Direct3D device using SlimDX. I've added many comments that should help.

import clr

import System.Drawing
import System.Windows.Forms

from System.Drawing import *
from System.Windows.Forms import *

from SlimDX.Direct3D9 import *

class MainForm(Form):
	def __init__(self):
		self.Paint += lambda *_:self.PaintStuff()
	def InitializeComponent(self):#this thing is required in SharpDevelop
		# MainForm
		self.ClientSize = System.Drawing.Size(537, 403)
		self.Name = "MainForm"
		self.Text = "Direct3D9FirstProgram"
	def InitGraphics(self):
		present_params = PresentParameters()#PresentParams for device creation below
   		present_params.Windowed = True#not full screen
   		present_params.SwapEffect = SwapEffect.Discard#dunno what it means, but it's important!
   		#Important Declaration Syntax (from SlimDX doc) - 
   					#public Device(
					#	Direct3D direct3D,
					#	int adapter,
					#	DeviceType deviceType,
					#	IntPtr controlHandle,
					#	CreateFlags createFlags,
					#	params PresentParameters[] presentParameters
		self.Device = Device(Direct3D(),  #I am unsure why this is needed as a param
							0,  #ordinal number that represents the adapter
							DeviceType.Hardware,  #Hardware device type
							self.Handle,  #the handle of the form
							CreateFlags.SoftwareVertexProcessing,  #device creation option

	def PaintStuff(self):
		#Hopefully this documentary will make the code below understandable:
			#public Result Clear(					
			#		ClearFlags clearFlags,
			#		int color,
			#		float zdepth,
			#		int stencil
			#public Result Present()


form = MainForm()

By the way, that code above is originally from here but it has been translated from C# and edited heavily, also. It has also been adapted to SlimDX, as the original code is for Microsoft's Managed DirectX. Tthe code above may be imperfect here and there. Feel free to carefully make it better.

You can use the SlimDX documentation to help you. It is included in the SDK download of SlimDX.


Please note that the picture's effects above may be a slight exaggeration of reality.

Back to Contents.