using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using D3D = Microsoft.DirectX.Direct3D;
namespace Nuclex {
/// A window that is drawn into using Direct3D
public partial class Direct3DForm : Form {
/// Initializes the window
public Direct3DForm() {
InitializeComponent();
}
/// Gets executed when the form is brought onto the screen
/// Not used
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
// Set up the presentation parameters for our little demonstration
presentParameters = new D3D.PresentParameters();
presentParameters.IsWindowed = true;
presentParameters.SwapEffect = D3D.SwapEffect.Discard;
presentParameters.EnableAutoDepthStencil = true;
presentParameters.AutoDepthStencilFormat = D3D.DepthFormat.D16;
presentParameters.BackBufferFormat = D3D.Format.R5G6B5;
presentParameters.BackBufferWidth = this.ClientSize.Width;
presentParameters.BackBufferHeight = this.ClientSize.Height;
d3dDevice = new D3D.Device(
0,
D3D.DeviceType.Hardware,
this.Handle,
D3D.CreateFlags.SoftwareVertexProcessing,
presentParameters
);
}
/// Executed when the window has been closed for good
/// Not used
protected override void OnClosed(EventArgs e) {
d3dDevice.Dispose();
}
/// We don't want to paint here to avoid flickering
/// Additional parameters that control painting
protected override void OnPaintBackground(PaintEventArgs e) {}
/// Paints the window contents
/// Additional parameters that control painting
protected override void OnPaint(PaintEventArgs e) {
d3dDevice.BeginScene();
d3dDevice.Clear(
D3D.ClearFlags.Target | D3D.ClearFlags.ZBuffer,
SystemColors.Control,
1.0f,
0
);
OnRender(EventArgs.Empty);
d3dDevice.EndScene();
d3dDevice.Present();
}
protected override void OnResize(EventArgs e) {
base.OnResize(e);
if(d3dDevice != null) {
presentParameters.BackBufferWidth = this.ClientSize.Width;
presentParameters.BackBufferHeight = this.ClientSize.Height;
d3dDevice.Reset(presentParameters);
}
}
/// Executed to render the Direct3D scene
/// Not used
protected virtual void OnRender(EventArgs e) {}
/// The Direct3D rendering device
protected D3D.Device d3dDevice;
/// Presentation settings for the active Direct3D device
protected D3D.PresentParameters presentParameters;
}
} // namespace Nuclex