![]() |
| Home About Us Services Clients Resources Blog Careers Orderbox™ | contact us Site map links |
Managed Direct3D via IronPython Tutorial Part 2 - Interactive control of a Managed Direct3D DeviceIn this tutorial, we show you how to create a WinForms Form via the IronPython console, embed a Direct3D device within the form and control the Direct3D device interactively. This is an extremely cool thing that is currently only possible to do with IronPython and vividly illustrates why dynamic languages and .NET's CLR are a match made in heaven. First, fire up the IronPython console > ipy
>>> import clr
>>> clr.AddReference("System")
>>> clr.AddReference("System.Drawing");
>>> clr.AddReference("System.Windows.Forms")
>>> clr.AddReferenceByName(
"Microsoft.DirectX, Version=1.0.2902.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35" )
>>> clr.AddReferenceByName(
"Microsoft.DirectX.Direct3D, Version=1.0.2902.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35" )
>>> from System.Windows.Forms import *
>>> from System.Drawing import *
>>> from Microsoft.DirectX import *
>>> from Microsoft.DirectX.Direct3D import *
>>> import winforms
You are now all set to begin your learning adventure. Creating an empty Winforms form in IronPython is ridiculously easy compared to C# or C in Win32: Two lines is all you really need to show a WinForms Form in IronPython>>> f=Form() >>> f.Show() Yes, woohoooooo! This is all the goodness of Python brought over to the CLR. I'd like to take a moment to thank Jim Hugunin and the rest of the IronPython crew for their wonderful gift. Now, Create the Managed Direct3D device and bind it to the form>>> pp=PresentParameters() >>> pp.Windowed=True >>> pp.SwapEffect=SwapEffect.Discard >>> d=Device(0, DeviceType.Hardware, f.Handle, CreateFlags.SoftwareVertexProcessing, pp) A complete discussion of the calls you
see here is beyond the scope of this tutorial and you should refer
to the Managed DirectX 1.1 SDK documentation for the full (and gory)
details. But to summarize, what you did was to set some
parameters in a >>> d.Present() # update to what the device surface contains at the moment, might be garbage >>> d.Clear(ClearFlags.Target, Color.Green, 1.0, 0 ) >>> d.Present() # clear the device viewport to green >>> surf=d.GetRenderTarget(0) # get a reference to the 'Render Target' surface >>> d.ColorFill(surf,Rectangle(10,10,40,40),Color.Blue) >>> d.Present() >>> d.ColorFill(surf,Rectangle(100,100,80,10),Color.Lavender) >>> d.Present() As you can see, the API is rather schizophrenic
and there are many subtleties involved here. You can clear
the device viewport by calling it directly without referring to
a surface. But to do some drawing, you need to get a specific
surface, the 0th render target surface
(depending on the hardware, there may be multiple)
and then tell the device to
draw on that particular one. Why wasn't the API designed
so that we can just ask the surface to draw itself
and avoid the complexity of having to refer
to the device, since the surface ought to know which
Part 1 - Importing and Referencing the MDX 1.1 Assemblies Part 3 - Rendering alpha-blended sprites Discussion:
© 2008 by Andy Sy |
Web Development / Rich Internet Applications (RIA) Development
Programming LanguagesPlatformsDatabase Development |
| © 2003-2008 Neotitans Technologies Inc. | contact/hire us |