Free C# DirectX Control
Download C# DirectX Control and Source Code (version
10)
See copyright notice at the bottom of this page.
This is a managed DirectX control I wrote as a base to make C# DirectX applications
easier to write. Just create a form, plop down the control, write a draw function, and voila - you're in business.
The Direct3d class sets up the DirectX device, and is responsible for
maintaining and drawing all of the visible Direct3d controls. It can resize itself
to fit the form or it can go to full screen mode. Use the DxLoaded
event to create your AutoMesh's, AutoTexture's, and AutoVertexBuffer's.
Use the DxRender3d event to draw your scene. Changing from windowed
to full screen mode is as simple as setting the DxFullScreen property to
true.
The AutoMesh, AutoTexture, AutoVertexBuffer, and AutoIndexBuffer
classes manage
the DirectX objects so that you don't have to recreate them when the device is
lost and restored.
Report bugs, and fixes to me (see my e-mail address on
the left).

NOTE: Compile the project before viewing the form in the designer.
Otherwise the designer can't display the form with the DirectX control, and
gives you an error.
Classes:
Direct3d: The main direct3d control. You can have multiple
direct3d controls on multiple forms.
AutoMesh: Wrapper for a mesh object. This class saves and
restores mesh data when the device is lost or restored.
AutoTexture: Wrapper for a texture object. This class saves and
restores texture data when the device is lost or restored.
AutoVertexBuffer: Wrapper for a vertex buffer object. This class
saves and restores vertex data when the device is lost or restored.
AutoIndexBuffer: Wrapper for an index buffer object. This class
saves and restores index buffer data when the device is lost or restored.
Color32: Light weight wrapper for storing colors. Quick
conversion to/from int and System.Drawing.Color. Has support for software alpha blending
(see Fade function).
VertexTypeXXXX: VertexType helper classes (struct's) as follows:
VertexTypeP: Position only
VertexTypePC: Position, color (diffuse)
VertexTypePT: Position, texture
VertexTypePCT: Position, color, texture
VertexTypePN: Position, Normal
VertexTypePNC: Position, Normal, Color
VertexTypePNT: Position, Normal, Texture
VertexTypePNCT: Position, Normal, Color, Texture (not yet
tested)
Events and Properties of Direct3d:
DxAutoResize: When true, the control auto resizes to fill the whole
form. Setting this back to false leaves the control the size of the whole
form. NOTE: This should only be set to true when the form is the
main window of a game, and there are no other controls on the form. (ie.
don't do what I did in the example). Otherwise other controls on the form
could cause problems.
DxFullScreen: Get/Set full screen mode. In full screen mode, the form
that this control is on is expanded to the full screen, and the Direct3d control
is also expanded. The menu is removed. When returning to windowed mode, the
form, control, and menu is restored. You shouldn't have more than one
control set to full screen at any given time. WARNING: Any forms owned by
the parent form of this control are unlinked (ChildForm.Owner = null) when
switched to full screen mode. This is done to prevent a child form from blocking
the user input since the form wouldn't normally be visible.
DxSimulateFullScreen: When true, full screen mode is simulated
in a window that is expanded to fit the whole screen. This allows dialogs to be
displayed on top of the Direct3D device. This property is automatically set to
true if the video card runs out of video memory when the DxBackBuffer count is
1. Changing this property does not automatically recreate the device.
Call DxForceDeviceUpdate() to force the setting to take effect.
WARNING: Any forms owned by the parent form of this control are unlinked (ChildForm.Owner
= null) when switched to full screen mode. This is done to prevent a child form
from blocking the user input since the form wouldn't normally be visible.
Dx: Returns the DirectX device. Do not store this, as it can change.
The DirectX device is NULL until the control is loaded, and is NULL if
there was an error.
DxLoaded: Occurs once after DirectX has been initialized for the first
time. Setup AutoMesh's, AutoTexture's, and AutoVertexBuffer's in this event.
DxRestore: Occurs when a new DirectX device has been initialized.
We don't have control over when this may happen. You can restore your
DirectX objects here, but you don't need to do anything with AutoMesh's,
AutoTexture's, or AutoVertexBuffer's.
DxLost: This event is called whenever DirectX decides to toss our
surfaces. You don't need to use this event if you always use the Auto
classes.
DxResizing: Occurs when the surface is resized.
DxRenderPre: Occurs before 3d rendering. When this event is used, you
must manually clear the screen (use dx.Clear).
DxRender3d: Occurs when it is time to render 3d objects. Place all 3d
drawing code in this event.
DxRender2d: Occurs after Render3d, to draw 2d graphics over the 3d scene.
Provides a System.Drawing.Graphics for drawing over the scene
using GDI.
There is a speed penalty when using this event.
DxMouseRay: Returns a ray that represents the last mouse position in
world coordinates. This can be used along with Geometry.SphereBoundProbe to see if the mouse is hovering over an object.
Helper Functions:
AutoMesh.LoadFromXFile: Loads a mesh (and any textures it refers to)
from an X file. The textures are assumed to be in the same directory as
the X file. NOTE: The AutoMesh assumes it owns the textures,
so AutoMesh.Dispose() calls AutoTexture.Dispose() on all of the
subtextures. Aside from that, AutoMesh and AutoTexture are
independent entities.
AutoMesh.Draw: Draw all subsets of the mesh with
the mesh texture. Optionally use the material included with the mesh
(loaded from the X file).
AutoMesh.BoundingBox: Returns the bounding box of the given mesh, and
caches the result for quick response on subsequent calls.
AutoMesh.BoundingSphere: Returns the bounding sphere of the given
mesh, and caches the result for quick response on subsequent calls.
AutoMesh.BoundingSphereMin: Returns the smaller of the bounding box or
bounding sphere, and caches the result for quick response on subsequent calls.
For some reason, DirectX doesn't always return the smallest (or even a small)
bounding sphere. This function comes closer, even though it's not perfect.
AutoMesh.GetVertices: Returns an array<Vector3> of all the
vertices in the mesh, and caches the result for quick response on subsequent
calls. This can be useful for hit detection.
AutoMesh.Clone: Clone a mesh and all the textures it uses, optionally
changing the format.
AutoTexture.Clone: Clone a texture, optionally changing the format.
About Main() and the "Render Loop":
You can use the default Main() function generated by Visual C# Express.
Upon initialization, the DirectX control sets up a thread which sends your
application a
message whenever a new frame needs to be rendered. The frame is presented
by this thread. Your windows application thread is never blocked by
DirectX.
Loader Lock Exception:
If you are creating your project from scratch, you might get a Loader Lock
exception when debugging in the IDE. The workaround is to disable the
LoaderLock exception from the Debug... Exceptions.. Managed Debugging
Assistants menu. WARNING: See my next comment below!
Versions:
Version 10, Feb 3, 2007: 1) Better handling of the
OutOfVideoMemoryException. The control now detects this situation and
tries to reduce video memory usage, first by using a BackBufferCount of
one, and then by simulating the full screen mode in windowed mode. 2)
The DxFullScreen property now switches screen modes immediately
provided it is not changed from the the render function and the control has been
initialized. 3) Added DxSimulateFullScreen property which
allows you to mix full screen mode with other forms and windows. 4)
The control detects if it is running inside the Visual Studio designer, and
won't run. This prevents the control from crashing the visual studio
designer. 5) Added a short delay at startup (debug mode only) to prevent
visual studio from hanging when debugging. 6) Other misc. changes.
Version 9, 6-16-2006: Replaced debug DLL with retail DLL. Ooops!
Version 8, 5-30-2006: Multi-Threaded for completely non-blocking UI
thread. Synchronizes to monitor refresh (VSYNC) in windowed mode.
Upgraded to April 2006 DirectX SDK. Fixed hibernation/logout bug.
Fixed (very rare) DirectX setup bug.
Version 7, 11-19-2005: Fixed bug when user shrinks window too much
with DxAutoResize set to true. In full screen mode the control always
grabs the focus to prevent other "invisible" controls from steeling it.
Removed debug code because Visual C# Express can debug exceptions correctly.
NOTE: Former versions of this control used a modified Main()
function which did not call SetCompatibleTextRenderingDefault.
Version 6a, 11-13-2005: Control upgraded for non beta version of .NET
2.0 and latest release of DirectX SDK (October 2005).
Version 5, 10-23-2005: When switching from full screen mode to a
maximized window, the form size now accounts for the windows task bar and screen
flicker is reduced.
Version 4, 10-19-2005: Added support for mesh subsets with different
textures. Added AutoMesh.LoadFromXFile. Added mesh
viewer to sample code. Changed ComputeBoundingBox and ComputeBoundingSphere to
BoundingBox and BoundingSphere.
Added BoundingSphereMin function.
Version 3, 09-13-2005: Added Color32 class. Added alpha
to sample.
Version 2, 09-05-2005: Added AutoVertexBuffer and AutoTexture example
code.
Version 1, 09-04-2005: Initial version.
Copyright Notice:
Some portions of this control are copyright by Microsoft. All other
parts are released with the following copyright notice:
Copyright (C) 2005 by Jeremy Spiller. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|