Results 1 to 26 of 26

Thread: [RESOLVED] DirectX referencing

  1. #1

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

    Resolved [RESOLVED] DirectX referencing

    DirectX and DirectDraw are things I’m trying to learn how to do, especially directdraw. I’ve tried a few various tutorials showing how to draw images or shapes but I’ve never been able to do them because visual basic always crashes. Not really, it just stops working for reasons that I will come to.

    Okay, so I create a new project and go to references. First I reference the Microsoft.DirectX.dll. Now all of a sudden my computer starts working hard(the fan turns on and starts making a racket). Next I reference the Microsoft.DirectX.DirectDraw.dll. All is good. Next step in the tutorial, I put this in the top of my form’s code(this doesn’t need to be added to make it crash):

    Code:
    Imports Microsoft.DirectX
    Imports Microsoft.DirectX.DirectDraw
    Now the problem arises. There are two ways of getting it to happen. #1 is to run the project and #2 is to save the project. After i run it nothing happens. Then after a little bit a message comes up. It’s the “Delay Notification” thing which has the “Switch To…” and “Continue Waiting” buttons”. Neither of them do anything (the message just comes up the next time I click on vb2010) and I have to end the program through task manager. After a little wait another dialog comes up that says “vshost32.exe has encountered a user-defined breakpoint.” Now I can’t hear the fan anymore and the computer is calm. I'm not sure if it makes a difference but i'm using the express version of vb.net so maybe that's the problem.
    Hooked for good.

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

    Re: DirectX referencing

    VB.Net 2010 tends to crash frequently for me too. Thats why I stick with 2008. But if you need help getting DX working here ya go. Be sure the references you link in VB.Net are exactly the same version or there will be problems:

    Code:
    How to setup DirectX on multiple platforms and languages
    
    Note: BE SURE YOU INSTALLED THE LATEST VERSION OF THE DIRECTX SDK! The latest version contains DirectX9, DirectX10, and DirectX11. However if you want samples for all of the versions of the DirectX SDK's You'll need to install (in the proper order) DirectX 8.1 SDK, DirectX SDK October 2006 (DX9), DirectX SDK August 2008 (DX10), DirectX SDK June 2010 (DX11) (Check for later version of DirectX11 prior to downloading). If you do not do it in the proper order, it'll confuse the IDE and sometimes your DirectX apps won't work properly.
    
    
    -------------------------------
    VB6 
    -------------------------------
    Setting up Project:
    - Start a new project and create a Standard EXE.
    
    Setting up for DirectX8:
    - Go up in the menu to Project > References...
    - Put a check next to "DirectX 8 For Visual Basic Type Library". 
    - Click Ok
    
    Note For Windows 7 users: this will not be shown in your References. And you will only need to do these parts once ever: 
    - Put the dx8vb.dll file in your Windows\SysWOW64 folder.
    - Drag it into regsvr32.exe to register it in order to use it in VB6 from now on and have it shown in References. 
    - Go ahead and reopen Visual Basic 6 and check the References again to see if it's there.
    
    If it's still not there, the best approach is to run Command Prompt as an Administrator and reference it, as sometimes simply dragging a file into regsvr32 is not enough. To do this:
    - Goto your Start Menu.
    - click on the bottom where it says > All Programs and goto Accessories
    - Right click Command Prompt. 
    - Click Run as administrator (should be located under Open). 
    - Next type regsvr32 "YOUR FILE PATH HERE OF DX8VB.DLL" For example:
    
    regsvr32 "C:\Windows\SysWOW64\dx8vb.dll" 
    
    - Now open up a new project in VB,  Go up in the menu to Project > References and see if its there? Tada!!! You can now use DirectX8 in VB6 on Windows 7 from now on and no longer need to go through all the BS of using regsvr32 and command prompt.
    - You are now ready to code with DirectX
    
    
    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    
    -------------------------------
    Visual C++ 6.0
    -------------------------------
    Setting up Project:
    - Go up in the menu and click File > New... 
    - Click the Projects tab if it's not there already
    - Create a new empty Win32 Application Workspace
    - Be sure Empty project is selected and click Finish 
    - Go back up in the menu and click File > New...
    - Be sure Files tab is selected and select C++ Source file.
    - Name the source file anything you want such as Main
    - Click Ok 
    - Go back up in the menu and click Project > Settings...
    - Click the C/C++ tab and remove all Preprocessor definitions. Leave it blank
    - Click Ok
    
    Setting up for DirectX8:
    - Note: BE SURE DIRECTX8.1 SDK IS INSTALLED!
    - Go up in the menu and click Project > Settings...
    - Click the C/C++ tab
    - Goto the combo box Category: and select Preprocessor
    - Go down where it says Additional include directories and manually put in the path to the include directory for whatever DirectX SDK you wanna use
    - Next click on the Link tab
    - Goto the combo box Category: and select Input
    - Go down where it says Additional library path: and manually put in the lib path for whatever DirectX SDK you wanna use. Be sure it's the same DirectX SDK as include!
    - Next go back upto the combo box Category: and select General
    - Go down to Object/library modules: and add in d3d8.lib and/or whatever DirectX libraries you wanna use that's within the DirectX SDK\lib directory with the correct consistant version number. It doesn't need to be all of them. It can be just one d3d# lib file if it's a simple basic DirectX app. However don't remove the other libraries within Object/library modules: or you won't be able to use them! 
    - Click Ok
    - You are now ready to code with DirectX
    
    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    
    -------------------------------
    VB.Net 2008
    -------------------------------
    Setting up Project:
    - Start a new project and create a Windows Forms Application.
    
    Setting up for DirectX9:
    - Go up in the menu to Project > Add Reference... 
    - Next goto .NET tab. 
    - Scroll down to Microsoft.DirectX and click it. 
    - Hold control and also add Microsoft.DirectX.Direct3D. You are welcome to adding more DirectX references if you choose. 
    - Click Ok. Now you are ready to code in DirectX9. 
                
    Note: If any of the DirectX stuff isn't located in your .NET tab, you may need to manually add it using your Browse tab under (your harddrive letter):\Windows\Microsoft.NET\DirectX For Managed Code\1.0.2902.0 and add your necessary .dll files such as Microsoft.DirectX.dll and Microsoft.DirectX.Direct3D.dll, etc. For any other new projects afterwards, if it's still not there, look under the Recent Tab.
    
    Project must be in X86 for DirectX9 apps to run. And must be done for every new DirectX project. Here's how to set it up.
    - Go up in the menu to Build > Configuration Manager... 
    - If Configuration Manager is not shown, you only need to do this one part once ever. On the Visual Studio Tools menu, click Options, expand Projects and Solutions, and then click General. Click to check Show advanced build configurations and Click Ok. Now it should show up in the menu bar within Build. Go up in the menu to Build > Configuration Manager... 
    - Go where it says Platform (and NOT Active Solution Platform). Select <New...>. A new window should pop up.
    - Where it says New Platform, select x86.
    - Click Ok.
    - Both Platform and Active Solution Platform should have changed to x86. Click Close.
    - You are now ready to code with DirectX
    
    
    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    
    -------------------------------
    VB.Net 2010
    -------------------------------
    Setting up Project:
    - Start a new project and create a Windows Forms Application.
    
    Setting up for DirectX9:
    - Go up in the menu to Project > Add Reference... 
    - Next goto .NET tab. 
    - Scroll down to Microsoft.DirectX and click it. 
    - Hold control and also add Microsoft.DirectX.Direct3D. You are welcome to adding more DirectX references if you choose. 
    - Click Ok.
                
    Note: If any of the DirectX stuff isn't located in your .NET tab, you may need to manually add it using your Browse tab under (your harddrive letter):\Windows\Microsoft.NET\DirectX For Managed Code\1.0.2902.0 and add your necessary .dll files such as Microsoft.DirectX.dll and Microsoft.DirectX.Direct3D.dll, etc. For any other new projects afterwards, if it's still not there, look under the Recent Tab.
    
    Project must be in X86 for DirectX9 apps to run. And thankfully it's x86 by default. However the .Net Framework is 4.0 and needs changed to 3.5. Here's how to set it up:
    - Go towards the right of the screen where it says Solution Explorer.
    - Right click your project name and goto Properties.
    - On the left side, select Compile.
    - Click on the button Advanced Compile Options... A new window should pop up.
    - On the bottom where it says Target Framework, select .NET Framework 3.5
    - Click Ok.
    - You are now ready to code with DirectX
    
    
    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    
    -------------------------------
    Visual C# 2008
    -------------------------------
    Setting up Project:
    - Start a new project and create a Windows Forms Application.
    
    Setting up for DirectX9:
    - Go up in the menu to Project > Add Reference... 
    - Next goto .NET tab. 
    - Scroll down to Microsoft.DirectX and click it. 
    - Hold control and also add Microsoft.DirectX.Direct3D. You are welcome to adding more DirectX references if you choose. 
    - Click Ok. Now you are ready to code in DirectX9. 
                
    Note: If any of the DirectX stuff isn't located in your .NET tab, you may need to manually add it using your Browse tab under (your harddrive letter):\Windows\Microsoft.NET\DirectX For Managed Code\1.0.2902.0 and add your necessary .dll files such as Microsoft.DirectX.dll and Microsoft.DirectX.Direct3D.dll, etc. For any other new projects afterwards, if it's still not there, look under the Recent Tab.
    
    Project must be in X86 for DirectX9 apps to run. And must be done for every new DirectX project. Here's how to set it up.
    - Go up in the menu to Build > Configuration Manager... 
    - If Configuration Manager is not shown, you only need to do this one part once ever. On the Visual Studio Tools menu, click Options, expand Projects and Solutions, and then click General. Click to check Show advanced build configurations and Click Ok. Now it should show up in the menu bar within Build. Go up in the menu to Build > Configuration Manager... 
    - Go where it says Platform (and NOT Active Solution Platform). Select <New...>. A new window should pop up.
    - Where it says New Platform, select x86.
    - Click Ok.
    - Both Platform and Active Solution Platform should have changed to x86. Click Close.
    - You are now ready to code with DirectX
    
    
    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    
    -------------------------------
    Visual C# 2010
    -------------------------------
    Setting up Project:
    - Start a new project and create a Windows Forms Application.
    
    Setting up for DirectX9:
    - Go up in the menu to Project > Add Reference... 
    - Next goto .NET tab. 
    - Scroll down to Microsoft.DirectX and click it. 
    - Hold control and also add Microsoft.DirectX.Direct3D. You are welcome to adding more DirectX references if you choose. 
    - Click Ok.
                
    Note: If any of the DirectX stuff isn't located in your .NET tab, you may need to manually add it using your Browse tab under (your harddrive letter):\Windows\Microsoft.NET\DirectX For Managed Code\1.0.2902.0 and add your necessary .dll files such as Microsoft.DirectX.dll and Microsoft.DirectX.Direct3D.dll, etc. For any other new projects afterwards, if it's still not there, look under the Recent Tab.
    
    Project must be in X86 for DirectX9 apps to run. And thankfully it's x86 by default. However the .Net Framework is 4.0 and needs changed to 3.5. Here's how to set it up:
    - Go towards the right of the screen where it says Solution Explorer.
    - Right click your project name and goto Properties.
    - On the left side, select Application if it's not there already.
    - Where it says Target Framework, select .NET Framework 3.5. Be sure to save your project prior to this or you will receive an error.
    - You are now ready to code with DirectX
    
    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    .....................CONTINUED NEXT POST

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

    Re: DirectX referencing

    ..............CONTINUED FROM LAST POST

    Code:
    --------------------------------
    Visual C++ 2008
    --------------------------------
    Setting up Project:
    - Start a new project.
    - Goto General and select Empty Project.
    - Choose the name of your project.
    - Choose the Location of your project. 
    - Click Ok
    - Goto Source Files and right click it.
    - Click Add New Item...
    - Click C++ File(.cpp) and name it whatever you want.
    - Click Ok.
    
    Setting up for DirectX9: 
    
    For the first time of working with DirectX9, you'll only need to do this part once ever:
    - Go up in the menu to Tools > Options
    - Goto Projects and Solutions
    - Goto VC++ Directories
    - Goto the combobox where it says "Show directories for:" and clik Include files. 
    - If you do not see $(DXSDK_DIR)Include, click New Line and manually put it in. To avoid compiler confusion With multiple DirectX sDK's installed, remove all the other DirectX SDK file paths to the include directory by using Cut Line (Delete).
    - Next goto the combobox where it says "Show directories for:" and click Library files.
    - If you do not see $(DXSDK_DIR)lib\x86, click New Line and manually put it in. To avoid compiler confusion With multiple DirectX sDK's installed, remove all the other DirectX SDK file paths to the lib directory by using Cut Line (Delete). 
    - Click Ok.
    - You no longer need to do these steps with Include and Library. 
    
    Now with every new project you will need to do this: 
    - On the left where your project name is within Solution Explorer, right click it and goto Properties. 
    - Goto Configuration Properties > Linker > Input. 
    - Towards the right where it says Additional Dependencies, click it to reveal a [...] and click that button.
    - On top in the listbox add d3d9.lib. You are welcome to adding more dx librarys for what ever you need todo such as d3dx9.lib, dsound.lib, dplay.lib, etc.
    - Click Ok. 
    - You are now ready to code with DirectX.
    
    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    
    
    --------------------------------
    Visual C++ 2010
    --------------------------------
    Setting up Project:
    - Start a new project.
    - Goto General and select Empty Project.
    - Choose the name of your project.
    - Choose the Location of your project. Note: There's a bug that Microsoft is aware of in Visual C++ 2010 where the file path must be under 128 characters or it will not recoginze the location of your cpp file. Choose a short file path. And when your project is saved / compiled you can move it to a longer file path that you desire where you are free to edit and expand upon.
    - Click Ok
    - Goto Source Files and right click it.
    - Click Add New Item...
    - Click C++ File(.cpp) and name it whatever you want.
    - Click Ok.
    
    Setting up for DirectX9: 
    
    For the first time of working with DirectX9, you'll only need to do this part once ever:
    - On the left where your project name is within Solution Explorer, right click it and goto Properties. 
    - Goto Configuration Properties > VC++ Directories
    - Goto Include Directories and click it to reveal a [v]. Click it and goto <Edit...>. 
    - If you do not see $(DXSDK_DIR)Include, manually put it in and click Ok. 
    - Next goto Library Directories and click it to reveal a [v]. Click it and goto <Edit...>. 
    - If you do not see $(DXSDK_DIR)Lib\x86, manually put it in and click OK. You no longer need to do that with Include and Library. 
    
    Now with every new project you will need to do this: 
    - On the left where your project name is within Solution Explorer, right click it and goto Properties. 
    - Goto Configuration Properties and goto > Linker > Input. 
    - Towards the right where it says Additional Dependencies, click it to reveal a [v] and goto <Edit...>. 
    - On top in the listbox add d3d9.lib. You are welcome to adding more dx librarys for what ever you need todo such as d3dx9.lib, dsound.lib, dplay.lib, etc. 
    - You are now ready to code with DirectX.
    This is a notepad that I made thats gonna be included in my massive directx tutorial I'm currently working on. Also If I were you I'd use Direct3D for 2D cause in DirectDraw there are limitations such as not being able to rotate surfaces or alphablend, etc. So you'll be limited. It's also easier to code 2D stuff using Direct3D than Directdraw. I have tons of examples if you would like to see.

  4. #4

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

    Re: DirectX referencing

    I followed the one for vb2010 and got a lot farther than before. I was referencing the wrong files so at least now i've got the ones in "\Microsoft.NET\DirectX For Managed Code\1.0.2902.0\...". When i first tried to switch it to the 3.5 framework it said to save my project first. I saved it and then tried to switch it again. You can see the screenshot of what it said.

    Note: BE SURE YOU INSTALLED THE LATEST VERSION OF THE DIRECTX SDK!
    I'll try to get that as soon as i can and see if it fixes this problem.

    This is a notepad that I made thats gonna be included in my massive directx tutorial I'm currently working on. Also If I were you I'd use Direct3D for 2D cause in DirectDraw there are limitations such as not being able to rotate surfaces or alphablend, etc. So you'll be limited. It's also easier to code 2D stuff using Direct3D than Directdraw. I have tons of examples if you would like to see.
    Ok, i would have thought that directdraw could do those things but i guess d3d is the way to go. I'd love to see your examples! I'm checking you website quite frequently to see if it's up yet.
    Attached Images Attached Images  
    Hooked for good.

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

    Re: DirectX referencing

    Here's a texture mapping example I made which works in VB.Net 2008/2010:

    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 Device As Device
    10.     Private Display_Mode As DisplayMode
    11.     Private Screen As PresentParameters = New PresentParameters
    12.  
    13.     Private Fullscreen_Enabled As Boolean
    14.     Private Running As Boolean = True
    15.  
    16.     Private Vertex_List As CustomVertex.TransformedColoredTextured() = New CustomVertex.TransformedColoredTextured(0 To 3) {}
    17.     Private Texture As Texture
    18.  
    19.     Private Sub DirectX_Initialize()
    20.  
    21.         If Fullscreen_Enabled = True Then
    22.             Display_Mode.Width = 800
    23.             Display_Mode.Height = 600
    24.             Display_Mode.Format = Format.R5G6B5
    25.             Screen.Windowed = False
    26.             Screen.BackBufferCount = 1
    27.             Screen.BackBufferWidth = Display_Mode.Width
    28.             Screen.BackBufferHeight = Display_Mode.Height
    29.         Else
    30.             Screen.Windowed = True
    31.         End If
    32.  
    33.         Screen.SwapEffect = SwapEffect.Copy
    34.         Screen.BackBufferFormat = Display_Mode.Format
    35.  
    36.         Device = New Device(0, DeviceType.Hardware, Me.Handle, CreateFlags.SoftwareVertexProcessing, Screen)
    37.  
    38.     End Sub
    39.  
    40.     Private Function Create_Custom_Vertex(ByVal X As Single, ByVal Y As Single, ByVal Z As Single, ByVal Color As Integer, ByVal TU As Integer, ByVal TV As Integer) As CustomVertex.TransformedColoredTextured
    41.  
    42.         Dim Vertex As CustomVertex.TransformedColoredTextured = New CustomVertex.TransformedColoredTextured
    43.  
    44.         Vertex.Position = New Vector4(X, Y, Z, 1)
    45.         Vertex.Color = Color
    46.         Vertex.Tu = TU
    47.         Vertex.Tv = TV
    48.  
    49.         Return Vertex
    50.  
    51.     End Function
    52.  
    53.     Private Sub Create_Polygon()
    54.  
    55.         Vertex_List(0) = Create_Custom_Vertex(0, 0, 0, System.Drawing.Color.FromArgb(255, 255, 255, 255).ToArgb, 0, 0)
    56.         Vertex_List(1) = Create_Custom_Vertex(100, 0, 0, System.Drawing.Color.FromArgb(255, 255, 255, 255).ToArgb, 1, 0)
    57.         Vertex_List(2) = Create_Custom_Vertex(0, 100, 0, System.Drawing.Color.FromArgb(255, 255, 255, 255).ToArgb, 0, 1)
    58.         Vertex_List(3) = Create_Custom_Vertex(100, 100, 0, System.Drawing.Color.FromArgb(255, 255, 255, 255).ToArgb, 1, 1)
    59.  
    60.     End Sub
    61.  
    62.     Private Sub Draw_Polygon()
    63.  
    64.         Device.VertexFormat = CustomVertex.TransformedColoredTextured.Format
    65.         Device.SetTexture(0, Texture)
    66.         Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, Vertex_List)
    67.  
    68.     End Sub
    69.  
    70.     Private Sub Load_Texture(ByRef Texture As Texture, ByVal Filepath As String, ByVal Transparency_Color As Integer)
    71.  
    72.         Texture = TextureLoader.FromFile(Device, Filepath, 512, 512, 1, Usage.None, Format.A8B8G8R8, Pool.Managed, Filter.Point, Filter.Point, Transparency_Color)
    73.  
    74.     End Sub
    75.  
    76.     Private Sub Render()
    77.  
    78.         Device.Clear(ClearFlags.Target, Color.Black, 1.0, 0)
    79.         Device.BeginScene()
    80.         'Rendering code goes here.
    81.         Create_Polygon()
    82.         Draw_Polygon()
    83.         Device.EndScene()
    84.         Device.Present()
    85.  
    86.     End Sub
    87.  
    88.     Private Sub Game_Loop()
    89.  
    90.         Do While Running = True
    91.             Render()
    92.             Application.DoEvents()
    93.         Loop
    94.  
    95.     End Sub
    96.  
    97.     Private Sub Main()
    98.  
    99.         If MessageBox.Show("Click Yes to go to full screen (Recommended)", "", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = DialogResult.Yes Then
    100.             Fullscreen_Enabled = True
    101.         End If
    102.  
    103.         With Me
    104.             .Show()
    105.             .Width = 330
    106.             .Height = 250
    107.             .SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.Opaque, True)
    108.             .Text = "DirectX Tutorial"
    109.             If Fullscreen_Enabled = True Then .FormBorderStyle = Windows.Forms.FormBorderStyle.None
    110.         End With
    111.  
    112.         DirectX_Initialize()
    113.         Load_Texture(Texture, Application.StartupPath() & "\Texture.jpg", System.Drawing.Color.FromArgb(0, 0, 0, 0).ToArgb)
    114.         Running = True
    115.  
    116.     End Sub
    117.  
    118.     Private Sub Shutdown()
    119.  
    120.         Running = False
    121.         Texture = Nothing
    122.         Device = Nothing
    123.         Application.Exit()
    124.  
    125.     End Sub
    126.  
    127.  
    128.     Private Sub frmMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    129.  
    130.         If e.KeyCode = Keys.Escape Then Shutdown()
    131.  
    132.     End Sub
    133.  
    134.     Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    135.  
    136.         Main()
    137.  
    138.     End Sub
    139.  
    140.     Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    141.  
    142.         Shutdown()
    143.  
    144.     End Sub
    145.  
    146.     Private Sub frmMain_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    147.  
    148.         Game_Loop()
    149.  
    150.     End Sub
    151.  
    152.     Private Sub frmMain_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
    153.  
    154.         If Fullscreen_Enabled = False Then
    155.             Running = False
    156.             Device.Reset(Screen)
    157.             Application.DoEvents()
    158.             Running = True
    159.         End If
    160.  
    161.     End Sub
    162. End Class

    Here's the same program in VB6:
    vb Code:
    1. Option Explicit
    2.  
    3. Private Type CUSTOM_VERTEX
    4.  
    5.     X As Single
    6.     Y As Single
    7.     Z As Single
    8.     RHW As Single
    9.     Color As Long
    10.     TU As Single
    11.     TV As Single
    12.    
    13. End Type
    14.  
    15. Private Const CUSTOM_VERTEX_FORMAT As Long = D3DFVF_XYZRHW Or D3DFVF_TEX1 Or D3DFVF_DIFFUSE
    16.  
    17. Private DX As DirectX8
    18. Private D3D As Direct3D8
    19. Private D3DX As D3DX8
    20. Private Device As Direct3DDevice8
    21. Private Display_Mode As D3DDISPLAYMODE
    22. Private Screen As D3DPRESENT_PARAMETERS
    23.    
    24. Private Fullscreen_Enabled As Boolean
    25. Private Running As Boolean
    26.  
    27. Private Vertex_List(3) As CUSTOM_VERTEX
    28. Private Texture As Direct3DTexture8
    29.  
    30. Private Sub DirectX_Initialize()
    31.  
    32.     Set DX = New DirectX8
    33.     Set D3D = DX.Direct3DCreate()
    34.     Set D3DX = New D3DX8
    35.    
    36.     If Fullscreen_Enabled = True Then
    37.         Display_Mode.Width = 800
    38.         Display_Mode.Height = 600
    39.         Display_Mode.Format = D3DFMT_R5G6B5
    40.         Screen.Windowed = False
    41.         Screen.BackBufferCount = 1
    42.         Screen.BackBufferWidth = Display_Mode.Width
    43.         Screen.BackBufferHeight = Display_Mode.Height
    44.         Screen.hDeviceWindow = frmMain.hWnd
    45.     Else
    46.         D3D.GetAdapterDisplayMode D3DADAPTER_DEFAULT, Display_Mode
    47.         Screen.Windowed = True
    48.     End If
    49.    
    50.     Screen.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC
    51.     Screen.BackBufferFormat = Display_Mode.Format
    52.  
    53.     Set Device = D3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, frmMain.hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, Screen)
    54.  
    55. End Sub
    56.  
    57. Private Function Create_Custom_Vertex(ByVal X As Single, ByVal Y As Single, ByVal Z As Single, ByVal RHW As Single, ByVal Color As Long, ByVal TU As Single, ByVal TV As Single) As CUSTOM_VERTEX
    58.  
    59.     Create_Custom_Vertex.X = X
    60.     Create_Custom_Vertex.Y = Y
    61.     Create_Custom_Vertex.Z = Z
    62.     Create_Custom_Vertex.RHW = RHW
    63.     Create_Custom_Vertex.Color = Color
    64.     Create_Custom_Vertex.TU = TU
    65.     Create_Custom_Vertex.TV = TV
    66.    
    67. End Function
    68.  
    69. Private Sub Load_Texture(ByRef Texture As Direct3DTexture8, ByRef File_Path As String, ByVal Transparency_Color As Long)
    70.  
    71.     Set Texture = D3DX.CreateTextureFromFileEx(Device, File_Path, 512, 512, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_FILTER_POINT, D3DX_FILTER_POINT, Transparency_Color, ByVal 0, ByVal 0)
    72.  
    73. End Sub
    74.  
    75. Private Sub Create_Polygon()
    76.  
    77.     Vertex_List(0) = Create_Custom_Vertex(0, 0, 0, 1, D3DColorXRGB(255, 255, 255), 0, 0)
    78.     Vertex_List(1) = Create_Custom_Vertex(100, 0, 0, 1, D3DColorXRGB(255, 255, 255), 1, 0)
    79.     Vertex_List(2) = Create_Custom_Vertex(0, 100, 0, 1, D3DColorXRGB(255, 255, 255), 0, 1)
    80.     Vertex_List(3) = Create_Custom_Vertex(100, 100, 0, 1, D3DColorXRGB(255, 255, 255), 1, 1)
    81.  
    82. End Sub
    83.  
    84. Private Sub Draw_Polygon()
    85.  
    86.     Device.SetVertexShader CUSTOM_VERTEX_FORMAT
    87.     Device.SetTexture 0, Texture
    88.     Device.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Vertex_List(0), Len(Vertex_List(0))
    89.  
    90. End Sub
    91.  
    92. Private Sub Render()
    93.  
    94.     Device.Clear 0, ByVal 0, D3DCLEAR_TARGET, D3DColorXRGB(0, 0, 0), 1#, 0
    95.     Device.BeginScene
    96.     'Rendering code goes here
    97.     Create_Polygon
    98.     Draw_Polygon
    99.     Device.EndScene
    100.     Device.Present ByVal 0, ByVal 0, 0, ByVal 0
    101.  
    102. End Sub
    103.  
    104. Private Sub Game_Loop()
    105.  
    106.     Do While Running = True
    107.         Render
    108.         DoEvents
    109.     Loop
    110.  
    111. End Sub
    112.  
    113. Private Sub Main()
    114.  
    115.     If MsgBox("Click Yes to go to fullscreen (Recommended)", vbQuestion Or vbYesNo, "Options") = vbYes Then Fullscreen_Enabled = True
    116.    
    117.     With Me
    118.         If Fullscreen_Enabled = True Then .BorderStyle = vbBSNone
    119.         .Show
    120.         .Caption = "DirectX Tutorial"
    121.     End With
    122.    
    123.     DirectX_Initialize
    124.     Load_Texture Texture, App.Path & "\texture.jpg", D3DColorXRGB(0, 0, 0)
    125.     Running = True
    126.     Game_Loop
    127.    
    128. End Sub
    129.  
    130. Private Sub Shutdown()
    131.  
    132.     Running = False
    133.     Set Texture = Nothing
    134.     Set Device = Nothing
    135.     Set D3DX = Nothing
    136.     Set D3D = Nothing
    137.     Set DX = Nothing
    138.     Unload Me
    139.    
    140. End Sub
    141.  
    142. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    143.  
    144.     If KeyCode = vbKeyEscape Then Shutdown
    145.  
    146. End Sub
    147.  
    148. Private Sub Form_Load()
    149.  
    150.     Main
    151.  
    152. End Sub
    153.  
    154. Private Sub Form_Unload(Cancel As Integer)
    155.  
    156.     Shutdown
    157.    
    158. End Sub

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

    Re: DirectX referencing

    .....CONTINUED

    Although for funsies, I even included how to do Texture Mapping in C# 2008/2010

    c# Code:
    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Windows.Forms;
    9. using Microsoft.DirectX;
    10. using Microsoft.DirectX.Direct3D;
    11.  
    12. namespace DX_Tut
    13. {
    14.     public partial class frmMain : Form
    15.     {
    16.         Device Device;
    17.         DisplayMode Display_Mode;
    18.         PresentParameters Screen;
    19.  
    20.         bool Fullscreen_Enabled;
    21.         bool Running;
    22.         CustomVertex.TransformedColoredTextured[] Vertex_List = new CustomVertex.TransformedColoredTextured[4];
    23.  
    24.         Texture Texture;
    25.  
    26.         public void DirectX_Initialize()
    27.         {
    28.             Screen = new PresentParameters();
    29.             if (Fullscreen_Enabled == true)
    30.             {
    31.                 Display_Mode.Width = 800;
    32.                 Display_Mode.Height = 600;
    33.                 Display_Mode.Format = Format.R5G6B5;
    34.                 Screen.Windowed = false;
    35.                 Screen.BackBufferCount = 1;
    36.                 Screen.BackBufferWidth = Display_Mode.Width;
    37.                 Screen.BackBufferHeight = Display_Mode.Height;
    38.             }
    39.             else
    40.             {
    41.                 Screen.Windowed = true;
    42.             }
    43.             Screen.SwapEffect = SwapEffect.Copy;
    44.             Screen.BackBufferFormat = Display_Mode.Format;
    45.             Device = new Device(0, DeviceType.Hardware, this.Handle, CreateFlags.SoftwareVertexProcessing, Screen);
    46.         }
    47.  
    48.         CustomVertex.TransformedColoredTextured Create_Custom_Vertex(float X, float Y, float Z, float RHW, int Color, float TU, float TV)
    49.         {
    50.             CustomVertex.TransformedColoredTextured Vertex = new CustomVertex.TransformedColoredTextured();
    51.             Vertex.Position = new Vector4(X, Y, Z, 1);
    52.             Vertex.Rhw = RHW;
    53.             Vertex.Color = Color;
    54.             Vertex.Tu = TU;
    55.             Vertex.Tv = TV;
    56.  
    57.             return Vertex;
    58.         }
    59.  
    60.         void Create_Polygon()
    61.         {
    62.             Vertex_List[0] = Create_Custom_Vertex(0, 0, 0, 1, Color.FromArgb(255, 255, 255).ToArgb(), 0, 0);
    63.             Vertex_List[1] = Create_Custom_Vertex(100, 0, 0, 1, Color.FromArgb(255, 255, 255).ToArgb(), 1, 0);
    64.             Vertex_List[2] = Create_Custom_Vertex(0, 100, 0, 1, Color.FromArgb(255, 255, 255).ToArgb(), 0, 1);
    65.             Vertex_List[3] = Create_Custom_Vertex(100, 100, 0, 1, Color.FromArgb(255, 255, 255).ToArgb(), 1, 1);
    66.         }
    67.  
    68.         void Draw_Polygon()
    69.         {
    70.             Device.VertexFormat = CustomVertex.TransformedColoredTextured.Format;
    71.             Device.SetTexture(0, Texture);
    72.             Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, Vertex_List);
    73.         }
    74.  
    75.         public void Load_Texture(string Filepath, int Transparency_Color)
    76.         {
    77.             Texture = TextureLoader.FromFile(Device, Filepath, 512, 512, 1, Usage.None, Format.A8B8G8R8, Pool.Managed, Filter.Point, Filter.Point, Transparency_Color);
    78.         }
    79.  
    80.         public void Render()
    81.         {
    82.             Device.Clear(ClearFlags.Target, Color.FromArgb(0, 0, 0), 1.0f, 0);
    83.             Device.BeginScene();
    84.             Create_Polygon();
    85.             Draw_Polygon();
    86.             Device.EndScene();
    87.             Device.Present();
    88.         }
    89.  
    90.         public void Game_Loop()
    91.         {
    92.             do
    93.             {
    94.                 Render();
    95.                 Application.DoEvents();
    96.             } while (Running == true);
    97.         }
    98.  
    99.         public void Main()
    100.         {
    101.             if (MessageBox.Show("Click Yes to go to fullscreen (Recommended)", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
    102.                 Fullscreen_Enabled = true;
    103.  
    104.             this.Show();
    105.             this.KeyPreview = true;
    106.             this.Width = 330;
    107.             this.Height = 250;
    108.             this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
    109.             this.Text = "DirectX Tutorial";
    110.             if (Fullscreen_Enabled == true)
    111.                 this.FormBorderStyle = FormBorderStyle.None;
    112.             DirectX_Initialize();
    113.             Load_Texture(Application.StartupPath + @"\Texture.jpg", 0);
    114.            
    115.             Running = true;
    116.  
    117.         }
    118.  
    119.         public void Shutdown()
    120.         {
    121.             Running = false;
    122.             Texture = null;
    123.             Device = null;
    124.             Application.Exit();
    125.         }
    126.  
    127.         public frmMain()
    128.         {
    129.             InitializeComponent();
    130.         }
    131.  
    132.         private void frmMain_Load(object sender, EventArgs e)
    133.         {
    134.             Main();
    135.         }
    136.  
    137.         private void frmMain_KeyDown(object sender, KeyEventArgs e)
    138.         {
    139.             if (e.KeyCode == Keys.Escape)
    140.                 Shutdown();
    141.         }
    142.  
    143.         private void frmMain_Paint(object sender, PaintEventArgs e)
    144.         {
    145.             Game_Loop();
    146.         }
    147.  
    148.         private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
    149.         {
    150.             Shutdown();
    151.         }
    152.  
    153.         private void frmMain_Resize(object sender, EventArgs e)
    154.         {
    155.             if (Fullscreen_Enabled == false)
    156.             {
    157.                 Running = false;
    158.                 Device.Reset(Screen);
    159.                 Application.DoEvents();
    160.                 Running = true;
    161.             }
    162.         }
    163.     }
    164. }

    C++ 2008/2010 Version

    C++ Code:
    1. #include <windows.h>
    2. #include <d3d9.h>
    3. #include <d3dx9.h>
    4.  
    5. struct CUSTOM_VERTEX
    6. {
    7.     float X, Y, Z, RHW;
    8.     DWORD Color;
    9.     float TU, TV;
    10. };
    11.  
    12. #define CUSTOM_VERTEX_FORMAT (D3DFVF_XYZRHW | D3DFVF_TEX1 | D3DFVF_DIFFUSE)
    13.  
    14. LPDIRECT3D9 D3D = NULL;
    15. LPDIRECT3DDEVICE9 Device = NULL;
    16. D3DDISPLAYMODE Display_Mode;
    17. D3DPRESENT_PARAMETERS Screen;
    18. HWND hWnd;
    19. MSG msg;
    20. bool Fullscreen_Enabled;
    21. bool Running;
    22. CUSTOM_VERTEX Vertex_List[4];
    23. LPDIRECT3DTEXTURE9 Texture = NULL;
    24.  
    25. CUSTOM_VERTEX Create_Custom_Vertex();
    26. void Load_Texture();
    27. void Create_Polygon();
    28. void Draw_Polygon();
    29. void Render();
    30. void Game_Loop();
    31. void Main();
    32. void Shutdown();
    33. void DirectX_Initialize();
    34. LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    35.  
    36. void DirectX_Initialize()
    37. {
    38.     D3D = Direct3DCreate9(D3D_SDK_VERSION);
    39.     memset(&Screen, 0, sizeof(D3DPRESENT_PARAMETERS));
    40.  
    41.     if (Fullscreen_Enabled == true)
    42.     {
    43.        Display_Mode.Width = 800;
    44.        Display_Mode.Height = 600;
    45.        Display_Mode.Format = D3DFMT_R5G6B5;
    46.        Screen.Windowed = FALSE;
    47.        Screen.BackBufferCount = 1;
    48.        Screen.BackBufferWidth = Display_Mode.Width;
    49.        Screen.BackBufferHeight = Display_Mode.Height;
    50.        Screen.hDeviceWindow = hWnd;
    51.     }
    52.     else
    53.     {
    54.        D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &Display_Mode);
    55.        Screen.Windowed = TRUE;
    56.     }
    57.     Screen.SwapEffect = D3DSWAPEFFECT_COPY;
    58.     Screen.BackBufferFormat = Display_Mode.Format;
    59.     D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &Screen, &Device);
    60. }
    61.  
    62. CUSTOM_VERTEX Create_Custom_Vertex(float X, float Y, float Z, float RHW, DWORD Color, float TU, float TV)
    63. {
    64.     CUSTOM_VERTEX Vertex;
    65.  
    66.     Vertex.X = X;
    67.     Vertex.Y = Y;
    68.     Vertex.Z = Z;
    69.     Vertex.RHW = RHW;
    70.     Vertex.Color = Color;
    71.     Vertex.TU = TU;
    72.     Vertex.TV = TV;
    73.  
    74.     return Vertex;
    75. }
    76.  
    77. void Load_Texture(LPDIRECT3DTEXTURE9 *Texture, char *File_Path, int Transparency_Color)
    78. {
    79.     D3DXCreateTextureFromFileEx(Device, File_Path, 512, 512, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, D3DX_FILTER_POINT, D3DX_FILTER_POINT, Transparency_Color, NULL, NULL, Texture);
    80. }
    81.  
    82. void Create_Polygon()
    83. {
    84.     Vertex_List[0] = Create_Custom_Vertex(0, 0, 0, 1, D3DCOLOR_XRGB(255, 255, 255), 0, 0);
    85.     Vertex_List[1] = Create_Custom_Vertex(100, 0, 0, 1, D3DCOLOR_XRGB(255, 255, 255), 1, 0);
    86.     Vertex_List[2] = Create_Custom_Vertex(0, 100, 0, 1, D3DCOLOR_XRGB(255, 255, 255), 0, 1);
    87.     Vertex_List[3] = Create_Custom_Vertex(100, 100, 0, 1, D3DCOLOR_XRGB(255, 255, 255), 1, 1);
    88. }
    89.  
    90. void Draw_Polygon()
    91. {
    92.     Device->SetFVF(CUSTOM_VERTEX_FORMAT);
    93.     Device->SetTexture(0, Texture);
    94.     Device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, Vertex_List, sizeof(CUSTOM_VERTEX));
    95. }
    96.  
    97. void Render()
    98. {    
    99.     Device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); // clear frame
    100.     Device->BeginScene();
    101.     //Rendering code goes here
    102.     Create_Polygon();
    103.     Draw_Polygon();
    104.     Device->EndScene();
    105.     Device->Present(NULL, NULL, NULL, NULL);
    106. }
    107.  
    108. void Game_Loop()
    109. {
    110.     while (Running == true)
    111.     {
    112.         if (PeekMessage(&msg,NULL,0,0,PM_REMOVE) > 0)
    113.         {
    114.             if (WM_QUIT == msg.message) break;
    115.             TranslateMessage (&msg);
    116.             DispatchMessage (&msg);
    117.         }
    118.         else
    119.             Render();
    120.     }
    121. }
    122.  
    123. void Main()
    124. {
    125.     DirectX_Initialize();
    126.     Load_Texture(&Texture, "Texture.jpg", D3DCOLOR_XRGB(0, 0, 0));
    127.     Running = true;
    128. }
    129.  
    130. void Shutdown()
    131. {
    132.     Running = false;
    133.     Texture = NULL;
    134.     Device->Release();
    135.     D3D->Release();
    136.     PostQuitMessage (0);
    137.     HANDLE Process;
    138.     Process = OpenProcess(PROCESS_ALL_ACCESS , true , GetCurrentProcessId());
    139.     TerminateProcess(Process , 0);
    140. }
    141.  
    142. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow)
    143. {
    144.    
    145.     WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_VREDRAW|CS_HREDRAW|CS_OWNDC, WindowProcedure, 0, 0, hInstance, NULL, LoadCursor(NULL, IDC_ARROW), NULL, NULL, "DX_TUT", NULL};
    146.     RegisterClassEx(&wc);
    147.  
    148.     if (MessageBox(hWnd, "Click Yes to go to fullscreen (Recommended)", "", MB_ICONQUESTION | MB_YESNO) == IDYES)
    149.        Fullscreen_Enabled = true;
    150.  
    151.     if (Fullscreen_Enabled == true)
    152.         hWnd = CreateWindowEx (0, "DX_TUT", "DirectX Tutorial", WS_VISIBLE | WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, CW_USEDEFAULT, CW_USEDEFAULT, 330, 250, HWND_DESKTOP, NULL, hInstance, NULL);
    153.     else
    154.         hWnd = CreateWindowEx (0, "DX_TUT", "DirectX Tutorial", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 330, 250, HWND_DESKTOP, NULL, hInstance, NULL);
    155.     ShowWindow (hWnd, nCmdShow);
    156.     Main();
    157.     Game_Loop();
    158.     return msg.wParam;
    159. }
    160.  
    161. LRESULT CALLBACK WindowProcedure (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    162. {
    163.     switch (msg)
    164.     {
    165.         case WM_DESTROY:
    166.             Shutdown();
    167.             break;
    168.         case WM_KEYDOWN:
    169.             if(wParam == VK_ESCAPE)
    170.             {
    171.                 DestroyWindow(hWnd);
    172.                 return(0);
    173.             }
    174.         default:
    175.             return DefWindowProc (hWnd, msg, wParam, lParam);
    176.     }
    177.     return 0;
    178. }

  7. #7

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

    Re: DirectX referencing

    The code looks really good! I put it into a test program and had some issues. First of all, i can't run it, same problem as before. I got a "File Load Exception was unhandled" error message when i ran it which said:

    "Mixed mode assembly is built against version 'v1.1.4322' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information."

    I'm guessing that this is because the project isn't switched to the 3.5 framework. Do i even get that with the original install or do i need to download it? I can try that next. I got the latest directx sdk from microsoft and installed it.

    Just a problem (or at least i think so) with the code. I had an error in the Load_Texture sub. TextureLoader is not declared.

    Code:
    Texture = TextureLoader.FromFile(Device, Filepath, 512, 512, 1, Usage.None, Format.A8B8G8R8, Pool.Managed, Filter.Point, Filter.Point, Transparency_Color)
    This code is awesome, just like your tutorials for vb6, thanks for sharing it!
    Hooked for good.

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

    Re: DirectX referencing

    You also have to reference Microsoft.DirectX.Direct3DX. Sorry that I forgot to mention it. Direct3DX is used for anything texture mapping related. Try it and TextureLoader along with everything else should work. Also I been using the Express edition of 2010 and 2008 and both come with the frameworks and both work fine.

    [EDIT]
    Also if you read the notepad for VB.Net 2010 you needed to do this to change the framework to 3.5 in your current project:

    Code:
    Project must be in X86 for DirectX9 apps to run. And thankfully it's x86 by default. However the .Net Framework is 4.0 and needs changed to 3.5. Here's how to set it up:
    - Go towards the right of the screen where it says Solution Explorer.
    - Right click your project name and goto Properties.
    - On the left side, select Compile.
    - Click on the button Advanced Compile Options... A new window should pop up.
    - On the bottom where it says Target Framework, select .NET Framework 3.5
    - Click Ok.
    - You are now ready to code with DirectX

  9. #9

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

    Re: DirectX referencing

    Wahoo!!! I just ran the example! I'm still having a problem switching to the 3.5 framework so i "cheated". I opened my project file in notepad and changed the line
    Code:
        <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
    to

    Code:
     <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
    I opened the project and it said that the target framework was 3.5. I was afraid the change was only on the surface but when i ran it, it worked.

    On a matter of criticism on your code: I first ran it in windowed mode which is much easier for debugging. I made a change in the code so that the resize event now looks like this because before it would fire when the size is set in the Main sub.

    Code:
    Private Sub frmMain_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
    
            If Fullscreen_Enabled = False And Device IsNot Nothing Then
                ...
            End If
    
        End Sub
    Other errors are:
    An error when i resize the form
    An error when i move the form off of the screen and then back on.

    Besides the problems i put a Texture.jpg in the correct folder and right now have my first basic dx9 program running.

    Edit:

    You also have to reference Microsoft.DirectX.Direct3DX.
    Right, i referenced it and the TextureLoader problem went away.
    Last edited by cheesebrother; Jan 31st, 2012 at 09:46 AM.
    Hooked for good.

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

    Re: DirectX referencing

    The resize code I had was necessary cause if you were to stretch the window with no code in the Resize event the program would lock up and you would be forced to hit the stop button in IDE. Its the same in C#. I don't know why it was like that. VB6 and C++ you didn't have to cause you can stretch the window with no problem. So if I were you I'd keep it the way it was. Try not to change the code I had in resize or there will be errors like you mentioned. Also I find it weird that even with the steps I gave you, you still couldn't change the framework. Visual Basic.Net 2010 has all sorts of problems. Like the random crashing when trying to save. But thats not the only language with that problem. Visual C++.Net 2010 has issues to when creating a project into a directory thats over 128 characters. The C++ program you made when trying to run gives you errors that it cant find certain files in the directory. I have a feeling all the 2010 languages have issues. It's best to downgrade to 2008 until something better comes out. You don't really need 2010 unless you absolutely need it.

  11. #11

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

    Re: DirectX referencing

    So do you have suggestions for fixing the errors that i mentioned? Also, would it be possible to share code for drawing opaque images (like .png) and how to alphablend images? That would be really super.
    Hooked for good.

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

    Re: DirectX referencing

    Quote Originally Posted by cheesebrother
    An error when i move the form off of the screen and then back on
    Here's a solution to the Alt+Tab issue. I have it in all languages btw. When you alt tab out of a regular directx program without the alt tab solution and try to reenter in regardless of what language it was written in, it crashes the program. This solves it:

    vb.net Code:
    1. Option Explicit On
    2. Option Strict On
    3.  
    4. Imports System.Threading
    5. Imports Microsoft.DirectX
    6. Imports Microsoft.DirectX.Direct3D
    7.  
    8. Public Class frmMain
    9.  
    10.     Private Device As Device
    11.     Private Display_Mode As DisplayMode
    12.     Private Screen As PresentParameters = New PresentParameters
    13.  
    14.     Private Running As Boolean = True
    15.  
    16.     Private Vertex_List As CustomVertex.TransformedColored() = New CustomVertex.TransformedColored(3) {}
    17.  
    18.     Private Sub DirectX_Initialize()
    19.  
    20.         Display_Mode.Width = 800
    21.         Display_Mode.Height = 600
    22.         Display_Mode.Format = Format.R5G6B5
    23.         Screen.Windowed = False
    24.         Screen.BackBufferCount = 1
    25.         Screen.BackBufferWidth = Display_Mode.Width
    26.         Screen.BackBufferHeight = Display_Mode.Height
    27.  
    28.         Screen.SwapEffect = SwapEffect.Copy
    29.         Screen.BackBufferFormat = Display_Mode.Format
    30.         Device = New Device(0, DeviceType.Hardware, Me.Handle, CreateFlags.SoftwareVertexProcessing, Screen)
    31.  
    32.         'Not needed but used in this example to put these back when reseting the device after entering back into the program
    33.         'after Alt+Tabbing out of it.
    34.         Device.SetTextureStageState(0, TextureStageStates.ColorOperation, TextureOperation.Modulate)
    35.         Device.SetTextureStageState(0, TextureStageStates.ColorArgument1, TextureArgument.TextureColor)
    36.         Device.SetTextureStageState(0, TextureStageStates.ColorArgument2, TextureArgument.Diffuse)
    37.         Device.SetTextureStageState(0, TextureStageStates.AlphaOperation, TextureOperation.Modulate)
    38.         Device.SetTextureStageState(0, TextureStageStates.AlphaArgument1, TextureArgument.TextureColor)
    39.         Device.SetTextureStageState(0, TextureStageStates.AlphaArgument2, TextureArgument.Diffuse)
    40.         Device.SetRenderState(RenderStates.SourceBlend, Blend.SourceAlpha)
    41.         Device.SetRenderState(RenderStates.DestinationBlend, Blend.InvSourceAlpha)
    42.         Device.SetRenderState(RenderStates.BlendOperation, BlendOperation.Add)
    43.         Device.SetSamplerState(0, SamplerStageStates.MinFilter, TextureFilter.Point)
    44.         Device.SetSamplerState(0, SamplerStageStates.MagFilter, TextureFilter.Point)
    45.  
    46.     End Sub
    47.  
    48.     Private Function Create_Custom_Vertex(ByVal X As Single, ByVal Y As Single, ByVal Z As Single, ByVal RHW As Single, ByVal Color As Integer) As CustomVertex.TransformedColored
    49.  
    50.         Dim Vertex As CustomVertex.TransformedColored = New CustomVertex.TransformedColored
    51.  
    52.         Vertex.Position = New Vector4(X, Y, Z, 1)
    53.         Vertex.Rhw = RHW
    54.         Vertex.Color = Color
    55.  
    56.         Return Vertex
    57.  
    58.     End Function
    59.  
    60.     Private Sub Create_Polygon()
    61.  
    62.         Vertex_List(0) = Create_Custom_Vertex(0, 0, 0, 1, Color.FromArgb(255, 255, 255).ToArgb)
    63.         Vertex_List(1) = Create_Custom_Vertex(100, 0, 0, 1, Color.FromArgb(255, 255, 255).ToArgb)
    64.         Vertex_List(2) = Create_Custom_Vertex(0, 100, 0, 1, Color.FromArgb(255, 255, 255).ToArgb)
    65.         Vertex_List(3) = Create_Custom_Vertex(100, 100, 0, 1, Color.FromArgb(255, 255, 255).ToArgb)
    66.  
    67.     End Sub
    68.  
    69.     Private Sub Draw_Polygon()
    70.  
    71.         Device.VertexFormat = CustomVertex.TransformedColored.Format
    72.         Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, Vertex_List)
    73.  
    74.     End Sub
    75.  
    76.     Private Sub Render()
    77.  
    78.         Device.Clear(ClearFlags.Target, Color.FromArgb(0, 0, 0), 1.0#, 0)
    79.         Device.BeginScene()
    80.         'Rendering code goes here
    81.         Create_Polygon()
    82.         Draw_Polygon()
    83.         Device.EndScene()
    84.         Device.Present()
    85.  
    86.     End Sub
    87.  
    88.     Private Sub Game_Loop()
    89.  
    90.         Do While Running = True
    91.             Try
    92.                 Device.TestCooperativeLevel()
    93.                 Render()
    94.             Catch hr As DeviceLostException
    95.                 Thread.Sleep(50)
    96.             Catch hr As DeviceNotResetException
    97.                 Device.Reset(Screen)
    98.                 Device.SetTextureStageState(0, TextureStageStates.ColorOperation, TextureOperation.Modulate)
    99.                 Device.SetTextureStageState(0, TextureStageStates.ColorArgument1, TextureArgument.TextureColor)
    100.                 Device.SetTextureStageState(0, TextureStageStates.ColorArgument2, TextureArgument.Diffuse)
    101.                 Device.SetTextureStageState(0, TextureStageStates.AlphaOperation, TextureOperation.Modulate)
    102.                 Device.SetTextureStageState(0, TextureStageStates.AlphaArgument1, TextureArgument.TextureColor)
    103.                 Device.SetTextureStageState(0, TextureStageStates.AlphaArgument2, TextureArgument.Diffuse)
    104.                 Device.SetRenderState(RenderStates.SourceBlend, Blend.SourceAlpha)
    105.                 Device.SetRenderState(RenderStates.DestinationBlend, Blend.InvSourceAlpha)
    106.                 Device.SetRenderState(RenderStates.BlendOperation, BlendOperation.Add)
    107.                 Device.SetSamplerState(0, SamplerStageStates.MinFilter, TextureFilter.Point)
    108.                 Device.SetSamplerState(0, SamplerStageStates.MagFilter, TextureFilter.Point)
    109.             End Try
    110.             Application.DoEvents()
    111.         Loop
    112.  
    113.     End Sub
    114.  
    115.     Private Sub Main()
    116.  
    117.         With Me
    118.             .Show()
    119.             .Width = 330
    120.             .Height = 250
    121.             .SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.Opaque, True)
    122.             .Text = "DirectX Tutorial"
    123.             .FormBorderStyle = Windows.Forms.FormBorderStyle.None
    124.         End With
    125.  
    126.         DirectX_Initialize()
    127.         Running = True
    128.  
    129.     End Sub
    130.  
    131.     Private Sub Shutdown()
    132.  
    133.         Running = False
    134.         Device = Nothing
    135.         Application.Exit()
    136.  
    137.     End Sub
    138.  
    139.     Private Sub frmMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    140.  
    141.         If e.KeyCode = Keys.Escape Then Shutdown()
    142.  
    143.     End Sub
    144.  
    145.     Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    146.  
    147.         Main()
    148.  
    149.     End Sub
    150.  
    151.     Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    152.  
    153.         Shutdown()
    154.  
    155.     End Sub
    156.  
    157.     Private Sub frmMain_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    158.  
    159.         Game_Loop()
    160.  
    161.     End Sub
    162.  
    163. End Class

    Try to alt tab out and enter back in. Boom it works.

    Quote Originally Posted by cheesebrother
    An error when I try resizing the form
    I already provided the solution. Stick with this code and try not to change it:

    vb.net Code:
    1. Private Sub frmMain_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
    2.  
    3.         If Fullscreen_Enabled = False Then
    4.             Running = False
    5.             Device.Reset(Screen)
    6.             Application.DoEvents()
    7.             Running = True
    8.         End If
    9.  
    10.     End Sub

    And I gotta goto work so I'll provide graphical examples when I get home late tonight.

  13. #13

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

    Re: DirectX referencing

    Ok, thanks.
    Hooked for good.

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

    Re: DirectX referencing

    Here's code to produce transparency so the background is removed from the sprite. Also the alpha values of the vertices can now be changed to where it can fade away or produce ghostlike results at any alpha you choose.

    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 Device As Device
    10.     Private Display_Mode As DisplayMode
    11.     Private Screen As PresentParameters = New PresentParameters
    12.  
    13.     Private Fullscreen_Enabled As Boolean
    14.     Private Running As Boolean = True
    15.  
    16.     Private Vertex_List As CustomVertex.TransformedColoredTextured() = New CustomVertex.TransformedColoredTextured(0 To 3) {} 'create an array of vertices
    17.     Private Texture As Texture
    18.  
    19.     Private Function Create_Custom_Vertex(ByVal X As Single, ByVal Y As Single, ByVal Z As Single, ByVal Color As Integer, ByVal TU As Integer, ByVal TV As Integer) As CustomVertex.TransformedColoredTextured
    20.  
    21.         Dim Vertex As CustomVertex.TransformedColoredTextured
    22.  
    23.         Vertex.Position = New Vector4(X, Y, Z, 1)
    24.         Vertex.Color = Color
    25.         Vertex.Tu = TU
    26.         Vertex.Tv = TV
    27.  
    28.         Return Vertex
    29.  
    30.     End Function
    31.  
    32.     Private Sub DirectX_Initialize()
    33.  
    34.         If Fullscreen_Enabled = True Then
    35.             Display_Mode.Width = 800
    36.             Display_Mode.Height = 600
    37.             Display_Mode.Format = Format.R5G6B5
    38.             Screen.Windowed = False
    39.             Screen.BackBufferCount = 1
    40.             Screen.BackBufferWidth = Display_Mode.Width
    41.             Screen.BackBufferHeight = Display_Mode.Height
    42.         Else
    43.             Screen.Windowed = True
    44.         End If
    45.  
    46.         Screen.SwapEffect = SwapEffect.Copy
    47.         Screen.BackBufferFormat = Display_Mode.Format
    48.  
    49.         Device = New Device(0, DeviceType.Hardware, Me.Handle, CreateFlags.SoftwareVertexProcessing, Screen)
    50.  
    51.         Device.SetRenderState(RenderStates.AlphaBlendEnable, True)
    52.  
    53.         Device.SetTextureStageState(0, TextureStageStates.ColorOperation, TextureOperation.Modulate)
    54.         Device.SetTextureStageState(0, TextureStageStates.ColorArgument1, TextureArgument.TextureColor)
    55.         Device.SetTextureStageState(0, TextureStageStates.ColorArgument2, TextureArgument.Diffuse)
    56.  
    57.         Device.SetTextureStageState(0, TextureStageStates.AlphaOperation, TextureOperation.Modulate)
    58.         Device.SetTextureStageState(0, TextureStageStates.AlphaArgument1, TextureArgument.TextureColor)
    59.         Device.SetTextureStageState(0, TextureStageStates.AlphaArgument2, TextureArgument.Diffuse)
    60.  
    61.         Device.SetRenderState(RenderStates.SourceBlend, Blend.SourceAlpha)
    62.         Device.SetRenderState(RenderStates.DestinationBlend, Blend.InvSourceAlpha)
    63.         Device.SetRenderState(RenderStates.BlendOperation, TextureOperation.Add)
    64.  
    65.         Device.SetSamplerState(0, SamplerStageStates.MinFilter, TextureFilter.Point)
    66.         Device.SetSamplerState(0, SamplerStageStates.MagFilter, TextureFilter.Point)
    67.  
    68.     End Sub
    69.  
    70.     Private Sub Create_Polygon()
    71.  
    72.         Vertex_List(0) = Create_Custom_Vertex(0, 0, 0, System.Drawing.Color.FromArgb(255, 255, 255, 255).ToArgb, 0, 0)
    73.         Vertex_List(1) = Create_Custom_Vertex(100, 0, 0, System.Drawing.Color.FromArgb(255, 255, 255, 255).ToArgb, 1, 0)
    74.         Vertex_List(2) = Create_Custom_Vertex(0, 100, 0, System.Drawing.Color.FromArgb(255, 255, 255, 255).ToArgb, 0, 1)
    75.         Vertex_List(3) = Create_Custom_Vertex(100, 100, 0, System.Drawing.Color.FromArgb(255, 255, 255, 255).ToArgb, 1, 1)
    76.  
    77.     End Sub
    78.  
    79.     Private Sub Draw_Polygon()
    80.  
    81.         Device.VertexFormat = CustomVertex.TransformedColoredTextured.Format
    82.         Device.SetTexture(0, Texture)
    83.         Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, Vertex_List)
    84.  
    85.     End Sub
    86.  
    87.     Private Sub Load_Texture(ByRef Texture As Texture, ByVal Filepath As String, ByVal Transparency_Color As Integer)
    88.  
    89.         Texture = TextureLoader.FromFile(Device, Filepath, 512, 512, 1, Usage.None, Format.A8B8G8R8, Pool.Managed, Filter.Point, Filter.Point, Transparency_Color)
    90.  
    91.     End Sub
    92.  
    93.     Private Sub Render()
    94.  
    95.         Device.Clear(ClearFlags.Target, Color.Black, 1.0, 0)
    96.         Device.BeginScene()
    97.         'Rendering code goes here.
    98.         Create_Polygon()
    99.         Draw_Polygon()
    100.         Device.EndScene()
    101.         Device.Present()
    102.         Application.DoEvents()
    103.  
    104.     End Sub
    105.  
    106.     Private Sub Game_Loop()
    107.  
    108.         Do While Running = True
    109.             Render()
    110.         Loop
    111.  
    112.     End Sub
    113.  
    114.     Private Sub Main()
    115.  
    116.         If MessageBox.Show("Click Yes to go to full screen (Recommended)", "", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = DialogResult.Yes Then
    117.             Fullscreen_Enabled = True
    118.         End If
    119.  
    120.         With Me
    121.             .Show()
    122.             .Width = 330
    123.             .Height = 250
    124.             .SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.Opaque, True) 'Do not draw forms background
    125.             .Text = "DirectX Tutorial"
    126.             If Fullscreen_Enabled = True Then .FormBorderStyle = Windows.Forms.FormBorderStyle.None
    127.         End With
    128.  
    129.         DirectX_Initialize()
    130.         Load_Texture(Texture, Application.StartupPath() & "\Sprite1.png", System.Drawing.Color.FromArgb(255, 0, 0, 0).ToArgb)
    131.         Running = True
    132.  
    133.     End Sub
    134.  
    135.     Private Sub Shutdown()
    136.  
    137.         Running = False
    138.         Texture = Nothing
    139.         Device = Nothing
    140.         Application.Exit()
    141.  
    142.     End Sub
    143.  
    144.  
    145.     Private Sub frmMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    146.  
    147.         If e.KeyCode = Keys.Escape Then Shutdown()
    148.  
    149.     End Sub
    150.  
    151.     Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    152.  
    153.         Main()
    154.  
    155.     End Sub
    156.  
    157.     Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    158.  
    159.         Shutdown()
    160.  
    161.     End Sub
    162.  
    163.     Private Sub frmMain_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    164.  
    165.         Game_Loop()
    166.  
    167.     End Sub
    168.  
    169.     Private Sub frmMain_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
    170.  
    171.         If Fullscreen_Enabled = False Then
    172.             Running = False
    173.             Device.Reset(Screen)
    174.             Application.DoEvents()
    175.             Running = True
    176.         End If
    177.  
    178.     End Sub
    179. End Class

  15. #15

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

    Re: DirectX referencing

    Hmm, i tried that and it didn't really work. I drew a .png and it displayed alright on the black background which you've set it to clear to. Then i changed the clear color to white so that i could see the shadow of my image. Now it wasn't drawing anything. I changed it to various colors and each time the image was drawn semi transparent on the background color. It's like the image was being mixed with the background color. I also edited the Create_Polygon sub to try out vertex transparency like this.

    Code:
    Private Sub Create_Polygon()
    
            Vertex_List(0) = Create_Custom_Vertex(0, 0, 0, System.Drawing.Color.FromArgb(0, 255, 255, 255).ToArgb, 0, 0)
            Vertex_List(1) = Create_Custom_Vertex(100, 0, 0, System.Drawing.Color.FromArgb(0, 255, 255, 255).ToArgb, 1, 0)
            Vertex_List(2) = Create_Custom_Vertex(0, 100, 0, System.Drawing.Color.FromArgb(255, 255, 255, 255).ToArgb, 0, 1)
            Vertex_List(3) = Create_Custom_Vertex(100, 100, 0, System.Drawing.Color.FromArgb(255, 255, 255, 255).ToArgb, 1, 1)
    
        End Sub
    Nothing happened, am I not doing it right or could it be connected with the first problem?
    Hooked for good.

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

    Re: DirectX referencing

    I think you might have issues with your SDK or something. Could you tell me the SDK's you installed and what DX files you are referencing?

    On my comp I have the SDK's installed in this order:

    Code:
    DirectX 8.1 SDK
    DirectX SDK October 2006
    DirectX SDK August 2008
    DirectX SDK June 2010
    If you were to install an older version after a later version it can confuse the IDE's. I also have this referenced:

    Code:
    Microsoft.DirectX       version 1.0.2902.0
    Microsoft.DirectX.Direct3D version 1.0.2902.0
    Microsoft.DirectX.Direct3DX version 1.0.2902.0

  17. #17

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

    Re: DirectX referencing

    Blast! You're probably right. I got the latest version of what i though was the sdk, it was "DirectX End-User Runtimes (June 2010) Full Download." I'll get the one called "June 2010 DirectX SDK", try it and see if it fixes these problems. This is the link where i've gotten it from: Webpage. I've only got the dx 8.1 sdk installed so there shouldn't be a problem there.

    Actually i'm just seeing that the real sdk is 571.1 mb so i've definitely got the wrong thing.
    Hooked for good.

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

    Re: DirectX referencing

    You are better off getting all of em as the June 2010 SDK has no samples in VB.Net. Just C++ and C#. What I have shown are the latest versions for DirectX 8, 9, 10, and 11. Also try this instead so you see it partially fade. Having 0 would make it not appear at all cause the alpha's 0 and you would only see half the image:

    Code:
    Private Sub Create_Polygon()
    
            Vertex_List(0) = Create_Custom_Vertex(0, 0, 0, System.Drawing.Color.FromArgb(155, 255, 255, 255).ToArgb, 0, 0)
            Vertex_List(1) = Create_Custom_Vertex(100, 0, 0, System.Drawing.Color.FromArgb(155, 255, 255, 255).ToArgb, 1, 0)
            Vertex_List(2) = Create_Custom_Vertex(0, 100, 0, System.Drawing.Color.FromArgb(155, 255, 255, 255).ToArgb, 0, 1)
            Vertex_List(3) = Create_Custom_Vertex(100, 100, 0, System.Drawing.Color.FromArgb(155, 255, 255, 255).ToArgb, 1, 1)
    
        End Sub

  19. #19

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

    Re: DirectX referencing

    You are better off getting all of em as the June 2010 SDK has no samples in VB.Net. Just C++ and C#. What I have shown are the latest versions for DirectX 8, 9, 10, and 11.
    Ok, you're right. I'd rather have the examples for vb.net. I'm using XP so getting the sdks for 10 and 11 would be useless. So if i were to get the dx9 sdk that would be enough to run your example, right?
    Hooked for good.

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

    Re: DirectX referencing

    Yes so you will need DirectX SDK October 2006 then

  21. #21

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

    Re: DirectX referencing

    Well, I installed the October 06 sdk and so far the example doesn't run any differently. Do i need to reference different files? Right now the ones that i am referencing are in "C:\WINDOWS\Microsoft.NET\DirectX for Managed Code\1.0.2902.0". I ran the sdk installer and i think that that's all that is needed for it to work but, as I said, there doesn’t seem to be any change except for a bunch of new things in my start menu. Also, I was looking at the documentation and found that all the examples are in C#, can this be changed?
    Hooked for good.

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

    Re: DirectX referencing

    I noticed that the latest SDK for DX 9 didn't have VB.Net examples either. Thats strange. As if Microsoft is trying to stay away from VB. I use to have an earlier sdk that did have em. But don't remember which one. Perhaps a google search would help. And you did claim that it did work until you set the background to white, in turn it drew nothing. So I'll do the same in this code example I'm about to show. I changed the color scheme from Color.Black to System.Drawing.Color.FromArgb(255, 255, 255, 255).ToArgb to use as white so try that. I also added some extra code in resize cause when you reset the device, it removes all the alpha / color texture info. You have to have that reinitialized or otherwise after stretching the window, the sprite is no longer transparent. I tested this exact code and it works:


    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 Device As Device
    10.     Private Display_Mode As DisplayMode
    11.     Private Screen As PresentParameters = New PresentParameters
    12.  
    13.     Private Fullscreen_Enabled As Boolean
    14.     Private Running As Boolean = True
    15.  
    16.     Private Vertex_List As CustomVertex.TransformedColoredTextured() = New CustomVertex.TransformedColoredTextured(0 To 3) {} 'create an array of vertices
    17.     Private Texture As Texture
    18.  
    19.     Private Function Create_Custom_Vertex(ByVal X As Single, ByVal Y As Single, ByVal Z As Single, ByVal Color As Integer, ByVal TU As Integer, ByVal TV As Integer) As CustomVertex.TransformedColoredTextured
    20.  
    21.         Dim Vertex As CustomVertex.TransformedColoredTextured
    22.  
    23.         Vertex.Position = New Vector4(X, Y, Z, 1)
    24.         Vertex.Color = Color
    25.         Vertex.Tu = TU
    26.         Vertex.Tv = TV
    27.  
    28.         Return Vertex
    29.  
    30.     End Function
    31.  
    32.     Private Sub DirectX_Initialize()
    33.  
    34.         If Fullscreen_Enabled = True Then
    35.             Display_Mode.Width = 800
    36.             Display_Mode.Height = 600
    37.             Display_Mode.Format = Format.R5G6B5
    38.             Screen.Windowed = False
    39.             Screen.BackBufferCount = 1
    40.             Screen.BackBufferWidth = Display_Mode.Width
    41.             Screen.BackBufferHeight = Display_Mode.Height
    42.         Else
    43.             Screen.Windowed = True
    44.         End If
    45.  
    46.         Screen.SwapEffect = SwapEffect.Copy
    47.         Screen.BackBufferFormat = Display_Mode.Format
    48.  
    49.         Device = New Device(0, DeviceType.Hardware, Me.Handle, CreateFlags.SoftwareVertexProcessing, Screen)
    50.  
    51.         Device.SetRenderState(RenderStates.AlphaBlendEnable, True)
    52.  
    53.         Device.SetTextureStageState(0, TextureStageStates.ColorOperation, TextureOperation.Modulate)
    54.         Device.SetTextureStageState(0, TextureStageStates.ColorArgument1, TextureArgument.TextureColor)
    55.         Device.SetTextureStageState(0, TextureStageStates.ColorArgument2, TextureArgument.Diffuse)
    56.  
    57.         Device.SetTextureStageState(0, TextureStageStates.AlphaOperation, TextureOperation.Modulate)
    58.         Device.SetTextureStageState(0, TextureStageStates.AlphaArgument1, TextureArgument.TextureColor)
    59.         Device.SetTextureStageState(0, TextureStageStates.AlphaArgument2, TextureArgument.Diffuse)
    60.  
    61.         Device.SetRenderState(RenderStates.SourceBlend, Blend.SourceAlpha)
    62.         Device.SetRenderState(RenderStates.DestinationBlend, Blend.InvSourceAlpha)
    63.         Device.SetRenderState(RenderStates.BlendOperation, TextureOperation.Add)
    64.  
    65.         Device.SetSamplerState(0, SamplerStageStates.MinFilter, TextureFilter.Point)
    66.         Device.SetSamplerState(0, SamplerStageStates.MagFilter, TextureFilter.Point)
    67.  
    68.     End Sub
    69.  
    70.     Private Sub Create_Polygon()
    71.  
    72.         Vertex_List(0) = Create_Custom_Vertex(0, 0, 0, System.Drawing.Color.FromArgb(255, 255, 255, 255).ToArgb, 0, 0)
    73.         Vertex_List(1) = Create_Custom_Vertex(100, 0, 0, System.Drawing.Color.FromArgb(255, 255, 255, 255).ToArgb, 1, 0)
    74.         Vertex_List(2) = Create_Custom_Vertex(0, 100, 0, System.Drawing.Color.FromArgb(255, 255, 255, 255).ToArgb, 0, 1)
    75.         Vertex_List(3) = Create_Custom_Vertex(100, 100, 0, System.Drawing.Color.FromArgb(255, 255, 255, 255).ToArgb, 1, 1)
    76.  
    77.     End Sub
    78.  
    79.     Private Sub Draw_Polygon()
    80.  
    81.         Device.VertexFormat = CustomVertex.TransformedColoredTextured.Format
    82.         Device.SetTexture(0, Texture)
    83.         Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, Vertex_List)
    84.  
    85.     End Sub
    86.  
    87.     Private Sub Load_Texture(ByRef Texture As Texture, ByVal Filepath As String, ByVal Transparency_Color As Integer)
    88.  
    89.         Texture = TextureLoader.FromFile(Device, Filepath, 512, 512, 1, Usage.None, Format.A8B8G8R8, Pool.Managed, Filter.Point, Filter.Point, Transparency_Color)
    90.  
    91.     End Sub
    92.  
    93.     Private Sub Render()
    94.  
    95.         Device.Clear(ClearFlags.Target, System.Drawing.Color.FromArgb(255, 255, 255, 255).ToArgb, 1.0, 0)
    96.         Device.BeginScene()
    97.         'Rendering code goes here.
    98.         Create_Polygon()
    99.         Draw_Polygon()
    100.         Device.EndScene()
    101.         Device.Present()
    102.         Application.DoEvents()
    103.  
    104.     End Sub
    105.  
    106.     Private Sub Game_Loop()
    107.  
    108.         Do While Running = True
    109.             Render()
    110.         Loop
    111.  
    112.     End Sub
    113.  
    114.     Private Sub Main()
    115.  
    116.         If MessageBox.Show("Click Yes to go to full screen (Recommended)", "", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = DialogResult.Yes Then
    117.             Fullscreen_Enabled = True
    118.         End If
    119.  
    120.         With Me
    121.             .Show()
    122.             .Width = 330
    123.             .Height = 250
    124.             .SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.Opaque, True) 'Do not draw forms background
    125.             .Text = "DirectX Tutorial"
    126.             If Fullscreen_Enabled = True Then .FormBorderStyle = Windows.Forms.FormBorderStyle.None
    127.         End With
    128.  
    129.         DirectX_Initialize()
    130.         Load_Texture(Texture, Application.StartupPath() & "\Sprite1.png", System.Drawing.Color.FromArgb(255, 0, 0, 0).ToArgb)
    131.         Running = True
    132.  
    133.     End Sub
    134.  
    135.     Private Sub Shutdown()
    136.  
    137.         Running = False
    138.         Texture = Nothing
    139.         Device = Nothing
    140.         Application.Exit()
    141.  
    142.     End Sub
    143.  
    144.  
    145.     Private Sub frmMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
    146.  
    147.         If e.KeyCode = Keys.Escape Then Shutdown()
    148.  
    149.     End Sub
    150.  
    151.     Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    152.  
    153.         Main()
    154.  
    155.     End Sub
    156.  
    157.     Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    158.  
    159.         Shutdown()
    160.  
    161.     End Sub
    162.  
    163.     Private Sub frmMain_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    164.  
    165.         Game_Loop()
    166.  
    167.     End Sub
    168.  
    169.     Private Sub frmMain_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
    170.  
    171.         If Fullscreen_Enabled = False Then
    172.             Running = False
    173.             'Resets everything
    174.             '---------------------------------------------
    175.             Device.Reset(Screen)
    176.             Device.SetRenderState(RenderStates.AlphaBlendEnable, True)
    177.  
    178.             Device.SetTextureStageState(0, TextureStageStates.ColorOperation, TextureOperation.Modulate)
    179.             Device.SetTextureStageState(0, TextureStageStates.ColorArgument1, TextureArgument.TextureColor)
    180.             Device.SetTextureStageState(0, TextureStageStates.ColorArgument2, TextureArgument.Diffuse)
    181.  
    182.             Device.SetTextureStageState(0, TextureStageStates.AlphaOperation, TextureOperation.Modulate)
    183.             Device.SetTextureStageState(0, TextureStageStates.AlphaArgument1, TextureArgument.TextureColor)
    184.             Device.SetTextureStageState(0, TextureStageStates.AlphaArgument2, TextureArgument.Diffuse)
    185.  
    186.             Device.SetRenderState(RenderStates.SourceBlend, Blend.SourceAlpha)
    187.             Device.SetRenderState(RenderStates.DestinationBlend, Blend.InvSourceAlpha)
    188.             Device.SetRenderState(RenderStates.BlendOperation, TextureOperation.Add)
    189.  
    190.             Device.SetSamplerState(0, SamplerStageStates.MinFilter, TextureFilter.Point)
    191.             Device.SetSamplerState(0, SamplerStageStates.MagFilter, TextureFilter.Point)
    192.             '---------------------------------------------
    193.             Application.DoEvents()
    194.             Running = True
    195.         End If
    196.  
    197.     End Sub
    198. End Class

  23. #23

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

    Re: DirectX referencing

    And you did claim that it did work until you set the background to white, in turn it drew nothing.
    Well actually i just tested the code you just posted on black with another .png image and it was a bit of a mess. You can see what the original looks like and what it looked like when drawn in directx (the orig is on the right).

    Right now a jpg will draw perfectly but this .png will not. Making a vertice with a transparent alpha value will not draw properly in any case.
    Okay. That's the message i just typed out 20 minutes ago. Now i would like to apologize to you for being a royal pain. I just did what i should have done a while ago. I tested your example on another pc. It worked. Drawing the ring properly worked and changing vertex alpha values worked. I'm not quite sure how to fix it on mine but at least i know that it's something with my hardware or more probably software that is the problem. Hopefully the latest drivers for my gc will be enough to fix it.

    Edit: I found what isn't happening on my computer. When i rem this out on the computer which works:

    "Device.SetRenderState(RenderStates.AlphaBlendEnable, True)"

    It looks exactly the same as it does on mine(like in the picture below on the left). I've never had a problem running games with my gc so maybe there's still something we can do in vb to fix it.
    Attached Images Attached Images  
    Last edited by cheesebrother; Feb 5th, 2012 at 09:55 AM.
    Hooked for good.

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

    Re: DirectX referencing

    Tommorow I'll test it in my Virtual PC which has XP Pro to see if its OS related. If it works then its gotta be your video card on your comp.

  25. #25

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

    Re: DirectX referencing

    An interesting thing which i found was that when i change the devicetype of the device it works on my pc

    Code:
    Device = New Device(0, DeviceType.Reference, Me.Handle, CreateFlags.SoftwareVertexProcessing, Screen)
    Alphablending works perfectly just like on the pc which works. The big problem with this is that it absolutely kills the render speed so it was an exciting but shallow discovery.

    I got the latest drivers for my graphics card and that didn't work. I've got an nvidia 9600 gt so it doesn't seem like there would be a lack of capability on the part of that.
    Hooked for good.

  26. #26

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

    Re: DirectX referencing

    I found that this enables alphablending images on my pc

    Code:
    Device.SetRenderState(RenderStates.AlphaBlendEnable, True)
            Device.SetRenderState(RenderStates.SourceBlend, Blend.SourceAlpha)
            Device.SetRenderState(RenderStates.DestinationBlend, Blend.InvSourceAlpha
    Only one thing, it doesn't work for changing alpha values on vertices. This is enough for most things though so i'm quite happy with it now. Thanks a lot for the examples and help.
    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