Results 1 to 17 of 17

Thread: Direct X Autoredraw

  1. #1

    Thread Starter
    Addicted Member cheesebrother's Avatar
    Join Date
    Jul 2011
    Posts
    153

    Direct X Autoredraw

    Been having a lot of fun with DirectX recently. I've got a slight problem here though, about autoredraw. DirectX draws time after time, making it the best to leave autoredraw false. My problem is when i show a message box. It pops up and the game loop stops because it's modal. When it appears a rectangle immediately appears on you drawing window. Then if you move it around even more will go to the form's/picturebox's backcolor because autoredraw is set to false. This makes it look ugly, and cheap so i'd like to take it out. I tried setting autoredraw to true and then show the msgbox but it didn't work.
    Attached Files Attached Files
    Hooked for good.

  2. #2
    College Grad!!! Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Re: Direct X Autoredraw

    Msgbox's I believe halt all events in the particular program. Not really good to use in DirectX apps. Infact non of them do! So what you need to do is probably create your own DirectX style by creating a box, adding some text, and create your own button that is clickable. I did something like that over in my BossKillers game. But yeah definitely keep your autoredraw the false cause DX does it for you.
    Attached Images Attached Images  

  3. #3

    Thread Starter
    Addicted Member cheesebrother's Avatar
    Join Date
    Jul 2011
    Posts
    153

    Re: Direct X Autoredraw

    That's okayish and i'll do that if i have to but isn't there a way to get the picture? In my program vb's msgbox fits the bill perfectly. Another place where i'm killed on this is when i show a common dialog box. A msgbox can be drawn but a common dialog box is much more complicated and also i want the system by system capability of the cdb. I know that in your game a custom drawn msgbox works alright but this isn't a game, it's a very graphical water drainage editor. There are endless problems with autoredraw off, msgbox, cdb, modal forms, and the whole lot. Hopefully there's got to be a way to preserve the screen.
    Hooked for good.

  4. #4
    College Grad!!! Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Re: Direct X Autoredraw

    You can actually use VB's common dialog box in DX apps and not have problems. I did that with my map editor. No halts or graphical problems or nothing.

  5. #5

    Thread Starter
    Addicted Member cheesebrother's Avatar
    Join Date
    Jul 2011
    Posts
    153

    Re: Direct X Autoredraw

    No? By saying no graphical problems do you mean that you can move the dialog box over the form and it won't mess it up? I solved part of the problem by calling the draw event in the form's paint. That fixes modal forms at least. I think that maybe the reason why cdbs don't mess it up for you is because of your os. I noticed on vista that a form autoredraws by default when it doesn't have focus whereas in xp the paint event will fire. Actually i just tried you map editor and it's just the same as msgboxes, it must be a difference with 7. Isn't there some way of directx communicating with the outside like writing an image file? If you could do that then it would be simple. Save a jpg with DX, load it into a picturebox and then your screen will be preserved.
    Hooked for good.

  6. #6
    College Grad!!! Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Re: Direct X Autoredraw

    Oooo I see now. It does mess it up. I'm using Windows 7 Pro 64 bit. But I wouldn't worry too much about that. Like in my map editor, once you load what you need to load, its back to normal. But if you are really really worried about it still, the best you can do is create your own dialog box. It's tricky but it shouldn't be a problem if you are able to work with files through code. You'll need an Origin File Path which generally should start where your application is at. Other programs tend to use Documents as the origin. Then within that folder you load all the folders that exist and put em into a list. Then you load all the files that exist and put em into a list. Once you have all the information you display them through directX into your window you created showing all the folders. With files you generally want to show only the files that are .jpgs or whatever pic formats you want and even need the option to show all files. The folders displayed in itself should be buttons so the user can go through the harddrive. If ya need help I might be able to help ya do this.

  7. #7

    Thread Starter
    Addicted Member cheesebrother's Avatar
    Join Date
    Jul 2011
    Posts
    153

    Re: Direct X Autoredraw

    Ok, so i'm just wondering this now. Is there a way to save the directx screen to an image file? I'd think that dx would have something to accomplish that.
    Hooked for good.

  8. #8
    College Grad!!! Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Re: Direct X Autoredraw

    vb Code:
    1. Public Sub Snapshot(ByVal File_Path As String)
    2.  
    3.     Dim Surface As Direct3DSurface8
    4.     Dim SrcPalette As PALETTEENTRY
    5.     Dim SrcRect As RECT
    6.     Dim Direct3D_Display_Mode As D3DDISPLAYMODE
    7.  
    8.     'get display dimensions
    9.     Direct3D_Device.GetDisplayMode Direct3D_Display_Mode
    10.  
    11.     'create a surface to put front buffer on,
    12.     'GetFrontBuffer always returns D3DFMT_A8R8G8B8
    13.     Set Surface = Direct3D_Device.CreateImageSurface(Direct3D_Display_Mode.Width, Direct3D_Display_Mode.Height, D3DFMT_A8R8G8B8)
    14.  
    15.     'get data from front buffer
    16.     Direct3D_Device.GetFrontBuffer Surface
    17.  
    18.     'we are saving entire area of this surface
    19.    
    20.     With SrcRect
    21.    
    22.         .Left = 0
    23.         .Right = Direct3D_Display_Mode.Width
    24.         .Top = 0
    25.         .Bottom = Direct3D_Display_Mode.Height
    26.        
    27.     End With
    28.  
    29.     'save this surface to a BMP file
    30.     Direct3DX.SaveSurfaceToFile File_Path, D3DXIFF_BMP, Surface, SrcPalette, SrcRect
    31.  
    32. End Sub
    33.  
    34. Public Sub Take_Snapshot()
    35.  
    36. 'Put this in your loop assuming you enabled DirectInput
    37.    
    38. 'If you pressed print screen itll create a directory if it doesnt exist and add snapshots.
    39. 'As you take more snapshots it'll add more and put the appropriate snapshot number
    40. 'depending how many are in that directory.
    41.     If DirectInput_Key_State(DIK_SYSRQ) <> 0 Then
    42.         If Dir$(App.Path & "\Snapshots\", vbDirectory) = "" Then
    43.             MkDir App.Path & "\Snapshots\"
    44.         End If
    45.        
    46.         If Dir$(App.Path & "\Snapshots\SNAP" & Format(Snapshot_Number, "####") & ".bmp") = "" Then
    47.             Snapshot App.Path & "\Snapshots\SNAP" & Format(Snapshot_Number, "####") & ".bmp"
    48.         End If
    49.        
    50.         While Dir$(App.Path & "\Snapshots\SNAP" & Format(Snapshot_Number, "####") & ".bmp") <> ""
    51.             DoEvents
    52.             Snapshot_Number = Snapshot_Number + 1
    53.         Wend
    54.     End If
    55.  
    56. End Sub

  9. #9

    Thread Starter
    Addicted Member cheesebrother's Avatar
    Join Date
    Jul 2011
    Posts
    153

    Re: Direct X Autoredraw

    Wow! That looks perfect. I've got some strange errors though. When i try the snapshot sub I get the error "variable not defined" because of "Direct3DX." I tried the other dx variables in the project but none of them had "SaveSurfactToFile". When i tried take_snapshot, "DirectInput_Key_State" is a "sub or function not defined". This is especially weird as i would think that it would be in the dx library. Thanks for these subs! My plan for using them is when i do a msgbox or something to take a snapshot, load it into a vb picturebox and then there would be no more uglyness.
    Hooked for good.

  10. #10
    College Grad!!! Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Re: Direct X Autoredraw

    I declared Direct3DX As D3DX8. For DirectInput I simply used this:

    vb Code:
    1. Public Direct_Input As DirectInput8
    2. Public Keyboard_Device As DirectInputDevice8
    3. Public Keyboard_State As DIKEYBOARDSTATE
    4.  
    5. Public Sub DirectInput_Initialize_Keyboard(Window As Form)
    6.    
    7.     'Use in Form_Load to initialize DirectInput for Keyboard
    8.  
    9.     Set Direct_Input = DX.DirectInputCreate
    10.     Set Keyboard_Device = Direct_Input.CreateDevice("GUID_SysKeyboard")
    11.     Keyboard_Device.SetCommonDataFormat DIFORMAT_KEYBOARD
    12.     Keyboard_Device.SetCooperativeLevel Window.hWnd, DISCL_BACKGROUND Or DISCL_NONEXCLUSIVE
    13.     Keyboard_Device.Acquire
    14.     Keyboard_Device.GetDeviceStateKeyboard Keyboard_State
    15.  
    16. End Sub
    17.  
    18. Public Function DirectInput_Key_State(Key_Code As Long) As Long
    19.    
    20.     'Use during your game loop to check for keys pressed. DO NOT USE Keycodes.
    21.     'Only use DIK_ variables and the appropriate key.
    22.     Keyboard_Device.GetDeviceStateKeyboard Keyboard_State
    23.    
    24.     DirectInput_Key_State = Keyboard_State.Key(Key_Code)
    25.  
    26. End Function

  11. #11

    Thread Starter
    Addicted Member cheesebrother's Avatar
    Join Date
    Jul 2011
    Posts
    153

    Re: Direct X Autoredraw

    Sorry, although i'm able to use the direct input now but i'm still getting the error on the D3DX8 variable, it isn't being set up. Could you just post a form or the entire form code? That would be the easiest as then there wouldn't be a mix up in variable names or the complete lack of certain variables.
    Hooked for good.

  12. #12
    College Grad!!! Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Re: Direct X Autoredraw

    Whoops forgot you gotta initialize Direct3DX as well

    Code:
    Set Direct3DX = New D3DX8
    Heres an example program of it in action:

    [EDIT] I found that holding the print screen key or whatever key will use will sometimes end up taking multiple snapshots.
    To handle this just use a boolean flag. If the keys held down its true, then the snapshot will only be taken once even when
    keys held. I edited the program to do this:

    vb Code:
    1. Option Explicit
    2.  
    3. Private Const COLOR_DEPTH_16_BIT As Long = D3DFMT_R5G6B5
    4. Private Const COLOR_DEPTH_24_BIT As Long = D3DFMT_A8R8G8B8
    5. Private Const COLOR_DEPTH_32_BIT As Long = D3DFMT_X8R8G8B8
    6.  
    7. Private DX As DirectX8 'The master DirectX object.
    8. Private Direct3D As Direct3D8 'Controls all things 3D.
    9. Private Direct3D_Device As Direct3DDevice8 'Represents the hardware rendering.
    10. Private Direct3DX As D3DX8
    11.  
    12. Private Direct_Input As DirectInput8
    13. Private Keyboard_Device As DirectInputDevice8
    14. Private Keyboard_State As DIKEYBOARDSTATE
    15.  
    16. Private Fullscreen_Enabled As Boolean 'Helps determine whether it's fullscreen mode.
    17. Private Running As Boolean 'Helps determine whether the main game loop is running.
    18.  
    19. Private Snapshot_Number As Long
    20. Private Snapshot_Flag as Boolean
    21.  
    22. Private Sub DirectX_Initialize()
    23.  
    24.     Dim Display_Mode As D3DDISPLAYMODE 'Display mode desciption.
    25.     Dim Direct3D_Window As D3DPRESENT_PARAMETERS 'Backbuffer and viewport description.
    26.  
    27.     Set DX = New DirectX8 'Creates the DirectX object.
    28.     Set Direct3D = DX.Direct3DCreate() 'Creates the Direct3D object using the DirectX object.
    29.     Set Direct3DX = New D3DX8
    30.    
    31.     If Fullscreen_Enabled = True Then
    32.    
    33.         'Now that we are working with fullscreen mode, we must set up the
    34.         'screen resolution to switch to, rather than use the default screen
    35.         'resolution.
    36.        
    37.         Display_Mode.Width = 800
    38.         Display_Mode.Height = 600
    39.         Display_Mode.Format = COLOR_DEPTH_16_BIT
    40.    
    41.         Direct3D_Window.Windowed = False 'The app will be in fullscreen mode.
    42.         Direct3D_Window.BackBufferCount = 1 '1 backbuffer only
    43.         Direct3D_Window.BackBufferWidth = Display_Mode.Width 'Match the backbuffer width with the display width
    44.         Direct3D_Window.BackBufferHeight = Display_Mode.Height 'Match the backbuffer height with the display height
    45.         Direct3D_Window.hDeviceWindow = frmMain.hWnd 'Use frmMain as the device window.
    46.        
    47.     Else
    48.    
    49.         Direct3D.GetAdapterDisplayMode D3DADAPTER_DEFAULT, Display_Mode 'Use the current display mode that you
    50.                                                                         'are already on. Incase you are confused, I'm
    51.                                                                         'talking about your current screen resolution. ;)
    52.        
    53.         Direct3D_Window.Windowed = True 'The app will be in windowed mode.
    54.    
    55.     End If
    56.    
    57.     Direct3D_Window.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC 'Refresh when the monitor does.
    58.     Direct3D_Window.BackBufferFormat = Display_Mode.Format 'Sets the format that was retrieved into the backbuffer.
    59.    
    60.     'Creates the rendering device with some useful info, along with the info
    61.     'we've already setup for Direct3D_Window.
    62.     Set Direct3D_Device = Direct3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, frmMain.hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, Direct3D_Window)
    63.  
    64. End Sub
    65.  
    66. Private Sub DirectInput_Initialize_Keyboard(Window As Form)
    67.    
    68.     'Use in Form_Load to initialize DirectInput for Keyboard
    69.  
    70.     Set Direct_Input = DX.DirectInputCreate
    71.     Set Keyboard_Device = Direct_Input.CreateDevice("GUID_SysKeyboard")
    72.     Keyboard_Device.SetCommonDataFormat DIFORMAT_KEYBOARD
    73.     Keyboard_Device.SetCooperativeLevel Window.hWnd, DISCL_BACKGROUND Or DISCL_NONEXCLUSIVE
    74.     Keyboard_Device.Acquire
    75.     Keyboard_Device.GetDeviceStateKeyboard Keyboard_State
    76.  
    77. End Sub
    78.  
    79. Private Function DirectInput_Key_State(Key_Code As Long) As Long
    80.    
    81.     'Use during your game loop to check for keys pressed. DO NOT USE Keycodes.
    82.     'Only use DIK_ variables and the appropriate key.
    83.     Keyboard_Device.GetDeviceStateKeyboard Keyboard_State
    84.    
    85.     DirectInput_Key_State = Keyboard_State.Key(Key_Code)
    86.  
    87. End Function
    88.  
    89. Private Sub Keyboard_Controls()
    90.    
    91.     If DirectInput_Key_State(DIK_SYSRQ) <> 0 Then
    92.         If Snapshot_Flag = False Then
    93.             Snapshot_Flag = True
    94.             If Dir$(App.Path & "\Snapshots\", vbDirectory) = "" Then
    95.                 MkDir App.Path & "\Snapshots\"
    96.             End If
    97.            
    98.             If Dir$(App.Path & "\Snapshots\SNAP" & Format(Snapshot_Number, "####") & ".bmp") = "" Then
    99.                 Snapshot App.Path & "\Snapshots\SNAP" & Format(Snapshot_Number, "####") & ".bmp"
    100.             End If
    101.            
    102.             While Dir$(App.Path & "\Snapshots\SNAP" & Format(Snapshot_Number, "####") & ".bmp") <> ""
    103.                 DoEvents
    104.                 Snapshot_Number = Snapshot_Number + 1
    105.             Wend
    106.         End If
    107.     ElseIf DirectInput_Key_State(DIK_SYSRQ) = 0 Then
    108.         Snapshot_Flag = False
    109.     End If
    110.  
    111. End Sub
    112.  
    113. Private Sub Snapshot(ByVal File_Path As String)
    114.  
    115.     Dim Surface As Direct3DSurface8
    116.     Dim SrcPalette As PALETTEENTRY
    117.     Dim SrcRect As RECT
    118.     Dim Direct3D_Display_Mode As D3DDISPLAYMODE
    119.  
    120.     'get display dimensions
    121.     Direct3D_Device.GetDisplayMode Direct3D_Display_Mode
    122.  
    123.     'create a surface to put front buffer on,
    124.     'GetFrontBuffer always returns D3DFMT_A8R8G8B8
    125.     Set Surface = Direct3D_Device.CreateImageSurface(Direct3D_Display_Mode.Width, Direct3D_Display_Mode.Height, D3DFMT_A8R8G8B8)
    126.  
    127.     'get data from front buffer
    128.     Direct3D_Device.GetFrontBuffer Surface
    129.  
    130.     'we are saving entire area of this surface
    131.    
    132.     With SrcRect
    133.    
    134.         .Left = 0
    135.         .Right = Direct3D_Display_Mode.Width
    136.         .Top = 0
    137.         .bottom = Direct3D_Display_Mode.Height
    138.        
    139.     End With
    140.  
    141.     'save this surface to a BMP file
    142.     Direct3DX.SaveSurfaceToFile File_Path, D3DXIFF_BMP, Surface, SrcPalette, SrcRect
    143.  
    144. End Sub
    145.  
    146. Private Sub Game_Loop()
    147.  
    148.     Do While Running = True
    149.        
    150.         Keyboard_Controls
    151.        
    152.         'Clears the backbuffer.
    153.         Direct3D_Device.Clear 0, ByVal 0, D3DCLEAR_TARGET, D3DColorRGBA(0, 0, 0, 0), 1#, 0
    154.        
    155.             'Rendering code goes here, but in this tutorial, it will be empty for now.
    156.        
    157.         'Flips the backbuffer into the form window.
    158.         Direct3D_Device.Present ByVal 0, ByVal 0, 0, ByVal 0
    159.        
    160.         If DirectInput_Key_State(DIK_ESCAPE) Then  'If the user presses the Esc key...
    161.             Shut_Down
    162.         End If
    163.        
    164.         DoEvents 'Allow events to happen so the program doesn't lock up.
    165.                  'Found out the hardway that it must be at the end of
    166.                  'the loop if you plan to exit out of the program
    167.                  'properly without using End
    168.  
    169.     Loop
    170.  
    171. End Sub
    172.  
    173. Private Sub Main()
    174.  
    175.     'This event will fire before the form has completely loaded
    176.    
    177.     If MsgBox("Click Yes to go to full screen (Recommended)", vbQuestion Or vbYesNo, "Options") = vbYes Then Fullscreen_Enabled = True
    178.    
    179.     Me.Show
    180.  
    181.     frmMain.Caption = "DirectX Tutorial"
    182.    
    183.     DirectX_Initialize
    184.     DirectInput_Initialize_Keyboard frmMain
    185.  
    186.     Running = True 'Initializations all set. It's now ok to activate the game loop.
    187.  
    188.     Game_Loop
    189.    
    190. End Sub
    191.  
    192. Private Sub Shut_Down()
    193.  
    194.     Running = False 'Helps the program bail out of the game loop.
    195.    
    196.     'Unload all of the DirectX objects
    197.    
    198.     Set Direct_Input = Nothing
    199.     Set Direct3D_Device = Nothing
    200.     Set Direct3D = Nothing
    201.     Set DX = Nothing
    202.    
    203.     Unload Me 'Unload the form
    204.  
    205. End Sub
    206.  
    207. Private Sub Form_Load()
    208.  
    209.     Main
    210.  
    211. End Sub
    212.  
    213. Private Sub Form_Unload(Cancel As Integer)
    214.  
    215.     Shut_Down
    216.    
    217. End Sub

  13. #13

    Thread Starter
    Addicted Member cheesebrother's Avatar
    Join Date
    Jul 2011
    Posts
    153

    Re: Direct X Autoredraw

    Thanks a million for the help. I'll try to fix this up to accomplish what i want and it should be able to work. Just wondering, should it be so slow?
    Hooked for good.

  14. #14
    College Grad!!! Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Re: Direct X Autoredraw

    It's not really. Unless you plan on taking 1000's upon 1000's of snapshots, then it would cause you'd then have to wait until it reaches that number. To speed it up you could use random numbers or hex, or whatever you'd like.

    Also if you know anything about 2D clipping or are able to decipher krtxmrtz's 2D clipping program and splice it into mine (link included), I'm having issues doing that over in the 2D Polygon Clipping (Scissoring) thread. I know I know, I'm desperate

  15. #15

    Thread Starter
    Addicted Member cheesebrother's Avatar
    Join Date
    Jul 2011
    Posts
    153

    Re: Direct X Autoredraw

    Hmmmmm. Something's wrong then, when i tried it it took ages. I put this in to take a screenshot:

    Code:
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
      If KeyCode = vbKeyF1 Then
        Dim StartTime: StartTime = Timer
        Snapshot App.Path & "screenshot.jpg"
        MsgBox Timer - StartTime
      End If
    End Sub
    Just to make it easy for you this is my entire form code now:

    vb Code:
    1. Option Explicit
    2.  
    3. Private Const COLOR_DEPTH_16_BIT As Long = D3DFMT_R5G6B5
    4. Private Const COLOR_DEPTH_24_BIT As Long = D3DFMT_A8R8G8B8
    5. Private Const COLOR_DEPTH_32_BIT As Long = D3DFMT_X8R8G8B8
    6.  
    7. Private DX As DirectX8 'The master DirectX object.
    8. Private Direct3D As Direct3D8 'Controls all things 3D.
    9. Private Direct3D_Device As Direct3DDevice8 'Represents the hardware rendering.
    10. Private Direct3DX As D3DX8
    11.  
    12. Private Direct_Input As DirectInput8
    13. Private Keyboard_Device As DirectInputDevice8
    14. Private Keyboard_State As DIKEYBOARDSTATE
    15.  
    16. Private Fullscreen_Enabled As Boolean 'Helps determine whether it's fullscreen mode.
    17. Private Running As Boolean 'Helps determine whether the main game loop is running.
    18.  
    19. Private Snapshot_Number As Long
    20. Private Snapshot_Flag As Boolean
    21.  
    22. Private Sub DirectX_Initialize()
    23.  
    24.     Dim Display_Mode As D3DDISPLAYMODE 'Display mode desciption.
    25.     Dim Direct3D_Window As D3DPRESENT_PARAMETERS 'Backbuffer and viewport description.
    26.  
    27.     Set DX = New DirectX8 'Creates the DirectX object.
    28.     Set Direct3D = DX.Direct3DCreate() 'Creates the Direct3D object using the DirectX object.
    29.     Set Direct3DX = New D3DX8
    30.    
    31.     If Fullscreen_Enabled = True Then
    32.    
    33.         'Now that we are working with fullscreen mode, we must set up the
    34.         'screen resolution to switch to, rather than use the default screen
    35.         'resolution.
    36.        
    37.         Display_Mode.Width = 800
    38.         Display_Mode.Height = 600
    39.         Display_Mode.Format = COLOR_DEPTH_16_BIT
    40.    
    41.         Direct3D_Window.Windowed = False 'The app will be in fullscreen mode.
    42.         Direct3D_Window.BackBufferCount = 1 '1 backbuffer only
    43.         Direct3D_Window.BackBufferWidth = Display_Mode.Width 'Match the backbuffer width with the display width
    44.         Direct3D_Window.BackBufferHeight = Display_Mode.Height 'Match the backbuffer height with the display height
    45.         Direct3D_Window.hDeviceWindow = frmMain.hWnd 'Use frmMain as the device window.
    46.        
    47.     Else
    48.    
    49.         Direct3D.GetAdapterDisplayMode D3DADAPTER_DEFAULT, Display_Mode 'Use the current display mode that you
    50.                                                                         'are already on. Incase you are confused, I'm
    51.                                                                         'talking about your current screen resolution. ;)
    52.        
    53.         Direct3D_Window.Windowed = True 'The app will be in windowed mode.
    54.    
    55.     End If
    56.    
    57.     Direct3D_Window.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC 'Refresh when the monitor does.
    58.     Direct3D_Window.BackBufferFormat = Display_Mode.Format 'Sets the format that was retrieved into the backbuffer.
    59.    
    60.     'Creates the rendering device with some useful info, along with the info
    61.     'we've already setup for Direct3D_Window.
    62.     Set Direct3D_Device = Direct3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, frmMain.hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, Direct3D_Window)
    63.  
    64. End Sub
    65.  
    66. Private Sub DirectInput_Initialize_Keyboard(Window As Form)
    67.    
    68.     'Use in Form_Load to initialize DirectInput for Keyboard
    69.  
    70.     Set Direct_Input = DX.DirectInputCreate
    71.     Set Keyboard_Device = Direct_Input.CreateDevice("GUID_SysKeyboard")
    72.     Keyboard_Device.SetCommonDataFormat DIFORMAT_KEYBOARD
    73.     Keyboard_Device.SetCooperativeLevel Window.hWnd, DISCL_BACKGROUND Or DISCL_NONEXCLUSIVE
    74.     Keyboard_Device.Acquire
    75.     Keyboard_Device.GetDeviceStateKeyboard Keyboard_State
    76.  
    77. End Sub
    78.  
    79. Private Function DirectInput_Key_State(Key_Code As Long) As Long
    80.    
    81.     'Use during your game loop to check for keys pressed. DO NOT USE Keycodes.
    82.     'Only use DIK_ variables and the appropriate key.
    83.     Keyboard_Device.GetDeviceStateKeyboard Keyboard_State
    84.    
    85.     DirectInput_Key_State = Keyboard_State.Key(Key_Code)
    86.  
    87. End Function
    88.  
    89. Private Sub Keyboard_Controls()
    90.    
    91.     If DirectInput_Key_State(DIK_SYSRQ) <> 0 Then
    92.         If Snapshot_Flag = False Then
    93.             Snapshot_Flag = True
    94.             If Dir$(App.Path & "\Snapshots\", vbDirectory) = "" Then
    95.                 MkDir App.Path & "\Snapshots\"
    96.             End If
    97.            
    98.             If Dir$(App.Path & "\Snapshots\SNAP" & Format(Snapshot_Number, "####") & ".bmp") = "" Then
    99.                 Snapshot App.Path & "\Snapshots\SNAP" & Format(Snapshot_Number, "####") & ".bmp"
    100.             End If
    101.            
    102.             While Dir$(App.Path & "\Snapshots\SNAP" & Format(Snapshot_Number, "####") & ".bmp") <> ""
    103.                 DoEvents
    104.                 Snapshot_Number = Snapshot_Number + 1
    105.             Wend
    106.         End If
    107.     ElseIf DirectInput_Key_State(DIK_SYSRQ) = 0 Then
    108.         Snapshot_Flag = False
    109.     End If
    110.  
    111. End Sub
    112.  
    113. Private Sub Snapshot(ByVal File_Path As String)
    114.  
    115.     Dim Surface As Direct3DSurface8
    116.     Dim SrcPalette As PALETTEENTRY
    117.     Dim SrcRect As RECT
    118.     Dim Direct3D_Display_Mode As D3DDISPLAYMODE
    119.  
    120.     'get display dimensions
    121.     Direct3D_Device.GetDisplayMode Direct3D_Display_Mode
    122.  
    123.     'create a surface to put front buffer on,
    124.     'GetFrontBuffer always returns D3DFMT_A8R8G8B8
    125.     Set Surface = Direct3D_Device.CreateImageSurface(Direct3D_Display_Mode.Width, Direct3D_Display_Mode.Height, D3DFMT_A8R8G8B8)
    126.  
    127.     'get data from front buffer
    128.     Direct3D_Device.GetFrontBuffer Surface
    129.  
    130.     'we are saving entire area of this surface
    131.    
    132.     With SrcRect
    133.    
    134.         .Left = 0
    135.         .Right = Direct3D_Display_Mode.Width
    136.         .Top = 0
    137.         .bottom = Direct3D_Display_Mode.Height
    138.        
    139.     End With
    140.  
    141.     'save this surface to a BMP file
    142.     Direct3DX.SaveSurfaceToFile File_Path, D3DXIFF_BMP, Surface, SrcPalette, SrcRect
    143.  
    144. End Sub
    145.  
    146. Private Sub Game_Loop()
    147.  
    148.     Do While Running = True
    149.        
    150.         Keyboard_Controls
    151.        
    152.         'Clears the backbuffer.
    153.         Direct3D_Device.Clear 0, ByVal 0, D3DCLEAR_TARGET, D3DColorRGBA(0, 0, 0, 0), 1#, 0
    154.        
    155.             'Rendering code goes here, but in this tutorial, it will be empty for now.
    156.        
    157.         'Flips the backbuffer into the form window.
    158.         Direct3D_Device.Present ByVal 0, ByVal 0, 0, ByVal 0
    159.        
    160.         If DirectInput_Key_State(DIK_ESCAPE) Then  'If the user presses the Esc key...
    161.             Shut_Down
    162.         End If
    163.        
    164.         DoEvents 'Allow events to happen so the program doesn't lock up.
    165.                  'Found out the hardway that it must be at the end of
    166.                  'the loop if you plan to exit out of the program
    167.                  'properly without using End
    168.  
    169.     Loop
    170.  
    171. End Sub
    172.  
    173. Private Sub Main()
    174.  
    175.     'This event will fire before the form has completely loaded
    176.    
    177.     If MsgBox("Click Yes to go to full screen (Recommended)", vbQuestion Or vbYesNo, "Options") = vbYes Then Fullscreen_Enabled = True
    178.    
    179.     Me.Show
    180.  
    181.     frmMain.Caption = "DirectX Tutorial"
    182.    
    183.     DirectX_Initialize
    184.     DirectInput_Initialize_Keyboard frmMain
    185.  
    186.     Running = True 'Initializations all set. It's now ok to activate the game loop.
    187.  
    188.     Game_Loop
    189.    
    190. End Sub
    191.  
    192. Private Sub Shut_Down()
    193.  
    194.     Running = False 'Helps the program bail out of the game loop.
    195.    
    196.     'Unload all of the DirectX objects
    197.    
    198.     Set Direct_Input = Nothing
    199.     Set Direct3D_Device = Nothing
    200.     Set Direct3D = Nothing
    201.     Set DX = Nothing
    202.    
    203.     Unload Me 'Unload the form
    204.  
    205. End Sub
    206.  
    207. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    208.   If KeyCode = vbKeyF1 Then
    209.     Dim StartTime: StartTime = Timer
    210.     Snapshot App.Path & "screenshot.jpg"
    211.     MsgBox Timer - StartTime
    212.   End If
    213. End Sub
    214.  
    215. Private Sub Form_Load()
    216.  
    217.     Main
    218.  
    219. End Sub
    220.  
    221. Private Sub Form_Unload(Cancel As Integer)
    222.  
    223.     Shut_Down
    224.    
    225. End Sub

    For some reason when i take a screenshot it spends 1.3 seconds doing so. The part where it spends all the time is on this line in the screenshot sub: "Direct3D_Device.GetFrontBuffer Surface"


    As for the clipping i'm sorry that i can't help you . When it comes to directx you're the boss, i would have no idea how to do that.
    Hooked for good.

  16. #16
    College Grad!!! Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,339

    Re: Direct X Autoredraw

    Ever play World of Warcraft? Their screenshot button has a noticable pause too. That's cause Windows is busy creating the file. It affects all programs running really. It's just something you gotta deal with.

  17. #17

    Thread Starter
    Addicted Member cheesebrother's Avatar
    Join Date
    Jul 2011
    Posts
    153

    Re: Direct X Autoredraw

    Ok, i'll figure something out. Thanks again.

    Edit: Update. I just found a fast way to take a screenshot, which works entirely in the program. reacen posted it here:http://www.vbforums.com/showthread.p...form+menu+fade. This is plenty fast and now i can preserve my screen by doing this (by the way in the program i'm drawing dx in a picturebox).

    Code:
    Private Sub LockScreen()
      Me.Picture = modSc.CaptureWindow(Me.hWnd, False, 4, 30, (Me.Width / 15) - 8, (Me.Height / 15) - 34)
      picWorld.Visible = False
    End Sub
    
    Private Sub UnLockScreen()
      picWorld.Visible = True
      Draw
      Me.Picture = Nothing
    End Sub
    So i lock the screen, show the msgbox or cdb and then unlock the screen. This is totally resolved!
    Last edited by cheesebrother; Dec 16th, 2011 at 02:35 PM.
    Hooked for good.

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