home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / sources / chapter04 / DrawTile / Form1.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  2004-11-06  |  5.8 KB  |  182 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 4 - DrawTile program
  20. '---------------------------------------------------------------
  21. 'make sure every variable is declared
  22. Option Explicit
  23. 'make all arrays start with 0 instead of 1
  24. Option Base 0
  25. 'customize the program here
  26. Const SCREENWIDTH As Long = 800
  27. Const SCREENHEIGHT As Long = 600
  28. Const FULLSCREEN As Boolean = False
  29. Const C_BLACK As Long = &H0
  30. Const C_RED As Long = &HFF0000
  31. 'the DirectX objects
  32. Dim dx As DirectX8
  33. Dim d3d As Direct3D8
  34. Dim d3dx As New D3DX8
  35. Dim dispmode As D3DDISPLAYMODE
  36. Dim d3dpp As D3DPRESENT_PARAMETERS
  37. Dim d3ddev As Direct3DDevice8
  38. 'some surfaces
  39. Dim backbuffer As Direct3DSurface8
  40. Dim castle As Direct3DSurface8
  41. Private Sub Form_Load()
  42.     'set up the main form
  43.     Form1.Caption = "DrawTile"
  44.     Form1.ScaleMode = 3
  45.     Form1.width = Screen.TwipsPerPixelX * (SCREENWIDTH + 12)
  46.     Form1.height = Screen.TwipsPerPixelY * (SCREENHEIGHT + 30)
  47.     Form1.Show
  48.     'initialize Direct3D
  49.     InitDirect3D Me.hwnd, SCREENWIDTH, SCREENHEIGHT, FULLSCREEN
  50.     'get reference to the back buffer
  51.     Set backbuffer = d3ddev.GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO)
  52.     'load the bitmap file
  53.     Set castle = LoadSurface(App.Path & "\castle.bmp", 1024, 1024)
  54. End Sub
  55. Public Sub InitDirect3D(ByVal hwnd As Long, ByVal lWidth As Long, ByVal lHeight As Long, ByVal bFullscreen As Boolean)
  56.     'catch any errors here
  57.     On Local Error GoTo fatal_error
  58.     'create the DirectX object
  59.     Set dx = New DirectX8
  60.     'create the Direct3D object
  61.     Set d3d = dx.Direct3DCreate()
  62.     If d3d Is Nothing Then
  63.         MsgBox "Error initializing Direct3D!"
  64.         Shutdown
  65.     End If
  66.     'tell D3D to use the current color depth
  67.     d3d.GetAdapterDisplayMode D3DADAPTER_DEFAULT, dispmode
  68.     'set the display settings used to create the device
  69.     Dim d3dpp As D3DPRESENT_PARAMETERS
  70.     d3dpp.hDeviceWindow = hwnd
  71.     d3dpp.BackBufferCount = 1
  72.     d3dpp.BackBufferWidth = lWidth
  73.     d3dpp.BackBufferHeight = lHeight
  74.     d3dpp.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC
  75.     d3dpp.BackBufferFormat = dispmode.Format
  76.     'set windowed or fullscreen mode
  77.     If bFullscreen Then
  78.         d3dpp.Windowed = 0
  79.     Else
  80.         d3dpp.Windowed = 1
  81.     End If
  82.     'chapter 9
  83.     d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE
  84.     d3dpp.AutoDepthStencilFormat = D3DFMT_D32
  85.     'create the D3D primary device
  86.     Set d3ddev = d3d.CreateDevice( _
  87.         D3DADAPTER_DEFAULT, _
  88.         D3DDEVTYPE_HAL, _
  89.         hwnd, _
  90.         D3DCREATE_SOFTWARE_VERTEXPROCESSING, _
  91.         d3dpp)
  92.         
  93.     If d3ddev Is Nothing Then
  94.         MsgBox "Error creating the Direct3D device!"
  95.         Shutdown
  96.     End If
  97.     Exit Sub
  98. fatal_error:
  99.     MsgBox "Critical error in Start_Direct3D!"
  100.     Shutdown
  101. End Sub
  102. Private Function LoadSurface( _
  103.     ByVal filename As String, _
  104.     ByVal width As Long, _
  105.     ByVal height As Long) _
  106.     As Direct3DSurface8
  107.     On Local Error GoTo fatal_error
  108.     Dim surf As Direct3DSurface8
  109.     'return error by default
  110.     Set LoadSurface = Nothing
  111.     'create the new surface
  112.     Set surf = d3ddev.CreateImageSurface(width, height, dispmode.Format)
  113.     If surf Is Nothing Then
  114.         MsgBox "Error creating surface!"
  115.         Exit Function
  116.     End If
  117.     'load surface from file
  118.     d3dx.LoadSurfaceFromFile _
  119.         surf, _
  120.         ByVal 0, _
  121.         ByVal 0, _
  122.         filename, _
  123.         ByVal 0, _
  124.         D3DX_DEFAULT, _
  125.         0, _
  126.         ByVal 0
  127.         
  128.     If surf Is Nothing Then
  129.         MsgBox "Error loading " & filename & "!"
  130.         Exit Function
  131.     End If
  132.     'return the new surface
  133.     Set LoadSurface = surf
  134. fatal_error:
  135.     Exit Function
  136. End Function
  137. Private Sub DrawTile( _
  138.     ByRef source As Direct3DSurface8, _
  139.     ByVal sourcex As Long, _
  140.     ByVal sourcey As Long, _
  141.     ByVal width As Long, _
  142.     ByVal height As Long, _
  143.     ByVal destx As Long, _
  144.     ByVal desty As Long)
  145.     'create a RECT to describe the source image
  146.     Dim sourceRect As DxVBLibA.RECT
  147.     'set the upper left corner of the source image
  148.     sourceRect.Left = sourcex
  149.     sourceRect.Top = sourcey
  150.     'set the bottom right corner of the source image
  151.     sourceRect.Right = sourcex + width
  152.     sourceRect.bottom = sourcey + height
  153.     'create a POINT to define the destination
  154.     Dim point1 As DxVBLibA.point
  155.     'set the upper left corner of where to draw the image
  156.     point1.x = destx
  157.     point1.y = desty
  158.     'draw the source bitmap tile image
  159.     d3ddev.CopyRects source, sourceRect, 1, backbuffer, point1
  160. End Sub
  161. Private Sub Form_Paint()
  162.     'clear the background of the screen
  163.     d3ddev.Clear 0, ByVal 0, D3DCLEAR_TARGET, C_BLACK, 1, 0
  164.     'draw the castle bitmap "tile" image
  165.     DrawTile castle, 0, 0, 511, 511, 25, 25
  166.     'send the back buffer to the screen
  167.     d3ddev.Present ByVal 0, ByVal 0, 0, ByVal 0
  168. End Sub
  169. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  170.     If KeyCode = 27 Then Shutdown
  171. End Sub
  172. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  173.     Shutdown
  174. End Sub
  175. Private Sub Shutdown()
  176.     Set castle = Nothing
  177.     Set d3ddev = Nothing
  178.     Set d3d = Nothing
  179.     Set dx = Nothing
  180.     End
  181. End Sub
  182.