Results 1 to 14 of 14

Thread: MDX, SlimDX, SharpDX Tutorials, Sample Codes, Documentations and Links - for Newbies

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    12

    MDX, SlimDX, SharpDX Tutorials, Sample Codes, Documentations and Links - for Newbies

    Although years rolled by, there is still pretty much low support for beginners on this area, especially as for VB.NET, so I've decided to start a thread here and would like to ask everyone who has stuff to share in the topic to add it here. My initial contribution to the thread is a very simple and basic SharpDX VB.NET (VS 2010) keyboard reading example, here it is:

    vb Code:
    1. Imports SharpDX.DirectInput
    2. Imports System.Windows.Forms
    3.  
    4. Module Module1
    5.  
    6. Public ApplicationWindow As New Form1
    7. Public UserQuitted As Boolean
    8.  
    9. Sub Main()
    10.  
    11. Dim SharpDXDirectInput As New DirectInput
    12. Dim SharpDXKeyboard As Object
    13. Dim SharpDXKeyboardState As KeyboardState
    14.  
    15. Dim PreviousScrollLockStatePressed As Boolean = False
    16. Dim ScrollLockStatePressed As Boolean
    17.  
    18. ApplicationWindow.Show()
    19.  
    20. SharpDXKeyboard = New Keyboard(SharpDXDirectInput)
    21. With SharpDXKeyboard
    22.  
    23. .SetCooperativeLevel _
    24. (ApplicationWindow, _
    25.  CooperativeLevel.Background Or CooperativeLevel.NonExclusive)
    26.  
    27. .Acquire()
    28. End With
    29.  
    30. Console.WriteLine("Current State of the Scroll Lock key: Released")
    31. Console.Write(StrDup(46, "-") & vbCrLf & "Press 'Q' to quit.")
    32.  
    33. Do
    34. SharpDXKeyboardState = SharpDXKeyboard.GetCurrentState()
    35. ScrollLockStatePressed = SharpDXKeyboardState.IsPressed(Key.ScrollLock)
    36.  
    37. If ScrollLockStatePressed <> PreviousScrollLockStatePressed Then
    38. Console.SetCursorPosition(38, 0)
    39.  
    40. If ScrollLockStatePressed Then
    41. Console.Write("Pressed ")
    42.  
    43. Else
    44. Console.Write("Released")
    45. End If
    46.  
    47. PreviousScrollLockStatePressed = ScrollLockStatePressed
    48. End If
    49.  
    50. Application.DoEvents()
    51. Loop Until SharpDXKeyboardState.IsPressed(Key.Q) Or UserQuitted
    52.  
    53. ApplicationWindow.Close()
    54. SharpDXKeyboard.Unacquire()
    55. End Sub
    56. End Module

    You need to dl and extract SharpDX to a local folder and compile the code above as a Console Application (VS 2010) in order to run it.

    The following .NET references are required to be added to this project :

    SharpDX.dll (SharpDXInstallFolder\Bin\Standard-net40\SharpDX.dll)

    SharpDX.DirectInput.dll (SharpDXInstallFolder\Bin\Standard-net40\SharpDX.DirectInput.dll)

    System.Windows.Forms - can be added from .NET reference list

    Additionally, you need to add a Form named Form1 to the project with the following Form code:

    vb Code:
    1. Public Class Form1
    2.  
    3. Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    4. UserQuitted = True
    5. End Sub
    6. End Class

    This code demonstrates 3 things:

    1.) Receiving keyboard input using SharpDX

    2.) Using Console Applications for test purposes (coz this example could be written as a simple Windows Application, it intentionally isn't)

    3.) Don't be scared of this "VS 2010 is for object oriented coding" thing

    The code itself does only 2 things:

    1.) Detects the state of the ScrollLock key (pressed or released) and displays it on the Console.

    2.) Quits when the user closes the appwindow (not the Console window but the Form) or presses the Q key.

    To create this simple code, I used the following:

    - SharpDX C# sample codes (only Joystick DirectInput Sample is present in the currently distributed pack)

    - "Managed DirectX 9 Kick Start: Graphics and Game Programming" - a book by Tom Miller, also in C#

    - Visual Studio 2010 Objectbrowser

    Another Initial Contribution To The Topic

    There's a small but pretty much interesting 'lil sample set for SlimDX, downloadable from here :

    http://www.ventsim.com/files/samplesvb.zip

    P.s: I know about tutorials and samples available on the official sites of SlimDX and SharpDX.
    Last edited by Visualizer; Mar 7th, 2013 at 07:39 PM.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: MDX, SlimDX, SharpDX Tutorials, Sample Codes, Documentations and Links - for Newb

    My understanding is that these are all DirectX wrappers of one sort or another for managed languages. Do any of them allow you to use DirectX to draw individual Windows controls in a Windows forms app?
    My usual boring signature: Nothing

  3. #3
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: MDX, SlimDX, SharpDX Tutorials, Sample Codes, Documentations and Links - for Newb

    Yea they are wrappers and theres really no point of learning them in my opinion because there already is...DirectX itself. They say that the libraries make it to where you can focus on the code in general, but in reality, theres not much DX code to type, and you focus on the game code itself anyways. Once you initialize DirectX, its all easy street from there.

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    12

    Re: MDX, SlimDX, SharpDX Tutorials, Sample Codes, Documentations and Links - for Newb

    Quote Originally Posted by Jacob Roman
    Yea they are wrappers and theres really no point of learning them in my opinion because there already is...DirectX itself. ...
    No doubt that the items enumerated in the title are just wrappers for the actual DirectX. No doubt you can develope any DirectX code using VC++ and/or low level code or whatever, and codes written that way will prolly run more optimized and faster. The reason to start such thread is I'm hopefully not the only one who has more or less difficulties with understanding and/or writing DirectX code in VC++ (I meant the topic for newbies, casual and/or hobby programmers, check title), not to mention that coding in VB.NET/VC# is pretty much different comparing them to VC++ coding. Another good reason for starting such thread is these wrappers are even different from each other. For example, while MS MDX is an old .NET version library set and only supports DirectX 9.0c, SharpDX has stuff for the latest OS's with the latest DirectX versions including libraries for both .NET 2.0 and 4.0, and their namespaces, class libraries are also different. As you can see shaggy hiker already has a question which can just be one of the many that belong to this thread. I'm sure there is a need for such thread, the number of views on it is already proving it.

    Quote Originally Posted by Jacob Roman
    ... They say that the libraries make it to where you can focus on the code in general, but in reality, theres not much DX code to type, and you focus on the game code itself anyways. Once you initialize DirectX, its all easy street from there. ...
    If a DirectX newbie or casual/hobby programmer takes a look at the 2010 jun DirectX SDK or adds an MDX reference to his VB.NET project and takes a look at declarations in the object browser, he/she gets the shivers ... You could say, "learn DirectX first", then I would say "ok, should I do it in VC ++ ? ..." etc. the final conclusion will be the same, you will need easy samples to learn / casually write DirectX code in VB.NET and even VC#.

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

    Re: MDX, SlimDX, SharpDX Tutorials, Sample Codes, Documentations and Links - for Newb

    I actually have tons of samples I've written in all languages if you need me to post em.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: MDX, SlimDX, SharpDX Tutorials, Sample Codes, Documentations and Links - for Newb

    Ok. So, can any of those be used to draw individual controls in a Windows Forms App? I'm not interested in full game loops, because I long since concluded that I suck at anything artistic, so I will confine my designs to square and mostly gray, where my lack of artististry won't make the user hurl chunder on their keyboard. However, I'm currently using XNA to draw some controls that can't be drawn fast enough using GDI. If XNA goes extinct, those controls can probably still work for quite some time (look at how long VB6 has been hanging on), but what is the future for faster controls in WinForms?
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    12

    Re: MDX, SlimDX, SharpDX Tutorials, Sample Codes, Documentations and Links - for Newb

    Quote Originally Posted by Jacob Roman
    I actually have tons of samples I've written in all languages if you need me to post em.
    I definitely expected samples from you, Jacob I would be interested in even a collection of them that would be upped in just a zip/rar somewhere with a link given here (as I did it in my initial post). Anything that belongs to the topic would be appreciated. I am mostly interested in examples written in VS 2010 VB.NET but VC# samples might also come.

    Quote Originally Posted by Shaggy Hiker
    ... what is the future for faster controls in WinForms?
    Well it can be read here and there that Mircosoft kinda "tends to replace" conventional GDI with DirectX, a part of this process is including latest DirectX SDK into Windows 8 SDK ... As for current chances of drawing UI Windows and Controls using DirectX, as a DirectX newbie I can't really add anything to this except that you can obviously do anything using MDX wrappers that you would be able to do using DirectX itself. If someone would have actual coding practice on this area, any VB.NET/VC# samples would be appreciated.

  8. #8
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: MDX, SlimDX, SharpDX Tutorials, Sample Codes, Documentations and Links - for Newb

    In the past I was trying to make a Massive DirectX Tutorial targeting all languages, but its incomplete and the fact I no longer have time to complete it due to work, spending time with my gf, working on my Nintoaster project (Nintendo inside a toaster), working on my Nintendo Emulator, enjoing life, etc etc, put the whole tutorial on hold. But heres just a taste of what I have to offer. This is an example of a first person camera in 3D. I wrote it in all languages with similar structure.

    Visual Basic 6.0
    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.     Color As Long
    9.    
    10. End Type
    11.  
    12. Private Const CUSTOM_VERTEX_FORMAT As Long = D3DFVF_XYZ Or D3DFVF_DIFFUSE
    13.  
    14. Private Const PI As Single = 3.141592654
    15. Private Const FOV As Single = PI / 4
    16. Private Const ASPECT_RATIO As Single = 3 / 4
    17. Private Const NEAR_Z As Single = 1
    18. Private Const FAR_Z As Single = 10000
    19.  
    20. Private DX As DirectX8
    21. Private D3D As Direct3D8
    22. Private Device As Direct3DDevice8
    23. Private Display_Mode As D3DDISPLAYMODE
    24. Private Screen As D3DPRESENT_PARAMETERS
    25.  
    26. Private DI As DirectInput8
    27.  
    28. Private Mouse_Device As DirectInputDevice8
    29. Private Mouse_State As DIMOUSESTATE
    30. Private Mouse As D3DVECTOR2
    31.  
    32. Private Keyboard_Device As DirectInputDevice8
    33. Private Keyboard_State As DIKEYBOARDSTATE
    34.  
    35. Private Vertex_List(3) As CUSTOM_VERTEX
    36.  
    37. Private View_Matrix As D3DMATRIX
    38. Private Projection_Matrix As D3DMATRIX
    39.  
    40. Private Camera_Position As D3DVECTOR
    41. Private Camera_Angle As D3DVECTOR
    42.  
    43. Private Fullscreen_Enabled As Boolean
    44. Private Running As Boolean
    45.  
    46. Private Sub DirectX_Initialize()
    47.  
    48.     Set DX = New DirectX8
    49.     Set D3D = DX.Direct3DCreate()
    50.    
    51.     If Fullscreen_Enabled = True Then
    52.         Display_Mode.Width = 800
    53.         Display_Mode.Height = 600
    54.         Display_Mode.Format = D3DFMT_R5G6B5
    55.         Screen.Windowed = False
    56.         Screen.BackBufferCount = 1
    57.         Screen.BackBufferWidth = Display_Mode.Width
    58.         Screen.BackBufferHeight = Display_Mode.Height
    59.         Screen.hDeviceWindow = frmMain.hWnd
    60.     Else
    61.         D3D.GetAdapterDisplayMode D3DADAPTER_DEFAULT, Display_Mode
    62.         Screen.Windowed = True
    63.     End If
    64.    
    65.     Screen.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC
    66.     Screen.BackBufferFormat = Display_Mode.Format
    67.     Screen.AutoDepthStencilFormat = D3DFMT_D16
    68.     Screen.EnableAutoDepthStencil = 1
    69.  
    70.     Set Device = D3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, frmMain.hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, Screen)
    71.    
    72. End Sub
    73.  
    74. Private Sub DirectInput_Initialize()
    75.  
    76.     Set DI = DX.DirectInputCreate
    77.  
    78. End Sub
    79.  
    80. Private Sub DirectInput_Initialize_Mouse()
    81.  
    82.     Set Mouse_Device = DI.CreateDevice("GUID_SYSMOUSE")
    83.     Mouse_Device.SetCommonDataFormat DIFORMAT_MOUSE
    84.     Mouse_Device.SetCooperativeLevel frmMain.hWnd, DISCL_BACKGROUND Or DISCL_NONEXCLUSIVE
    85.     Mouse_Device.Acquire
    86.    
    87. End Sub
    88.  
    89. Private Sub Mouse_Controls()
    90.  
    91.     Mouse_Device.GetDeviceStateMouse Mouse_State
    92.    
    93.     Mouse.X = Mouse.X + Mouse_State.lX
    94.     Mouse.Y = Mouse.Y + Mouse_State.lY
    95.    
    96.     If Mouse.X > frmMain.ScaleWidth Then Mouse.X = frmMain.ScaleWidth
    97.     If Mouse.X < 0 Then Mouse.X = 0
    98.    
    99.     If Mouse.Y > frmMain.ScaleHeight Then Mouse.Y = frmMain.ScaleHeight
    100.     If Mouse.Y < 0 Then Mouse.Y = 0
    101.    
    102.     Camera_Angle.X = Camera_Angle.X + Mouse_State.lY
    103.     Camera_Angle.Y = Camera_Angle.Y + Mouse_State.lX
    104.  
    105. End Sub
    106.  
    107. Private Sub DirectInput_Initialize_Keyboard()
    108.    
    109.     Set Keyboard_Device = DI.CreateDevice("GUID_SysKeyboard")
    110.     Keyboard_Device.SetCommonDataFormat DIFORMAT_KEYBOARD
    111.     Keyboard_Device.SetCooperativeLevel frmMain.hWnd, DISCL_BACKGROUND Or DISCL_NONEXCLUSIVE
    112.     Keyboard_Device.Acquire
    113.  
    114. End Sub
    115.  
    116. Private Function DirectInput_Key_State(Key_Code As Long) As Boolean
    117.  
    118.     Keyboard_Device.GetDeviceStateKeyboard Keyboard_State
    119.     DirectInput_Key_State = Keyboard_State.Key(Key_Code) And &H80
    120.  
    121. End Function
    122.  
    123. Private Sub Keyboard_Controls()
    124.    
    125.     Const Camera_Speed As Long = 5
    126.    
    127.     If DirectInput_Key_State(DIK_W) Then 'Move Forward
    128.         Camera_Position.X = Camera_Position.X - Sin(Camera_Angle.Y * PI / 180) * Camera_Speed
    129.         Camera_Position.Z = Camera_Position.Z - Cos(Camera_Angle.Y * PI / 180) * Camera_Speed
    130.     End If
    131.    
    132.     If DirectInput_Key_State(DIK_S) Then 'Move Backward
    133.         Camera_Position.X = Camera_Position.X + Sin(Camera_Angle.Y * PI / 180) * Camera_Speed
    134.         Camera_Position.Z = Camera_Position.Z + Cos(Camera_Angle.Y * PI / 180) * Camera_Speed
    135.     End If
    136.    
    137.     If DirectInput_Key_State(DIK_A) Then 'Move Left
    138.         Camera_Position.X = Camera_Position.X + Cos(Camera_Angle.Y * PI / 180) * Camera_Speed
    139.         Camera_Position.Z = Camera_Position.Z - Sin(Camera_Angle.Y * PI / 180) * Camera_Speed
    140.     End If
    141.    
    142.     If DirectInput_Key_State(DIK_D) Then 'Move Right
    143.         Camera_Position.X = Camera_Position.X - Cos(Camera_Angle.Y * PI / 180) * Camera_Speed
    144.         Camera_Position.Z = Camera_Position.Z + Sin(Camera_Angle.Y * PI / 180) * Camera_Speed
    145.     End If
    146.    
    147.     If DirectInput_Key_State(DIK_Q) Then 'Look Left
    148.         Camera_Angle.Y = Camera_Angle.Y - 1
    149.     End If
    150.    
    151.     If DirectInput_Key_State(DIK_E) Then 'Look Right
    152.         Camera_Angle.Y = Camera_Angle.Y + 1
    153.     End If
    154.    
    155.     If DirectInput_Key_State(DIK_R) Then 'Look Up
    156.         Camera_Angle.X = Camera_Angle.X - 1
    157.     End If
    158.    
    159.     If DirectInput_Key_State(DIK_F) Then 'Look Down
    160.         Camera_Angle.X = Camera_Angle.X + 1
    161.     End If
    162.    
    163.     If DirectInput_Key_State(DIK_C) Then 'Move Up
    164.         Camera_Position.Y = Camera_Position.Y - Camera_Speed
    165.     End If
    166.    
    167.     If DirectInput_Key_State(DIK_Z) Then 'Move Down
    168.         Camera_Position.Y = Camera_Position.Y + Camera_Speed
    169.     End If
    170.  
    171. End Sub
    172.  
    173. Private Sub Settings()
    174.  
    175.     Device.SetRenderState D3DRS_ZENABLE, D3DZB_TRUE
    176.     Device.SetRenderState D3DRS_ZWRITEENABLE, 1
    177.     Device.SetRenderState D3DRS_CULLMODE, D3DCULL_NONE
    178.     Device.SetRenderState D3DRS_LIGHTING, 0
    179.  
    180. End Sub
    181.  
    182. Private Function Create_Vertex(X As Single, Y As Single, Z As Single) As D3DVECTOR
    183.  
    184.     Create_Vertex.X = X
    185.     Create_Vertex.Y = Y
    186.     Create_Vertex.Z = Z
    187.    
    188. End Function
    189.  
    190. Private Function Create_Custom_Vertex(ByVal X As Single, ByVal Y As Single, ByVal Z As Single, ByVal Color As Long) As CUSTOM_VERTEX
    191.  
    192.     Create_Custom_Vertex.X = X
    193.     Create_Custom_Vertex.Y = Y
    194.     Create_Custom_Vertex.Z = Z
    195.     Create_Custom_Vertex.Color = Color
    196.    
    197. End Function
    198.  
    199. Private Sub Setup_Matrices()
    200.    
    201.     D3DXMatrixLookAtRH View_Matrix, Create_Vertex(Camera_Position.X, Camera_Position.Y, Camera_Position.Z), Create_Vertex(0, 0, 0), Create_Vertex(0, 1, 0)
    202.     Device.SetTransform D3DTS_VIEW, View_Matrix
    203.    
    204.     D3DXMatrixPerspectiveFovRH Projection_Matrix, FOV, ASPECT_RATIO, NEAR_Z, FAR_Z
    205.     Device.SetTransform D3DTS_PROJECTION, Projection_Matrix
    206.  
    207. End Sub
    208.  
    209. Public Sub Camera_Control()
    210.  
    211.     Dim Camera_Translation_Matrix As D3DMATRIX
    212.     Dim Camera_Angle_Matrix_X As D3DMATRIX
    213.     Dim Camera_Angle_Matrix_Y As D3DMATRIX
    214.    
    215.     D3DXMatrixIdentity View_Matrix
    216.     D3DXMatrixIdentity Camera_Translation_Matrix
    217.    
    218.     D3DXMatrixTranslation Camera_Translation_Matrix, Camera_Position.X, Camera_Position.Y, -Camera_Position.Z
    219.     D3DXMatrixMultiply View_Matrix, View_Matrix, Camera_Translation_Matrix
    220.  
    221.     D3DXMatrixRotationY Camera_Angle_Matrix_Y, (PI * Camera_Angle.Y) / 180
    222.     D3DXMatrixMultiply View_Matrix, View_Matrix, Camera_Angle_Matrix_Y
    223.  
    224.     D3DXMatrixRotationX Camera_Angle_Matrix_X, (PI * Camera_Angle.X) / 180
    225.     D3DXMatrixMultiply View_Matrix, View_Matrix, Camera_Angle_Matrix_X
    226.    
    227.     Device.SetTransform D3DTS_VIEW, View_Matrix
    228.    
    229. End Sub
    230.  
    231. Private Sub Create_Polygon()
    232.  
    233.     Vertex_List(0) = Create_Custom_Vertex(-50, 100, 0, D3DColorXRGB(255, 255, 255))
    234.     Vertex_List(1) = Create_Custom_Vertex(50, 100, 0, D3DColorXRGB(255, 255, 255))
    235.     Vertex_List(2) = Create_Custom_Vertex(-50, 0, 0, D3DColorXRGB(255, 255, 255))
    236.     Vertex_List(3) = Create_Custom_Vertex(50, 0, 0, D3DColorXRGB(255, 255, 255))
    237.  
    238. End Sub
    239.  
    240. Private Sub Draw_Polygon()
    241.  
    242.     Device.SetVertexShader CUSTOM_VERTEX_FORMAT
    243.     Device.DrawPrimitiveUP D3DPT_TRIANGLESTRIP, 2, Vertex_List(0), Len(Vertex_List(0))
    244.  
    245. End Sub
    246.  
    247. Private Sub Create_Platform()
    248.  
    249.     Vertex_List(0) = Create_Custom_Vertex(-1000, 0, -1000, D3DColorXRGB(255, 0, 0))
    250.     Vertex_List(1) = Create_Custom_Vertex(1000, 0, -1000, D3DColorXRGB(0, 255, 0))
    251.     Vertex_List(2) = Create_Custom_Vertex(-1000, 0, 1000, D3DColorXRGB(0, 0, 255))
    252.     Vertex_List(3) = Create_Custom_Vertex(1000, 0, 1000, D3DColorXRGB(255, 0, 255))
    253.  
    254. End Sub
    255.  
    256. Private Sub Render()
    257.  
    258.     Device.Clear 0, ByVal 0, D3DCLEAR_TARGET Or D3DCLEAR_ZBUFFER, D3DColorXRGB(0, 0, 0), 1, 0
    259.     Device.BeginScene
    260.     'Rendering code goes here
    261.     Create_Platform
    262.     Draw_Polygon
    263.     Create_Polygon
    264.     Draw_Polygon
    265.     Device.EndScene
    266.     Device.Present ByVal 0, ByVal 0, 0, ByVal 0
    267.  
    268. End Sub
    269.  
    270. Private Sub Game_Loop()
    271.  
    272.     Do While Running = True
    273.         Mouse_Controls
    274.         Keyboard_Controls
    275.         Camera_Control
    276.         Render
    277.         If DirectInput_Key_State(DIK_ESCAPE) <> 0 Then Shutdown
    278.         DoEvents
    279.     Loop
    280.  
    281. End Sub
    282.  
    283. Private Sub Main()
    284.  
    285.     If MsgBox("Click Yes to go to fullscreen (Recommended)", vbQuestion Or vbYesNo, "Options") = vbYes Then
    286.         Fullscreen_Enabled = True
    287.     End If
    288.    
    289.     With Me
    290.         .Show
    291.         .ScaleMode = vbPixels
    292.         .ScaleWidth = 312
    293.         .ScaleHeight = 213
    294.         .Caption = "DirectX Tutorial"
    295.         If Fullscreen_Enabled = True Then .BorderStyle = vbBSNone
    296.     End With
    297.    
    298.     Camera_Position.X = 0
    299.     Camera_Position.Y = -50
    300.     Camera_Position.Z = 250
    301.    
    302.     DirectX_Initialize
    303.     DirectInput_Initialize
    304.     DirectInput_Initialize_Mouse
    305.     DirectInput_Initialize_Keyboard
    306.     Settings
    307.     Setup_Matrices
    308.     Running = True
    309.     Game_Loop
    310.    
    311. End Sub
    312.  
    313. Private Sub Shutdown()
    314.  
    315.     If Running = True Then
    316.         Running = False
    317.         Mouse_Device.Unacquire
    318.         Set Mouse_Device = Nothing
    319.         Keyboard_Device.Unacquire
    320.         Set Keyboard_Device = Nothing
    321.         Set DI = Nothing
    322.         Set Device = Nothing
    323.         Set D3D = Nothing
    324.         Set DX = Nothing
    325.         Unload Me
    326.     End If
    327.  
    328. End Sub
    329.  
    330. Private Sub Form_Load()
    331.  
    332.     Main
    333.  
    334. End Sub
    335.  
    336. Private Sub Form_Unload(Cancel As Integer)
    337.  
    338.     Shutdown
    339.  
    340. End Sub

    ...Continued Next Post

  9. #9
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: MDX, SlimDX, SharpDX Tutorials, Sample Codes, Documentations and Links - for Newb

    ...Continued from last post

    Visual Basic.NET
    vb.net Code:
    1. Option Explicit On
    2. Option Strict On
    3.  
    4. Imports Microsoft.DirectX
    5. Imports Microsoft.DirectX.Direct3D
    6. Imports Microsoft.DirectX.DirectInput
    7.  
    8. Public Class frmMain
    9.  
    10.     Private Const FOV As Single = Math.PI / 4
    11.     Private Const ASPECT_RATIO As Single = 4 / 3
    12.     Private Const NEAR_Z As Single = 1
    13.     Private Const FAR_Z As Single = 10000
    14.  
    15.     Private Device As Direct3D.Device
    16.     Private Display_Mode As DisplayMode
    17.     Private Screen As New PresentParameters
    18.  
    19.     Private Mouse_Device As DirectInput.Device
    20.     Private Mouse_State As MouseState
    21.     Private Mouse As Vector2
    22.  
    23.     Private Keyboard_Device As DirectInput.Device
    24.     Private Keyboard_State As KeyboardState
    25.  
    26.     Private Vertex_List(3) As CustomVertex.PositionColored
    27.  
    28.     Private View_Matrix As Matrix
    29.     Private Projection_Matrix As Matrix
    30.  
    31.     Private Camera_Position As Vector3
    32.     Private Camera_Angle As Vector3
    33.  
    34.     Private Fullscreen_Enabled As Boolean
    35.     Private Running As Boolean
    36.  
    37.     Private Sub DirectX_Initialize()
    38.  
    39.         If Fullscreen_Enabled = True Then
    40.             Display_Mode.Width = 800
    41.             Display_Mode.Height = 600
    42.             Display_Mode.Format = Direct3D.Format.R5G6B5
    43.             Screen.Windowed = False
    44.             Screen.BackBufferCount = 1
    45.             Screen.BackBufferWidth = Display_Mode.Width
    46.             Screen.BackBufferHeight = Display_Mode.Height
    47.         Else
    48.             Screen.Windowed = True
    49.         End If
    50.  
    51.         Screen.SwapEffect = SwapEffect.Copy
    52.         Screen.BackBufferFormat = Display_Mode.Format
    53.         Screen.AutoDepthStencilFormat = DepthFormat.D16
    54.         Screen.EnableAutoDepthStencil = True
    55.  
    56.         Device = New Direct3D.Device(0, Direct3D.DeviceType.Hardware, Me.Handle, CreateFlags.SoftwareVertexProcessing, Screen)
    57.  
    58.     End Sub
    59.  
    60.     Private Sub DirectInput_Initialize_Mouse()
    61.  
    62.         Mouse_Device = New DirectInput.Device(SystemGuid.Mouse)
    63.         Mouse_Device.SetDataFormat(DeviceDataFormat.Mouse)
    64.         Mouse_Device.SetCooperativeLevel(Me, CooperativeLevelFlags.Background Or CooperativeLevelFlags.NonExclusive)
    65.         Mouse_Device.Acquire()
    66.  
    67.     End Sub
    68.  
    69.     Private Sub Mouse_Controls()
    70.  
    71.         Mouse_State = Mouse_Device.CurrentMouseState
    72.  
    73.         Mouse.X = Mouse.X + Mouse_State.X
    74.         Mouse.Y = Mouse.Y + Mouse_State.Y
    75.  
    76.         If Mouse.X > Me.Width Then Mouse.X = Me.Width
    77.         If Mouse.X < 0 Then Mouse.X = 0
    78.  
    79.         If Mouse.Y > Me.Height Then Mouse.Y = Me.Height
    80.         If Mouse.Y < 0 Then Mouse.Y = 0
    81.  
    82.         Camera_Angle.X += Mouse_State.Y
    83.         Camera_Angle.Y += Mouse_State.X
    84.  
    85.         If Camera_Angle.X >= 90 Then Camera_Angle.X = 90
    86.         If Camera_Angle.X <= -90 Then Camera_Angle.X = -90
    87.  
    88.     End Sub
    89.  
    90.     Private Sub DirectInput_Initialize_Keyboard()
    91.  
    92.         Keyboard_Device = New DirectInput.Device(SystemGuid.Keyboard)
    93.         Keyboard_Device.SetDataFormat(DeviceDataFormat.Keyboard)
    94.         Keyboard_Device.SetCooperativeLevel(Me, CooperativeLevelFlags.Background Or CooperativeLevelFlags.NonExclusive)
    95.         Keyboard_Device.Acquire()
    96.  
    97.     End Sub
    98.  
    99.     Private Function DirectInput_Key_State(ByVal Key_Code As DirectInput.Key) As Boolean
    100.  
    101.         Keyboard_State = Keyboard_Device.GetCurrentKeyboardState
    102.         Return Keyboard_State.Item(Key_Code)
    103.  
    104.     End Function
    105.  
    106.     Private Sub Keyboard_Controls()
    107.  
    108.         Const Camera_Speed As Long = 5
    109.  
    110.         If DirectInput_Key_State(Key.W) Then 'Move Forward
    111.             Camera_Position.X -= Convert.ToSingle(Math.Sin(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed
    112.             Camera_Position.Z -= Convert.ToSingle(Math.Cos(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed
    113.         End If
    114.  
    115.         If DirectInput_Key_State(Key.S) Then 'Move Backward
    116.             Camera_Position.X += Convert.ToSingle(Math.Sin(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed
    117.             Camera_Position.Z += Convert.ToSingle(Math.Cos(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed
    118.         End If
    119.  
    120.         If DirectInput_Key_State(Key.A) Then 'Move Left
    121.             Camera_Position.X += Convert.ToSingle(Math.Cos(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed
    122.             Camera_Position.Z -= Convert.ToSingle(Math.Sin(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed
    123.         End If
    124.  
    125.         If DirectInput_Key_State(Key.D) Then 'Move Right
    126.             Camera_Position.X -= Convert.ToSingle(Math.Cos(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed
    127.             Camera_Position.Z += Convert.ToSingle(Math.Sin(Camera_Angle.Y * Math.PI / 180)) * Camera_Speed
    128.         End If
    129.  
    130.         If DirectInput_Key_State(Key.Q) Then 'Look Left
    131.             Camera_Angle.Y -= 1
    132.         End If
    133.  
    134.         If DirectInput_Key_State(Key.E) Then 'Look Right
    135.             Camera_Angle.Y += 1
    136.         End If
    137.  
    138.         If DirectInput_Key_State(Key.R) Then 'Look Up
    139.             Camera_Angle.X -= 1
    140.         End If
    141.  
    142.         If DirectInput_Key_State(Key.F) Then 'Look Down
    143.             Camera_Angle.X += 1
    144.         End If
    145.  
    146.         If DirectInput_Key_State(Key.C) Then 'Move Up
    147.             Camera_Position.Y -= Camera_Speed
    148.         End If
    149.  
    150.         If DirectInput_Key_State(Key.Z) Then 'Move Down
    151.             Camera_Position.Y += Camera_Speed
    152.         End If
    153.  
    154.     End Sub
    155.  
    156.     Private Sub Settings()
    157.  
    158.         Device.SetRenderState(RenderStates.ZEnable, True)
    159.         Device.SetRenderState(RenderStates.ZBufferWriteEnable, True)
    160.         Device.SetRenderState(RenderStates.CullMode, Cull.None)
    161.         Device.SetRenderState(RenderStates.Lighting, False)
    162.  
    163.     End Sub
    164.  
    165.     Private Function Create_Vertex(ByVal X As Single, ByVal Y As Single, ByVal Z As Single) As Vector3
    166.  
    167.         Dim Vertex As Vector3
    168.  
    169.         Vertex.X = X
    170.         Vertex.Y = Y
    171.         Vertex.Z = Z
    172.  
    173.         Return Vertex
    174.  
    175.     End Function
    176.  
    177.     Private Function Create_Custom_Vertex(ByVal X As Single, ByVal Y As Single, ByVal Z As Single, ByVal Color As Integer) As CustomVertex.PositionColored
    178.  
    179.         Dim Vertex As CustomVertex.PositionColored = New CustomVertex.PositionColored
    180.  
    181.         Vertex.Position = New Vector3(X, Y, Z)
    182.         Vertex.Color = Color
    183.  
    184.         Return Vertex
    185.  
    186.     End Function
    187.  
    188.     Private Sub Setup_Matrices()
    189.  
    190.         View_Matrix = Matrix.LookAtRH(Create_Vertex(Camera_Position.X, Camera_Position.Y, Camera_Position.Z), Create_Vertex(0, 0, 0), Create_Vertex(0, 1, 0))
    191.         Device.Transform.View = View_Matrix
    192.  
    193.         Projection_Matrix = Matrix.PerspectiveFovRH(FOV, ASPECT_RATIO, NEAR_Z, FAR_Z)
    194.         Device.Transform.Projection = Projection_Matrix
    195.  
    196.     End Sub
    197.  
    198.     Private Sub Camera_Control()
    199.  
    200.         Dim Camera_Translation_Matrix As Matrix
    201.         Dim Camera_Angle_Matrix_X As Matrix
    202.         Dim Camera_Angle_Matrix_Y As Matrix
    203.  
    204.         View_Matrix = Matrix.Identity
    205.         Camera_Translation_Matrix = Matrix.Identity
    206.  
    207.         Camera_Translation_Matrix = Matrix.Translation(Camera_Position.X, Camera_Position.Y, -Camera_Position.Z)
    208.         View_Matrix = Matrix.Multiply(View_Matrix, Camera_Translation_Matrix)
    209.  
    210.         Camera_Angle_Matrix_Y = Matrix.RotationY((Convert.ToSingle(Math.PI) * Camera_Angle.Y) / 180)
    211.         View_Matrix = Matrix.Multiply(View_Matrix, Camera_Angle_Matrix_Y)
    212.  
    213.         Camera_Angle_Matrix_X = Matrix.RotationX((Convert.ToSingle(Math.PI) * Camera_Angle.X) / 180)
    214.         View_Matrix = Matrix.Multiply(View_Matrix, Camera_Angle_Matrix_X)
    215.  
    216.         Device.Transform.View = View_Matrix
    217.  
    218.     End Sub
    219.  
    220.     Private Sub Create_Polygon()
    221.  
    222.         Vertex_List(0) = Create_Custom_Vertex(-50, 100, 0, Color.FromArgb(255, 255, 255).ToArgb)
    223.         Vertex_List(1) = Create_Custom_Vertex(50, 100, 0, Color.FromArgb(255, 255, 255).ToArgb)
    224.         Vertex_List(2) = Create_Custom_Vertex(-50, 0, 0, Color.FromArgb(255, 255, 255).ToArgb)
    225.         Vertex_List(3) = Create_Custom_Vertex(50, 0, 0, Color.FromArgb(255, 255, 255).ToArgb)
    226.  
    227.     End Sub
    228.  
    229.     Private Sub Draw_Polygon()
    230.  
    231.         Device.VertexFormat = CustomVertex.PositionColored.Format
    232.         Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, Vertex_List)
    233.  
    234.     End Sub
    235.  
    236.     Private Sub Create_Platform()
    237.  
    238.         Vertex_List(0) = Create_Custom_Vertex(-1000, 0, -1000, Color.FromArgb(255, 0, 0).ToArgb)
    239.         Vertex_List(1) = Create_Custom_Vertex(1000, 0, -1000, Color.FromArgb(0, 255, 0).ToArgb)
    240.         Vertex_List(2) = Create_Custom_Vertex(-1000, 0, 1000, Color.FromArgb(0, 0, 255).ToArgb)
    241.         Vertex_List(3) = Create_Custom_Vertex(1000, 0, 1000, Color.FromArgb(255, 0, 255).ToArgb)
    242.  
    243.     End Sub
    244.  
    245.     Private Sub Reset_Device()
    246.  
    247.         If Me.WindowState <> FormWindowState.Minimized And Fullscreen_Enabled = False Then
    248.             Running = False
    249.             Device.Reset(Screen)
    250.             Settings()
    251.             Setup_Matrices()
    252.             Application.DoEvents()
    253.             Running = True
    254.         End If
    255.  
    256.     End Sub
    257.  
    258.     Private Sub Render()
    259.  
    260.         Device.Clear(ClearFlags.Target Or ClearFlags.ZBuffer, Color.FromArgb(0, 0, 0), 1, 0)
    261.         Device.BeginScene()
    262.         Create_Polygon()
    263.         Draw_Polygon()
    264.         Create_Platform()
    265.         Draw_Polygon()
    266.         Device.EndScene()
    267.         Device.Present()
    268.  
    269.     End Sub
    270.  
    271.     Private Sub Game_Loop()
    272.  
    273.         Do While Running = True
    274.             Mouse_Controls()
    275.             Keyboard_Controls()
    276.             Camera_Control()
    277.             Render()
    278.             If DirectInput_Key_State(Key.Escape) Then Shutdown()
    279.             Application.DoEvents()
    280.         Loop
    281.  
    282.     End Sub
    283.  
    284.     Private Sub Main()
    285.  
    286.         If MessageBox.Show("Click Yes to go to fullscreen (Recommended)", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
    287.             Fullscreen_Enabled = True
    288.         End If
    289.  
    290.         With Me
    291.             .Show()
    292.             .Width = 330
    293.             .Height = 250
    294.             .SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.Opaque, True)
    295.             .Text = "DirectX Tutorial"
    296.             If Fullscreen_Enabled = True Then .FormBorderStyle = Windows.Forms.FormBorderStyle.None
    297.         End With
    298.  
    299.         Camera_Position.X = 0
    300.         Camera_Position.Y = -50
    301.         Camera_Position.Z = 250
    302.  
    303.         DirectX_Initialize()
    304.         DirectInput_Initialize_Mouse()
    305.         DirectInput_Initialize_Keyboard()
    306.         Settings()
    307.         Setup_Matrices()
    308.         Running = True
    309.  
    310.     End Sub
    311.  
    312.     Private Sub Shutdown()
    313.  
    314.         If Running = True Then
    315.             Running = False
    316.             Mouse_Device.Unacquire()
    317.             Mouse_Device.Dispose()
    318.             Keyboard_Device.Unacquire()
    319.             Keyboard_Device.Dispose()
    320.             Device.Dispose()
    321.             Application.Exit()
    322.         End If
    323.  
    324.     End Sub
    325.  
    326.     Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    327.  
    328.         Main()
    329.  
    330.     End Sub
    331.  
    332.     Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    333.  
    334.         Shutdown()
    335.  
    336.     End Sub
    337.  
    338.     Private Sub frmMain_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    339.  
    340.         Game_Loop()
    341.  
    342.     End Sub
    343.  
    344.     Private Sub frmMain_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
    345.  
    346.         Reset_Device()
    347.  
    348.     End Sub
    349.  
    350. End Class

  10. #10
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: MDX, SlimDX, SharpDX Tutorials, Sample Codes, Documentations and Links - for Newb

    ...Continued from last post

    C#
    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. using Microsoft.DirectX.DirectInput;
    12.  
    13. namespace DX_Tut
    14. {
    15.     public partial class frmMain : Form
    16.     {
    17.         private const float FOV = (float)Math.PI / 4.0f;
    18.         private const float ASPECT_RATIO = 4.0f / 3.0f;
    19.         private const float NEAR_Z = 1.0f;
    20.         private const float FAR_Z = 10000.0f;
    21.  
    22.         Microsoft.DirectX.Direct3D.Device Device;
    23.         DisplayMode Display_Mode;
    24.         PresentParameters Screen;
    25.  
    26.         Microsoft.DirectX.DirectInput.Device Mouse_Device;
    27.         MouseState Mouse_State;
    28.         Vector2 Mouse;
    29.  
    30.         Microsoft.DirectX.DirectInput.Device Keyboard_Device;
    31.         KeyboardState Keyboard_State;
    32.  
    33.         CustomVertex.PositionColored[] Vertex_List = new CustomVertex.PositionColored[4];
    34.  
    35.         Matrix View_Matrix;
    36.         Matrix Projection_Matrix;
    37.  
    38.         Vector3 Camera_Position;
    39.         Vector3 Camera_Angle;
    40.  
    41.         bool Fullscreen_Enabled;
    42.         bool Running;
    43.  
    44.         private void DirectX_Initialize()
    45.         {
    46.             Screen = new PresentParameters();
    47.             if (Fullscreen_Enabled == true)
    48.             {
    49.                 Display_Mode.Width = 800;
    50.                 Display_Mode.Height = 600;
    51.                 Display_Mode.Format = Format.R5G6B5;
    52.                 Screen.Windowed = false;
    53.                 Screen.BackBufferCount = 1;
    54.                 Screen.BackBufferWidth = Display_Mode.Width;
    55.                 Screen.BackBufferHeight = Display_Mode.Height;
    56.             }
    57.             else
    58.             {
    59.                 Screen.Windowed = true;
    60.             }
    61.             Screen.SwapEffect = SwapEffect.Copy;
    62.             Screen.BackBufferFormat = Display_Mode.Format;
    63.             Screen.AutoDepthStencilFormat = DepthFormat.D16;
    64.             Screen.EnableAutoDepthStencil = true;
    65.  
    66.             Device = new Microsoft.DirectX.Direct3D.Device(0, Microsoft.DirectX.Direct3D.DeviceType.Hardware, this.Handle, CreateFlags.SoftwareVertexProcessing, Screen);
    67.         }
    68.  
    69.         private void DirectInput_Initialize_Mouse()
    70.         {
    71.             Mouse_Device = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Mouse);
    72.             Mouse_Device.SetDataFormat(DeviceDataFormat.Mouse);
    73.             Mouse_Device.SetCooperativeLevel(this, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);
    74.             Mouse_Device.Acquire();
    75.         }
    76.  
    77.         private void Mouse_Controls()
    78.         {
    79.             Mouse_State = Mouse_Device.CurrentMouseState;
    80.  
    81.             Mouse.X += Mouse_State.X;
    82.             Mouse.Y += Mouse_State.Y;
    83.  
    84.             if (Mouse.X > this.Width) Mouse.X = this.Width;
    85.             if (Mouse.X < 0) Mouse.X = 0;
    86.  
    87.             if (Mouse.Y > this.Height) Mouse.Y = this.Height;
    88.             if (Mouse.Y < 0) Mouse.Y = 0;
    89.  
    90.             Camera_Angle.X += Mouse_State.Y;
    91.             Camera_Angle.Y += Mouse_State.X;
    92.  
    93.             if (Camera_Angle.X >= 90) Camera_Angle.X = 90;
    94.             if (Camera_Angle.X <= -90) Camera_Angle.X = -90;
    95.         }
    96.  
    97.         private void DirectInput_Initialize_Keyboard()
    98.         {
    99.             Keyboard_Device = new Microsoft.DirectX.DirectInput.Device(SystemGuid.Keyboard);
    100.             Keyboard_Device.SetDataFormat(DeviceDataFormat.Keyboard);
    101.             Keyboard_Device.SetCooperativeLevel(this, CooperativeLevelFlags.Background | CooperativeLevelFlags.NonExclusive);
    102.             Keyboard_Device.Acquire();
    103.         }
    104.  
    105.         private bool DirectInput_Key_State(Microsoft.DirectX.DirectInput.Key Key_Code)
    106.         {
    107.             Keyboard_State = Keyboard_Device.GetCurrentKeyboardState();
    108.             return Keyboard_State[Key_Code];
    109.         }
    110.  
    111.         private void Keyboard_Controls()
    112.         {
    113.             const int Camera_Speed = 5;
    114.  
    115.             if (DirectInput_Key_State(Key.W)) //Move Forward
    116.             {
    117.                 Camera_Position.X -= (float)Math.Sin(Camera_Angle.Y * Math.PI / 180) * Camera_Speed;
    118.                 Camera_Position.Z -= (float)Math.Cos(Camera_Angle.Y * Math.PI / 180) * Camera_Speed;
    119.             }
    120.  
    121.             if (DirectInput_Key_State(Key.S)) //Move Backward
    122.             {
    123.                 Camera_Position.X += (float)Math.Sin(Camera_Angle.Y * Math.PI / 180) * Camera_Speed;
    124.                 Camera_Position.Z += (float)Math.Cos(Camera_Angle.Y * Math.PI / 180) * Camera_Speed;
    125.             }
    126.  
    127.             if (DirectInput_Key_State(Key.A)) //Move Left
    128.             {
    129.                 Camera_Position.X += (float)Math.Cos(Camera_Angle.Y * Math.PI / 180) * Camera_Speed;
    130.                 Camera_Position.Z -= (float)Math.Sin(Camera_Angle.Y * Math.PI / 180) * Camera_Speed;
    131.             }
    132.  
    133.             if (DirectInput_Key_State(Key.D)) //Move Right
    134.             {
    135.                 Camera_Position.X -= (float)Math.Cos(Camera_Angle.Y * Math.PI / 180) * Camera_Speed;
    136.                 Camera_Position.Z += (float)Math.Sin(Camera_Angle.Y * Math.PI / 180) * Camera_Speed;
    137.             }
    138.  
    139.             if (DirectInput_Key_State(Key.Q)) //Look Left
    140.                 Camera_Angle.Y -= 1;
    141.  
    142.             if (DirectInput_Key_State(Key.E)) //Look Right
    143.                 Camera_Angle.Y += 1;
    144.  
    145.             if (DirectInput_Key_State(Key.R)) //Look Up
    146.                 Camera_Angle.X -= 1;
    147.  
    148.             if (DirectInput_Key_State(Key.F)) //Look Down
    149.                 Camera_Angle.X += 1;
    150.  
    151.             if (DirectInput_Key_State(Key.C)) //Move Up
    152.                 Camera_Position.Y -= Camera_Speed;
    153.  
    154.             if (DirectInput_Key_State(Key.Z)) //Move Down
    155.                 Camera_Position.Y += Camera_Speed;
    156.         }
    157.  
    158.         private void Settings()
    159.         {
    160.             Device.SetRenderState(RenderStates.ZEnable, true);
    161.             Device.SetRenderState(RenderStates.ZBufferWriteEnable, true);
    162.             Device.SetRenderState(RenderStates.CullMode, (int)Cull.None);
    163.             Device.SetRenderState(RenderStates.Lighting, false);
    164.         }
    165.  
    166.         private Vector3 Create_Vertex(float X, float Y, float Z)
    167.         {
    168.             Vector3 Vertex;
    169.  
    170.             Vertex.X = X;
    171.             Vertex.Y = Y;
    172.             Vertex.Z = Z;
    173.  
    174.             return Vertex;
    175.         }
    176.  
    177.         private CustomVertex.PositionColored Create_Custom_Vertex(float X, float Y, float Z, int Color)
    178.         {
    179.             CustomVertex.PositionColored Vertex = new CustomVertex.PositionColored();
    180.             Vertex.Position = new Vector3(X, Y, Z);
    181.             Vertex.Color = Color;
    182.  
    183.             return Vertex;
    184.         }
    185.  
    186.         private void Setup_Matrices()
    187.         {
    188.             View_Matrix = Matrix.LookAtRH(Create_Vertex(Camera_Position.X, Camera_Position.Y, Camera_Position.Z), Create_Vertex(0, 0, 0), Create_Vertex(0, 1, 0));
    189.             Device.Transform.View = View_Matrix;
    190.  
    191.             Projection_Matrix = Matrix.PerspectiveFovRH(FOV, ASPECT_RATIO, NEAR_Z, FAR_Z);
    192.             Device.Transform.Projection = Projection_Matrix;
    193.         }
    194.  
    195.         private void Camera_Control()
    196.         {
    197.             Matrix Camera_Translation_Matrix;
    198.             Matrix Camera_Angle_Matrix_X;
    199.             Matrix Camera_Angle_Matrix_Y;
    200.  
    201.             View_Matrix = Matrix.Identity;
    202.             Camera_Translation_Matrix = Matrix.Identity;
    203.  
    204.             Camera_Translation_Matrix = Matrix.Translation(Camera_Position.X, Camera_Position.Y, -Camera_Position.Z);
    205.             View_Matrix = Matrix.Multiply(View_Matrix, Camera_Translation_Matrix);
    206.  
    207.             Camera_Angle_Matrix_Y = Matrix.RotationY((Convert.ToSingle(Math.PI) * Camera_Angle.Y) / 180);
    208.             View_Matrix = Matrix.Multiply(View_Matrix, Camera_Angle_Matrix_Y);
    209.  
    210.             Camera_Angle_Matrix_X = Matrix.RotationX((Convert.ToSingle(Math.PI) * Camera_Angle.X) / 180);
    211.             View_Matrix = Matrix.Multiply(View_Matrix, Camera_Angle_Matrix_X);
    212.  
    213.             Device.Transform.View = View_Matrix;
    214.         }
    215.  
    216.         private void Create_Polygon()
    217.         {
    218.             Vertex_List[0] = Create_Custom_Vertex(-50, 100, 0, Color.FromArgb(255, 255, 255).ToArgb());
    219.             Vertex_List[1] = Create_Custom_Vertex(50, 100, 0, Color.FromArgb(255, 255, 255).ToArgb());
    220.             Vertex_List[2] = Create_Custom_Vertex(-50, 0, 0, Color.FromArgb(255, 255, 255).ToArgb());
    221.             Vertex_List[3] = Create_Custom_Vertex(50, 0, 0, Color.FromArgb(255, 255, 255).ToArgb());
    222.         }
    223.  
    224.         private void Draw_Polygon()
    225.         {
    226.             Device.VertexFormat = CustomVertex.PositionColored.Format;
    227.             Device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, Vertex_List);
    228.         }
    229.  
    230.         private void Create_Platform()
    231.         {
    232.             Vertex_List[0] = Create_Custom_Vertex(-1000, 0, -1000, Color.FromArgb(255, 0, 0).ToArgb());
    233.             Vertex_List[1] = Create_Custom_Vertex(1000, 0, -1000, Color.FromArgb(0, 255, 0).ToArgb());
    234.             Vertex_List[2] = Create_Custom_Vertex(-1000, 0, 1000, Color.FromArgb(0, 0, 255).ToArgb());
    235.             Vertex_List[3] = Create_Custom_Vertex(1000, 0, 1000, Color.FromArgb(255, 0, 255).ToArgb());
    236.         }
    237.  
    238.         private void Reset_Device()
    239.         {
    240.             if (this.WindowState != FormWindowState.Minimized && Fullscreen_Enabled == false)
    241.             {
    242.                 Running = false;
    243.                 Device.Reset(Screen);
    244.                 Settings();
    245.                 Setup_Matrices();
    246.                 Application.DoEvents();
    247.                 Running = true;
    248.             }
    249.         }
    250.  
    251.         private void Render()
    252.         {
    253.             Device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.FromArgb(0, 0, 0), 1.0f, 0);
    254.             Device.BeginScene();
    255.             Create_Platform();
    256.             Draw_Polygon();
    257.             Create_Polygon();
    258.             Draw_Polygon();
    259.             Device.EndScene();
    260.             Device.Present();
    261.         }
    262.  
    263.         private void Game_Loop()
    264.         {
    265.             do
    266.             {
    267.                 Mouse_Controls();
    268.                 Keyboard_Controls();
    269.                 Camera_Control();
    270.                 Render();
    271.                 if (DirectInput_Key_State(Key.Escape)) Shutdown();
    272.                 Application.DoEvents();
    273.             } while (Running == true);
    274.         }
    275.  
    276.         private void Main()
    277.         {
    278.             if (MessageBox.Show("Click Yes to go to fullscreen (Recommended)", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
    279.                 Fullscreen_Enabled = true;
    280.  
    281.             this.Show();
    282.             this.KeyPreview = true;
    283.             this.Width = 330;
    284.             this.Height = 250;
    285.             this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
    286.             this.Text = "DirectX Tutorial";
    287.             if (Fullscreen_Enabled == true)
    288.                 this.FormBorderStyle = FormBorderStyle.None;
    289.  
    290.             Camera_Position.X = 0;
    291.             Camera_Position.Y = -50;
    292.             Camera_Position.Z = 250;
    293.  
    294.             DirectX_Initialize();
    295.             DirectInput_Initialize_Mouse();
    296.             DirectInput_Initialize_Keyboard();
    297.             Settings();
    298.             Setup_Matrices();
    299.             Running = true;
    300.         }
    301.  
    302.         void Shutdown()
    303.         {
    304.             if (Running == true)
    305.             {
    306.                 Running = false;
    307.                 Mouse_Device.Unacquire();
    308.                 Mouse_Device.Dispose();
    309.                 Keyboard_Device.Unacquire();
    310.                 Keyboard_Device.Dispose();
    311.                 Device.Dispose();
    312.                 Application.Exit();
    313.             }
    314.         }
    315.  
    316.         public frmMain()
    317.         {
    318.             InitializeComponent();
    319.         }
    320.  
    321.         private void frmMain_Load(object sender, EventArgs e)
    322.         {
    323.             Main();
    324.         }
    325.  
    326.         private void frmMain_Paint(object sender, PaintEventArgs e)
    327.         {
    328.             Game_Loop();
    329.         }
    330.  
    331.         private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
    332.         {
    333.             Shutdown();
    334.         }
    335.  
    336.         private void frmMain_Resize(object sender, EventArgs e)
    337.         {
    338.             Reset_Device();
    339.         }
    340.     }
    341. }

    ...Continued next post

  11. #11
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: MDX, SlimDX, SharpDX Tutorials, Sample Codes, Documentations and Links - for Newb

    ...Continued from last post

    C++
    c++ Code:
    1. #include <windows.h>
    2. #include <d3d9.h>
    3. #include <d3dx9.h>
    4. #include <dinput.h>
    5.  
    6. #pragma comment (lib, "d3d9.lib")
    7. #pragma comment (lib, "d3dx9.lib")
    8. #pragma comment (lib, "dinput8.lib")
    9. #pragma comment (lib, "dxguid.lib")
    10.  
    11. struct CUSTOM_VERTEX
    12. {
    13.     float X, Y, Z;
    14.     DWORD Color;
    15. };
    16.  
    17. #define CUSTOM_VERTEX_FORMAT (D3DFVF_XYZ | D3DFVF_DIFFUSE)
    18.  
    19. const float PI = 3.141592654f;
    20. const float FOV = PI / 4.0f;
    21. const float ASPECT_RATIO = 4.0f / 3.0f;
    22. const float NEAR_Z = 1.0f;
    23. const float FAR_Z = 10000.0f;
    24.  
    25. HWND hWnd;
    26. MSG msg;
    27. HINSTANCE hInstance;
    28. int Width;
    29. int Height;
    30.  
    31. LPDIRECT3D9 D3D;
    32. LPDIRECT3DDEVICE9 Device;
    33. D3DDISPLAYMODE Display_Mode;
    34. D3DPRESENT_PARAMETERS Screen;
    35.  
    36. LPDIRECTINPUT8 DI;
    37.  
    38. LPDIRECTINPUTDEVICE8 Mouse_Device;
    39. DIMOUSESTATE Mouse_State;
    40. D3DXVECTOR2 Mouse;
    41.  
    42. LPDIRECTINPUTDEVICE8 Keyboard_Device;
    43. BYTE Keyboard_State[256];
    44.  
    45. CUSTOM_VERTEX Vertex_List[4];
    46.  
    47. D3DXMATRIX View_Matrix;
    48. D3DXMATRIX Projection_Matrix;
    49.  
    50. D3DVECTOR Camera_Position;
    51. D3DVECTOR Camera_Angle;
    52.  
    53. bool Fullscreen_Enabled;
    54. bool Running;
    55.  
    56. void DirectX_Initialize(void);
    57. void DirectInput_Initialize(void);
    58. void DirectInput_Initialize_Mouse(void);
    59. void Mouse_Controls(void);
    60. void DirectInput_Initialize_Keyboard(void);
    61. bool DirectInput_Key_State(void);
    62. void Keyboard_Controls(void);
    63. void Settings(void);
    64. D3DXVECTOR3 Create_Vertex(void);
    65. CUSTOM_VERTEX Create_Custom_Vertex(void);
    66. void Setup_Matrices(void);
    67. void Camera_Control(void);
    68. void Create_Polygon(void);
    69. void Draw_Polygon(void);
    70. void Create_Platform(void);
    71. void Render(void);
    72. void Game_Loop(void);
    73. void Main(void);
    74. void Shutdown(void);
    75.  
    76. LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
    77.  
    78. void DirectX_Initialize()
    79. {
    80.     D3D = Direct3DCreate9(D3D_SDK_VERSION);
    81.     memset(&Screen, 0, sizeof(D3DPRESENT_PARAMETERS));
    82.  
    83.     if (Fullscreen_Enabled == true)
    84.     {
    85.        Display_Mode.Width = 800;
    86.        Display_Mode.Height = 600;
    87.        Display_Mode.Format = D3DFMT_R5G6B5;
    88.        Screen.Windowed = FALSE;
    89.        Screen.BackBufferCount = 1;
    90.        Screen.BackBufferWidth = Display_Mode.Width;
    91.        Screen.BackBufferHeight = Display_Mode.Height;
    92.        Screen.hDeviceWindow = hWnd;
    93.     }
    94.     else
    95.     {
    96.        D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &Display_Mode);
    97.        Screen.Windowed = TRUE;
    98.     }
    99.  
    100.     Screen.SwapEffect = D3DSWAPEFFECT_DISCARD;
    101.     Screen.BackBufferFormat = D3DFMT_R5G6B5;
    102.     Screen.EnableAutoDepthStencil = TRUE;
    103.     Screen.AutoDepthStencilFormat = D3DFMT_D16;
    104.  
    105.     D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &Screen, &Device);
    106. }
    107.  
    108. void DirectInput_Initialize()
    109. {
    110.     DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&DI, NULL);
    111. }
    112.  
    113. void DirectInput_Initialize_Mouse()
    114. {
    115.     DI->CreateDevice(GUID_SysMouse, &Mouse_Device, NULL);
    116.     Mouse_Device->SetDataFormat(&c_dfDIMouse);
    117.     Mouse_Device->SetCooperativeLevel(hWnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE);
    118.     Mouse_Device->Acquire();
    119. }
    120.  
    121. void Mouse_Controls()
    122. {
    123.     Mouse_Device->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&Mouse_State);
    124.  
    125.     Mouse.x += Mouse_State.lX;
    126.     Mouse.y += Mouse_State.lY;
    127.    
    128.     if (Mouse.x > Width) Mouse.x = (float)Width;
    129.     if (Mouse.y < 0) Mouse.x = 0;
    130.    
    131.     if (Mouse.x > Height) Mouse.y = (float)Height;
    132.     if (Mouse.y < 0) Mouse.y = 0;
    133.    
    134.     Camera_Angle.x += Mouse_State.lY;
    135.     Camera_Angle.y += Mouse_State.lX;
    136.  
    137.     if (Camera_Angle.x >= 90) Camera_Angle.x = 90;
    138.     if (Camera_Angle.x <= -90) Camera_Angle.x = -90;
    139. }
    140.  
    141. void DirectInput_Initialize_Keyboard()
    142. {
    143.     DI->CreateDevice(GUID_SysKeyboard, &Keyboard_Device, NULL);
    144.     Keyboard_Device->SetDataFormat(&c_dfDIKeyboard);
    145.     Keyboard_Device->SetCooperativeLevel(hWnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE);
    146.     Keyboard_Device->Acquire();
    147. }
    148.  
    149. bool DirectInput_Key_State(int Key_Code)
    150. {
    151.     Keyboard_Device->GetDeviceState(256, (LPVOID)Keyboard_State);
    152.     return Keyboard_State[Key_Code] && 0x80;
    153. }
    154.  
    155. void Keyboard_Controls()
    156. {
    157.     const int Camera_Speed = 5;
    158.  
    159.     if (DirectInput_Key_State(DIK_W))
    160.     {
    161.         Camera_Position.x -= (float)sin(Camera_Angle.y * PI / 180) * Camera_Speed;
    162.         Camera_Position.z -= (float)cos(Camera_Angle.y * PI / 180) * Camera_Speed;
    163.     }
    164.  
    165.     if (DirectInput_Key_State(DIK_S))
    166.     {
    167.         Camera_Position.x += (float)sin(Camera_Angle.y * PI / 180) * Camera_Speed;
    168.         Camera_Position.z += (float)cos(Camera_Angle.y * PI / 180) * Camera_Speed;
    169.     }
    170.  
    171.     if (DirectInput_Key_State(DIK_A))
    172.     {
    173.         Camera_Position.x += (float)cos(Camera_Angle.y * PI / 180) * Camera_Speed;
    174.         Camera_Position.z -= (float)sin(Camera_Angle.y * PI / 180) * Camera_Speed;
    175.     }
    176.  
    177.     if (DirectInput_Key_State(DIK_D))
    178.     {
    179.         Camera_Position.x -= (float)cos(Camera_Angle.y * PI / 180) * Camera_Speed;
    180.         Camera_Position.z += (float)sin(Camera_Angle.y * PI / 180) * Camera_Speed;
    181.     }
    182.  
    183.     if (DirectInput_Key_State(DIK_Q))
    184.     {
    185.         Camera_Angle.y -= 1;
    186.     }
    187.  
    188.     if (DirectInput_Key_State(DIK_E))
    189.     {
    190.         Camera_Angle.y += 1;
    191.     }
    192.  
    193.     if (DirectInput_Key_State(DIK_R))
    194.     {
    195.         Camera_Angle.x -= 1;
    196.     }
    197.  
    198.     if (DirectInput_Key_State(DIK_F))
    199.     {
    200.         Camera_Angle.x += 1;
    201.     }
    202.  
    203.     if (DirectInput_Key_State(DIK_C))
    204.     {
    205.         Camera_Position.y -= Camera_Speed;
    206.     }
    207.  
    208.     if (DirectInput_Key_State(DIK_Z))
    209.     {
    210.         Camera_Position.y += Camera_Speed;
    211.     }
    212. }
    213.  
    214. void Settings()
    215. {
    216.     Device->SetRenderState(D3DRS_ZENABLE, TRUE);
    217.     Device->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
    218.     Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
    219.     Device->SetRenderState(D3DRS_LIGHTING, FALSE);
    220. }
    221.  
    222. D3DXVECTOR3 Create_Vertex(float X, float Y, float Z)
    223. {
    224.     D3DXVECTOR3 Vertex;
    225.  
    226.     Vertex.x = X;
    227.     Vertex.y = Y;
    228.     Vertex.z = Z;
    229.  
    230.     return Vertex;
    231. }
    232.  
    233. CUSTOM_VERTEX Create_Custom_Vertex(float X, float Y, float Z, DWORD Color)
    234. {
    235.     CUSTOM_VERTEX Vertex;
    236.  
    237.     Vertex.X = X;
    238.     Vertex.Y = Y;
    239.     Vertex.Z = Z;
    240.     Vertex.Color = Color;
    241.  
    242.     return Vertex;
    243. }
    244.  
    245. void Setup_Matrices()
    246. {
    247.     D3DXMatrixLookAtRH(&View_Matrix, &Create_Vertex(Camera_Position.x, Camera_Position.y, Camera_Position.z), &Create_Vertex(0, 0, 0), &Create_Vertex(0.0f, 1.0f, 0.0f));
    248.     Device->SetTransform(D3DTS_VIEW, &View_Matrix);
    249.  
    250.     D3DXMatrixPerspectiveFovRH(&Projection_Matrix, FOV, ASPECT_RATIO, NEAR_Z, FAR_Z);
    251.     Device->SetTransform(D3DTS_PROJECTION, &Projection_Matrix);
    252. }
    253.  
    254. void Camera_Control()
    255. {
    256.     D3DXMATRIX Camera_Translation_Matrix;
    257.     D3DXMATRIX Camera_Angle_Matrix_X;
    258.     D3DXMATRIX Camera_Angle_Matrix_Y;
    259.    
    260.     D3DXMatrixIdentity(&View_Matrix);
    261.     D3DXMatrixIdentity(&Camera_Translation_Matrix);
    262.  
    263.     D3DXMatrixTranslation(&Camera_Translation_Matrix, Camera_Position.x, Camera_Position.y, -Camera_Position.z);
    264.     D3DXMatrixMultiply(&View_Matrix, &View_Matrix, &Camera_Translation_Matrix);
    265.  
    266.     D3DXMatrixRotationY(&Camera_Angle_Matrix_Y, (PI * Camera_Angle.y) / 180);
    267.     D3DXMatrixMultiply(&View_Matrix, &View_Matrix, &Camera_Angle_Matrix_Y);
    268.  
    269.     D3DXMatrixRotationX(&Camera_Angle_Matrix_X, (PI * Camera_Angle.x) / 180);
    270.     D3DXMatrixMultiply(&View_Matrix, &View_Matrix, &Camera_Angle_Matrix_X);
    271.  
    272.     Device->SetTransform(D3DTS_VIEW, &View_Matrix);
    273. }
    274.  
    275. void Create_Polygon()
    276. {
    277.     Vertex_List[0] = Create_Custom_Vertex(-50.0f, 100.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255));
    278.     Vertex_List[1] = Create_Custom_Vertex(50.0f, 100.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255));
    279.     Vertex_List[2] = Create_Custom_Vertex(-50.0f, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255));
    280.     Vertex_List[3] = Create_Custom_Vertex(50.0f, 0.0f, 0.0f, D3DCOLOR_XRGB(255, 255, 255));
    281. }
    282.  
    283. void Draw_Polygon()
    284. {
    285.     Device->SetFVF(CUSTOM_VERTEX_FORMAT);
    286.     Device->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, Vertex_List, sizeof(CUSTOM_VERTEX));
    287. }
    288.  
    289. void Create_Platform()
    290. {
    291.     Vertex_List[0] = Create_Custom_Vertex(-1000.0f, 0.0f, -1000.0f, D3DCOLOR_XRGB(255, 0, 0));
    292.     Vertex_List[1] = Create_Custom_Vertex(1000.0f, 0.0f, -1000.0f, D3DCOLOR_XRGB(0, 255, 0));
    293.     Vertex_List[2] = Create_Custom_Vertex(-1000.0f, 0.0f, 1000.0f, D3DCOLOR_XRGB(0, 0, 255));
    294.     Vertex_List[3] = Create_Custom_Vertex(1000.0f, 0.0f, 1000.0f, D3DCOLOR_XRGB(255, 0, 255));
    295. }
    296.  
    297. void Render()
    298. {
    299.     Device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
    300.     Device->BeginScene();
    301.     //Rendering code goes here
    302.     Create_Polygon();
    303.     Draw_Polygon();
    304.     Create_Platform();
    305.     Draw_Polygon();
    306.     Device->EndScene();
    307.     Device->Present(NULL, NULL, NULL, NULL);
    308. }
    309.  
    310. void Game_Loop()
    311. {
    312.     while (Running == true)
    313.     {
    314.         if (PeekMessage(&msg,NULL,0,0,PM_REMOVE) > 0)
    315.         {
    316.             if (WM_QUIT == msg.message) break;
    317.             TranslateMessage (&msg);
    318.             DispatchMessage (&msg);
    319.         }
    320.         else
    321.         {
    322.             Mouse_Controls();
    323.             Keyboard_Controls();
    324.             Camera_Control();
    325.             Render();
    326.             if (DirectInput_Key_State(DIK_ESCAPE)) DestroyWindow(hWnd);
    327.         }
    328.     }
    329. }
    330.  
    331. void Main()
    332. {
    333.     Camera_Position.x = 0.0f;
    334.     Camera_Position.y = -50.0f;
    335.     Camera_Position.z = 250.0f;
    336.  
    337.     DirectX_Initialize();
    338.     DirectInput_Initialize();
    339.     DirectInput_Initialize_Mouse();
    340.     DirectInput_Initialize_Keyboard();
    341.     Settings();
    342.     Setup_Matrices();
    343.     Running = true;
    344. }
    345.  
    346. void Shutdown()
    347. {
    348.     if (Running == true)
    349.     {
    350.         Running = false;
    351.         Mouse_Device->Unacquire();
    352.         Mouse_Device->Release();
    353.         Keyboard_Device->Unacquire();
    354.         Keyboard_Device->Release();
    355.         Device->Release();
    356.         D3D->Release();
    357.         PostQuitMessage (0);
    358.         HANDLE Process;
    359.         Process = OpenProcess(PROCESS_ALL_ACCESS , true , GetCurrentProcessId());
    360.         TerminateProcess(Process , 0);
    361.     }
    362. }
    363.  
    364. int WINAPI WinMain (HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow)
    365. {
    366.    
    367.     WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_VREDRAW|CS_HREDRAW|CS_OWNDC, WindowProcedure, 0, 0, hinstance, NULL, LoadCursor(NULL, IDC_ARROW), NULL, NULL, "DX_TUT", NULL};
    368.     RegisterClassEx(&wc);
    369.    
    370.     if (MessageBox(hWnd, "Click Yes to go to fullscreen (Recommended)", "", MB_ICONQUESTION | MB_YESNO) == IDYES)
    371.        Fullscreen_Enabled = true;
    372.  
    373.     if (Fullscreen_Enabled == true)
    374.         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);
    375.     else
    376.         hWnd = CreateWindowEx (0, "DX_TUT", "DirectX Tutorial", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 330, 250, HWND_DESKTOP, NULL, hinstance, NULL);
    377.     hInstance = hinstance;
    378.     ShowWindow (hWnd, nCmdShow);
    379.  
    380.     if (Fullscreen_Enabled == true)
    381.     {
    382.         Width = 800;
    383.         Height = 600;
    384.     }
    385.     else
    386.     {
    387.         Width = 330;
    388.         Height = 250;
    389.     }
    390.  
    391.     Main();
    392.     Game_Loop();
    393.     return msg.wParam;
    394. }
    395.  
    396. LRESULT CALLBACK WindowProcedure (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    397. {
    398.     switch (msg)
    399.     {
    400.         case WM_DESTROY:
    401.             Shutdown();
    402.             break;
    403.         default:
    404.             return DefWindowProc (hWnd, msg, wParam, lParam);
    405.     }
    406.     return 0;
    407. }

  12. #12

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    12

    Re: MDX, SlimDX, SharpDX Tutorials, Sample Codes, Documentations and Links - for Newb

    Yea I got the VB.NET version of this code already from a previous post of you and it's a must for newbies, surely one that does belong to here

  13. #13
    New Member
    Join Date
    Feb 2014
    Posts
    1

    Re: MDX, SlimDX, SharpDX Tutorials, Sample Codes, Documentations and Links - for Newb

    Thank you guys, I wrote a piece of code using SlimDX 4, Directx 11 in Visual Studio 2013 Vb net.
    I wuold like your thought about

    to download the code go there
    http://vbgraphic.altervista.org/directx11/directx11.htm

    Fabio

  14. #14

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    12

    Re: MDX, SlimDX, SharpDX Tutorials, Sample Codes, Documentations and Links - for Newb

    Thanx bro, you're the 3rd one in almost a year Will check it out

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