September 16, 2004

DirectX 9.0c (Summer 2004) and Windows Forms

In the original release of Managed DirectX, the MDX team did a few things to make DirectX friendlier to Windows Forms developers.

For example, if you created a windowed device that resized, when the device resized, it would automatically resize the appropriate buffers for you. It was even called out as a desirable feature in Tom Miller's Managed DirectX book from SAMS Publishing. No longer. Now, it keeps the buffers the same size, but scales the output to the new output size.

If you want the original behavior, store your Device and your PresentParameters objects as Form-scope (or Control-scope, depending on your usage) variables. In your ResizeEvent, put the following (fill in the {} with the appropriate variable names:

If {Device} Is Nothing Then Return

If {Device}.Disposed Then Return

{PresentParameters}.BackBufferWidth = Math.Max(Me.Width, 1)
{PresentParameters}.BackBufferHeight = Math.Max(Me.Height, 1)

Viola! Fixed.

By the way, Tom, it's undocumented behavior changes like this that would cause me not to use Managed DirectX.

Update: Added Math.Max(x, 1) to fix bug where width or height could be 0, thereby returning an extremely unhelpful exception.

No comments: