home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / DX7_Textur488891152002.psc / ddwrapper.bas next >
Encoding:
BASIC Source File  |  2002-01-15  |  5.8 KB  |  197 lines

  1. Attribute VB_Name = "modGFX"
  2. Option Explicit
  3.  
  4. Public DX As New DirectX7
  5. Public DDraw As DirectDraw7
  6.  
  7. Public bFS As Boolean 'full screen flag
  8. Public hWnd As Long 'Handle to Window
  9.  
  10. 'The DD7 Surfaces
  11. Private Type DDSurf
  12.     ddSurface As DirectDrawSurface7
  13.     ddDescription As DDSURFACEDESC2
  14.     ddClipper As DirectDrawClipper
  15. End Type
  16.  
  17. Public DDSurfFileCount As Integer
  18. Public LoadSurface() As DDSurf
  19.  
  20. Public ddPrimary As DDSurf 'Primary Buffer - We always have a Primary buffer
  21. Public ddBuffer As DDSurf 'Back Buffer
  22.  
  23.  
  24. '    Example of Full screen
  25. '    1. Set the full screen flag to true for a direct draw full screen application
  26. '    DX_Draw_SetUp Me.hWnd, 800, 600, 16, True
  27. '    DDCreateSurface "c:\1.bmp"
  28. '    Draw 100, 10
  29.  
  30.  
  31. '    Example of Windowed
  32. '    1. Set the full screen flag to false for a direct draw windows application
  33. '    DX_Draw_SetUp Me.hWnd, 0, 0, 16, False
  34. '    DDCreateSurface "c:\1.bmp"
  35. '    Draw 100, 10
  36.  
  37.  
  38.  
  39. '---Function------
  40. 'DX_Draw_SetUp
  41. '-----------------
  42. 'mHandle = Handle to DC - E.G. form.hWnd
  43. 'mHeight = Screen Res height - E.G.600, 480
  44. 'mWidth  = Screen Res Width - E.G. 800,640
  45. 'mCDepth = Color Depth - E.G 16,24,32
  46.  
  47. Public Function DX_Draw_SetUp(mHandle As Long, mWidth As Integer, mHeight As Integer, mCDepth As Integer, Optional mFullScreen As Boolean)
  48.     On Error GoTo DDraw_SetUp_Error
  49.     
  50.     bFS = mFullScreen
  51.     hWnd = mHandle
  52.     'Create DD Obj...
  53.     Set DDraw = DX.DirectDrawCreate("")
  54.     
  55.     
  56.      If mFullScreen Then    'Full Screen Flag Set to true ... Set DD as full screen!
  57.             
  58.             
  59.             
  60.             
  61.             Call DDraw.SetCooperativeLevel(mHandle, DDSCL_FULLSCREEN Or DDSCL_ALLOWMODEX Or DDSCL_EXCLUSIVE)
  62.             DDraw.SetDisplayMode mWidth, mHeight, mCDepth, 0, DDSDM_DEFAULT
  63.                    
  64.             ddPrimary.ddDescription.lFlags = DDSD_CAPS Or DDSD_BACKBUFFERCOUNT
  65.             ddPrimary.ddDescription.ddsCaps.lCaps = DDSCAPS_PRIMARYSURFACE Or DDSCAPS_FLIP Or DDSCAPS_COMPLEX
  66.             ddPrimary.ddDescription.lBackBufferCount = 1
  67.         
  68.             Set ddPrimary.ddSurface = DDraw.CreateSurface(ddPrimary.ddDescription)
  69.     
  70.             Dim caps As DDSCAPS2
  71.             
  72.             caps.lCaps = DDSCAPS_BACKBUFFER
  73.     
  74.             Set ddBuffer.ddSurface = ddPrimary.ddSurface.GetAttachedSurface(caps)
  75.             ddBuffer.ddSurface.GetSurfaceDesc ddBuffer.ddDescription
  76.      
  77.             'Use black for transparent color key (&h0)
  78.             Dim key As DDCOLORKEY
  79.             
  80.             key.low = 0
  81.             key.high = 0
  82.             
  83.             ddBuffer.ddSurface.SetColorKey DDCKEY_SRCBLT, key
  84.         Else 'For DDraw Windowed
  85.             
  86.             
  87.             Call DDraw.SetCooperativeLevel(mHandle, DDSCL_NORMAL)
  88.         
  89.             ddPrimary.ddDescription.lFlags = DDSD_CAPS
  90.             ddPrimary.ddDescription.ddsCaps.lCaps = DDSCAPS_PRIMARYSURFACE
  91.     
  92.             Set ddPrimary.ddSurface = DDraw.CreateSurface(ddPrimary.ddDescription)
  93.         
  94.             'DD7 Clipper --- So we don't draw outside the window or hWnd we select as mHandle
  95.             Set ddPrimary.ddClipper = DDraw.CreateClipper(0)
  96.             ddPrimary.ddClipper.SetHWnd mHandle
  97.             ddPrimary.ddSurface.SetClipper ddPrimary.ddClipper
  98.             
  99.         
  100.         
  101.         End If
  102.         
  103.         Exit Function
  104.  
  105. 'Handles any errors
  106. DDraw_SetUp_Error:
  107.     MsgBox "An Error Occurred While Setting Up DirectDraw 7", 0, "Error"
  108.     End
  109.     
  110. End Function
  111.  
  112. Public Function DDCreateSurface(sFileName As String)
  113. On Error GoTo Err_DDCreateSurface
  114.  
  115.     ReDim Preserve LoadSurface(DDSurfFileCount)
  116.         
  117.     Set LoadSurface(DDSurfFileCount).ddSurface = Nothing
  118.     
  119.   
  120.     LoadSurface(DDSurfFileCount).ddDescription.lFlags = DDSD_CAPS Or DDSD_HEIGHT Or DDSD_WIDTH
  121.     LoadSurface(DDSurfFileCount).ddDescription.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN
  122.     LoadSurface(DDSurfFileCount).ddDescription.lWidth = 600
  123.     LoadSurface(DDSurfFileCount).ddDescription.lHeight = 600
  124.     
  125.     Set LoadSurface(DDSurfFileCount).ddSurface = DDraw.CreateSurfaceFromFile(sFileName, LoadSurface(DDSurfFileCount).ddDescription)
  126.     
  127.     DDSurfFileCount = DDSurfFileCount + 1
  128.  
  129.     Exit Function
  130.     
  131. Err_DDCreateSurface:
  132.     If Err = 91 Then
  133.         MsgBox "Direct Draw Must Be Set-Up Before Loading A Bitmap!", 0, "Error"
  134.         End
  135.     Else
  136.         MsgBox "" + Error$ + "   =  " & Err, 0, "Error"
  137.         End
  138.     End If
  139.     
  140.     
  141. End Function
  142.  
  143. Public Function Draw(x As Integer, y As Integer)
  144.     Dim srcRect As RECT
  145.     Dim dstRect As RECT
  146.     
  147.     srcRect.Left = 0
  148.     srcRect.Right = LoadSurface(DDSurfFileCount - 1).ddDescription.lWidth
  149.     srcRect.Top = 0
  150.     srcRect.Bottom = LoadSurface(DDSurfFileCount - 1).ddDescription.lHeight
  151.     
  152.     
  153.     If bFS = False Then 'Windowed
  154.         DX.GetWindowRect hWnd, dstRect
  155.     End If
  156.         
  157.     dstRect.Left = x
  158.     dstRect.Right = x + srcRect.Right
  159.     dstRect.Top = y
  160.     dstRect.Bottom = y + srcRect.Bottom
  161.     
  162.     If bFS = False Then 'Windowed
  163.         Call ddPrimary.ddSurface.Blt(dstRect, LoadSurface(DDSurfFileCount - 1).ddSurface, srcRect, DDBLTFAST_SRCCOLORKEY Or DDBLTFAST_WAIT)
  164.     Else
  165.         'Use the back buffer
  166.         DDClear 'Clear the back buffer
  167.         Call ddBuffer.ddSurface.Blt(dstRect, LoadSurface(DDSurfFileCount - 1).ddSurface, srcRect, DDBLTFAST_SRCCOLORKEY Or DDBLTFAST_WAIT)
  168.         Call ddPrimary.ddSurface.Flip(ddBuffer.ddSurface, DDFLIP_WAIT)
  169.     End If
  170.     
  171. End Function
  172.  
  173. Public Function DDRestore()
  174.     DDraw.RestoreAllSurfaces
  175. End Function
  176.  
  177.  
  178. Public Sub DDClear()
  179.  
  180.     Dim dstRect As RECT
  181.  
  182.     With dstRect
  183.         .Top = 0
  184.         .Bottom = Screen.Height
  185.         .Left = 0
  186.         .Right = Screen.Width
  187.     End With
  188.     
  189.     'Fill the entire backbuffer
  190.     ddBuffer.ddSurface.BltColorFill dstRect, 0
  191.     
  192.  
  193. End Sub
  194.  
  195.  
  196.  
  197.