![]() |
| Home About Us Services Clients Resources Blog Careers Orderbox™ | contact us Site map links |
Managed Direct3D via IronPython Tutorial Part 3 - Rendering Alpha Blended 2-D SpritesIn the previous tutorial, we showed you the way to control a Direct3D device interactively. For this tutorial, we will be displaying and animating sprites, so we will use a render/game loop and run the program from a script instead of the command line. The code for displaying animated sprites using Managed DirectX and IronPython is amazingly simple. We start with the imports below... import clr
clr.AddReference("System")
clr.AddReference("System.Drawing")
clr.AddReference("System.Windows.Forms")
clr.AddReference("Microsoft.DirectX")
clr.AddReference("Microsoft.DirectX.Direct3D")
clr.AddReference("Microsoft.DirectX.Direct3DX")
from System.Drawing import *
from System.Windows.Forms import *
from Microsoft.DirectX import *
from Microsoft.DirectX.Direct3D import *
import winforms
This program uses the simpler way of importing the MDX
assemblies which should work as long as you don't
have MDX 2.0 installed in your machine. See the
first tutorial
for a clarification.
Notice that we now import a new assembly reference
f = Form() pp = PresentParameters() pp.Windowed = True pp.SwapEffect = SwapEffect.Discard d=Device(0, DeviceType.Hardware, f.Handle, CreateFlags.SoftwareVertexProcessing, pp) Above, we create our form and associate a Direct3D device (initialized using the desired parameters) with it. Then, below, we load in the texture that we want to use as a sprite, and create an instance of the Sprite class: t = TextureLoader.FromFile(d,"window.png") s = Sprite(d) ![]() window.png has been modified within Photoshop so that the stained glass area has transparency values All that remains to be done now is to show the form and render the sprite in it! f.Show() angle1=0 angle2=0 while f.Created: d.Clear(ClearFlags.Target, Color.Black, 1.0, 0 ) d.BeginScene() s.Begin(SpriteFlags.AlphaBlend) s.Draw2D(t,PointF(63,63),angle1,PointF(100,100),Color.White) s.Draw2D(t,PointF(63,63),angle2,PointF(125,125),Color.White) s.End() d.EndScene() d.Present() angle1+=.02 angle2-=.02 Application.DoEvents() The loop above is explained as follows.
At the start of each frame, we clear the device background
to
Note that the inconsistent, haphazard design of the DirectX API once again rears its ugly head here. Contrary to what you may expect, the Sprite class does not encapsulate a sprite object at all! It is merely there to provide you with calls to display the textures you have loaded and to delimit the beginning and end of the sprite display code. Needless to say, APIs like this illustrates just how easily OOP can be abused and how its usage often leads to confusion. Part 1 - Importing and Referencing the MDX 1.1 Assemblies Part 2 - Interactive control of a Managed Direct3D Device Discussion:
© 2007 by Andy Sy |
|
Web Development / Rich Internet Applications (RIA) Development
Programming LanguagesPlatformsDatabase Development
|
| © 2003-2008 Neotitans Technologies Inc. | contact/hire us |