Managed DirectX, Part 1

posted in Journal #259850
Published July 06, 2005
Advertisement
MainForm.cs:
using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using Direct3D = Microsoft.DirectX.Direct3D;namespace Skeleton{	public class MainForm : System.Windows.Forms.Form	{		/// 		/// Clean up any resources being used.		/// 

protected override void Dispose( bool disposing )
{
...
}

#region Windows Form Designer generated code
...
#endregion

#region Declarations

Direct3D.Device device = null;

#endregion

public MainForm()
{
// Required for Windows Form Designer support
InitializeComponent();

Application.Idle += new EventHandler(OnApplicationIdle);
InitializeGraphics();
}

// The main entry point for the application.
[STAThread]
static void Main()
{
using(MainForm form = new MainForm())
Application.Run(form);
}

void OnApplicationIdle(object sender, EventArgs e)
{
while(IsApplicationIdle)
{
// Render a frame during idle time (no messages are waiting)
device.Clear(Direct3D.ClearFlags.ZBuffer | Direct3D.ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0, null);
device.BeginScene();

device.EndScene();
device.Present();
}
}

bool IsApplicationIdle
{
get
{
NativeMethods.Message msg;
return !NativeMethods.PeekMessage(out msg, IntPtr.Zero, 0, 0, 0);
}
}

void InitializeGraphics()
{
device = new Direct3D.Device(GetAdapter(), GetDeviceType(), GetGraphicsWindow(), GetCreateFlags(),
GetPresentParameters());
}

int GetAdapter()
{
return 0;
}

Direct3D.DeviceType GetDeviceType()
{
return Direct3D.DeviceType.Hardware;
}

System.Windows.Forms.Control GetGraphicsWindow()
{
return this;
}

Direct3D.CreateFlags GetCreateFlags()
{
return Direct3D.CreateFlags.SoftwareVertexProcessing;
}

Direct3D.PresentParameters GetPresentParameters()
{
Direct3D.PresentParameters presentParameters = new Direct3D.PresentParameters();
presentParameters.Windowed = true;
presentParameters.EnableAutoDepthStencil = true;
presentParameters.AutoDepthStencilFormat = Direct3D.DepthFormat.D16;
presentParameters.PresentationInterval = Direct3D.PresentInterval.Immediate;
presentParameters.BackBufferFormat = Direct3D.Format.Unknown;
presentParameters.SwapEffect = Direct3D.SwapEffect.Discard;

return presentParameters;
}
}
}




NativeMethods.cs:
using System;using System.Runtime.InteropServices;class NativeMethods{	[StructLayout(LayoutKind.Sequential)]	public struct Message	{		public IntPtr hWnd;		public uint msg;		public IntPtr wParam;		public IntPtr lParam;		public uint time;		public System.Drawing.Point p;	}	[System.Security.SuppressUnmanagedCodeSecurity] // We won't use this maliciously	[DllImport("User32.dll", CharSet=CharSet.Auto)]	public static extern bool PeekMessage(out Message msg, IntPtr hWnd, uint messageFilterMin, uint messageFilterMax, uint flags);}
Previous Entry Finals, .NET and NeXe
0 likes 2 comments

Comments

jollyjeffers
Is that code from the SDK, or absolutely minimal stuff you wrote from scratch?

Looks quite neat-and-tidy, which is nice.

Quote:// We won't use this maliciously

[lol] Honest? I'll trust you then...

Jack
July 06, 2005 03:17 PM
Muhammad Haggag
Quote:Is that code from the SDK, or absolutely minimal stuff you wrote from scratch?

I wrote the code from scratch, but the message-loop code is adapted (i.e. stolen shamelessly) from Tom Miller's Blog.


Quote:
Quote:// We won't use this maliciously

Honest? I'll trust you then...

[lol]
It's Tom's comment, as is. I thought it was funny too, when I saw the code [smile]
July 06, 2005 03:52 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement

Latest Entries

Opera 9 w000000t

1315 views

Damn

1202 views

VBE fun, #2

1310 views

VBE fun

1353 views

French, #1

1438 views

Mult-tasking

1199 views

DirectInput hooking

1089 views

MDX overlay sample

1355 views
Advertisement