PDA

Click to See Complete Forum and Search --> : VB - DirectX7


Algar
Jun 24th, 2003, 09:49 PM
I've made a DirectX7 tutorial and was wondering if you guys could comment on this. Thanks


''''''''''''''''''''''''''''''''''''''''''''
'DirectX 7 Tutorial By B-Rabbit '
'This tutorial explains how to set up DX7, '
'and the basics of loading images, and '
'how to set transparencies in sprites '
''''''''''''''''''''''''''''''''''''''''''''

Option Explicit

'These are the two most important objects in
'DirectX7. You must use these.

Private DX As New DirectX7
Private DDraw As DirectDraw7

'''''''''''''''''''''''''''''
'Here's everything w/DInput'
Private di As DirectInput '
Private diDEV As DirectInputDevice '
Private diState As DIKEYBOARDSTATE '
'''''''''''''''''''''''''''''End of DInput

'Now these are the buffers - one is the primary
'surface, one is the backbuffer, and one is our
'picture we are going to load. Our picture is an array
'so if we have to have different char directions

Private ddsPRIMARY As DirectDrawSurface7
Private ddsBACKBUFFER As DirectDrawSurface7
Private ddsSKATER(1) As DirectDrawSurface7 'We have
'2 objects, ddsSKATER(0) and ddsSKATER(1)

'Now, we must describe each object so its
'width and height is properly set

Private ddsdPRIMARY As DDSURFACEDESC2
Private ddsdSKATER As DDSURFACEDESC2

'Now we must position our sprites, and so we can index them
Private SpritePosX As Single
Private SpritePosY As Single
Private SpriteNum As Byte 'This number will be either
'zero or 1, so don't use anything large

'To stop running the game
Private bFinished As Boolean

Private Sub Form_Load()

Set DDraw = DX.DirectDrawCreate("") '"" Means the default driver
Set di = DX.DirectInputCreate()
Set diDEV = di.CreateDevice("GUID_SysKeyboard")

diDEV.SetCommonDataFormat DIFORMAT_KEYBOARD
diDEV.SetCooperativeLevel Me.hWnd, DISCL_BACKGROUND Or DISCL_NONEXCLUSIVE
diDEV.Acquire

DDraw.SetCooperativeLevel Me.hWnd, DDSCL_FULLSCREEN Or DDSCL_ALLOWMODEX Or DDSCL_EXCLUSIVE
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.
'16 bit isn't as good as 32 bit

ddsdPRIMARY.lFlags = DDSD_CAPS Or DDSD_BACKBUFFERCOUNT
ddsdPRIMARY.ddsCaps.lCaps = DDSCAPS_PRIMARYSURFACE Or DDSCAPS_FLIP Or DDSCAPS_COMPLEX
ddsdPRIMARY.lBackBufferCount = 1
'Now we create the primary buffer
Set ddsPRIMARY = DDraw.CreateSurface(ddsdPRIMARY)

Dim CAPS As DDSCAPS2
CAPS.lCaps = DDSCAPS_BACKBUFFER
'Now lets create our backbuffer
Set ddsBACKBUFFER = ddsPRIMARY.GetAttachedSurface(CAPS)

LoadGraphics

'Lets set SpritePosX and SpritePosY
SpritePosY = 200
SpritePosX = 20
Do Until bFinished
DoEvents 'A tight loop is bad.....
CheckKeys 'Make sure you check what your pressing before you BLT,
'because if you check after, you'll be 1/60th a frame slower.
Render
Loop

EndGame
End Sub
Private Sub EndGame()

Dim n As Byte

Call DDraw.SetCooperativeLevel(Me.hWnd, DDSCL_NORMAL) 'Give everything back to windows
Call DDraw.RestoreDisplayMode

For n = 0 To UBound(ddsSKATER)
Set ddsSKATER(n) = Nothing
Next n
Set ddsBACKBUFFER = Nothing
Set ddsPRIMARY = Nothing
Set DDraw = Nothing
Set DX = Nothing

Unload Me
End Sub
Private Sub LoadGraphics()

Dim n As Byte
Dim cKEY As DDCOLORKEY

ddsdSKATER.lFlags = DDSD_CAPS Or DDSD_WIDTH Or DDSD_HEIGHT
ddsdSKATER.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN
ddsdSKATER.lHeight = 32
ddsdSKATER.lWidth = 21

Set ddsSKATER(0) = DDraw.CreateSurfaceFromFile(App.Path & "\SkaterRight.bmp", ddsdSKATER)
Set ddsSKATER(1) = DDraw.CreateSurfaceFromFile(App.Path & "\SkaterLeft.bmp", ddsdSKATER)
End Sub


cKEY.low = RGB(0, 0, 0) 'Now, you can change the RGB in
cKEY.high = RGB(0, 0, 0) 'BltColorFill, and it'll look normal

For n = LBound(ddsSKATER) To UBound(ddsSKATER)
ddsSKATER(n).SetColorKey DDCKEY_SRCBLT, cKEY 'Set color key
Next n
Private Sub Render()

Dim rBack As RECT

ddsBACKBUFFER.BltColorFill rBack, RGB(0, 0, 0)
ddsBACKBUFFER.BltFast SpritePosX, SpritePosY, ddsSKATER(SpriteNum), rBack, DDBLTFAST_WAIT Or DDBLTFAST_SRCCOLORKEY

ddsPRIMARY.Flip ddsBACKBUFFER, DDFLIP_WAIT 'Flip
End Sub
Private Sub CheckKeys()

diDEV.GetDeviceStateKeyboard diState 'Get our keyboard state

If diState.Key(205) <> 0 Then '205 is going right
SpriteNum = 0
SpritePosX = SpritePosX + 1
ElseIf diState.Key(203) Then '203 is going left
SpriteNum = 1
SpritePosX = SpritePosX - 1
ElseIf diState.Key(1) Then '1 is escape
bFinished = True
End If

If SpritePosX >= 319 Then SpritePosX = 0 'So we don't go off the screen
If SpritePosX < 0 Then SpritePosX = 0
End Sub


~B-Rabbit

Lee_S
Jun 26th, 2003, 08:50 PM
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

Algar
Jun 26th, 2003, 09:34 PM
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.