Dim Running As Boolean 'Set to false to end the game loop. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load 'This is where the application will begin... in the Form Load. 'The first thing we want to do is make a device. Dim D9 As Device 'A device is the object that facilitates all of the operations that go on with DirectX 9 3D and VB.NET. 'Set this to a new instance ASAP when you are using DX9 3D 'So, we want to create a new device: there are three overloads that we can use: '#1: 'D9 = New Device(0, DeviceType, HandleToWindow, CreateFlags, Presentation Parameters) '#2 which is the same as #1 except you pass the form instead of the form handle. 'D9 = New Device(0, DeviceType, Form, CreateFlags, Presentation Parameters) '#3 is an IntPtr (to what I may never know). 'D9 = New Device(Ip) 'So, we will probably not be using overload #3. 'We must use overload #1 or #2. This means that we need values to go into all of these parameters. 'Very well: 'D9 = New Device(0, DeviceType.Hardware, Me, CreateFlags.SoftwareVertexProcessing, PsPms) 'But we need a value for PsPms. Object Browser (Ctrl Alt J) says that Presentation Parameters is a class. Dim PsPms As PresentParameters 'So, we declare it. PsPms = New PresentParameters 'And set it to a new instance. 'Now what do we put in it? PsPms.DeviceWindow = Me 'This looks important. PsPms.SwapEffect = SwapEffect.Discard 'This one is easy to set... Discard is what the documentation uses, but I figure this just draws over the 'last drawing. PsPms.Windowed = True 'We'll let it be windowed for now. :) 'Now that we've set some properties, we can make our Device. D9 = New Device(0, DeviceType.Hardware, Me, CreateFlags.SoftwareVertexProcessing, PsPms) 'And there's our device. 'Now, with all DirectX applications, we are obliged to use a game loop. 'The DirectDraw tutorial starts game loops on episode 3, but DirectX9 is harder... we must do 'this now. (Actually, we probably don't have to do this now) 'So first, we need to make sure the form is visible before we do any drawing. Me.Show() Me.Focus() 'And then of course the game loop. Running = True Do While Running 'Now, inside the game loop we do all of the processing. 'Before we do any drawing, we would do the game manipulations and such, but 'since we aren't manipulating any game classes, variables, etc. 'we do nothing here, but go directly into the drawing phase. 'For DX9 3D, the first and most simplest thing is the clear statement. 'Not that it's very easy. D9.Clear(ClearFlags.Target, Color.Gray, 1.0F, 0) 'The other overloads either sport an array of rectangles at the end, or make you assign a color in the ARGB integer format. 'When doing more sophisticated drawing, our drawing to the device would be between a .BeginScene and 'an .EndScene() call. D9.BeginScene() 'So, we'd be drawing here. D9.EndScene() 'And we would NOT be drawing here. 'Typically the line immediately after .EndScene is .Present D9.Present() 'And of course, Present to present what the device has drawn on its own memory 'The device has its own drawing surface: present just transports it to the screen. Application.DoEvents() 'Of course, we MUST have a DoEvents in the loop, otherwise, Windows will have a seizure. Loop Me.Close() 'And close the form when the game loop is over. End Sub Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown If e.KeyCode = Keys.Escape Then Running = False 'End the game loop when Escape is pressed. End If End Sub Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing Running = False 'End the game loop so that the device doesn't draw again. End Sub |