Results 1 to 2 of 2

Thread: [RESOLVED] DirectX9 Window Resizing Problem

  1. #1

    Thread Starter
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Resolved [RESOLVED] DirectX9 Window Resizing Problem

    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:

    vb.net Code:
    1. Option Explicit On
    2. Option Strict On
    3.  
    4. Imports Microsoft.DirectX
    5. Imports Microsoft.DirectX.Direct3D
    6.  
    7. Public Class frmMain
    8.  
    9.     Private Const COLOR_DEPTH_16_BIT As Format = Direct3D.Format.R5G6B5
    10.     Private Const COLOR_DEPTH_24_BIT As Format = Direct3D.Format.A8R8G8B8
    11.     Private Const COLOR_DEPTH_32_BIT As Format = Direct3D.Format.X8R8G8B8
    12.  
    13.     Private Direct3D_Device As Direct3D.Device
    14.  
    15.     Private Fullscreen_Enabled As Boolean
    16.     Private Running As Boolean = True
    17.  
    18.     Private Vertex_List As CustomVertex.TransformedColoredTextured() = New CustomVertex.TransformedColoredTextured(0 To 3) {} 'create an array of vertices
    19.     Private Texture As Direct3D.Texture
    20.  
    21.     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
    22.  
    23.         Create_TLVertex.Position = New Vector4(X, Y, Z, 1)
    24.         Create_TLVertex.Rhw = RHW
    25.         Create_TLVertex.Color = Color
    26.         Create_TLVertex.Tu = TU
    27.         Create_TLVertex.Tv = TV
    28.  
    29.     End Function
    30.  
    31.     Private Sub DirectX9_Initialize()
    32.  
    33.         Dim Display_Mode As Direct3D.DisplayMode
    34.         Dim Direct3D_Window As PresentParameters = New PresentParameters
    35.  
    36.         If Fullscreen_Enabled = True Then
    37.             Display_Mode.Width = 800
    38.             Display_Mode.Height = 600
    39.             Display_Mode.Format = COLOR_DEPTH_16_BIT
    40.             'Check to see if fullscreen mode is supported before you use it.
    41.             If Direct3D.Manager.CheckDeviceType(0, Direct3D.DeviceType.Hardware, Display_Mode.Format, Display_Mode.Format, False) Then
    42.                 ' Perfect, this is valid
    43.                 Direct3D_Window.Windowed = False
    44.                 Direct3D_Window.BackBufferCount = 1
    45.                 Direct3D_Window.BackBufferWidth = Display_Mode.Width
    46.                 Direct3D_Window.BackBufferHeight = Display_Mode.Height
    47.             Else
    48.                 MessageBox.Show("Your video card doesn't support this screen resolution.", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
    49.             End If
    50.         Else
    51.             Direct3D_Window.Windowed = True
    52.         End If
    53.  
    54.         Direct3D_Window.SwapEffect = SwapEffect.Copy
    55.         Direct3D_Window.BackBufferFormat = Display_Mode.Format
    56.  
    57.         'Create our device
    58.         Direct3D_Device = New Direct3D.Device(0, Direct3D.DeviceType.Hardware, Me.Handle, CreateFlags.SoftwareVertexProcessing, Direct3D_Window)
    59.  
    60.     End Sub
    61.  
    62.     Private Sub Create_Polygon()
    63.  
    64.         Vertex_List(0) = Create_TLVertex(0, 0, 0, 1, System.Drawing.Color.FromArgb(255, 255, 255, 255).ToArgb, 0, 0)
    65.         Vertex_List(1) = Create_TLVertex(50, 0, 0, 1, System.Drawing.Color.FromArgb(255, 255, 255, 255).ToArgb, 1, 0)
    66.         Vertex_List(2) = Create_TLVertex(0, 50, 0, 1, System.Drawing.Color.FromArgb(255, 255, 255, 255).ToArgb, 0, 1)
    67.         Vertex_List(3) = Create_TLVertex(50, 50, 0, 1, System.Drawing.Color.FromArgb(255, 255, 255, 255).ToArgb, 1, 1)
    68.  
    69.     End Sub
    70.  
    71.     Private Sub Draw_Polygon()
    72.  
    73.         Direct3D_Device.VertexFormat = CustomVertex.TransformedColored.Format
    74.         Direct3D_Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, Vertex_List)
    75.  
    76.     End Sub
    77.  
    78.     Private Sub Game_Loop()
    79.  
    80.         Do While Running = True
    81.             Direct3D_Device.Clear(ClearFlags.Target, Color.Black, 1.0, 0)
    82.             Direct3D_Device.BeginScene()
    83.             'Rendering code goes here.
    84.             Create_Polygon()
    85.             Draw_Polygon()
    86.             Direct3D_Device.EndScene()
    87.             Direct3D_Device.Present()
    88.             Application.DoEvents()
    89.         Loop
    90.  
    91.     End Sub
    92.  
    93.     Private Sub Main()
    94.  
    95.         If MessageBox.Show("Click Yes to go to full screen (Recommended)", "", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = DialogResult.Yes Then
    96.             Fullscreen_Enabled = True
    97.         End If
    98.  
    99.         With Me
    100.             .Show()
    101.             .SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.Opaque, True) 'Do not draw forms background
    102.             .Text = "DirectX Tutorial"
    103.             If Fullscreen_Enabled = True Then .FormBorderStyle = Windows.Forms.FormBorderStyle.None
    104.         End With
    105.  
    106.         DirectX9_Initialize()
    107.  
    108.     End Sub
    109.  
    110.     Private Sub Shutdown()
    111.  
    112.         Running = False
    113.         Direct3D_Device = Nothing
    114.         Application.Exit()
    115.  
    116.     End Sub
    117.  
    118.     Private Sub frmMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    119.  
    120.         If e.KeyCode = Keys.Escape Then Shutdown()
    121.  
    122.     End Sub
    123.  
    124.     Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    125.  
    126.         Main()
    127.  
    128.     End Sub
    129.  
    130.     Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    131.  
    132.         Shutdown()
    133.  
    134.     End Sub
    135.  
    136.     Private Sub frmMain_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    137.  
    138.         Game_Loop()
    139.  
    140.     End Sub
    141. End Class

  2. #2

    Thread Starter
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: DirectX9 Window Resizing Problem

    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:

    vb.net Code:
    1. Option Explicit On
    2. Option Strict On
    3.  
    4. Imports Microsoft.DirectX
    5. Imports Microsoft.DirectX.Direct3D
    6.  
    7. Public Class frmMain
    8.  
    9.     Private Const COLOR_DEPTH_16_BIT As Format = Direct3D.Format.R5G6B5
    10.     Private Const COLOR_DEPTH_24_BIT As Format = Direct3D.Format.A8R8G8B8
    11.     Private Const COLOR_DEPTH_32_BIT As Format = Direct3D.Format.X8R8G8B8
    12.  
    13.     Private Direct3D_Device As Direct3D.Device
    14.     Private Display_Mode As Direct3D.DisplayMode
    15.     Private Direct3D_Window As PresentParameters = New PresentParameters
    16.  
    17.     Private Fullscreen_Enabled As Boolean
    18.     Private Running As Boolean = True
    19.  
    20.     Private Vertex_List As CustomVertex.TransformedColoredTextured() = New CustomVertex.TransformedColoredTextured(0 To 3) {} 'create an array of vertices
    21.     Private Texture As Direct3D.Texture
    22.  
    23.     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
    24.  
    25.         Create_TLVertex.Position = New Vector4(X, Y, Z, 1)
    26.         Create_TLVertex.Rhw = RHW
    27.         Create_TLVertex.Color = Color
    28.         Create_TLVertex.Tu = TU
    29.         Create_TLVertex.Tv = TV
    30.  
    31.     End Function
    32.  
    33.     Private Sub DirectX9_Initialize()
    34.  
    35.         If Fullscreen_Enabled = True Then
    36.             Display_Mode.Width = 800
    37.             Display_Mode.Height = 600
    38.             Display_Mode.Format = COLOR_DEPTH_16_BIT
    39.             'Check to see if fullscreen mode is supported before you use it.
    40.             If Direct3D.Manager.CheckDeviceType(0, Direct3D.DeviceType.Hardware, Display_Mode.Format, Display_Mode.Format, False) Then
    41.                 ' Perfect, this is valid
    42.                 Direct3D_Window.Windowed = False
    43.                 Direct3D_Window.BackBufferCount = 1
    44.                 Direct3D_Window.BackBufferWidth = Display_Mode.Width
    45.                 Direct3D_Window.BackBufferHeight = Display_Mode.Height
    46.             Else
    47.                 MessageBox.Show("Your video card doesn't support this screen resolution.", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
    48.             End If
    49.         Else
    50.             Direct3D_Window.Windowed = True
    51.         End If
    52.  
    53.         Direct3D_Window.SwapEffect = SwapEffect.Copy
    54.         Direct3D_Window.BackBufferFormat = Display_Mode.Format
    55.  
    56.         'Create our device
    57.         Direct3D_Device = New Direct3D.Device(0, Direct3D.DeviceType.Hardware, Me.Handle, CreateFlags.SoftwareVertexProcessing, Direct3D_Window)
    58.  
    59.     End Sub
    60.  
    61.     Private Sub Create_Polygon()
    62.  
    63.         Vertex_List(0) = Create_TLVertex(0, 0, 0, 1, System.Drawing.Color.FromArgb(255, 255, 255, 255).ToArgb, 0, 0)
    64.         Vertex_List(1) = Create_TLVertex(50, 0, 0, 1, System.Drawing.Color.FromArgb(255, 255, 255, 255).ToArgb, 1, 0)
    65.         Vertex_List(2) = Create_TLVertex(0, 50, 0, 1, System.Drawing.Color.FromArgb(255, 255, 255, 255).ToArgb, 0, 1)
    66.         Vertex_List(3) = Create_TLVertex(50, 50, 0, 1, System.Drawing.Color.FromArgb(255, 255, 255, 255).ToArgb, 1, 1)
    67.  
    68.     End Sub
    69.  
    70.     Private Sub Draw_Polygon()
    71.  
    72.         Direct3D_Device.VertexFormat = CustomVertex.TransformedColored.Format
    73.         Direct3D_Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, Vertex_List)
    74.  
    75.     End Sub
    76.  
    77.     Private Sub Render()
    78.  
    79.         Direct3D_Device.Clear(ClearFlags.Target, Color.Black, 1.0, 0)
    80.         Direct3D_Device.BeginScene()
    81.         'Rendering code goes here.
    82.         Create_Polygon()
    83.         Draw_Polygon()
    84.         Direct3D_Device.EndScene()
    85.         Direct3D_Device.Present()
    86.         Application.DoEvents()
    87.  
    88.     End Sub
    89.  
    90.     Private Sub Game_Loop()
    91.  
    92.         Do While Running = True
    93.             Render()
    94.         Loop
    95.  
    96.     End Sub
    97.  
    98.     Private Sub Main()
    99.  
    100.         If MessageBox.Show("Click Yes to go to full screen (Recommended)", "", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = DialogResult.Yes Then
    101.             Fullscreen_Enabled = True
    102.         End If
    103.  
    104.         With Me
    105.             .Show()
    106.             .SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.Opaque, True) 'Do not draw forms background
    107.             .Text = "DirectX Tutorial"
    108.             If Fullscreen_Enabled = True Then .FormBorderStyle = Windows.Forms.FormBorderStyle.None
    109.         End With
    110.  
    111.         DirectX9_Initialize()
    112.  
    113.     End Sub
    114.  
    115.     Private Sub Shutdown()
    116.  
    117.         Running = False
    118.         Direct3D_Device = Nothing
    119.         Application.Exit()
    120.  
    121.     End Sub
    122.  
    123.     Private Sub frmMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    124.  
    125.         If e.KeyCode = Keys.Escape Then Shutdown()
    126.  
    127.     End Sub
    128.  
    129.     Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    130.  
    131.         Main()
    132.  
    133.     End Sub
    134.  
    135.     Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    136.  
    137.         Shutdown()
    138.  
    139.     End Sub
    140.  
    141.     Private Sub frmMain_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    142.  
    143.         Game_Loop()
    144.  
    145.     End Sub
    146.  
    147.     Private Sub frmMain_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
    148.  
    149.         If Fullscreen_Enabled = False Then
    150.             Running = False
    151.             Render()
    152.             Running = True
    153.         End If
    154.     End Sub
    155.  
    156. End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width