Hi, my problem is currently that running Managed DirectX 9.0 (MDX 1.1 I believe) in windowed mode causes an InvalidCallException whenever the form (and therefore device) is resized. I'm not using any VertexBuffers which usually seem to cause the problem, only Sprites and Textures.
At the moment, I recreate the presentation parameters and reset the device in the DeviceResizing event. I've tried most of the simple things already but feel free to suggest anything still.
Here's my code:
[Device Initialisation]
Protected Overridable Sub InitializeGraphics()
If _disposed Then Throw New ObjectDisposedException(Me.GetType().Name)
Trace.WriteLine("Initializing Graphics Device")
' Get display mode and capibilities
Dim displayMode As DisplayMode
Dim displayCaps As Direct3D.Caps
displayMode = Direct3D.Manager.Adapters.Default.CurrentDisplayMode
displayCaps = Direct3D.Manager.GetDeviceCaps(Direct3D.Manager.Adapters.Default.Adapter, Direct3D.DeviceType.Hardware)
' Check for hardware processing
Dim flags As CreateFlags
If displayCaps.DeviceCaps.SupportsHardwareRasterization Then
flags = CreateFlags.HardwareVertexProcessing
Else
flags = CreateFlags.SoftwareVertexProcessing
End If
' Check for pure device
If displayCaps.DeviceCaps.SupportsPureDevice AndAlso _
((flags AndAlso CreateFlags.HardwareVertexProcessing) <> 0) Then
flags = flags Or CreateFlags.PureDevice
End If
' Create device from presentation parameters
BuildPresentParameters()
_graphics = New Direct3D.Device( _
Direct3D.Manager.Adapters.Default.Adapter, _
displayCaps.DeviceType, _
Me, flags, _presentParams)
' Set form size
Me.Size = _resolution
End Sub
And here's the DeviceResizing code plus additional helper functions need for init & resizing:
Public Sub ResetGraphics()
If _graphics Is Nothing Or _deviceLost Then Exit Sub
' Stop engine timer
_timer.Stop()
' Recreate presentation parameters
BuildPresentParameters()
' Reset graphics device
Try
_graphics.Reset(_presentParams)
Catch ex As DeviceLostException
' Device lost
_deviceLost = True
' Write to trace
Trace.WriteLine("Device lost in game loop")
End Try
End Sub
Protected Sub BuildPresentParameters()
If _disposed Then Throw New ObjectDisposedException(Me.GetType().Name)
Trace.WriteLine("Build Presentation Parameters")
' Create presentation parameters for graphics device
_presentParams = New PresentParameters()
_presentParams.BackBufferCount = 1
_presentParams.BackBufferFormat = Format.A8R8G8B8
_presentParams.BackBufferWidth = _resolution.Width
_presentParams.BackBufferHeight = _resolution.Height
_presentParams.BackBufferCount = 1
_presentParams.ForceNoMultiThreadedFlag = False
_presentParams.SwapEffect = SwapEffect.Discard
_presentParams.PresentationInterval = PresentInterval.Immediate
_presentParams.PresentFlag = PresentFlag.None
_presentParams.Windowed = _isWindowed
End Sub
Private Sub _graphics_DeviceResizing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles _graphics.DeviceResizing
'' Stop device resizing
'e.Cancel = True
ResetGraphics()
End Sub
I hope this is a simple problem...you should have all the code/information here but please tell me if you need more.
Thanks in advance for any help.