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);}
Looks quite neat-and-tidy, which is nice.
[lol] Honest? I'll trust you then...
Jack