home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / sources / chapter06 / ScrollScreen / Form1.frm (.txt) next >
Encoding:
Visual Basic Form  |  2004-11-06  |  7.0 KB  |  225 lines

  1. VERSION 5.00
  2. Begin VB.Form Form1 
  3.    Caption         =   "Form1"
  4.    ClientHeight    =   3195
  5.    ClientLeft      =   60
  6.    ClientTop       =   345
  7.    ClientWidth     =   4680
  8.    LinkTopic       =   "Form1"
  9.    ScaleHeight     =   3195
  10.    ScaleWidth      =   4680
  11.    StartUpPosition =   3  'Windows Default
  12. Attribute VB_Name = "Form1"
  13. Attribute VB_GlobalNameSpace = False
  14. Attribute VB_Creatable = False
  15. Attribute VB_PredeclaredId = True
  16. Attribute VB_Exposed = False
  17. '---------------------------------------------------------------
  18. ' Visual Basic Game Programming for Teens
  19. ' Chapter 6 - ScrollScreen program
  20. '---------------------------------------------------------------
  21. Private Declare Function GetTickCount Lib "kernel32" () As Long
  22. 'make sure every variable is declared
  23. Option Explicit
  24. 'make all arrays start with 0
  25. Option Base 0
  26. 'customize the program here
  27. Const SCREENWIDTH As Long = 800
  28. Const SCREENHEIGHT As Long = 600
  29. Const FULLSCREEN As Boolean = False
  30. Const GAMEWORLDWIDTH As Long = 1600
  31. Const GAMEWORLDHEIGHT As Long = 1152
  32. 'keyboard codes
  33. Const KEY_LEFT As Integer = 72
  34. Const KEY_RIGHT As Integer = 74
  35. Const KEY_UP As Integer = 76
  36. Const KEY_DOWN As Integer = 82
  37. 'the DirectX objects
  38. Dim dx As DirectX8
  39. Dim d3d As Direct3D8
  40. Dim d3dx As New D3DX8
  41. Dim dispmode As D3DDISPLAYMODE
  42. Dim d3dpp As D3DPRESENT_PARAMETERS
  43. Dim d3ddev As Direct3DDevice8
  44. 'some surfaces
  45. Dim backbuffer As Direct3DSurface8
  46. Dim gameworld As Direct3DSurface8
  47. 'scrolling values
  48. Const STEP As Long = 8
  49. Dim ScrollX As Long
  50. Dim ScrollY As Long
  51. Dim SpeedX As Integer
  52. Dim SpeedY As Integer
  53. Private Sub Form_Load()
  54.     'set up the main form
  55.     Form1.Caption = "ScrollScreen"
  56.     Form1.ScaleMode = 3
  57.     Form1.width = Screen.TwipsPerPixelX * (SCREENWIDTH + 12)
  58.     Form1.height = Screen.TwipsPerPixelY * (SCREENHEIGHT + 30)
  59.     Form1.Show
  60.     'initialize Direct3D
  61.     InitDirect3D Me.hwnd, SCREENWIDTH, SCREENHEIGHT, FULLSCREEN
  62.     'get reference to the back buffer
  63.     Set backbuffer = d3ddev.GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO)
  64.     'load the bitmap file
  65.     Set gameworld = LoadSurface(App.Path & "\gameworld.bmp", GAMEWORLDWIDTH, GAMEWORLDHEIGHT)
  66.     'this helps to keep a steady framerate
  67.     Dim start As Long
  68.     start = GetTickCount()
  69.     'main loop
  70.     Do While (True)
  71.         
  72.         'update the scrolling viewport
  73.         ScrollScreen
  74.         
  75.         'set the screen refresh to about 40 fps
  76.         If GetTickCount - start > 25 Then
  77.             d3ddev.Present ByVal 0, ByVal 0, 0, ByVal 0
  78.             start = GetTickCount
  79.             DoEvents
  80.         End If
  81.     Loop
  82. End Sub
  83. Public Sub InitDirect3D( _
  84.     ByVal hwnd As Long, _
  85.     ByVal lWidth As Long, _
  86.     ByVal lHeight As Long, _
  87.     ByVal bFullscreen As Boolean)
  88.     'catch any errors here
  89.     On Local Error GoTo fatal_error
  90.     'create the DirectX object
  91.     Set dx = New DirectX8
  92.     'create the Direct3D object
  93.     Set d3d = dx.Direct3DCreate()
  94.     If d3d Is Nothing Then
  95.         MsgBox "Error initializing Direct3D!"
  96.         Shutdown
  97.     End If
  98.     'tell D3D to use the current color depth
  99.     d3d.GetAdapterDisplayMode D3DADAPTER_DEFAULT, dispmode
  100.     'set the display settings used to create the device
  101.     Dim d3dpp As D3DPRESENT_PARAMETERS
  102.     d3dpp.hDeviceWindow = hwnd
  103.     d3dpp.BackBufferCount = 1
  104.     d3dpp.BackBufferWidth = lWidth
  105.     d3dpp.BackBufferHeight = lHeight
  106.     d3dpp.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC
  107.     d3dpp.BackBufferFormat = dispmode.Format
  108.     'set windowed or fullscreen mode
  109.     If bFullscreen Then
  110.         d3dpp.Windowed = 0
  111.     Else
  112.         d3dpp.Windowed = 1
  113.     End If
  114.     'chapter 9
  115.     d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE
  116.     d3dpp.AutoDepthStencilFormat = D3DFMT_D32
  117.     'create the D3D primary device
  118.     Set d3ddev = d3d.CreateDevice( _
  119.         D3DADAPTER_DEFAULT, _
  120.         D3DDEVTYPE_HAL, _
  121.         hwnd, _
  122.         D3DCREATE_SOFTWARE_VERTEXPROCESSING, _
  123.         d3dpp)
  124.         
  125.     If d3ddev Is Nothing Then
  126.         MsgBox "Error creating the Direct3D device!"
  127.         Shutdown
  128.     End If
  129.     Exit Sub
  130. fatal_error:
  131.     MsgBox "Critical error in Start_Direct3D!"
  132.     Shutdown
  133. End Sub
  134. Private Function LoadSurface( _
  135.     ByVal filename As String, _
  136.     ByVal width As Long, _
  137.     ByVal height As Long) _
  138.     As Direct3DSurface8
  139.     On Local Error GoTo fatal_error
  140.     Dim surf As Direct3DSurface8
  141.     'return error by default
  142.     Set LoadSurface = Nothing
  143.     'create the new surface
  144.     Set surf = d3ddev.CreateImageSurface(width, height, dispmode.Format)
  145.     If surf Is Nothing Then
  146.         MsgBox "Error creating surface!"
  147.         Exit Function
  148.     End If
  149.     'load surface from file
  150.     d3dx.LoadSurfaceFromFile _
  151.         surf, _
  152.         ByVal 0, _
  153.         ByVal 0, _
  154.         filename, _
  155.         ByVal 0, _
  156.         D3DX_DEFAULT, _
  157.         0, _
  158.         ByVal 0
  159.         
  160.     If surf Is Nothing Then
  161.         MsgBox "Error loading " & filename & "!"
  162.         Exit Function
  163.     End If
  164.     'return the new surface
  165.     Set LoadSurface = surf
  166. fatal_error:
  167.     Exit Function
  168. End Function
  169. Public Sub ScrollScreen()
  170.     'update horizontal scrolling position and speed
  171.     ScrollX = ScrollX + SpeedX
  172.     If (ScrollX < 0) Then
  173.         ScrollX = 0
  174.         SpeedX = 0
  175.     ElseIf ScrollX > GAMEWORLDWIDTH - SCREENWIDTH Then
  176.         ScrollX = GAMEWORLDWIDTH - SCREENWIDTH
  177.         SpeedX = 0
  178.     End If
  179.     'update vertical scrolling position and speed
  180.     ScrollY = ScrollY + SpeedY
  181.     If ScrollY < 0 Then
  182.         ScrollY = 0
  183.         SpeedY = 0
  184.     ElseIf ScrollY > GAMEWORLDHEIGHT - SCREENHEIGHT Then
  185.         ScrollY = GAMEWORLDHEIGHT - SCREENHEIGHT
  186.         SpeedY = 0
  187.     End If
  188.     'set dimensions of the source image
  189.     Dim r As DxVBLibA.RECT
  190.     r.Left = ScrollX
  191.     r.Top = ScrollY
  192.     r.Right = ScrollX + SCREENWIDTH
  193.     r.bottom = ScrollY + SCREENHEIGHT
  194.     'set the destination point
  195.     Dim point As DxVBLibA.point
  196.     point.X = 0
  197.     point.Y = 0
  198.     'draw the current game world view
  199.     d3ddev.CopyRects gameworld, r, 1, backbuffer, point
  200. End Sub
  201. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  202.     'hit ESC key to quit
  203.     If KeyCode = 27 Then Shutdown
  204. End Sub
  205. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  206.     'move mouse on left side to scroll left
  207.     If X < SCREENWIDTH / 2 Then SpeedX = -STEP
  208.     'move mouse on right side to scroll right
  209.     If X > SCREENWIDTH / 2 Then SpeedX = STEP
  210.     'move mouse on top half to scroll up
  211.     If Y < SCREENHEIGHT / 2 Then SpeedY = -STEP
  212.     'move mouse on bottom half to scroll down
  213.     If Y > SCREENHEIGHT / 2 Then SpeedY = STEP
  214. End Sub
  215. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  216.     Shutdown
  217. End Sub
  218. Private Sub Shutdown()
  219.     Set gameworld = Nothing
  220.     Set d3ddev = Nothing
  221.     Set d3d = Nothing
  222.     Set dx = Nothing
  223.     End
  224. End Sub
  225.