public bool InitializeGraphics()
{
try
{
Cursor.Hide();
AdapterInformation ai = Manager.Adapters.Default;
Caps caps = Manager.GetDeviceCaps(ai.Adapter, DeviceType.Hardware);
presentParams.Windowed = !doFullscreen;
presentParams.SwapEffect = SwapEffect.Discard; // Discard the frames
presentParams.EnableAutoDepthStencil = true; // Turn on a Depth stencil
presentParams.AutoDepthStencilFormat = DepthFormat.D16; // And the stencil format
presentParams.BackBufferCount = 1;
presentParams.BackBufferWidth = m_dwWidth; //screen width
presentParams.BackBufferHeight = m_dwHeight; //screen height
presentParams.BackBufferFormat = ai.CurrentDisplayMode.Format; //color depth
presentParams.DeviceWindow = this;
presentParams.MultiSample = MultiSampleType.None; //anti-aliasing
presentParams.PresentationInterval = PresentInterval.Immediate; //don't wait... draw right away
// Check if gfx card can do HW T&L
CreateFlags flags = CreateFlags.SoftwareVertexProcessing;
if (caps.DeviceCaps.SupportsHardwareTransformAndLight)
flags = CreateFlags.HardwareVertexProcessing;
// Check if gfx card can do rasterization etc
if (caps.DeviceCaps.SupportsPureDevice)
flags |= CreateFlags.PureDevice;
device
= new Device
(ai.
Adapter, DeviceType.
Hardware,
this, flags, presentParams
); //Create a device
device.
DeviceReset += new System.
EventHandler(this.
OnResetDevice);
// Add event handler to cancel device resize
device.
DeviceResizing += new System.ComponentModel.
CancelEventHandler(OnDeviceResizing
);
this.OnCreateDevice(device, null);
this.OnResetDevice(device, null);
return true;
}
catch (DirectXException)
{
// Catch any errors and return a failure
return false;
}
}
void OnDeviceResizing(object sender, System.ComponentModel.CancelEventArgs e)
{
Device device = sender as Device;
// Don't let the device automatically resize itself to the window dimensions in fullscreen mode
// This is needed because at this point the application hasn't gained control of the device yet, leading to a crash.
e.Cancel = !device.PresentationParameters.Windowed;
}