Results 1 to 3 of 3

Thread: VB - DirectX7

  1. #1

    Thread Starter
    Lively Member Algar's Avatar
    Join Date
    Jun 2003
    Location
    A place that never existed
    Posts
    127

    VB - DirectX7

    I've made a DirectX7 tutorial and was wondering if you guys could comment on this. Thanks

    VB Code:
    1. ''''''''''''''''''''''''''''''''''''''''''''
    2. 'DirectX 7 Tutorial By B-Rabbit            '
    3. 'This tutorial explains how to set up DX7, '
    4. 'and the basics of loading images, and     '
    5. 'how to set transparencies in sprites      '
    6. ''''''''''''''''''''''''''''''''''''''''''''
    7.  
    8. Option Explicit
    9.  
    10. 'These are the two most important objects in
    11. 'DirectX7. You must use these.
    12.  
    13. Private DX As New DirectX7
    14. Private DDraw As DirectDraw7
    15.  
    16. '''''''''''''''''''''''''''''
    17. 'Here's everything w/DInput'
    18. Private di As DirectInput '
    19. Private diDEV As DirectInputDevice '
    20. Private diState As DIKEYBOARDSTATE '
    21. '''''''''''''''''''''''''''''End of DInput
    22.  
    23. 'Now these are the buffers - one is the primary
    24. 'surface, one is the backbuffer, and one is our
    25. 'picture we are going to load. Our picture is an array
    26. 'so if we have to have different char directions
    27.  
    28. Private ddsPRIMARY As DirectDrawSurface7
    29. Private ddsBACKBUFFER As DirectDrawSurface7
    30. Private ddsSKATER(1) As DirectDrawSurface7 'We have
    31. '2 objects, ddsSKATER(0) and ddsSKATER(1)
    32.  
    33. 'Now, we must describe each object so its
    34. 'width and height is properly set
    35.  
    36. Private ddsdPRIMARY As DDSURFACEDESC2
    37. Private ddsdSKATER As DDSURFACEDESC2
    38.  
    39. 'Now we must position our sprites, and so we can index them
    40. Private SpritePosX As Single
    41. Private SpritePosY As Single
    42. Private SpriteNum As Byte 'This number will be either
    43.                           'zero or 1, so don't use anything large
    44.  
    45. 'To stop running the game
    46. Private bFinished As Boolean
    47.  
    48. Private Sub Form_Load()
    49.  
    50.     Set DDraw = DX.DirectDrawCreate("") '"" Means the default driver
    51.     Set di = DX.DirectInputCreate()
    52.     Set diDEV = di.CreateDevice("GUID_SysKeyboard")
    53.    
    54.     diDEV.SetCommonDataFormat DIFORMAT_KEYBOARD
    55.     diDEV.SetCooperativeLevel Me.hWnd, DISCL_BACKGROUND Or DISCL_NONEXCLUSIVE
    56.     diDEV.Acquire
    57.    
    58.     DDraw.SetCooperativeLevel Me.hWnd, DDSCL_FULLSCREEN Or DDSCL_ALLOWMODEX Or DDSCL_EXCLUSIVE
    59.     DDraw.SetDisplayMode 320, 240, 32, 0, DDSDM_DEFAULT 'Some people prefer 16 instead of 32. That will change the numbers to get a certain color from RGB.
    60.     '16 bit isn't as good as 32 bit
    61.    
    62.     ddsdPRIMARY.lFlags = DDSD_CAPS Or DDSD_BACKBUFFERCOUNT
    63.     ddsdPRIMARY.ddsCaps.lCaps = DDSCAPS_PRIMARYSURFACE Or DDSCAPS_FLIP Or DDSCAPS_COMPLEX
    64.     ddsdPRIMARY.lBackBufferCount = 1
    65.     'Now we create the primary buffer
    66.     Set ddsPRIMARY = DDraw.CreateSurface(ddsdPRIMARY)
    67.    
    68.     Dim CAPS As DDSCAPS2
    69.     CAPS.lCaps = DDSCAPS_BACKBUFFER
    70.     'Now lets create our backbuffer
    71.     Set ddsBACKBUFFER = ddsPRIMARY.GetAttachedSurface(CAPS)
    72.    
    73.     LoadGraphics
    74.    
    75.     'Lets set SpritePosX and SpritePosY
    76.     SpritePosY = 200
    77.     SpritePosX = 20
    78.     Do Until bFinished
    79.         DoEvents 'A tight loop is bad.....
    80.         CheckKeys 'Make sure you check what your pressing before you BLT,
    81.                   'because if you check after, you'll be 1/60th a frame slower.
    82.         Render
    83.     Loop
    84.    
    85.     EndGame
    86. End Sub
    87. Private Sub EndGame()
    88.    
    89.     Dim n As Byte
    90.    
    91.     Call DDraw.SetCooperativeLevel(Me.hWnd, DDSCL_NORMAL) 'Give everything back to windows
    92.     Call DDraw.RestoreDisplayMode
    93.    
    94.     For n = 0 To UBound(ddsSKATER)
    95.         Set ddsSKATER(n) = Nothing
    96.     Next n
    97.     Set ddsBACKBUFFER = Nothing
    98.     Set ddsPRIMARY = Nothing
    99.     Set DDraw = Nothing
    100.     Set DX = Nothing
    101.    
    102.     Unload Me
    103. End Sub
    104. Private Sub LoadGraphics()
    105.  
    106.     Dim n As Byte
    107.     Dim cKEY As DDCOLORKEY
    108.  
    109.     ddsdSKATER.lFlags = DDSD_CAPS Or DDSD_WIDTH Or DDSD_HEIGHT
    110.     ddsdSKATER.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN
    111.     ddsdSKATER.lHeight = 32
    112.     ddsdSKATER.lWidth = 21
    113.    
    114.     Set ddsSKATER(0) = DDraw.CreateSurfaceFromFile(App.Path & "\SkaterRight.bmp", ddsdSKATER)
    115.     Set ddsSKATER(1) = DDraw.CreateSurfaceFromFile(App.Path & "\SkaterLeft.bmp", ddsdSKATER)
    116. End Sub
    117.  
    118.    
    119.     cKEY.low = RGB(0, 0, 0) 'Now, you can change the RGB in
    120.     cKEY.high = RGB(0, 0, 0) 'BltColorFill, and it'll look normal
    121.  
    122.     For n = LBound(ddsSKATER) To UBound(ddsSKATER)
    123.         ddsSKATER(n).SetColorKey DDCKEY_SRCBLT, cKEY 'Set color key
    124.     Next n
    125. Private Sub Render()
    126.  
    127.     Dim rBack As RECT
    128.    
    129.     ddsBACKBUFFER.BltColorFill rBack, RGB(0, 0, 0)
    130.     ddsBACKBUFFER.BltFast SpritePosX, SpritePosY, ddsSKATER(SpriteNum), rBack, DDBLTFAST_WAIT Or DDBLTFAST_SRCCOLORKEY
    131.    
    132.     ddsPRIMARY.Flip ddsBACKBUFFER, DDFLIP_WAIT 'Flip
    133. End Sub
    134. Private Sub CheckKeys()
    135.  
    136.     diDEV.GetDeviceStateKeyboard diState 'Get our keyboard state
    137.    
    138.     If diState.Key(205) <> 0 Then '205 is going right
    139.         SpriteNum = 0
    140.         SpritePosX = SpritePosX + 1
    141.     ElseIf diState.Key(203) Then '203 is going left
    142.         SpriteNum = 1
    143.         SpritePosX = SpritePosX - 1
    144.     ElseIf diState.Key(1) Then '1 is escape
    145.         bFinished = True
    146.     End If
    147.    
    148.     If SpritePosX >= 319 Then SpritePosX = 0 'So we don't go off the screen
    149.     If SpritePosX < 0 Then SpritePosX = 0
    150. End Sub

    ~B-Rabbit
    Attached Files Attached Files

  2. #2
    Addicted Member Lee_S's Avatar
    Join Date
    Dec 2000
    Location
    New Zealand
    Posts
    250
    Hmm, well without upsetting you.. It's a decent commented example of setting up a DX game, but its not really what I would call a tutorial.

    I'd say you need to explain more of it, and show where things could differ, what options you have with different functions, what difference they make, etc.

    You're also just giving the user a lump of code and saying 'use this', rather than 'tutor'ing them.

    Wow this is more negative than i meant it to be. OK on the plus side! This is the codebank, so lumps of code is good! LOL

    Sorry :P
    Lee Saunders
    Win XP Professional : VB6 Enterprise / VB 2005 Express

    History admires the wise, but it elevates the brave.

  3. #3

    Thread Starter
    Lively Member Algar's Avatar
    Join Date
    Jun 2003
    Location
    A place that never existed
    Posts
    127
    No no, thats alright man. I wanted comments on it, negative or positive. But it's alright, cause thats what I really mean. To give a code example.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width