home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 5_2007-2008.ISO / data / Zips / A_200_segm204037122007.psc / frmMain.frm < prev    next >
Text File  |  2007-01-02  |  20KB  |  635 lines

  1. VERSION 5.00
  2. Begin VB.Form frmMain 
  3.    BorderStyle     =   1  'Fixed Single
  4.    Caption         =   "Serpent"
  5.    ClientHeight    =   7470
  6.    ClientLeft      =   45
  7.    ClientTop       =   330
  8.    ClientWidth     =   9975
  9.    Icon            =   "frmMain.frx":0000
  10.    KeyPreview      =   -1  'True
  11.    LinkTopic       =   "Form1"
  12.    MaxButton       =   0   'False
  13.    MinButton       =   0   'False
  14.    ScaleHeight     =   498
  15.    ScaleMode       =   3  'Pixel
  16.    ScaleWidth      =   665
  17.    StartUpPosition =   3  'Windows Default
  18.    Begin VB.CheckBox chkRandomMove 
  19.       Caption         =   "Random Movement"
  20.       Height          =   255
  21.       Left            =   7680
  22.       TabIndex        =   12
  23.       Top             =   2400
  24.       Value           =   1  'Checked
  25.       Width           =   2055
  26.    End
  27.    Begin VB.HScrollBar hscNumSegments 
  28.       Height          =   255
  29.       LargeChange     =   10
  30.       Left            =   7560
  31.       Max             =   200
  32.       Min             =   10
  33.       SmallChange     =   2
  34.       TabIndex        =   10
  35.       Top             =   360
  36.       Value           =   50
  37.       Width           =   2295
  38.    End
  39.    Begin VB.HScrollBar hscShrink 
  40.       Height          =   255
  41.       LargeChange     =   10
  42.       Left            =   7560
  43.       Max             =   100
  44.       TabIndex        =   8
  45.       Top             =   1200
  46.       Value           =   50
  47.       Width           =   2295
  48.    End
  49.    Begin VB.CheckBox chkAngleSpeed 
  50.       Caption         =   "Lock Turns"
  51.       Height          =   255
  52.       Left            =   7680
  53.       TabIndex        =   7
  54.       Top             =   1920
  55.       Width           =   1215
  56.    End
  57.    Begin VB.TextBox Text1 
  58.       Appearance      =   0  'Flat
  59.       Height          =   285
  60.       Left            =   9120
  61.       Locked          =   -1  'True
  62.       TabIndex        =   6
  63.       Text            =   "0.0"
  64.       Top             =   1920
  65.       Visible         =   0   'False
  66.       Width           =   735
  67.    End
  68.    Begin VB.Timer Timer2 
  69.       Interval        =   600
  70.       Left            =   9000
  71.       Top             =   3840
  72.    End
  73.    Begin VB.Timer Timer1 
  74.       Enabled         =   0   'False
  75.       Interval        =   1000
  76.       Left            =   7800
  77.       Top             =   3840
  78.    End
  79.    Begin VB.CommandButton cmdPause 
  80.       Caption         =   "START"
  81.       Height          =   495
  82.       Left            =   7560
  83.       TabIndex        =   2
  84.       Top             =   6840
  85.       Width           =   2295
  86.    End
  87.    Begin VB.PictureBox picSource 
  88.       AutoRedraw      =   -1  'True
  89.       AutoSize        =   -1  'True
  90.       Height          =   1980
  91.       Left            =   9480
  92.       ScaleHeight     =   128
  93.       ScaleMode       =   3  'Pixel
  94.       ScaleWidth      =   128
  95.       TabIndex        =   1
  96.       Top             =   4320
  97.       Visible         =   0   'False
  98.       Width           =   1980
  99.    End
  100.    Begin VB.PictureBox picMain 
  101.       BeginProperty Font 
  102.          Name            =   "MS Sans Serif"
  103.          Size            =   12
  104.          Charset         =   0
  105.          Weight          =   700
  106.          Underline       =   0   'False
  107.          Italic          =   0   'False
  108.          Strikethrough   =   0   'False
  109.       EndProperty
  110.       Height          =   7335
  111.       Left            =   120
  112.       ScaleHeight     =   485
  113.       ScaleMode       =   3  'Pixel
  114.       ScaleWidth      =   485
  115.       TabIndex        =   0
  116.       Top             =   0
  117.       Width           =   7335
  118.    End
  119.    Begin VB.Label Label2 
  120.       Alignment       =   2  'Center
  121.       Caption         =   "Number of Segments"
  122.       Height          =   255
  123.       Left            =   7560
  124.       TabIndex        =   11
  125.       Top             =   120
  126.       Width           =   2295
  127.    End
  128.    Begin VB.Label Label1 
  129.       Alignment       =   2  'Center
  130.       Caption         =   "Tail Shrinkage"
  131.       Height          =   255
  132.       Left            =   7560
  133.       TabIndex        =   9
  134.       Top             =   960
  135.       Width           =   2295
  136.    End
  137.    Begin VB.Label lblPix 
  138.       Alignment       =   2  'Center
  139.       BeginProperty Font 
  140.          Name            =   "MS Sans Serif"
  141.          Size            =   18
  142.          Charset         =   0
  143.          Weight          =   400
  144.          Underline       =   0   'False
  145.          Italic          =   0   'False
  146.          Strikethrough   =   0   'False
  147.       EndProperty
  148.       Height          =   975
  149.       Left            =   7560
  150.       TabIndex        =   5
  151.       Top             =   5040
  152.       Width           =   2295
  153.    End
  154.    Begin VB.Label lblCogs 
  155.       Alignment       =   2  'Center
  156.       BeginProperty Font 
  157.          Name            =   "MS Sans Serif"
  158.          Size            =   13.5
  159.          Charset         =   0
  160.          Weight          =   400
  161.          Underline       =   0   'False
  162.          Italic          =   0   'False
  163.          Strikethrough   =   0   'False
  164.       EndProperty
  165.       Height          =   495
  166.       Left            =   7560
  167.       TabIndex        =   4
  168.       Top             =   4320
  169.       Width           =   2295
  170.    End
  171.    Begin VB.Label lblFPS 
  172.       Alignment       =   2  'Center
  173.       BeginProperty Font 
  174.          Name            =   "MS Sans Serif"
  175.          Size            =   24
  176.          Charset         =   0
  177.          Weight          =   400
  178.          Underline       =   0   'False
  179.          Italic          =   0   'False
  180.          Strikethrough   =   0   'False
  181.       EndProperty
  182.       Height          =   495
  183.       Left            =   7560
  184.       TabIndex        =   3
  185.       Top             =   6240
  186.       Width           =   2295
  187.    End
  188. End
  189. Attribute VB_Name = "frmMain"
  190. Attribute VB_GlobalNameSpace = False
  191. Attribute VB_Creatable = False
  192. Attribute VB_PredeclaredId = True
  193. Attribute VB_Exposed = False
  194. Option Explicit
  195. Option Base 0
  196.  
  197. 'How it works
  198. 'mPositions holds an array containing the X and Y position and the rotation of the serpent segments.
  199. 'Each position is 1 (SNAKE_SPEED) pixel apart so for the first segment (33 pixels wide) we step
  200. '33 positions through the positions array to find it's position.
  201. '   This step value reduces as the snake segment shrinks in size.
  202.  
  203. 'mHead and mTail are positions in the array. Both scan through the array backwards (decremented).
  204. '   The mHead position is always decremented once after storing the position data each frame.
  205.  
  206. 'The mTail position is different because the user can change the size and length of the snake.
  207. '   mTail is calculated at startup and each time the user changes the length and shrinkage.
  208. '   Then it works the same as mHead - Decremented once each frame.
  209.  
  210. 'When drawing the snake we start at the TAIL and step forwards (increment) through the array to
  211. 'get the segment positions.
  212.  
  213.  
  214. Const MAX_NUM_SEGMENTS As Integer = 201 'If you change this - change the form's ScrollBar
  215. Const SEGMENT_SPACE As Integer = 33
  216. Const NUM_POSITIONS As Integer = 6700   'Must be more than MAX_NUM_SEGMENTS * SEGMENT_SPACE
  217. Const SNAKE_SPEED As Single = 1#
  218. Const TURN_SPEED_DELTA As Single = 0.00005
  219.  
  220. 'The LIGHT SPEED classes - one for each picturebox
  221. Private mLightSpeed8(0 To 1) As clsLightSpeed8
  222. ' frequently used class values
  223. Private mLightSpeedPitch0 As Integer
  224. Private mLightSpeedPitch1 As Integer
  225. 'Arrays for direct image access
  226. Private mArray0() As Byte
  227. Private mArray1() As Byte
  228. 'To reset arrays to original state before erasing
  229. Private mArray0Pnt As Long
  230. Private mArray1Pnt As Long
  231.  
  232. 'Window size
  233. Private mWinSizeX As Integer, mWinSizeY As Integer
  234. 'Cursor pos
  235. Private mCursorX As Integer, mCursorY As Integer
  236.  
  237. ' Speed stats
  238. Private mFrame As Integer
  239. Private mNumVisibleSegs As Integer
  240. Private mNumPix As Long
  241. ' Options
  242. Private mRun As Boolean
  243. Private mVB_CODE As Boolean
  244.  
  245.  
  246. ' Position of segment
  247. Private Type tPosition
  248.     x As Single
  249.     y As Single
  250.     ang As Single
  251. End Type
  252. Private mPositions(0 To NUM_POSITIONS) As tPosition
  253.  
  254. ' Which graphic piece to use and the segment zoom factor
  255. Private Type tSegment
  256.     graphic As Integer
  257.     zoom As Single
  258. End Type
  259. Private mSegments(0 To MAX_NUM_SEGMENTS) As tSegment
  260.  
  261. Private mNumSegments As Integer
  262. Private mHead As Integer, mTail As Integer
  263. 'Turn speed
  264. Private mTurnSpeed As Single, mTurnSpeedTarget As Single
  265.  
  266.  
  267. '---------------------------------------------------------------------------------
  268. ' The main loop
  269. '---------------------------------------------------------------------------------
  270. Private Sub Animate()
  271.     Dim i As Integer, scan As Integer, piece As Integer
  272.     Dim x As Integer, y As Integer
  273.     Dim directionToCursor As Single
  274.     Dim headX As Single, headY As Single, newAng As Single
  275.     
  276.     mRun = True
  277.     Timer1.Enabled = True
  278.  
  279.     mTurnSpeed = 0.01
  280.     mTurnSpeedTarget = 0.01
  281.     
  282.     ' set segment pieces, sizes and mTail position
  283.     Call hscShrink_Change
  284.     
  285.     Do While mRun
  286.         Call mLightSpeed8(1).FillZero
  287.         mNumVisibleSegs = 0
  288.         'Draw segments TAIL first
  289.         scan = mTail
  290.         For i = 0 To mNumSegments - 1
  291.             x = mPositions(scan).x
  292.             y = mPositions(scan).y
  293.             Call DrawSegment(x, y, mPositions(scan).ang, mSegments(i).graphic, mSegments(i).zoom)
  294.             scan = scan - SEGMENT_SPACE * mSegments(i).zoom
  295.             If scan < 0 Then scan = scan + NUM_POSITIONS
  296.         Next
  297.         
  298.         'Get next direction
  299.         headX = mPositions(mHead).x
  300.         headY = mPositions(mHead).y
  301.         directionToCursor = Atan2(mCursorY - headY, mCursorX - headX)
  302.         newAng = StepAngle(mPositions(mHead).ang, directionToCursor, mTurnSpeed)
  303.         'Move
  304.         headX = headX + Cos(newAng) * SNAKE_SPEED
  305.         headY = headY + Sin(newAng) * SNAKE_SPEED
  306.         'New Head Pos
  307.         mHead = mHead - 1
  308.         If mHead < 0 Then
  309.             mHead = mHead + NUM_POSITIONS
  310.         End If
  311.         'Save new position
  312.         mPositions(mHead).x = headX
  313.         mPositions(mHead).y = headY
  314.         mPositions(mHead).ang = newAng
  315.             
  316.         'New Tail Pos
  317.         mTail = mTail - 1
  318.         If mTail < 0 Then
  319.             mTail = mTail + NUM_POSITIONS
  320.         End If
  321.         
  322.         'Change the serpent turning speed
  323.         If chkAngleSpeed.value = 1 Then
  324.             mTurnSpeed = 0.01
  325.         Else
  326.             If Abs(mTurnSpeed - mTurnSpeedTarget) < TURN_SPEED_DELTA Then
  327.                 mTurnSpeed = mTurnSpeedTarget
  328.             Else
  329.                 If mTurnSpeedTarget > mTurnSpeed Then
  330.                     mTurnSpeed = mTurnSpeed + TURN_SPEED_DELTA
  331.                 Else
  332.                     mTurnSpeed = mTurnSpeed - TURN_SPEED_DELTA
  333.                 End If
  334.             End If
  335.         End If
  336. '        Text1.text = Format(mTurnSpeed, "#0.00000")
  337.         
  338.         lblCogs.Caption = Str(mNumVisibleSegs) & " Segments"
  339.         Call mLightSpeed8(1).PutPicture
  340.         
  341.         mFrame = mFrame + 1
  342.         DoEvents
  343.     Loop
  344.     Timer1.Enabled = False
  345. End Sub
  346.  
  347.  
  348.  
  349.  
  350. '---------------------------------------------------------------------------------
  351. '---------------------------------------------------------------------------------
  352. Private Sub DrawSegment(centerX As Integer, centerY As Integer, ang As Single, piece As Integer, zoom As Single)
  353.     Dim left As Integer, top As Integer
  354.     Dim right As Integer, bottom As Integer
  355.     Dim width As Integer, height As Integer
  356.     Dim srcLeft As Integer, srcTop As Integer
  357.     Dim srcWidth As Integer, srcHeight As Integer
  358.     Dim srcCenterX As Integer, srcCenterY As Integer
  359.     Dim segRad As Integer
  360.     
  361.     Select Case piece
  362.     Case 0
  363.         srcLeft = 0
  364.         srcTop = 0
  365.         srcWidth = 62
  366.         srcHeight = 66
  367.         srcCenterX = 15
  368.         srcCenterY = 32
  369.         segRad = 44
  370.     Case 1
  371.         srcLeft = 63
  372.         srcTop = 0
  373.         srcWidth = 62
  374.         srcHeight = 66
  375.         srcCenterX = 15
  376.         srcCenterY = 32
  377.         segRad = 44
  378.     Case 2  'Head
  379.         srcLeft = 126
  380.         srcTop = 0
  381.         srcWidth = 130
  382.         srcHeight = 82
  383.         srcCenterX = 15
  384.         srcCenterY = 40
  385.         segRad = 120
  386.     Case 3  'Tail
  387.         srcLeft = 1
  388.         srcTop = 84
  389.         srcWidth = 184
  390.         srcHeight = 66
  391.         srcCenterX = 137
  392.         srcCenterY = 32
  393.         segRad = 140
  394.     End Select
  395.     
  396.     ' Shrink destination area by zoom factor
  397.     segRad = segRad * zoom
  398.     
  399.     ' cull off-screen pieces
  400.     left = centerX - segRad
  401.     top = centerY - segRad
  402.     If left >= mWinSizeX Then Exit Sub
  403.     If top >= mWinSizeY Then Exit Sub
  404.     right = centerX + segRad
  405.     bottom = centerY + segRad
  406.     If right <= 0 Then Exit Sub
  407.     If bottom <= 0 Then Exit Sub
  408.     
  409.     ' ensure we don't write to off screen memory - crash
  410.     If left < 0 Then left = 0
  411.     If top < 0 Then top = 0
  412.     If right > mWinSizeX Then right = mWinSizeX
  413.     If bottom > mWinSizeY Then bottom = mWinSizeY
  414.     
  415.     width = right - left
  416.     height = bottom - top
  417.  
  418.         Call VB8_ScaleRotate(mArray1(), mLightSpeedPitch1, _
  419.                          left, top, centerX - left, centerY - top, _
  420.                          width, height, mArray0(), mLightSpeedPitch0, _
  421.                          srcLeft, srcTop, srcCenterX, srcCenterY, _
  422.                          srcWidth, srcHeight, _
  423.                          ang, zoom, True)
  424.     
  425.     mNumVisibleSegs = mNumVisibleSegs + 1
  426.     mNumPix = mNumPix + CLng(width) * CLng(height)
  427. End Sub
  428.  
  429.  
  430.  
  431.  
  432.  
  433. '---------------------------------------------------------------------------------
  434. ' Snake Parameters
  435. '---------------------------------------------------------------------------------
  436. Private Sub hscShrink_Change()
  437.     Dim i As Integer, scan As Integer
  438.     Dim value As Single, subt As Single
  439.     
  440.     'Ensure even number of segments
  441.     mNumSegments = hscNumSegments.value And &HFFFE
  442.  
  443.     'Use the length and shrinkage values to calcuate tail positon
  444.     'Set zoom factors at the same time
  445.     subt = (hscShrink.value / 100) / mNumSegments
  446.     value = 1#
  447.     scan = mHead
  448.     For i = mNumSegments - 1 To 0 Step -1
  449.         mSegments(i).zoom = value
  450.         value = value - subt
  451.         
  452.         scan = scan + SEGMENT_SPACE * value
  453.         If scan >= NUM_POSITIONS Then scan = scan - NUM_POSITIONS
  454.     Next
  455.     mTail = scan
  456.     
  457.     ' set segment pieces
  458.     For i = 0 To mNumSegments - 1
  459.         mSegments(i).graphic = i And 1
  460.     Next
  461.     'Head and Tail
  462.     mSegments(mNumSegments - 1).graphic = 2
  463.     mSegments(0).graphic = 3
  464. End Sub
  465. Private Sub hscShrink_Scroll()
  466.     Call hscShrink_Change
  467. End Sub
  468. Private Sub hscNumSegments_Change()
  469.     Call hscShrink_Change
  470. End Sub
  471. Private Sub hscNumSegments_Scroll()
  472.     Call hscShrink_Change
  473. End Sub
  474.  
  475. '---------------------------------------------------------------------------------
  476. ' New Target Position
  477. '---------------------------------------------------------------------------------
  478. Private Sub Timer2_Timer()
  479.     Dim x As Single, y As Single
  480.     If chkRandomMove.value Then
  481.         x = Rnd() * mWinSizeX
  482.         y = Rnd() * mWinSizeY
  483.         Call picMain_MouseMove(1, 0, x, y)
  484.         mTurnSpeedTarget = 0.0005 + Rnd() * 0.015
  485.     End If
  486. End Sub
  487.  
  488.  
  489.  
  490. '---------------------------------------------------------------------------------
  491. ' UI Stuff
  492. '---------------------------------------------------------------------------------
  493. Private Sub optLanguage_Click(index As Integer)
  494.     mVB_CODE = index
  495. End Sub
  496.  
  497. Private Sub cmdPause_Click()
  498.     If mRun Then
  499.         mRun = False
  500.         cmdPause.Caption = "START"
  501.     Else
  502.         cmdPause.Caption = "STOP"
  503.         Call Animate
  504.     End If
  505. End Sub
  506.  
  507. Private Sub picMain_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
  508.     If Button Then
  509.         mCursorX = x
  510.         mCursorY = y
  511.     End If
  512. End Sub
  513. Private Sub picMain_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
  514.     Call picMain_MouseMove(Button, Shift, x, y)
  515. End Sub
  516.  
  517.  
  518.  
  519. '---------------------------------------------------------------------------------
  520. ' Statistics
  521. '---------------------------------------------------------------------------------
  522. Private Sub Timer1_Timer()
  523.     lblFPS.Caption = "FPS:" & Str(mFrame)
  524.     lblPix.Caption = Format(mNumPix / 1000000, "##0.0#") & " million pixels/second"
  525.     mFrame = 0
  526.     mNumPix = 0
  527. End Sub
  528.  
  529.  
  530. '---------------------------------------------------------------------------------
  531. ' LOAD - UNLOAD
  532. '---------------------------------------------------------------------------------
  533. Private Sub Form_Load()
  534.     Dim i As Integer
  535.     If (App.LogMode <> 1) Then
  536.         Call MsgBox("COMPILE ME!", vbOKOnly)
  537.     End If
  538.  
  539.     'Position our snake off screen
  540.     For i = 0 To NUM_POSITIONS - 1
  541.         mPositions(i).x = -50
  542.         mPositions(i).y = -50
  543.         mPositions(i).ang = 0
  544.     Next
  545. End Sub
  546.  
  547. Private Sub Form_Activate()
  548.     picMain.CurrentX = 30
  549.     picMain.CurrentY = 60
  550.     picMain.Print "1. Click 'START' to Begin."
  551.     picMain.CurrentX = 80
  552.     picMain.CurrentY = 250
  553.     picMain.Print "There is NO block copying in this demo!"
  554.     picMain.CurrentX = 45
  555.     picMain.CurrentY = 280
  556.     picMain.Print "Each segment is idividually scaled and rotated."
  557.     
  558.     ' Load our pictures
  559.     picSource.Picture = LoadPicture("serpent.gif")
  560.  
  561.     mWinSizeX = picMain.ScaleWidth
  562.     mWinSizeY = picMain.ScaleHeight
  563.     mCursorX = mWinSizeX \ 2
  564.     mCursorY = mWinSizeY \ 2
  565.     
  566.     'Create LIGHT SPEED objects
  567.     Call CreateObjects
  568.  
  569.     'frequently used
  570.     mLightSpeedPitch0 = mLightSpeed8(0).GetPitch()
  571.     mLightSpeedPitch1 = mLightSpeed8(1).GetPitch()
  572.       
  573.     mVB_CODE = True
  574.     mRun = False
  575. End Sub
  576.  
  577. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  578.     Cancel = mRun
  579.     mRun = False
  580.     cmdPause.Caption = "START"
  581. End Sub
  582.  
  583.  
  584. Private Sub Form_Unload(Cancel As Integer)
  585.     Call DeleteObjects
  586. End Sub
  587.  
  588.  
  589. Private Sub CreateObjects()
  590.     Set mLightSpeed8(0) = New clsLightSpeed8
  591.     Set mLightSpeed8(1) = New clsLightSpeed8
  592.     
  593.     'Initialize the source LS object with the size we know the file is
  594.     Call mLightSpeed8(0).InitDimensions(256, 256)
  595.     'Set the palette to convert the GIF file to
  596.     mLightSpeed8(0).ReadPaletteFile ("serpent.pal")
  597.     Call mLightSpeed8(0).SetPalette(0, 256)
  598.     ' Capture the source picturebox to the DIB
  599.     Set mLightSpeed8(0).SetPictureBox = picSource
  600.     Call mLightSpeed8(0).GrabPicture
  601.  
  602.     'Initialize the Destination LS object and set it's palette to match the Source
  603.     Call mLightSpeed8(1).InitPicture(picMain, False)
  604.     mLightSpeed8(1).ReadPaletteFile ("serpent.pal")
  605.     Call mLightSpeed8(1).SetPalette(0, 256)
  606.     
  607.     'Store original pointer for array recovery
  608.     mArray0Pnt = mLightSpeed8(0).GetArray(mArray0)
  609.     mArray1Pnt = mLightSpeed8(1).GetArray(mArray1)
  610. End Sub
  611.  
  612.  
  613. Private Sub DeleteObjects()
  614.     'Recover our arrays and erase them
  615.     If mArray0Pnt Then
  616.         Call mLightSpeed8(0).FixArray(mArray0, mArray0Pnt)
  617.         Erase mArray0
  618.         mArray0Pnt = 0
  619.     End If
  620.     If mArray1Pnt Then
  621.         Call mLightSpeed8(1).FixArray(mArray1, mArray1Pnt)
  622.         Erase mArray1
  623.         mArray1Pnt = 0
  624.     End If
  625.     'Delete objects
  626.     If Not mLightSpeed8(0) Is Nothing Then
  627.         Set mLightSpeed8(0) = Nothing
  628.     End If
  629.     If Not mLightSpeed8(1) Is Nothing Then
  630.         Set mLightSpeed8(1) = Nothing
  631.     End If
  632. End Sub
  633.  
  634.  
  635.