home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / moving / bitdemo1.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1995-06-17  |  19.0 KB  |  515 lines

  1. VERSION 2.00
  2. Begin Form Form1 
  3.    BackColor       =   &H00C0C0C0&
  4.    Caption         =   "BitBlt and Sprite Example"
  5.    ClientHeight    =   6045
  6.    ClientLeft      =   390
  7.    ClientTop       =   645
  8.    ClientWidth     =   5550
  9.    Height          =   6450
  10.    Left            =   330
  11.    LinkTopic       =   "Form1"
  12.    ScaleHeight     =   6045
  13.    ScaleWidth      =   5550
  14.    Top             =   300
  15.    Width           =   5670
  16.    Begin CommandButton btnSprites 
  17.       Caption         =   " &Animated Sprites"
  18.       Height          =   375
  19.       Left            =   3720
  20.       TabIndex        =   19
  21.       Top             =   120
  22.       Width           =   1665
  23.    End
  24.    Begin PictureBox picEarthMask 
  25.       AutoRedraw      =   -1  'True
  26.       AutoSize        =   -1  'True
  27.       BorderStyle     =   0  'None
  28.       Height          =   480
  29.       Left            =   5670
  30.       Picture         =   BITDEMO1.FRX:0000
  31.       ScaleHeight     =   32
  32.       ScaleMode       =   3  'Pixel
  33.       ScaleWidth      =   32
  34.       TabIndex        =   18
  35.       Top             =   360
  36.       Width           =   480
  37.    End
  38.    Begin PictureBox picEarthSprites 
  39.       AutoRedraw      =   -1  'True
  40.       AutoSize        =   -1  'True
  41.       BorderStyle     =   0  'None
  42.       Height          =   480
  43.       Left            =   5670
  44.       Picture         =   BITDEMO1.FRX:027A
  45.       ScaleHeight     =   32
  46.       ScaleMode       =   3  'Pixel
  47.       ScaleWidth      =   256
  48.       TabIndex        =   17
  49.       Top             =   960
  50.       Width           =   3840
  51.    End
  52.    Begin CheckBox chkFlickerless 
  53.       BackColor       =   &H00C0C0C0&
  54.       Caption         =   "Flickerless"
  55.       Height          =   255
  56.       Left            =   3930
  57.       TabIndex        =   12
  58.       Top             =   5220
  59.       Value           =   1  'Checked
  60.       Width           =   1395
  61.    End
  62.    Begin CheckBox chkAutoRedraw 
  63.       BackColor       =   &H00C0C0C0&
  64.       Caption         =   "AutoRedraw"
  65.       Height          =   255
  66.       Left            =   3930
  67.       TabIndex        =   11
  68.       Top             =   4950
  69.       Value           =   1  'Checked
  70.       Width           =   1395
  71.    End
  72.    Begin PictureBox picWork 
  73.       AutoRedraw      =   -1  'True
  74.       AutoSize        =   -1  'True
  75.       BorderStyle     =   0  'None
  76.       Height          =   675
  77.       Left            =   2700
  78.       ScaleHeight     =   45
  79.       ScaleMode       =   3  'Pixel
  80.       ScaleWidth      =   45
  81.       TabIndex        =   10
  82.       Top             =   5100
  83.       Visible         =   0   'False
  84.       Width           =   675
  85.    End
  86.    Begin HScrollBar HScroll1 
  87.       Height          =   255
  88.       LargeChange     =   12
  89.       Left            =   180
  90.       TabIndex        =   9
  91.       Top             =   4440
  92.       Width           =   4965
  93.    End
  94.    Begin VScrollBar VScroll1 
  95.       Height          =   3675
  96.       LargeChange     =   12
  97.       Left            =   5130
  98.       TabIndex        =   8
  99.       Top             =   780
  100.       Width           =   255
  101.    End
  102.    Begin PictureBox picBMP 
  103.       AutoRedraw      =   -1  'True
  104.       AutoSize        =   -1  'True
  105.       Height          =   9030
  106.       Left            =   5670
  107.       Picture         =   BITDEMO1.FRX:12F4
  108.       ScaleHeight     =   600
  109.       ScaleMode       =   3  'Pixel
  110.       ScaleWidth      =   800
  111.       TabIndex        =   7
  112.       Top             =   1560
  113.       Width           =   12030
  114.    End
  115.    Begin CommandButton btnStep 
  116.       Caption         =   "Single &Step"
  117.       Height          =   375
  118.       Left            =   1950
  119.       TabIndex        =   5
  120.       Top             =   120
  121.       Width           =   1665
  122.    End
  123.    Begin CommandButton btnGo 
  124.       Caption         =   "&S&imple Sprites"
  125.       Height          =   375
  126.       Left            =   180
  127.       TabIndex        =   4
  128.       Top             =   120
  129.       Width           =   1665
  130.    End
  131.    Begin PictureBox picSave 
  132.       AutoRedraw      =   -1  'True
  133.       AutoSize        =   -1  'True
  134.       BorderStyle     =   0  'None
  135.       Height          =   750
  136.       Left            =   1860
  137.       ScaleHeight     =   50
  138.       ScaleMode       =   3  'Pixel
  139.       ScaleWidth      =   50
  140.       TabIndex        =   3
  141.       Top             =   5100
  142.       Visible         =   0   'False
  143.       Width           =   750
  144.    End
  145.    Begin Timer Timer1 
  146.       Enabled         =   0   'False
  147.       Interval        =   55
  148.       Left            =   6300
  149.       Top             =   60
  150.    End
  151.    Begin PictureBox PicImage 
  152.       AutoRedraw      =   -1  'True
  153.       AutoSize        =   -1  'True
  154.       BorderStyle     =   0  'None
  155.       Height          =   750
  156.       Left            =   1020
  157.       Picture         =   BITDEMO1.FRX:76A2E
  158.       ScaleHeight     =   50
  159.       ScaleMode       =   3  'Pixel
  160.       ScaleWidth      =   50
  161.       TabIndex        =   2
  162.       Top             =   5100
  163.       Width           =   750
  164.    End
  165.    Begin PictureBox picMask 
  166.       AutoRedraw      =   -1  'True
  167.       AutoSize        =   -1  'True
  168.       BorderStyle     =   0  'None
  169.       Height          =   750
  170.       Left            =   180
  171.       Picture         =   BITDEMO1.FRX:77020
  172.       ScaleHeight     =   50
  173.       ScaleMode       =   3  'Pixel
  174.       ScaleWidth      =   50
  175.       TabIndex        =   1
  176.       Top             =   5100
  177.       Width           =   750
  178.    End
  179.    Begin PictureBox picBackground 
  180.       BackColor       =   &H00FFFFFF&
  181.       Height          =   3675
  182.       Left            =   180
  183.       ScaleHeight     =   243
  184.       ScaleMode       =   3  'Pixel
  185.       ScaleWidth      =   329
  186.       TabIndex        =   0
  187.       Top             =   780
  188.       Width           =   4965
  189.    End
  190.    Begin Timer Timer2 
  191.       Enabled         =   0   'False
  192.       Interval        =   55
  193.       Left            =   6840
  194.       Top             =   60
  195.    End
  196.    Begin Label Label1 
  197.       Alignment       =   2  'Center
  198.       BackStyle       =   0  'Transparent
  199.       Caption         =   "Work"
  200.       ForeColor       =   &H000000C0&
  201.       Height          =   195
  202.       Index           =   3
  203.       Left            =   2670
  204.       TabIndex        =   16
  205.       Top             =   4860
  206.       Width           =   735
  207.    End
  208.    Begin Label Label1 
  209.       Alignment       =   2  'Center
  210.       BackStyle       =   0  'Transparent
  211.       Caption         =   "Saved"
  212.       ForeColor       =   &H000000C0&
  213.       Height          =   195
  214.       Index           =   2
  215.       Left            =   1860
  216.       TabIndex        =   15
  217.       Top             =   4860
  218.       Width           =   735
  219.    End
  220.    Begin Label Label1 
  221.       Alignment       =   2  'Center
  222.       BackStyle       =   0  'Transparent
  223.       Caption         =   "Sprite"
  224.       ForeColor       =   &H000000C0&
  225.       Height          =   195
  226.       Index           =   1
  227.       Left            =   1020
  228.       TabIndex        =   14
  229.       Top             =   4860
  230.       Width           =   735
  231.    End
  232.    Begin Label Label1 
  233.       Alignment       =   2  'Center
  234.       BackStyle       =   0  'Transparent
  235.       Caption         =   "Mask"
  236.       ForeColor       =   &H000000C0&
  237.       Height          =   195
  238.       Index           =   0
  239.       Left            =   150
  240.       TabIndex        =   13
  241.       Top             =   4860
  242.       Width           =   735
  243.    End
  244.    Begin Label lblStep 
  245.       Alignment       =   2  'Center
  246.       BackStyle       =   0  'Transparent
  247.       Height          =   195
  248.       Left            =   1560
  249.       TabIndex        =   6
  250.       Top             =   540
  251.       Width           =   2535
  252.    End
  253. Option Explicit
  254. '-----------------------------------------------------
  255. ' BITDEMO1.FRM
  256. ' This program demonstrates some of the methods used
  257. ' to display bitmaps and sprites.
  258. '-----------------------------------------------------
  259. ' The number of pixels to offset the sprite
  260. ' each time it is moved.
  261. Const INCREMENT = 3
  262. ' Constants for Raster Operations used by BitBlt function.
  263. Const SRCAND = &H8800C6      ' dest = source AND dest
  264. Const SRCCOPY = &HCC0020     ' dest = source
  265. Const SRCPAINT = &HEE0086    ' dest = source OR dest
  266. ' The BitBlt Windows API call.
  267. Declare Function BitBlt Lib "GDI" (ByVal hDestDC As Integer, ByVal X As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hSrcDC As Integer, ByVal XSrc As Integer, ByVal YSrc As Integer, ByVal dwRop As Long) As Integer
  268. ' The x and y coordinates for the Sprite
  269. Dim SpriteX As Integer
  270. Dim SpriteY As Integer
  271. ' The x and y coordinates for the upper left corner
  272. ' of the large bitmap (picBMP).
  273. Dim BackgroundX As Integer
  274. Dim BackgroundY As Integer
  275. ' The width and height of the work area bitmap (picWork).
  276. Dim WorkWidth As Integer
  277. Dim WorkHeight As Integer
  278. Sub AnimatedSpriteMove ()
  279. '-----------------------------------------------------
  280. ' Move the animated sprite to its next position.
  281. '-----------------------------------------------------
  282. Dim rc As Integer
  283. Static SpriteNum As Integer
  284.     ' Calculate the next position for the sprite, and any
  285.     ' necessary direction changes.
  286.     GetNextPos picEarthMask.ScaleWidth, picEarthMask.ScaleHeight
  287.     ' Copy a section of the large bitmap into the work area.
  288.     rc = BitBlt(picWork.hDC, 0, 0, WorkWidth, WorkHeight, picBMP.hDC, (BackgroundX + SpriteX) - INCREMENT, (BackgroundY + SpriteY) - INCREMENT, SRCCOPY)
  289.     ' Draw the sprite mask into the work area.
  290.     rc = BitBlt(picWork.hDC, INCREMENT, INCREMENT, picEarthMask.ScaleWidth, picEarthMask.ScaleHeight, picEarthMask.hDC, 0, 0, SRCAND)
  291.     ' The picEarthSprites bitmap contains 8 "frames".  Each frame
  292.     ' is displayed in sequence to animate the object.
  293.     rc = BitBlt(picWork.hDC, INCREMENT, INCREMENT, picEarthMask.ScaleWidth, picEarthMask.ScaleHeight, picEarthSprites.hDC, (SpriteNum \ 2) * 32, 0, SRCPAINT)
  294.     ' Increment the Sprite Frame number.
  295.     SpriteNum = (SpriteNum + 1) Mod 16
  296.     ' Copy the work area onto the background.
  297.     rc = BitBlt(picBackground.hDC, SpriteX - INCREMENT, SpriteY - INCREMENT, WorkWidth, WorkHeight, picWork.hDC, 0, 0, SRCCOPY)
  298.     DoEvents
  299. End Sub
  300. Sub btnGo_Click ()
  301. '-----------------------------------------------------
  302. ' Start the simple sprite demonstration when this
  303. ' button is pushed.
  304. '-----------------------------------------------------
  305. Dim rc As Integer
  306.     ' We're running.
  307.     If Timer1.Enabled Then
  308.     Timer1.Enabled = False
  309.     ' Restore BG
  310.     If chkFlickerless = 0 Then
  311.         rc = BitBlt(picBackground.hDC, SpriteX, SpriteY, picSave.ScaleWidth, picSave.ScaleHeight, picSave.hDC, 0, 0, SRCCOPY)
  312.     Else
  313.         VScroll1_Change
  314.     End If
  315.     ' We're stopped.
  316.     Else
  317.     ' Save BG
  318.     If chkFlickerless = 0 Then rc = BitBlt(picSave.hDC, 0, 0, picSave.ScaleWidth, picSave.ScaleHeight, picBackground.hDC, SpriteX, SpriteY, SRCCOPY)
  319.     Timer1.Enabled = True
  320.     End If
  321. End Sub
  322. Sub btnSprites_Click ()
  323. '-----------------------------------------------------
  324. ' Start the animated sprite demonstration.
  325. '-----------------------------------------------------
  326.     ' We're running.
  327.     If Timer2.Enabled Then
  328.     Timer2.Enabled = False
  329.     ' This makes sure the background is refreshed.
  330.     VScroll1_Change
  331.     ' We're stopped.
  332.     Else
  333.     Timer2.Enabled = True
  334.     End If
  335. End Sub
  336. Sub btnStep_Click ()
  337. '-----------------------------------------------------
  338. ' Run the next step of the Single Step demonstration
  339. ' when this button is pushed.
  340. '-----------------------------------------------------
  341.     SingleStep
  342. End Sub
  343. Sub chkAutoRedraw_Click ()
  344. '-----------------------------------------------------
  345. ' Show the effects of the AutoRedraw property on
  346. ' the sprite work and save areas.
  347. '-----------------------------------------------------
  348.     picSave.AutoRedraw = (chkAutoRedraw = 1)
  349.     If picSave.AutoRedraw Then picSave.Cls
  350.     picWork.AutoRedraw = (chkAutoRedraw = 1)
  351.     If picWork.AutoRedraw Then picWork.Cls
  352.     picSave.Refresh
  353.     picWork.Refresh
  354. End Sub
  355. Sub FlickerlessSpriteMove ()
  356. '-----------------------------------------------------
  357. ' Moving a sprite without flicker requires the use
  358. ' of an off-screen work area into which we copy a
  359. ' section of the background and sprite.
  360. '-----------------------------------------------------
  361. Dim rc As Integer
  362.     ' Calculate the next position for the sprite, and any
  363.     ' necessary direction changes.
  364.     GetNextPos picImage.ScaleWidth, picImage.ScaleHeight
  365.     ' Copy a section of the large bitmap into the work area.
  366.     rc = BitBlt(picWork.hDC, 0, 0, WorkWidth, WorkHeight, picBMP.hDC, (BackgroundX + SpriteX) - INCREMENT, (BackgroundY + SpriteY) - INCREMENT, SRCCOPY)
  367.     ' Draw the mask and sprite bitmaps into the work area.
  368.     rc = BitBlt(picWork.hDC, INCREMENT, INCREMENT, picMask.ScaleWidth, picMask.ScaleHeight, picMask.hDC, 0, 0, SRCAND)
  369.     rc = BitBlt(picWork.hDC, INCREMENT, INCREMENT, picImage.ScaleWidth, picImage.ScaleHeight, picImage.hDC, 0, 0, SRCPAINT)
  370.     ' Copy the work area onto the background.
  371.     rc = BitBlt(picBackground.hDC, SpriteX - INCREMENT, SpriteY - INCREMENT, WorkWidth, WorkHeight, picWork.hDC, 0, 0, SRCCOPY)
  372.     DoEvents
  373. End Sub
  374. Sub Form_Load ()
  375. '-----------------------------------------------------
  376. '-----------------------------------------------------
  377. Dim rc As Integer
  378.     Me.Show
  379.     chkAutoRedraw.Value = 1
  380.     ' Set the limits of the scroll bars.
  381.     HScroll1.Max = picBMP.ScaleWidth - picBackground.ScaleWidth
  382.     VScroll1.Max = picBMP.ScaleHeight - picBackground.ScaleHeight
  383.     ' Only enable the scrollers if picBMP is larger than
  384.     ' the picBackground bitmap.
  385.     If picBMP.ScaleWidth <= picBackground.ScaleWidth Then HScroll1.Enabled = False
  386.     If picBMP.ScaleHeight <= picBackground.ScaleHeight Then VScroll1.Enabled = False
  387.                   
  388.     ' Save this initial section of the background. It may be
  389.     ' needed for the flickering sprite demo.
  390.     rc = BitBlt(picBackground.hDC, 0, 0, picBackground.ScaleWidth, picBackground.ScaleHeight, picBMP.hDC, HScroll1, VScroll1, SRCCOPY)
  391.     ' Set the dimensions of the work bitmap.
  392.     WorkWidth = (picImage.Width / 15) + (INCREMENT * 2)
  393.     WorkHeight = (picImage.Height / 15) + (INCREMENT * 2)
  394.     picWork.Width = WorkWidth * 15
  395.     picWork.Height = WorkHeight * 15
  396.     Me.Refresh
  397. End Sub
  398. Sub GetNextPos (ByVal AWidth As Integer, ByVal AHeight As Integer)
  399. '-----------------------------------------------------
  400. ' Calculate the next position for the sprite, and
  401. ' make any necessary direction changes.
  402. '-----------------------------------------------------
  403. Static xdir As Integer
  404. Static ydir As Integer
  405.     ' If this is the first time into the routine,
  406.     ' then initialize the x and y direction indicators.
  407.     If xdir = 0 Then
  408.     xdir = 1
  409.     ydir = 1
  410.     End If
  411.     ' Calculate the new position for the sprite.
  412.     SpriteX = SpriteX + (INCREMENT * xdir)
  413.     SpriteY = SpriteY + (INCREMENT * ydir)
  414.     ' Change direction of the sprite if it reaches the edge
  415.     ' of the background bitmap.
  416.     If (SpriteX + AWidth) >= picBackground.ScaleWidth Then
  417.     xdir = -1
  418.     End If
  419.     If SpriteX <= 0 Then
  420.     xdir = 1
  421.     End If
  422.     If (SpriteY + AHeight) >= picBackground.ScaleHeight Then
  423.     ydir = -1
  424.     End If
  425.     If SpriteY <= 0 Then
  426.     ydir = 1
  427.     End If
  428. End Sub
  429. Sub HScroll1_Change ()
  430. '-----------------------------------------------------
  431. ' Move the background horizontally under scroller
  432. ' control.
  433. '-----------------------------------------------------
  434. Dim rc As Integer
  435.     BackgroundX = HScroll1
  436.     rc = BitBlt(picBackground.hDC, 0, 0, picBackground.ScaleWidth, picBackground.ScaleHeight, picBMP.hDC, BackgroundX, BackgroundY, SRCCOPY)
  437. End Sub
  438. Sub SingleStep ()
  439. '-----------------------------------------------------
  440. ' Single step through the process of displaying a
  441. ' sprite.
  442. '-----------------------------------------------------
  443. Dim rc As Integer
  444. Static cstep As Integer
  445.     ' Perform the current step.
  446.     Select Case cstep
  447.     Case 0:
  448.         lblStep = "1: Copy BG to Save Area"
  449.         rc = BitBlt(picSave.hDC, 0, 0, picSave.ScaleWidth, picSave.ScaleHeight, picBackground.hDC, SpriteX, SpriteY, SRCCOPY)
  450.         btnGo.Enabled = False
  451.     Case 1:
  452.         lblStep = "2: Bitwise AND Mask to BG"
  453.         rc = BitBlt(picBackground.hDC, SpriteX, SpriteY, picMask.ScaleWidth, picMask.ScaleHeight, picMask.hDC, 0, 0, SRCAND)
  454.     Case 2:
  455.         lblStep = "3: Bitwise OR Sprite to BG"
  456.         rc = BitBlt(picBackground.hDC, SpriteX, SpriteY, picImage.ScaleWidth, picImage.ScaleHeight, picImage.hDC, 0, 0, SRCPAINT)
  457.     Case 3:
  458.         lblStep = "4: Copy Save Area to BG"
  459.         rc = BitBlt(picBackground.hDC, SpriteX, SpriteY, picSave.ScaleWidth, picSave.ScaleHeight, picSave.hDC, 0, 0, SRCCOPY)
  460.         btnGo.Enabled = True
  461.         cstep = -1
  462.         GetNextPos picImage.ScaleWidth, picImage.ScaleHeight
  463.     End Select
  464.     ' Get ready for the next step.
  465.     cstep = cstep + 1
  466. End Sub
  467. Sub SpriteMove ()
  468. '-----------------------------------------------------
  469. ' A simple method for displaying a sprite.
  470. '-----------------------------------------------------
  471. Dim rc As Integer
  472.     ' Replace the background saved when sprite was
  473.     ' last displayed.
  474.     rc = BitBlt(picBackground.hDC, SpriteX, SpriteY, picSave.ScaleWidth, picSave.ScaleHeight, picSave.hDC, 0, 0, SRCCOPY)
  475.     ' Calculate the next position for the sprite, and any
  476.     ' necessary direction changes.
  477.     GetNextPos picImage.ScaleWidth, picImage.ScaleHeight
  478.     ' Save the area of the background where the sprite is
  479.     ' about to be drawn.  This saved area will be used to
  480.     ' "erase" the sprite before it is displayed at a new
  481.     ' position.
  482.     rc = BitBlt(picSave.hDC, 0, 0, picSave.ScaleWidth, picSave.ScaleHeight, picBackground.hDC, SpriteX, SpriteY, SRCCOPY)
  483.     ' Draw the sprite mask directly onto the background.
  484.     rc = BitBlt(picBackground.hDC, SpriteX, SpriteY, picMask.ScaleWidth, picMask.ScaleHeight, picMask.hDC, 0, 0, SRCAND)
  485.     ' Draw the sprite over top of the mask.
  486.     rc = BitBlt(picBackground.hDC, SpriteX, SpriteY, picImage.ScaleWidth, picImage.ScaleHeight, picImage.hDC, 0, 0, SRCPAINT)
  487.     DoEvents
  488. End Sub
  489. Sub Timer1_Timer ()
  490. '-----------------------------------------------------
  491. ' Depending on the value of the Flickerless check
  492. ' box, run one of the simple sprite move subroutines.
  493. '-----------------------------------------------------
  494.     If chkFlickerless = 1 Then
  495.     FlickerlessSpriteMove
  496.     Else
  497.     SpriteMove
  498.     End If
  499. End Sub
  500. Sub Timer2_Timer ()
  501. '-----------------------------------------------------
  502. ' Move the animated sprite to its next position.
  503. '-----------------------------------------------------
  504.     AnimatedSpriteMove
  505. End Sub
  506. Sub VScroll1_Change ()
  507. '-----------------------------------------------------
  508. ' Move the background vertically under scroller
  509. ' control.
  510. '-----------------------------------------------------
  511. Dim rc As Integer
  512.     BackgroundY = VScroll1
  513.     rc = BitBlt(picBackground.hDC, 0, 0, picBackground.ScaleWidth, picBackground.ScaleHeight, picBMP.hDC, BackgroundX, BackgroundY, SRCCOPY)
  514. End Sub
  515.