Hey guys, figured I'd finally take a break from OpenGL to look at some Direct3D in C# and stumbled upon this tutorial:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb153258%28v=vs.85%29.aspx
Works fine except for when you resize the window such that its client-height becomes 0; in which case the device suddenly becomes invalid and throws an InvalidCallException whenever you try to use it again.
This is the code I wrote while following the tutorial:
//
// D3DForm.cs
//
//
// Includes
//
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
//
// D3DForm class definition
//
class D3DForm : Form
{
//
// Used to manage a D3D rendering device
//
private Device m_device = null;
//=========================================================================
//
// DESCRIPTION
//
// Constructor
//
// PARAMETERS
//
// RETURNS
//
//=========================================================================
public D3DForm()
{
//
// Initialize the form
//
this.ClientSize = new Size(640, 480);
}
//=========================================================================
//
// DESCRIPTION
//
// Initializes the D3D rendering device
//
// PARAMETERS
//
// RETURNS
//
// Returns true if successful
//
//=========================================================================
public bool InitializeGraphics()
{
try
{
PresentParameters present_params = new PresentParameters();
present_params.Windowed = true;
present_params.SwapEffect = SwapEffect.Discard;
m_device = new Device
(
0,
DeviceType.Hardware,
this,
CreateFlags.SoftwareVertexProcessing,
present_params
);
}
catch(DirectXException)
{
return false;
}
return true;
}
//=========================================================================
//
// DESCRIPTION
//
// Renders the scene
//
// PARAMETERS
//
// RETURNS
//
//=========================================================================
public void Render()
{
if(m_device != null)
{
m_device.Clear(ClearFlags.Target, Color.Blue, 1.0f, 0); // InvalidCallException thrown
m_device.BeginScene();
m_device.EndScene();
m_device.Present();
}
}
//=========================================================================
//
// DESCRIPTION
//
// Application entry point
//
// PARAMETERS
//
// RETURNS
//
//=========================================================================
private static void Main()
{
using(D3DForm form = new D3DForm())
{
if(form.InitializeGraphics())
{
form.Show();
//
// Application loop
//
while(form.Created)
{
form.Render();
Application.DoEvents();
}
}
}
}
}
How do I repair the device once this happens? How do I prevent it from becoming invalid - do I have to prevent the window's client-size from becoming zero?
Thanks!