Jump to content
  • Advertisement
Sign in to follow this  
Psychopathetica

DirectX9 VB.Net Resizing Window

This topic is 2499 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm having an issue with DirectX9 and VB.Net with resizing. Whenever the window is resized, the program locks up. Everything else is working fine though. If anyone has any suggestions on what I should do let me know. Thanks in advance. Here's my code I currently have:


Option Explicit On
Option Strict On

Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D

Public Class frmMain

Private Const COLOR_DEPTH_16_BIT As Format = Direct3D.Format.R5G6B5
Private Const COLOR_DEPTH_24_BIT As Format = Direct3D.Format.A8R8G8B8
Private Const COLOR_DEPTH_32_BIT As Format = Direct3D.Format.X8R8G8B8

Private Direct3D_Device As Direct3D.Device

Private Fullscreen_Enabled As Boolean
Private Running As Boolean = True

Private Vertex_List As CustomVertex.TransformedColoredTextured() = New CustomVertex.TransformedColoredTextured(0 To 3) {} 'create an array of vertices
Private Texture As Direct3D.Texture

Private Function Create_TLVertex(ByVal X As Single, ByVal Y As Single, ByVal Z As Single, ByVal RHW As Single, ByVal Color As Integer, ByVal TU As Integer, ByVal TV As Integer) As CustomVertex.TransformedColoredTextured

Create_TLVertex.Position = New Vector4(X, Y, Z, 1)
Create_TLVertex.Rhw = RHW
Create_TLVertex.Color = Color
Create_TLVertex.Tu = TU
Create_TLVertex.Tv = TV

End Function

Private Sub DirectX9_Initialize()

Dim Display_Mode As Direct3D.DisplayMode
Dim Direct3D_Window As PresentParameters = New PresentParameters

If Fullscreen_Enabled = True Then
Display_Mode.Width = 800
Display_Mode.Height = 600
Display_Mode.Format = COLOR_DEPTH_16_BIT
'Check to see if fullscreen mode is supported before you use it.
If Direct3D.Manager.CheckDeviceType(0, Direct3D.DeviceType.Hardware, Display_Mode.Format, Display_Mode.Format, False) Then
' Perfect, this is valid
Direct3D_Window.Windowed = False
Direct3D_Window.BackBufferCount = 1
Direct3D_Window.BackBufferWidth = Display_Mode.Width
Direct3D_Window.BackBufferHeight = Display_Mode.Height
Else
MessageBox.Show("Your video card doesn't support this screen resolution.", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Else
Direct3D_Window.Windowed = True
End If

Direct3D_Window.SwapEffect = SwapEffect.Copy
Direct3D_Window.BackBufferFormat = Display_Mode.Format

'Create our device
Direct3D_Device = New Direct3D.Device(0, Direct3D.DeviceType.Hardware, Me.Handle, CreateFlags.SoftwareVertexProcessing, Direct3D_Window)

End Sub

Private Sub Create_Polygon()

Vertex_List(0) = Create_TLVertex(0, 0, 0, 1, System.Drawing.Color.FromArgb(255, 255, 255, 255).ToArgb, 0, 0)
Vertex_List(1) = Create_TLVertex(50, 0, 0, 1, System.Drawing.Color.FromArgb(255, 255, 255, 255).ToArgb, 1, 0)
Vertex_List(2) = Create_TLVertex(0, 50, 0, 1, System.Drawing.Color.FromArgb(255, 255, 255, 255).ToArgb, 0, 1)
Vertex_List(3) = Create_TLVertex(50, 50, 0, 1, System.Drawing.Color.FromArgb(255, 255, 255, 255).ToArgb, 1, 1)

End Sub

Private Sub Draw_Polygon()

Direct3D_Device.VertexFormat = CustomVertex.TransformedColored.Format
Direct3D_Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, Vertex_List)

End Sub

Private Sub Game_Loop()

Do While Running = True
Direct3D_Device.Clear(ClearFlags.Target, Color.Black, 1.0, 0)
Direct3D_Device.BeginScene()
'Rendering code goes here.
Create_Polygon()
Draw_Polygon()
Direct3D_Device.EndScene()
Direct3D_Device.Present()
Application.DoEvents()
Loop

End Sub

Private Sub Main()

If MessageBox.Show("Click Yes to go to full screen (Recommended)", "", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = DialogResult.Yes Then
Fullscreen_Enabled = True
End If

With Me
.Show()
.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.Opaque, True) 'Do not draw forms background
.Text = "DirectX Tutorial"
If Fullscreen_Enabled = True Then .FormBorderStyle = Windows.Forms.FormBorderStyle.None
End With

DirectX9_Initialize()

End Sub

Private Sub Shutdown()

Running = False
Direct3D_Device = Nothing
Application.Exit()

End Sub

Private Sub frmMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

If e.KeyCode = Keys.Escape Then Shutdown()

End Sub

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Main()

End Sub

Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

Shutdown()

End Sub

Private Sub frmMain_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

Game_Loop()

End Sub
End Class

Share this post


Link to post
Share on other sites
Advertisement
Nevermind I solved it. I simply put the game loop into its own function called Render() and whenever the Form gets resized, it stops the game loop by setting it to False, Renders, and sets it back to true. When you stop resizing, it fires the Paint event and enters back into the loop. I even ran some tests such as making the square move and it continues to move as it resizes rather than halts the program till release. Heres my solution:


Option Explicit On
Option Strict On

Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D

Public Class frmMain

Private Const COLOR_DEPTH_16_BIT As Format = Direct3D.Format.R5G6B5
Private Const COLOR_DEPTH_24_BIT As Format = Direct3D.Format.A8R8G8B8
Private Const COLOR_DEPTH_32_BIT As Format = Direct3D.Format.X8R8G8B8

Private Direct3D_Device As Direct3D.Device
Private Display_Mode As Direct3D.DisplayMode
Private Direct3D_Window As PresentParameters = New PresentParameters

Private Fullscreen_Enabled As Boolean
Private Running As Boolean = True

Private Vertex_List As CustomVertex.TransformedColoredTextured() = New CustomVertex.TransformedColoredTextured(0 To 3) {} 'create an array of vertices
Private Texture As Direct3D.Texture

Private Function Create_TLVertex(ByVal X As Single, ByVal Y As Single, ByVal Z As Single, ByVal RHW As Single, ByVal Color As Integer, ByVal TU As Integer, ByVal TV As Integer) As CustomVertex.TransformedColoredTextured

Create_TLVertex.Position = New Vector4(X, Y, Z, 1)
Create_TLVertex.Rhw = RHW
Create_TLVertex.Color = Color
Create_TLVertex.Tu = TU
Create_TLVertex.Tv = TV

End Function

Private Sub DirectX9_Initialize()

If Fullscreen_Enabled = True Then
Display_Mode.Width = 800
Display_Mode.Height = 600
Display_Mode.Format = COLOR_DEPTH_16_BIT
'Check to see if fullscreen mode is supported before you use it.
If Direct3D.Manager.CheckDeviceType(0, Direct3D.DeviceType.Hardware, Display_Mode.Format, Display_Mode.Format, False) Then
' Perfect, this is valid
Direct3D_Window.Windowed = False
Direct3D_Window.BackBufferCount = 1
Direct3D_Window.BackBufferWidth = Display_Mode.Width
Direct3D_Window.BackBufferHeight = Display_Mode.Height
Else
MessageBox.Show("Your video card doesn't support this screen resolution.", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Else
Direct3D_Window.Windowed = True
End If

Direct3D_Window.SwapEffect = SwapEffect.Copy
Direct3D_Window.BackBufferFormat = Display_Mode.Format

'Create our device
Direct3D_Device = New Direct3D.Device(0, Direct3D.DeviceType.Hardware, Me.Handle, CreateFlags.SoftwareVertexProcessing, Direct3D_Window)

End Sub

Private Sub Create_Polygon()

Vertex_List(0) = Create_TLVertex(0, 0, 0, 1, System.Drawing.Color.FromArgb(255, 255, 255, 255).ToArgb, 0, 0)
Vertex_List(1) = Create_TLVertex(50, 0, 0, 1, System.Drawing.Color.FromArgb(255, 255, 255, 255).ToArgb, 1, 0)
Vertex_List(2) = Create_TLVertex(0, 50, 0, 1, System.Drawing.Color.FromArgb(255, 255, 255, 255).ToArgb, 0, 1)
Vertex_List(3) = Create_TLVertex(50, 50, 0, 1, System.Drawing.Color.FromArgb(255, 255, 255, 255).ToArgb, 1, 1)

End Sub

Private Sub Draw_Polygon()

Direct3D_Device.VertexFormat = CustomVertex.TransformedColored.Format
Direct3D_Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, Vertex_List)

End Sub

Private Sub Render()

Direct3D_Device.Clear(ClearFlags.Target, Color.Black, 1.0, 0)
Direct3D_Device.BeginScene()
'Rendering code goes here.
Create_Polygon()
Draw_Polygon()
Direct3D_Device.EndScene()
Direct3D_Device.Present()
Application.DoEvents()

End Sub

Private Sub Game_Loop()

Do While Running = True
Render()
Loop

End Sub

Private Sub Main()

If MessageBox.Show("Click Yes to go to full screen (Recommended)", "", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = DialogResult.Yes Then
Fullscreen_Enabled = True
End If

With Me
.Show()
.SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.Opaque, True) 'Do not draw forms background
.Text = "DirectX Tutorial"
If Fullscreen_Enabled = True Then .FormBorderStyle = Windows.Forms.FormBorderStyle.None
End With

DirectX9_Initialize()

End Sub

Private Sub Shutdown()

Running = False
Direct3D_Device = Nothing
Application.Exit()

End Sub

Private Sub frmMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown

If e.KeyCode = Keys.Escape Then Shutdown()

End Sub

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Main()

End Sub

Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

Shutdown()

End Sub

Private Sub frmMain_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

Game_Loop()

End Sub

Private Sub frmMain_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize

If Fullscreen_Enabled = False Then
Running = False
Render()
Running = True
End If

End Sub

End Class

Share this post


Link to post
Share on other sites
Is that the latest Microsoft Direct3D library you are using? As a user of SLIMDX with VB.NET I'd be interested in hearing your thoughts on the microsoft library.

Share this post


Link to post
Share on other sites
I been programming in Visual Basic 4-6 for about 15 years now and been coding in DirectX8 for about 7 years. I just recently got into VB.Net a few years ago and DirectX9 literaly recently, so I'm fairly new to it yet have a lot of experience in DirectX in general. In VB6 and DirectX8, when you resize the window, it stretched it for you. I didn't like the fact that in DirectX9 you gotta manually code that part, but it does have nice features such as pixel shading and stuff. As for SLIMDX, I never heard of it. The DirectX I'm working with is 9 even though DirectX 11 is out, but it is the latest version of 9.

Share this post


Link to post
Share on other sites
OK - I see. SLIMDX is largely a .NET wrapper library to DirectX. I use it for my VB.NET code, and it supports everything from DX9 to DX11, in both 32 and 64 bit.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!