Web Hosting Philippines, Offshore Programming, Offshore SEO Philippines, Cheap Webhosting Manila Philippines
Home -> Resources -> .NET -> Managed Direct3D via IronPython Tutorial - Part 3

Managed Direct3D via IronPython Tutorial Part 3 - Rendering Alpha Blended 2-D Sprites

In 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


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 Microsoft.DirectX.Direct3DX which contains the library for Direct3D's Sprite functionality. The code in this assembly share the same namespace as Microsoft.DirectX.Direct3D, and there is no need import the stuff within it separately.

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!


while f.Created:
  d.Clear(ClearFlags.Target, Color.Black, 1.0, 0 )

The loop above is explained as follows.

f.Created is true as long as the form exists. Application.DoEvents() lets the application handle all the events that are sent to it and its window. Between these two lie our rendering loop.

At the start of each frame, we clear the device background to Color.Black. Next we tell the device that we are starting a render scene via d.BeginScene(). Then we place code for rendering whatever we want between this and d.EndScene(). In this particular case, we will be rendering 2-D sprites.

s.Begin(SpriteFlags.AlphaBlend) indicates the start of sprite rendering and that we want the sprites to be alpha-blended. The 2 calls to s.Draw2D() draws our texture sprites twice, each with a particular rotation - angle1 and angle2 - around the centers of the textures - PointF(63,63) - with the textures appearing at their respective offsets from the top-left origin, PointF(100,100) and PointF(125,125). The last parameter to s.Draw2D(), Color.White indicates the value that the alpha channel of the texture will be modulated with.

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


Back to Top

© 2014 by Andy Sy
last updated: 2007-Sep-9

Web Development / Rich Internet Applications (RIA) Development

Programming Languages


Database Development

Offshore Outsourced Corporate Web and Software Development Internet Marketing SEO
   © 2003-2019 Neotitans Technologies Inc.