home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / direct2a / frmgame.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-10-19  |  14.1 KB  |  433 lines

  1. VERSION 5.00
  2. Begin VB.Form frmGame 
  3.    BorderStyle     =   0  'None
  4.    Caption         =   "Engine01"
  5.    ClientHeight    =   5625
  6.    ClientLeft      =   2355
  7.    ClientTop       =   1620
  8.    ClientWidth     =   7065
  9.    Icon            =   "frmGame.frx":0000
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   375
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   471
  14. Attribute VB_Name = "frmGame"
  15. Attribute VB_GlobalNameSpace = False
  16. Attribute VB_Creatable = False
  17. Attribute VB_PredeclaredId = True
  18. Attribute VB_Exposed = False
  19. '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  20. 'DDRAW ENGINE 1
  21. 'Written by Jack Hoxley, based on an example by Microsoft
  22. 'EMAIL: JollyJeffers@GreenOnions.NetscapeOnline.co.uk
  23. 'WEB: HTTP://WWW.Parkstonemot.freeserve.co.uk/indexfw.htm
  24. 'Because a DirectX game takes a lot of work, i am only providing you with
  25. 'the tools and basic structure or an engine.
  26. 'To create your own game, you will need to make extensive changes to
  27. 'this example. I didn't want to give away a free game engine, as that would mean
  28. 'weeks of work for no reward other than someone changing the name and re-releasing it.
  29. 'There are several  bugs in this code, such as the player going out of site at some points.
  30. 'You'll have to fix these, i'm doing it on my copy, you'll just have to work it out yourself.....
  31. 'Please Email me with any helpful hints, but no requests for source code. I don't mind explaining/
  32. 'helping, but i refuse to do the work for you.
  33. 'There are several things that you'll need, the main one is levels. As the editor is quite
  34. 'complex i might decide to keep it rather than give it away. To make levels, you will need
  35. 'to create a loading sub. Examine the one supplied, either work out how to save it or re-write your
  36. 'own one.
  37. '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  38. Option Explicit
  39. Dim binit As Boolean
  40. '~~~~~DIRECT DRAW~~~~~
  41. Dim dx As New DirectX7
  42. Dim dd As DirectDraw7
  43. Dim lakesurf As DirectDrawSurface7
  44. Dim Bloksurf As DirectDrawSurface7
  45. Dim FruitSurf As DirectDrawSurface7
  46. Dim primary As DirectDrawSurface7
  47. Dim backbuffer As DirectDrawSurface7
  48. Dim ddsd1 As DDSURFACEDESC2
  49. Dim ddsd2 As DDSURFACEDESC2 'Gradient
  50. Dim ddsd3 As DDSURFACEDESC2 'blocks
  51. Dim ddsd4 As DDSURFACEDESC2 'used
  52. Dim ddsd5 As DDSURFACEDESC2 'Fruit
  53. Dim spriteWidth As Integer
  54. Dim spriteHeight As Integer
  55. Dim cols As Integer
  56. Dim rows As Integer
  57. Dim row As Integer
  58. Dim col As Integer
  59. Dim currentFrame As Integer
  60. Dim brunning As Boolean
  61. Dim CurModeActiveStatus As Boolean
  62. Dim bRestore As Boolean
  63. '~~~~~DIRECT INPUT~~~~~
  64. Dim di As DirectInput
  65. Dim diDEV As DirectInputDevice
  66. Dim diState As DIKEYBOARDSTATE
  67. Dim iKeyCounter As Integer
  68. Sub Center_Screen()
  69. SBox.X = (P1.X * 32) - 304
  70. SBox.Y = (P1.Y * 32) - 224
  71. 'If SBox.X <= 0 Then SBox.X = 0
  72. 'If SBox.X >= 1744 Then SBox.X = 1744
  73. 'If SBox.Y >= 1056 Then SBox.Y = 1056
  74. 'If SBox.Y <= 0 Then SBox.Y = 0
  75. If SBox.X <= 0 Then SBox.X = 0
  76. If SBox.Y <= 0 Then SBox.Y = 0
  77. '-------------------------------------------------
  78. If SBox.X >= 1408 Then SBox.X = 1408
  79. If SBox.Y >= 1056 Then SBox.Y = 1056
  80. End Sub
  81. Sub Check_Keys()
  82. diDEV.GetDeviceStateKeyboard diState
  83. For iKeyCounter = 0 To 255
  84. If diState.key(iKeyCounter) <> 0 Then
  85. Select Case iKeyCounter
  86.     Case 1  'escape
  87.     EndIt
  88.     Case 28 'return on keyboard
  89.     Case 59 'F1 info
  90.     If ShowInfo = True Then
  91.     ShowInfo = False
  92.     Else
  93.     ShowInfo = True
  94.     End If
  95.     Case 60 'F2 debug
  96.     If ShowDebug = True Then
  97.     ShowDebug = False
  98.     Else
  99.     ShowDebug = True
  100.     End If
  101.     Case 200 'up
  102.     If P1.Y > 0 Then
  103.     P1.Y = P1.Y - 1
  104.     End If
  105.     Case 203 'Left
  106.     If P1.X > 0 Then
  107.     P1.X = P1.X - 1
  108.     End If
  109.     Case 205 'Right
  110.     If P1.X < 63 Then
  111.     P1.X = P1.X + 1
  112.     End If
  113.     Case 208 'down
  114.     If P1.Y < 47 Then
  115.     P1.Y = P1.Y + 1
  116.     End If
  117. End Select
  118. End If
  119. End Sub
  120. Sub Draw_Gradient()
  121. On Error Resume Next
  122. S.R = GStart.R
  123. S.G = GStart.G
  124. S.B = GStart.B
  125. E.R = GEnd.R
  126. E.G = GEnd.G
  127. E.B = GEnd.B
  128. Dim d As Colour
  129.     d.R = (E.R - S.R) / 1536 'where.ScaleWidth
  130.     d.G = (E.G - S.G) / 1536 'where.ScaleWidth
  131.     d.B = (E.B - S.B) / 1536 'where.ScaleWidth
  132. Dim c As Colour
  133. c.R = S.R: c.G = S.G: c.B = S.B
  134. Dim xG As Integer
  135. For xG = 0 To 1536 'where.ScaleWidth
  136.     lakesurf.SetForeColor RGB(c.R, c.G, c.B)
  137.     lakesurf.DrawLine 0, xG, 2048, xG
  138.     c.R = c.R + d.R
  139.     c.G = c.G + d.G
  140.     c.B = c.B + d.B
  141. Next xG
  142. End Sub
  143. Sub Draw_Level()
  144. Dim RetVal As Long
  145. Dim rBlok As RECT
  146. rBlok.Top = 0
  147. rBlok.Bottom = 32
  148. Dim X As Integer, Y As Integer
  149. For X = 0 To 63
  150.     For Y = 0 To 47
  151.         If Floor(X, Y) <> 0 Then
  152.         rBlok.Left = Floor(X, Y) * 32
  153.         rBlok.Right = rBlok.Left + 32
  154.         RetVal = lakesurf.BltFast(X * 32, Y * 32, Bloksurf, rBlok, DDBLTFAST_SRCCOLORKEY)
  155.         End If
  156.     Next Y
  157. Next X
  158. End Sub
  159. Sub End_Level()
  160.     levelnum = levelnum + 1
  161.     Load_Level (levelnum)
  162.     Draw_Gradient
  163.     Draw_Level
  164. End Sub
  165. Public Function GetTextColour() As String
  166. Select Case UCase(txtColour)
  167.         Case "BLACK"
  168.             GetTextColour = vbBlack
  169.         Case "BLUE"
  170.             GetTextColour = vbBlue
  171.         Case "GREEN"
  172.             GetTextColour = vbGreen
  173.         Case "GREY"
  174.             GetTextColour = "vbgrey"
  175.         Case "RED"
  176.             GetTextColour = vbRed
  177.         Case "WHITE"
  178.             GetTextColour = vbWhite
  179.         Case "YELLOW"
  180.             GetTextColour = vbYellow
  181. End Select
  182. End Function
  183. Sub Init()
  184. '    On Local Error GoTo errOut
  185.     Dim file As String
  186.     Set dd = dx.DirectDrawCreate("")
  187.     Me.Show
  188.     'indicate that we dont need to change display depth
  189.     Call dd.SetCooperativeLevel(Me.hWnd, DDSCL_FULLSCREEN Or DDSCL_ALLOWMODEX Or DDSCL_EXCLUSIVE)
  190.     Call dd.SetDisplayMode(640, 480, 24, 0, DDSDM_DEFAULT)
  191.         
  192.     'get the screen surface and create a back buffer too
  193.     ddsd1.lFlags = DDSD_CAPS Or DDSD_BACKBUFFERCOUNT
  194.     ddsd1.ddsCaps.lCaps = DDSCAPS_PRIMARYSURFACE Or DDSCAPS_FLIP Or DDSCAPS_COMPLEX
  195.     ddsd1.lBackBufferCount = 1
  196.     Set primary = dd.CreateSurface(ddsd1)
  197.     'Get the backbuffer
  198.     Dim caps As DDSCAPS2
  199.     caps.lCaps = DDSCAPS_BACKBUFFER
  200.     Set backbuffer = primary.GetAttachedSurface(caps)
  201.     backbuffer.GetSurfaceDesc ddsd4
  202.          
  203.     'Create DrawableSurface class form backbuffer
  204.     backbuffer.SetFontTransparency True
  205.     backbuffer.SetForeColor vbGreen
  206.     levelnum = 0
  207.     ' init the surfaces
  208.     InitSurfaces
  209.     End_Level
  210.     '~~~~~DIRECT INPUT~~~~~
  211.     Set di = dx.DirectInputCreate()
  212.     Set diDEV = di.CreateDevice("GUID_SysKeyboard")
  213.     diDEV.SetCommonDataFormat DIFORMAT_KEYBOARD
  214.     diDEV.SetCooperativeLevel Me.hWnd, DISCL_BACKGROUND Or DISCL_NONEXCLUSIVE
  215.     diDEV.Acquire
  216.     '~~~~~END INPUT~~~~~
  217.     binit = True
  218.     brunning = True
  219.     Do While brunning
  220.         Check_Keys
  221.         Center_Screen
  222.         blt
  223.         DoEvents
  224.     Loop
  225. errOut:
  226.     EndIt
  227. End Sub
  228. Sub InitSurfaces()
  229.     Set lakesurf = Nothing
  230.     Set Bloksurf = Nothing
  231.     FindMediaDir "Dirt.bmp"
  232.     'load the bitmap into a surface - lake
  233.     ddsd2.lFlags = DDSD_CAPS Or DDSD_HEIGHT Or DDSD_WIDTH
  234.     ddsd2.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN
  235.     ddsd2.lWidth = 2048
  236.     ddsd2.lHeight = 1536
  237.     Set lakesurf = dd.CreateSurface(ddsd2)
  238.         
  239.     ddsd3.lFlags = DDSD_CAPS Or DDSD_HEIGHT Or DDSD_WIDTH
  240.     ddsd3.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN
  241.     ddsd3.lWidth = 320
  242.     ddsd3.lHeight = 32
  243.     Set Bloksurf = dd.CreateSurfaceFromFile("Dirt.bmp", ddsd3)
  244.     ddsd5.lFlags = DDSD_CAPS Or DDSD_HEIGHT Or DDSD_WIDTH
  245.     ddsd5.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN
  246.     ddsd5.lWidth = 320
  247.     ddsd5.lHeight = 32
  248.     Set FruitSurf = dd.CreateSurfaceFromFile("Fruit.bmp", ddsd5)
  249.     'use black for transparent color key which is on
  250.     'the source bitmap -> use src keying
  251.     Dim key As DDCOLORKEY
  252.     key.low = 0
  253.     key.high = 0
  254.     Bloksurf.SetColorKey DDCKEY_SRCBLT, key
  255.     FruitSurf.SetColorKey DDCKEY_SRCBLT, key
  256. End Sub
  257. Sub blt()
  258.     On Local Error GoTo errOut
  259.     If binit = False Then Exit Sub
  260.     Dim ddrval As Long
  261.     Static i As Integer
  262.     Dim rBack As RECT
  263.     Dim rLake As RECT
  264.     Dim rSprite As RECT
  265.     Dim rSprite2 As RECT
  266.     Dim rPrim As RECT
  267.     Dim rFruit As RECT
  268.     Static a As Single
  269.     Static X As Single
  270.     Static Y As Single
  271.     Static t As Single
  272.     Static t2 As Single
  273.     Static tLast As Single
  274.     Static fps As Single
  275.     ' this will keep us from trying to blt in case we lose the surfaces (alt-tab)
  276.     bRestore = False
  277.     Do Until ExModeActive
  278.         DoEvents
  279.         bRestore = True
  280.     Loop
  281.     ' if we lost and got back the surfaces, then restore them
  282.     DoEvents
  283.     If bRestore Then
  284.         bRestore = False
  285.         dd.RestoreAllSurfaces
  286.         InitSurfaces ' must init the surfaces again if they we're lost
  287.     End If
  288.     'get the area of the screen where our window is
  289.     rBack.Bottom = ddsd4.lHeight
  290.     rBack.Right = ddsd4.lWidth
  291.     'get the area of the bitmap we want ot blt
  292.     rLake.Left = SBox.X
  293.     rLake.Top = SBox.Y
  294.     rLake.Bottom = SBox.Y + 480 'ddsd2.lHeight
  295.     rLake.Right = SBox.X + 640 'ddsd2.lWidth
  296.     'blt to the backbuffer from our  surface to
  297.     'the screen surface such that our bitmap
  298.     'appears over the window
  299.     ddrval = backbuffer.BltFast(0, 0, lakesurf, rLake, DDBLTFAST_WAIT)
  300.     'DRAW FRUIT
  301.     rFruit.Top = 0
  302.     rFruit.Bottom = 32
  303.     rFruit.Left = 0
  304.     rFruit.Right = 32
  305.     ddrval = backbuffer.BltFast((P1.X * 32), (P1.Y * 32), FruitSurf, rFruit, DDBLTFAST_WAIT) ', DDBLTFAST_SRCCOLORKEY)
  306.     'Calculate the frame rate
  307.     If i = 30 Then
  308.         If tLast <> 0 Then fps = 30 / (Timer - tLast)
  309.         tLast = Timer
  310.         i = 0
  311.     End If
  312.     i = i + 1
  313.     Dim TXTc As String
  314.     TXTc = GetTextColour()
  315.     If TXTc = "vbgrey" Then
  316.     backbuffer.SetForeColor RGB(128, 128, 128)
  317.     Else
  318.     backbuffer.SetForeColor TXTc
  319.     End If
  320.     If ShowInfo = True Then
  321.     Call backbuffer.DrawText(10, 10, "-----INFORMATION-----", False)
  322.     Call backbuffer.DrawText(10, 25, "LevelName: " & LevelName, False)
  323.     Call backbuffer.DrawText(10, 40, "Level Number: " & levelnum, False)
  324.     Call backbuffer.DrawText(10, 55, "Creator: " & Creator, False)
  325.     Call backbuffer.DrawText(10, 70, "Points: " & Points, False)
  326.     End If
  327.     If ShowDebug = True Then
  328.     Call backbuffer.DrawText(10, 85, "-----DEBUG-----", False)
  329.     Call backbuffer.DrawText(10, 100, "Resolution: 640x480", False)
  330.     Call backbuffer.DrawText(10, 115, "Colour Depth: 16bit", False)
  331.     Call backbuffer.DrawText(10, 130, "Frames Per Second: " + Format$(fps, "#.0"), False)
  332.     Call backbuffer.DrawText(10, 145, "Player X=" & P1.X & " (" & (P1.X * 32) & ") Player Y=" & P1.Y & " (" & (P1.Y * 32) & ")", False)
  333.     Call backbuffer.DrawText(10, 160, "SBox.X=" & SBox.X & "  SBox.Y=" & SBox.Y, False)
  334.     Call backbuffer.DrawText(10, 175, "Press Escape to Exit", False)
  335.     End If
  336.     'flip the back buffer to the screen
  337.     primary.Flip Nothing, DDFLIP_WAIT
  338. errOut:
  339. End Sub
  340. Sub EndIt()
  341.     Call dd.RestoreDisplayMode
  342.     Call dd.SetCooperativeLevel(Me.hWnd, DDSCL_NORMAL)
  343.     End
  344. End Sub
  345. Sub Load_Level(levelnum As Integer)
  346. Dim Filename As String
  347. Filename = App.Path & "\levels\" & levelnum & ".FW2"
  348. Dim FileToBeLoaded As String
  349. Dim TheLineThatIsScrewed As String
  350. Dim IntD1 As Integer, IntD2 As Integer
  351. Dim X2 As Integer, Y2 As Integer
  352. Open Filename For Input As #2
  353. Input #2, FileToBeLoaded
  354. Close #2
  355. IntD1 = InStr(1, FileToBeLoaded, Chr$(13))
  356. IntD2 = InStr(IntD1 + 1, FileToBeLoaded, Chr$(13))
  357. TheLineThatIsScrewed = Mid$(FileToBeLoaded, IntD1, IntD2 - IntD1)
  358. LevelName = Right(TheLineThatIsScrewed, Len(TheLineThatIsScrewed) - 1)
  359. IntD1 = IntD2 + 1
  360. IntD2 = InStr(IntD1, FileToBeLoaded, Chr$(13))
  361. Creator = Mid$(FileToBeLoaded, IntD1, IntD2 - IntD1)
  362. IntD1 = IntD2 + 1
  363. IntD2 = InStr(IntD1, FileToBeLoaded, Chr$(13))
  364. Theme = Mid$(FileToBeLoaded, IntD1, IntD2 - IntD1)
  365. IntD1 = IntD2 + 1
  366. IntD2 = InStr(IntD1, FileToBeLoaded, Chr$(13))
  367. txtColour = Mid$(FileToBeLoaded, IntD1, IntD2 - IntD1)
  368. IntD1 = IntD2 + 1
  369. IntD2 = InStr(IntD1, FileToBeLoaded, Chr$(13))
  370. Password = Mid$(FileToBeLoaded, IntD1, IntD2 - IntD1)
  371. IntD1 = IntD2 + 1
  372. IntD2 = InStr(IntD1, FileToBeLoaded, Chr$(13))
  373. GStart.R = Mid$(FileToBeLoaded, IntD1, IntD2 - IntD1)
  374. IntD1 = IntD2 + 1
  375. IntD2 = InStr(IntD1, FileToBeLoaded, Chr$(13))
  376. GStart.G = Mid$(FileToBeLoaded, IntD1, IntD2 - IntD1)
  377. IntD1 = IntD2 + 1
  378. IntD2 = InStr(IntD1, FileToBeLoaded, Chr$(13))
  379. GStart.B = Mid$(FileToBeLoaded, IntD1, IntD2 - IntD1)
  380. IntD1 = IntD2 + 1
  381. IntD2 = InStr(IntD1, FileToBeLoaded, Chr$(13))
  382. GEnd.R = Mid$(FileToBeLoaded, IntD1, IntD2 - IntD1)
  383. IntD1 = IntD2 + 1
  384. IntD2 = InStr(IntD1, FileToBeLoaded, Chr$(13))
  385. GEnd.G = Mid$(FileToBeLoaded, IntD1, IntD2 - IntD1)
  386. IntD1 = IntD2 + 1
  387. IntD2 = InStr(IntD1, FileToBeLoaded, Chr$(13))
  388. GEnd.B = Mid$(FileToBeLoaded, IntD1, IntD2 - IntD1)
  389. For X2 = 0 To 63
  390. For Y2 = 0 To 47
  391. IntD1 = IntD2 + 1
  392. IntD2 = InStr(IntD1, FileToBeLoaded, Chr$(13))
  393. Floor(X2, Y2) = Mid$(FileToBeLoaded, IntD1, IntD2 - IntD1)
  394. Next Y2
  395. Next X2
  396. For X2 = 0 To 63
  397. For Y2 = 0 To 47
  398. IntD1 = IntD2 + 1
  399. IntD2 = InStr(IntD1, FileToBeLoaded, Chr$(13))
  400. Value(X2, Y2) = Mid$(FileToBeLoaded, IntD1, IntD2 - IntD1)
  401. Next Y2
  402. Next X2
  403. End Sub
  404. Private Sub Form_Load()
  405.     Init
  406. End Sub
  407. Private Sub Form_Paint()
  408.     blt
  409. End Sub
  410. Function ExModeActive() As Boolean
  411.     Dim TestCoopRes As Long
  412.     TestCoopRes = dd.TestCooperativeLevel
  413.     If (TestCoopRes = DD_OK) Then
  414.         ExModeActive = True
  415.     Else
  416.         ExModeActive = False
  417.     End If
  418. End Function
  419. Sub FindMediaDir(sFile As String)
  420.     On Local Error Resume Next
  421.     If Dir$(sFile) <> "" Then Exit Sub
  422.     If Mid$(App.Path, 2, 1) = ":" Then
  423.         ChDrive Mid$(App.Path, 1, 1)
  424.     End If
  425.     ChDir App.Path
  426.     If Dir$(sFile) = "" Then
  427.         ChDir "..\media"
  428.     End If
  429.     If Dir$(sFile) = "" Then
  430.         ChDir "..\..\media"
  431.     End If
  432. End Sub
  433.