home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD89078132000.psc / DX_DRAW.bas next >
Encoding:
BASIC Source File  |  2000-08-13  |  8.3 KB  |  291 lines

  1. Attribute VB_Name = "MOD_DX_DRAW"
  2. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  3. '
  4. '            MOD_DX_DRAW.BAS - BY SIMON PRICE
  5. '
  6. '          LOADS OF HANDY DIRECT DRAW FUNCTIONS
  7. '
  8. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  9.  
  10.  
  11. ' the great grand daddy of them all
  12. Public DX As New DirectX7
  13. ' the direct draw object, direct access to video card = cool!
  14. Public DX_DRAW As DirectDraw7
  15. ' have we started or not?
  16. Private InExclusiveMode As Boolean
  17.  
  18. ' surfaces
  19. Public BackBuffer As DirectDrawSurface7
  20. Public View As DirectDrawSurface7
  21. Public Background As DirectDrawSurface7
  22. Public Scene As DirectDrawSurface7
  23. Public Const NUM_TEX = 7
  24. Public Tex(NUM_TEX) As DirectDrawSurface7
  25. Public Const TEX_WALL = 0
  26. Public Const TEX_SIDEWALL = 1
  27. Public Const TEX_GRASS = 2
  28. Public Const TEX_WATER = 3
  29. Public Const TEX_FENCE = 4
  30. Public Const TEX_ROOF = 5
  31. Public Const TEX_PLANE_BOAT = 6
  32. Public Const TEX_TREE = 7
  33.  
  34. ' surface descriptions
  35. Public SurfDesc As DDSURFACEDESC2
  36.  
  37. ' back buffer capabilaties
  38. Public BackBufferCaps As DDSCAPS2
  39.  
  40. ' colour key for masking
  41. Public ColorKey As DDCOLORKEY
  42.  
  43. ' rects
  44. Public SrcRect As RECT
  45. Public DestRect As RECT
  46.  
  47. Public Declare Function GetInputState Lib "user32" () As Long
  48. Private Declare Function ShowCursor Lib "user32" (ByVal bShow As Long) As Long
  49. Private Cur As Long
  50.  
  51. Sub CrankItUp(hwnd As Long, FullScreen As Boolean)
  52. ' this sub gets it all going but creating Direct Draw
  53. On Error GoTo TheCrappyThingDidNotEvenStartUp
  54.  
  55. ' if we've already started, don't bother starting again
  56. If InExclusiveMode Then Exit Sub
  57.  
  58. ' create direct draw
  59. Set DX_DRAW = DX.DirectDrawCreate("")
  60.  
  61. If FullScreen Then
  62.     ' give us all the screen and all the power, yes!
  63.     DX_DRAW.SetCooperativeLevel hwnd, DDSCL_FULLSCREEN Or DDSCL_ALLOWMODEX Or DDSCL_EXCLUSIVE 'Or DDSCL_ALLOWREBOOT
  64.     InExclusiveMode = True
  65. Else
  66.     ' use normal mode
  67.     DX_DRAW.SetCooperativeLevel hwnd, DDSCL_NORMAL
  68. End If
  69.  
  70. Exit Sub
  71.  
  72. ' if thing go pear shaped, exit
  73. TheCrappyThingDidNotEvenStartUp:
  74. MsgBox "Error - Cannot activate DirectX 7 - make sure you have it installed correctly!", vbExclamation, "Error!"
  75. End
  76. End Sub
  77.  
  78. Sub EndIt(hwnd As Long)
  79. DX_DRAW.SetCooperativeLevel hwnd, DDSCL_NORMAL
  80. InExMode = False
  81. End Sub
  82.  
  83. Sub SetDisplayMode(Width As Integer, Height As Integer, Colors As Byte)
  84. 'set's the display mode to the required size and colors
  85.  DX_DRAW.SetDisplayMode Width, Height, Colors, 0, DDSDM_DEFAULT
  86. End Sub
  87.  
  88. Sub WaitTillOK()
  89. Dim bRestore As Boolean
  90.  
  91. bRestore = False
  92. Do Until ExModeActive 'short way of saying "do until it returns true"
  93.     DoEvents 'Lets windows do other things
  94.     bRestore = True
  95. Loop
  96.  
  97. ' if we lost and got back the surfaces, then restore them
  98. DoEvents 'Lets windows do it's things
  99. If bRestore Then
  100.     bRestore = False
  101.     DX_DRAW.RestoreAllSurfaces
  102.     ModSurfaces.LoadAllPics ' must init the surfaces again if they we're lost. When this happens the first line of initsurfaces is important
  103. End If
  104. End Sub
  105.  
  106. Function ExModeActive() As Boolean
  107.      Dim TestCoopRes As Long ' holds the return value of the test.
  108.  
  109.      TestCoopRes = DX_DRAW.TestCooperativeLevel ' Tells DDraw to do the test
  110.  
  111.      If (TestCoopRes = DD_OK) Then
  112.          ExModeActive = True ' everything is sweet
  113.      Else
  114.          ExModeActive = False ' summinks gone wrong
  115.      End If
  116.  End Function
  117.  
  118. Sub CreatePrimaryWithBackBuffer()
  119. ' does what it says in the name
  120. Set View = Nothing
  121. Set BackBuffer = Nothing
  122.  
  123. SurfDesc.lFlags = DDSD_CAPS Or DDSD_BACKBUFFERCOUNT
  124. SurfDesc.ddsCaps.lCaps = DDSCAPS_PRIMARYSURFACE Or DDSCAPS_FLIP Or DDSCAPS_COMPLEX
  125. SurfDesc.lBackBufferCount = 1
  126. Set View = DX_DRAW.CreateSurface(SurfDesc)
  127.  
  128. BackBufferCaps.lCaps = DDSCAPS_BACKBUFFER
  129. Set BackBuffer = View.GetAttachedSurface(BackBufferCaps)
  130. 'BackBuffer.GetSurfaceDesc ViewDesc
  131.  
  132. BackBuffer.SetFontTransparency True
  133. End Sub
  134.  
  135. Sub CreatePrimaryOnly()
  136. ' create a primary surface without a backbuffer,
  137. ' for use in normal mode
  138. SurfDesc.lFlags = DDSD_CAPS
  139. SurfDesc.ddsCaps.lCaps = DDSCAPS_PRIMARYSURFACE
  140. Set View = DX_DRAW.CreateSurface(SurfDesc)
  141. End Sub
  142.  
  143. Sub LoadAllSurfaces()
  144. ' loads every pic we need
  145.  
  146. If InExclusiveMode Then
  147.     ' load primary surface and backbuffer
  148.     CreatePrimaryWithBackBuffer
  149. Else
  150.     CreatePrimaryOnly
  151. End If
  152.  
  153. '*** add app specific pics here ***
  154.  
  155. ' create background
  156. CreateSurfaceFromFile Background, SurfDesc, App.Path & "\sky.bmp", 640, 240
  157.  
  158. ' set up the direct3d render target
  159. SurfDesc.lFlags = DDSD_HEIGHT Or DDSD_WIDTH Or DDSD_CAPS
  160. SurfDesc.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN Or DDSCAPS_3DDEVICE Or DDSCAPS_SYSTEMMEMORY
  161. ' create viewport
  162. SetRect DestRect, 0, 0, 640, 480
  163. SurfDesc.lWidth = DestRect.Right - DestRect.LEFT
  164. SurfDesc.lHeight = DestRect.Bottom - DestRect.Top
  165. ' create the render-target surface
  166. Set Scene = DX_DRAW.CreateSurface(SurfDesc)
  167. ' add color key
  168. AddColorKey Scene, vbBlack, vbBlack
  169. ' remember the dimensions of the render target
  170. With SrcRect
  171.     .LEFT = 0: .Top = 0
  172.     .Bottom = SurfDesc.lHeight
  173.     .Right = SurfDesc.lWidth
  174. End With
  175. 'create a DirectDrawClipper and attach it to the primary surface.
  176. 'Dim Clipper As DirectDrawClipper
  177. 'Set Clipper = DX_DRAW.CreateClipper(0)
  178. 'Clipper.SetHWnd Form1.hwnd
  179. 'Scene.SetClipper Clipper
  180.  
  181. ' create the z-buffer and attach to backbuffer
  182. Dim ddpfZBuffer As DDPIXELFORMAT
  183. Dim d3dEnumPFs As Direct3DEnumPixelFormats
  184.  
  185. Set DX_3D = DX_DRAW.GetDirect3D
  186. Set d3dEnumPFs = DX_3D.GetEnumZBufferFormats("IID_IDirect3DRGBDevice")
  187.  
  188. Dim i As Long
  189.  
  190. For i = 1 To d3dEnumPFs.GetCount()
  191. d3dEnumPFs.GetItem i, ddpfZBuffer
  192. If ddpfZBuffer.lFlags = DDPF_ZBUFFER Then
  193.   Exit For
  194. End If
  195. Next i
  196.  
  197. SetRect DestRect, 0, 0, 640, 480
  198. ' Prepare and create the z-buffer surface.
  199. SurfDesc.lFlags = DDSD_CAPS Or DDSD_WIDTH Or DDSD_HEIGHT Or DDSD_PIXELFORMAT
  200. SurfDesc.ddsCaps.lCaps = DDSCAPS_ZBUFFER
  201. SurfDesc.lWidth = DestRect.Right - DestRect.LEFT
  202. SurfDesc.lHeight = DestRect.Bottom - DestRect.Top
  203. SurfDesc.ddpfPixelFormat = ddpfZBuffer
  204. SurfDesc.ddsCaps.lCaps = SurfDesc.ddsCaps.lCaps Or DDSCAPS_SYSTEMMEMORY
  205.  
  206. Set ZBuff = DX_DRAW.CreateSurface(SurfDesc)
  207.  
  208. ' attach the z-buffer to the back buffer
  209. Scene.AddAttachedSurface ZBuff
  210. End Sub
  211.  
  212. Sub UnloadSurfaces()
  213. ' remember to call this one
  214. Set BackBuffer = Nothing
  215. Set View = Nothing
  216.  
  217. '*** add app specific pics here ***
  218. End Sub
  219.  
  220. Sub CreateSurfaceFromFile(Surface As DirectDrawSurface7, SurfDesc As DDSURFACEDESC2, FileName As String, Width As Integer, Height As Integer)
  221. On Error GoTo LostFile
  222. ' loads a bitmap from a file and makes a pic from it
  223.      SurfDesc.lFlags = DDSD_CAPS Or DDSD_HEIGHT Or DDSD_WIDTH
  224.      SurfDesc.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN
  225.      SurfDesc.lWidth = Width
  226.      SurfDesc.lHeight = Height
  227.      Set Surface = DX_DRAW.CreateSurfaceFromFile(FileName, SurfDesc)
  228. Exit Sub
  229. LostFile:
  230. Debug.Print "File not found : " & FileName
  231. End Sub
  232.  
  233. Sub SetRect2(Box As RECT, LEFT As Integer, Top As Integer, Right As Integer, Bottom As Integer)
  234. ' creates a rect of the required size
  235.     Box.LEFT = LEFT
  236.     Box.Top = Top
  237.     Box.Right = Right
  238.     Box.Bottom = Bottom
  239. End Sub
  240.  
  241. Sub SetRect(Box As RECT, LEFT As Integer, Top As Integer, Width As Integer, Height As Integer)
  242. ' creates a rect of the required size
  243.     Box.LEFT = LEFT
  244.     Box.Top = Top
  245.     Box.Right = LEFT + Width
  246.     Box.Bottom = Top + Height
  247. End Sub
  248.  
  249. Function MakeRect2(LEFT As Integer, Top As Integer, Right As Integer, Bottom As Integer) As RECT
  250.     MakeRect2.LEFT = LEFT
  251.     MakeRect2.Top = Top
  252.     MakeRect2.Right = Right
  253.     MakeRect2.Bottom = Bottom
  254. End Function
  255.  
  256. Function MakeRect(LEFT As Integer, Top As Integer, Width As Integer, Height As Integer) As RECT
  257.     MakeRect.LEFT = LEFT
  258.     MakeRect.Top = Top
  259.     MakeRect.Right = LEFT + Width
  260.     MakeRect.Bottom = Top + Height
  261. End Function
  262.  
  263. Sub AddColorKey(Surface As DirectDrawSurface7, low As Long, high As Long)
  264. ' for masking sprites
  265. ColorKey.low = low
  266. ColorKey.high = high
  267. Surface.SetColorKey DDCKEY_SRCBLT, ColorKey
  268. End Sub
  269.  
  270. Sub HideTheCursor()
  271. Cur = ShowCursor(0)
  272. End Sub
  273.  
  274. Sub ShowTheCursor()
  275. ShowCursor Cur
  276. End Sub
  277.  
  278. Function JPEG2BMP(FileName As String, LoadPB As PictureBox, SavePB As PictureBox) As Boolean
  279. On Error GoTo FileMuffUp
  280.  
  281. LoadPB = LoadPicture(FileName & ".jpg")
  282. SavePB = LoadPB
  283. SavePicture SavePB.Picture, FileName & ".bmp"
  284. JPEG2BMP = True
  285. Exit Function
  286.  
  287. FileMuffUp:
  288. JPEG2BMP = False
  289. End Function
  290.  
  291.