1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data
Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D
PublicClass d3dInjection
Inherits System.Windows.Forms.Form
Private hHandle AsLong
PrivateWithEvents device As Device
Private vertices As VertexBuffer
'Le device a t il t perdu et non-reinitialis?
Private deviceLost AsBoolean = False
Private pres As PresentParameters = New PresentParameters()
'boucle du jeu
PublicSubNew(OptionalByVal hwnd AsLong = 0)
If hwnd > 0 Then
hHandle = hwnd
Else
hHandle = Me.Handle
EndIf
Me.InitializeGraphics()
Me.Show()
WhileMe.Created
Me.Render()
Application.DoEvents()
EndWhile
Me.DisposeGraphics()
EndSub
ProtectedFunction InitializeGraphics() AsBoolean
pres.Windowed = True
pres.SwapEffect = SwapEffect.Discard
device = New Device(0, DeviceType.Hardware, hHandle, CreateFlags.SoftwareVertexProcessing, pres)
vertices = CreateVertexBuffer(device)
'Fait la configuration initiale de nos objets graphiques
SetupDevice()
ReturnTrue
EndFunction
ProtectedFunction CreateVertexBuffer(ByVal device As Device) As VertexBuffer
device.VertexFormat = CustomVertex.TransformedColored.Format
Dim verts As CustomVertex.TransformedColored() = New CustomVertex.TransformedColored(2) {}
verts(0) = New CustomVertex.TransformedColored(Me.Width / 2, Me.Height / 4, 0.5F, 1, Color.Blue.ToArgb())
verts(1) = New CustomVertex.TransformedColored(Me.Width * 3 / 4, Me.Height * 3 / 4, 0.5F, 1, Color.Green.ToArgb())
verts(2) = New CustomVertex.TransformedColored(Me.Width / 4, Me.Height * 3 / 4, 0.5F, 1, Color.Red.ToArgb())
Dim buf AsNew VertexBuffer(GetType(CustomVertex.TransformedColored), verts.Length, device, 0, CustomVertex.TransformedColored.Format, Pool.[Default])
Dim stm As GraphicsStream = buf.Lock(0, 0, 0)
stm.Write(verts)
buf.Unlock()
Return buf
EndFunction
ProtectedSub Render()
If deviceLost Then
' Tente de reprendre le device
AttemptRecovery()
EndIf
' Si le device n'a pas pu tre rcupr, n'essaye pas de faire le rendu
If deviceLost Then
ExitSub
EndIf
' Paint la surface en noir partout
device.Clear(ClearFlags.Target, Color.Black, 1.0F, 0)
device.BeginScene()
' Appels aux mthodes de dessin ici
device.SetStreamSource(0, vertices, 0)
device.DrawPrimitives(PrimitiveType.TriangleList, 0, 1)
device.EndScene()
Try
' Copie le back buffer l'cran
device.Present()
Catch generatedExceptionName As DeviceLostException
' Indique que le device est perdu
deviceLost = True
' Affiche un message dans la fentre du dbugger
Debug.WriteLine("Device was lost")
EndTry
EndSub
ProtectedSub AttemptRecovery()
Try
device.TestCooperativeLevel()
Catch generatedExceptionName As DeviceLostException
Catch generatedExceptionName As DeviceNotResetException
Try
device.Reset(pres)
deviceLost = False
' Affiche un message dans la fentre du dbugger
Debug.WriteLine("Device successfully reset")
Catch generatedExceptionName1 As DeviceLostException
' Si il est toujours perdu ou qu'il a encore t perdu
' ne fais rien
EndTry
EndTry
EndSub
ProtectedSub DisposeGraphics()
device.Dispose()
EndSub
PrivateSub device_DeviceResizing(ByVal sender AsObject, ByVal e As System.ComponentModel.CancelEventArgs) Handles device.DeviceResizing
e.Cancel = True
EndSub
PrivateSub device_DeviceReset(ByVal sender AsObject, ByVal e As System.EventArgs) Handles device.DeviceReset
SetupDevice()
EndSub
PrivateSub device_DeviceLost(ByVal sender AsObject, ByVal e As System.EventArgs) Handles device.DeviceLost
vertices.Dispose()
EndSub
ProtectedSub SetupDevice()
'Configure les "RenderStates" du device
device.RenderState.Lighting = False
device.RenderState.CullMode = Cull.None
'Cre le VertexBuffer
vertices = CreateVertexBuffer(device)
EndSub
EndClass
|
Partager