Code: Select all
; ------------------------------------------------------------------------------------------------------
; Tutorial 2 - Rendering Vertices
; ------------------------------------------------------------------------------------------------------
; Constants
#D3DFMT_UNKNOWN = 0
#D3DSWAPEFFECT_DISCARD = 1
#D3DDEVTYPE_HAL = 1
#D3DADAPTER_DEFAULT = 0
#D3DCREATE_SOFTWARE_VERTEXPROCESSING = 32
#D3D_SDK_VERSION = 31
#D3DCLEAR_TARGET = 1
#D3DPOOL_DEFAULT = 0
#D3DFVF_XYZRHW = 4
#D3DFVF_DIFFUSE = 64
#D3DPT_TRIANGLELIST = 4
; Structures
Structure D3DPRESENT_PARAMETERS
BackBufferWidth.l
BackBufferHeight.l
BackBufferFormat.l
BackBufferCount.l
MultiSampleType.l
MultiSampleQuality.l
SwapEffect.l
hDeviceWindow.l
Windowed.l
EnableAutoDepthStencil.l
AutoDepthStencilFormat.l
flags.l
FullScreen_RefreshRateInHz.l
PresentationInterval.l
EndStructure
; Macros
Macro D3DCOLOR_ARGB(A,R,G,B)
(A & $FF) << 24 | (R & $FF) << 16 | (G & $FF) << 8 | (B & $FF)
EndMacro
Macro D3DCOLOR_XRGB(R,G,B)
D3DCOLOR_ARGB($FF,R,G,B)
EndMacro
; ------------------------------------------------------------------------------------------------------
; EXAMPLE
; ------------------------------------------------------------------------------------------------------
Declare WindowProc(hWnd,uMsg,wParam,lParam)
Declare Render()
Declare Cleanup()
; Direct3D Interfaces
Global g_pD3D.IDirect3D9
Global g_pd3dDevice.IDirect3DDevice9
Global g_pVB.IDirect3DVertexBuffer9
; Create the application's window
OpenWindow(0,0,0,300,300,"Direct3D Tutorial 02: Rendering Vertices",#WS_OVERLAPPEDWINDOW | 1)
SetClassLongPtr_(WindowID(0),#GCL_STYLE,#CS_DBLCLKS | #CS_CLASSDC)
SetWindowCallback(@WindowProc())
; Initializing Direct3D
OpenLibrary(0,"D3D9.dll")
g_pD3D = CallFunction(0,"Direct3DCreate9",#D3D_SDK_VERSION)
d3dpp.D3DPRESENT_PARAMETERS
d3dpp\Windowed = #True
d3dpp\SwapEffect = #D3DSWAPEFFECT_DISCARD
d3dpp\BackBufferFormat = #D3DFMT_UNKNOWN
g_pD3D\CreateDevice(#D3DADAPTER_DEFAULT,#D3DDEVTYPE_HAL,WindowID(0),#D3DCREATE_SOFTWARE_VERTEXPROCESSING,d3dpp,@g_pd3dDevice)
; Defining a Custom Vertex Type
#D3DFVF_CUSTOMVERTEX = #D3DFVF_XYZRHW | #D3DFVF_DIFFUSE
Structure CUSTOMVERTEX
X.f : Y.f : Z.f : RHW.f ; The transformed position for the vertex.
Color.l ; The vertex color.
EndStructure
; Initialize values for three custom vertices
Global Dim vertices.CUSTOMVERTEX(2)
Procedure SetVertex(Index,X,Y,Z,RHW,Color)
vertices(Index)\X = X
vertices(Index)\Y = Y
vertices(Index)\Z = Z
vertices(Index)\RHW = RHW
vertices(Index)\Color = Color
EndProcedure
SetVertex(0,150.0, 50.0,0.5,1.0,$FFFF0000) ; X, Y, Z, RHW, Color
SetVertex(1,250.0,250.0,0.5,1.0,$FF00FF00)
SetVertex(2, 50.0,250.0,0.5,1.0,$FF00FFFF)
; Setting up the vertex buffer
g_pd3dDevice\CreateVertexBuffer(3 * SizeOf(CUSTOMVERTEX),0,#D3DFVF_CUSTOMVERTEX,#D3DPOOL_DEFAULT,@g_pVB,0)
; Fill buffer with data from the custom vertices
g_pVB\Lock(0,3 * SizeOf(CUSTOMVERTEX),@*pVertices,0)
CopyMemory(vertices(),*pVertices,3 * SizeOf(CUSTOMVERTEX))
g_pVB\Unlock()
; // Main Loop //
While WaitWindowEvent() ! #PB_Event_CloseWindow : Wend
; ------------------------------------------------------------------------------------------------------
; FUNCTIONS
; ------------------------------------------------------------------------------------------------------
Procedure WindowProc(hWnd,uMsg,wParam,lParam)
Select uMsg
Case #WM_DESTROY
Cleanup()
ProcedureReturn 0
Case #WM_PAINT
Render()
ValidateRect_(hWnd,0)
ProcedureReturn 0
EndSelect
ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Procedure Render()
g_pd3dDevice\Clear(0,0,#D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,255),1.0,0)
g_pd3dDevice\BeginScene()
; Rendering the Display
g_pd3dDevice\SetStreamSource(0,g_pVB,0,SizeOf(CUSTOMVERTEX))
g_pd3dDevice\SetFVF(#D3DFVF_CUSTOMVERTEX)
g_pd3dDevice\DrawPrimitive(#D3DPT_TRIANGLELIST,0,1)
g_pd3dDevice\EndScene()
g_pd3dDevice\Present(0,0,0,0)
EndProcedure
; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Procedure Cleanup()
If g_pd3dDevice ! 0 : g_pd3dDevice\Release() : EndIf
If g_pD3D ! 0 : g_pD3D\Release() : EndIf
If g_pVB ! 0 : g_pVB\Release() : EndIf
EndProcedure