home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH5 / SRC / SPRITES.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-01-08  |  9.4 KB  |  325 lines

  1. VERSION 4.00
  2. Begin VB.Form SpriteForm 
  3.    Caption         =   "Sprites"
  4.    ClientHeight    =   5235
  5.    ClientLeft      =   1320
  6.    ClientTop       =   1110
  7.    ClientWidth     =   6870
  8.    Height          =   5925
  9.    Left            =   1260
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   349
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   458
  14.    Top             =   480
  15.    Width           =   6990
  16.    Begin VB.TextBox FPSText 
  17.       Height          =   285
  18.       Left            =   1440
  19.       TabIndex        =   4
  20.       Text            =   "20"
  21.       Top             =   4920
  22.       Width           =   375
  23.    End
  24.    Begin VB.TextBox ObjText 
  25.       Height          =   285
  26.       Left            =   1440
  27.       TabIndex        =   3
  28.       Text            =   "20"
  29.       Top             =   4560
  30.       Width           =   375
  31.    End
  32.    Begin VB.CommandButton CmdStart 
  33.       Caption         =   "Start"
  34.       Default         =   -1  'True
  35.       Height          =   495
  36.       Left            =   2160
  37.       TabIndex        =   1
  38.       Top             =   4620
  39.       Width           =   855
  40.    End
  41.    Begin VB.PictureBox Court 
  42.       AutoRedraw      =   -1  'True
  43.       Height          =   4455
  44.       Left            =   0
  45.       ScaleHeight     =   293
  46.       ScaleMode       =   3  'Pixel
  47.       ScaleWidth      =   453
  48.       TabIndex        =   0
  49.       Top             =   0
  50.       Width           =   6855
  51.    End
  52.    Begin VB.Label Label1 
  53.       Caption         =   "Frames per second:"
  54.       Height          =   255
  55.       Index           =   0
  56.       Left            =   0
  57.       TabIndex        =   5
  58.       Top             =   4920
  59.       Width           =   1455
  60.    End
  61.    Begin VB.Label Label1 
  62.       Caption         =   "Number of objects:"
  63.       Height          =   255
  64.       Index           =   1
  65.       Left            =   0
  66.       TabIndex        =   2
  67.       Top             =   4560
  68.       Width           =   1455
  69.    End
  70.    Begin VB.Menu mnuFile 
  71.       Caption         =   "&File"
  72.       Begin VB.Menu mnuFileExit 
  73.          Caption         =   "E&xit"
  74.       End
  75.    End
  76. Attribute VB_Name = "SpriteForm"
  77. Attribute VB_Creatable = False
  78. Attribute VB_Exposed = False
  79. Option Explicit
  80. Dim xmax As Integer
  81. Dim ymax As Integer
  82. Dim Sprites As Collection
  83. Dim Playing As Boolean
  84. ' ************************************************
  85. ' Generate some random data.
  86. ' ************************************************
  87. Sub InitData()
  88. Dim obj As Object
  89. Dim num_obj As Integer
  90. Dim i As Integer
  91.     ' See how many objects there should be.
  92.     If Not IsNumeric(ObjText.Text) Then Exit Sub
  93.     num_obj = CInt(ObjText.Text)
  94.     If num_obj < 1 Then Exit Sub
  95.     ' Create the sprites.
  96.     Set Sprites = New Collection
  97.     For i = 1 To num_obj
  98.         ' Pick a random sprite type.
  99.         Select Case Int(3 * Rnd)
  100.             Case 0
  101.                 Set obj = InitRectangle()
  102.             Case 1
  103.                 Set obj = InitTriangle()
  104.             Case 2
  105.                 Set obj = InitCircle()
  106.         End Select
  107.         ' Add the sprite to the list.
  108.         Sprites.Add obj
  109.     Next i
  110. End Sub
  111. ' ************************************************
  112. ' Create and initialize a random CircleSprite.
  113. ' ************************************************
  114. Function InitCircle() As CircleSprite
  115. Dim obj As Object
  116. Dim radius As Integer
  117. Dim x As Integer
  118. Dim y As Integer
  119. Dim dx As Integer
  120. Dim dy As Integer
  121. Dim c As Integer
  122. Dim color As Long
  123.         
  124.     radius = Int(15 * Rnd + 5)
  125.     x = Int((xmax - radius + 1) * Rnd + radius / 2)
  126.     y = Int((ymax - radius + 1) * Rnd + radius / 2)
  127.     dx = Int(11 * Rnd - 5)
  128.     dy = Int(11 * Rnd - 5)
  129.     c = Int(15 * Rnd)
  130.     If c >= 7 Then c = c + 1
  131.     color = QBColor(c)
  132.     Set obj = New CircleSprite
  133.     obj.InitializeSprite radius, x, y, dx, dy, color
  134.     Set InitCircle = obj
  135. End Function
  136. ' ************************************************
  137. ' Create and initialize a random TriangleSprite.
  138. ' ************************************************
  139. Function InitTriangle() As TriangleSprite
  140. Const PI = 3.14159
  141. Const PI_OVER_3 = PI / 3
  142. Const PI_OVER_8 = PI / 8
  143. Const PI_OVER_16 = PI / 16
  144. Dim obj As Object
  145. Dim x As Integer
  146. Dim y As Integer
  147. Dim r1 As Integer
  148. Dim t1 As Integer
  149. Dim r2 As Integer
  150. Dim t2 As Integer
  151. Dim r3 As Integer
  152. Dim t3 As Integer
  153. Dim t As Single
  154. Dim dx As Integer
  155. Dim dy As Integer
  156. Dim dt As Single
  157. Dim c As Integer
  158. Dim color As Long
  159.             
  160.     x = Int((xmax - 20) * Rnd + 10)
  161.     y = Int((ymax - 20) * Rnd + 10)
  162.     r1 = Int(15 * Rnd + 10)
  163.     t1 = PI_OVER_3 * Rnd
  164.     r2 = Int(15 * Rnd + 10)
  165.     t2 = PI_OVER_3 * Rnd + 2 * PI_OVER_3
  166.     r3 = Int(15 * Rnd + 10)
  167.     t3 = PI_OVER_3 * Rnd + 4 * PI_OVER_3
  168.     dx = Int(11 * Rnd - 5)
  169.     dy = Int(11 * Rnd - 5)
  170.     dt = PI_OVER_8 * Rnd - PI_OVER_16
  171.     c = Int(15 * Rnd)
  172.     If c >= 7 Then c = c + 1
  173.     color = QBColor(c)
  174.     Set obj = New TriangleSprite
  175.     obj.InitializeSprite _
  176.         x, y, dx, dy, r1, t1, r2, t2, r3, t3, _
  177.         dt, color
  178.     Set InitTriangle = obj
  179. End Function
  180. ' ************************************************
  181. ' Create and initialize a random RectangleSprite.
  182. ' ************************************************
  183. Function InitRectangle() As RectangleSprite
  184. Const PI = 3.14159
  185. Const PI_OVER_2 = PI / 2
  186. Const PI_OVER_8 = PI / 8
  187. Const PI_OVER_16 = PI / 16
  188. Dim obj As Object
  189. Dim w As Integer
  190. Dim h As Integer
  191. Dim x As Integer
  192. Dim y As Integer
  193. Dim t As Single
  194. Dim dx As Integer
  195. Dim dy As Integer
  196. Dim dt As Single
  197. Dim c As Integer
  198. Dim color As Long
  199.         
  200.     w = Int(20 * Rnd + 10)
  201.     h = Int(20 * Rnd + 10)
  202.     x = Int((xmax - w + 1) * Rnd + w / 2)
  203.     y = Int((ymax - h + 1) * Rnd + h / 2)
  204.     dx = Int(11 * Rnd - 5)
  205.     dy = Int(11 * Rnd - 5)
  206.     t = PI_OVER_2 * Rnd
  207.     dt = PI_OVER_8 * Rnd - PI_OVER_16
  208.     c = Int(15 * Rnd)
  209.     If c >= 7 Then c = c + 1
  210.     color = QBColor(c)
  211.     Set obj = New RectangleSprite
  212.     obj.InitializeSprite _
  213.         w, h, x, y, t, dx, dy, dt, color
  214.     Set InitRectangle = obj
  215. End Function
  216. ' ************************************************
  217. ' Start the animation.
  218. ' ************************************************
  219. Private Sub CmdStart_Click()
  220.     If Playing Then
  221.         Playing = False
  222.         CmdStart.Caption = "Stopped"
  223.         CmdStart.Enabled = False
  224.     Else
  225.         CmdStart.Caption = "Stop"
  226.         Playing = True
  227.         InitData
  228.         PlayData
  229.         Playing = False
  230.         CmdStart.Caption = "Start"
  231.         CmdStart.Enabled = True
  232.     End If
  233. End Sub
  234. ' ************************************************
  235. ' Play the animation.
  236. ' ************************************************
  237. Sub PlayData()
  238. Dim mpf As Long     ' Milliseconds per frame.
  239. Dim next_time As Long
  240. Dim old_style As Integer
  241. Dim obj As Object
  242. Dim frames As Integer
  243. Dim start_time As Single
  244. Dim stop_time As Single
  245. Dim bm As BITMAP
  246. Dim hbm As Integer
  247. Dim status As Long
  248. Dim Wid As Long
  249. Dim Hgt As Long
  250. Dim num_bits As Long
  251. Dim bytes() As Byte
  252.     ' Set FillStyle to vbSolid.
  253.     old_style = Court.FillStyle
  254.     Court.FillStyle = vbSolid
  255.     ' See how fast we should go.
  256.     If Not IsNumeric(FPSText.Text) Then _
  257.         FPSText.Text = "10"
  258.     mpf = 1000 \ CLng(FPSText.Text)
  259.     ' Create a blank background.
  260.     Court.Line (0, 0)- _
  261.         Step(Court.ScaleWidth, Court.ScaleHeight), _
  262.         Court.BackColor, BF
  263.     ' Get the background image pixels.
  264.     hbm = Court.Image
  265.     status = GetObject(hbm, BITMAP_SIZE, bm)
  266.     Wid = bm.bmWidthBytes
  267.     Hgt = bm.bmHeight
  268.     num_bits = Wid * Hgt
  269.     ReDim bytes(1 To Wid, 1 To Hgt)
  270.     status = GetBitmapBits(hbm, num_bits, bytes(1, 1))
  271.     ' Start the animation.
  272.     next_time = GetTickCount()
  273.     start_time = Timer
  274.     Do While Playing
  275.         frames = frames + 1
  276.         
  277.         ' Move each sprite.
  278.         For Each obj In Sprites
  279.             obj.MoveSprite xmax, ymax
  280.         Next obj
  281.         
  282.         ' Wait until it's time for the next frame.
  283.         next_time = next_time + mpf
  284.         WaitTill next_time
  285.         
  286.         ' Erase using SetBitmapBits.
  287.         status = SetBitmapBits(hbm, num_bits, bytes(1, 1))
  288.                 
  289.         ' Draw the sprites.
  290.         For Each obj In Sprites
  291.             obj.DrawSprite Court
  292.         Next obj
  293.         Court.Refresh
  294.     Loop
  295.     stop_time = Timer
  296.     MsgBox "Displayed" & Str$(frames) & _
  297.         " frames in " & _
  298.         Format$(stop_time - start_time, "0.00") & _
  299.         " seconds (" & _
  300.         Format$(frames / (stop_time - start_time), "0.00") & _
  301.         " FPS)."
  302.     ' Restore the old FillStyle.
  303.     Court.FillStyle = old_style
  304. End Sub
  305. ' ************************************************
  306. ' Make the ball court nice and big.
  307. ' ************************************************
  308. Private Sub Form_Resize()
  309. Const GAP = 3
  310.     FPSText.Top = ScaleHeight - GAP - FPSText.Height
  311.     Label1(0).Top = FPSText.Top
  312.     ObjText.Top = FPSText.Top - GAP - ObjText.Height
  313.     Label1(1).Top = ObjText.Top
  314.     CmdStart.Top = (ObjText.Top + FPSText.Top + FPSText.Height - CmdStart.Height) / 2
  315.     Court.Move 0, 0, ScaleWidth, ObjText.Top - GAP
  316.     xmax = Court.ScaleWidth - 1
  317.     ymax = Court.ScaleHeight - 1
  318. End Sub
  319. Private Sub Form_Unload(Cancel As Integer)
  320.     End
  321. End Sub
  322. Private Sub mnuFileExit_Click()
  323.     Unload Me
  324. End Sub
  325.