home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 6_2008-2009.ISO / data / zips / API_Gaming216202972009.psc / frmMain.frm < prev    next >
Text File  |  2009-09-06  |  15KB  |  376 lines

  1. VERSION 5.00
  2. Begin VB.Form frmMain 
  3.    BorderStyle     =   1  'Fixed Single
  4.    Caption         =   "API Gaming Techniques Vol. 1 (Fast Sprite Manipulation)"
  5.    ClientHeight    =   9270
  6.    ClientLeft      =   45
  7.    ClientTop       =   435
  8.    ClientWidth     =   12570
  9.    LinkTopic       =   "Form1"
  10.    MaxButton       =   0   'False
  11.    ScaleHeight     =   618
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   838
  14.    StartUpPosition =   3  'Windows Default
  15.    Begin VB.CommandButton Command1 
  16.       Caption         =   "Select BG Color"
  17.       Height          =   735
  18.       Left            =   4560
  19.       TabIndex        =   6
  20.       Top             =   1080
  21.       Width           =   735
  22.    End
  23.    Begin VB.PictureBox picInfoA 
  24.       AutoRedraw      =   -1  'True
  25.       BorderStyle     =   0  'None
  26.       BeginProperty Font 
  27.          Name            =   "Lucida Console"
  28.          Size            =   8.25
  29.          Charset         =   0
  30.          Weight          =   400
  31.          Underline       =   0   'False
  32.          Italic          =   0   'False
  33.          Strikethrough   =   0   'False
  34.       EndProperty
  35.       Height          =   1335
  36.       Left            =   5400
  37.       ScaleHeight     =   1335
  38.       ScaleWidth      =   4335
  39.       TabIndex        =   4
  40.       Top             =   120
  41.       Width           =   4335
  42.    End
  43.    Begin VB.PictureBox picInfoC 
  44.       AutoRedraw      =   -1  'True
  45.       BorderStyle     =   0  'None
  46.       BeginProperty Font 
  47.          Name            =   "Lucida Console"
  48.          Size            =   8.25
  49.          Charset         =   0
  50.          Weight          =   400
  51.          Underline       =   0   'False
  52.          Italic          =   0   'False
  53.          Strikethrough   =   0   'False
  54.       EndProperty
  55.       Height          =   1335
  56.       Left            =   120
  57.       ScaleHeight     =   1335
  58.       ScaleWidth      =   4335
  59.       TabIndex        =   3
  60.       Top             =   120
  61.       Width           =   4335
  62.    End
  63.    Begin VB.HScrollBar sScaleArbiter 
  64.       Height          =   255
  65.       LargeChange     =   10
  66.       Left            =   5400
  67.       Max             =   200
  68.       TabIndex        =   2
  69.       Top             =   1560
  70.       Value           =   100
  71.       Width           =   4335
  72.    End
  73.    Begin VB.HScrollBar sScaleCarrier 
  74.       Height          =   255
  75.       LargeChange     =   10
  76.       Left            =   120
  77.       Max             =   200
  78.       TabIndex        =   1
  79.       Top             =   1560
  80.       Value           =   100
  81.       Width           =   4335
  82.    End
  83.    Begin VB.PictureBox picField 
  84.       AutoRedraw      =   -1  'True
  85.       BackColor       =   &H00000000&
  86.       BeginProperty Font 
  87.          Name            =   "Lucida Console"
  88.          Size            =   15.75
  89.          Charset         =   0
  90.          Weight          =   700
  91.          Underline       =   0   'False
  92.          Italic          =   0   'False
  93.          Strikethrough   =   0   'False
  94.       EndProperty
  95.       ForeColor       =   &H0000FF00&
  96.       Height          =   7260
  97.       Left            =   120
  98.       ScaleHeight     =   480
  99.       ScaleMode       =   3  'Pixel
  100.       ScaleWidth      =   640
  101.       TabIndex        =   0
  102.       Top             =   1920
  103.       Width           =   9660
  104.    End
  105.    Begin VB.Label lbDesc 
  106.       Caption         =   $"frmMain.frx":0000
  107.       Height          =   5775
  108.       Left            =   9840
  109.       TabIndex        =   5
  110.       Top             =   120
  111.       Width           =   2535
  112.    End
  113. End
  114. Attribute VB_Name = "frmMain"
  115. Attribute VB_GlobalNameSpace = False
  116. Attribute VB_Creatable = False
  117. Attribute VB_PredeclaredId = True
  118. Attribute VB_Exposed = False
  119. Option Explicit 'Force variables to be defined
  120.  
  121. 'Declarations
  122. Private Declare Function TransparentBlt Lib "msimg32.dll" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal crTransparent As Long) As Boolean
  123. Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
  124. Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
  125. Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
  126. Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
  127. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
  128. Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
  129. Private Declare Function PlgBlt Lib "gdi32" (ByVal hdcDest As Long, lpPoint As POINTAPI, ByVal hdcSrc As Long, ByVal nXSrc As Long, ByVal nYSrc As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hbmMask As Long, ByVal xMask As Long, ByVal yMask As Long) As Long
  130. Private Declare Function CHOOSECOLOR Lib "comdlg32.dll" Alias "ChooseColorA" (pChoosecolor As CHOOSECOLOR) As Long
  131.  
  132. 'Types
  133. Private Type POINTAPI
  134.    X As Long
  135.    Y As Long
  136. End Type
  137.  
  138. Private Type CHOOSECOLOR 'Used for selecting the backcolor in this example
  139.   lStructSize As Long
  140.   hwndOwner As Long
  141.   hInstance As Long
  142.   rgbResult As Long
  143.   lpCustColors As String
  144.   flags As Long
  145.   lCustData As Long
  146.   lpfnHook As Long
  147.   lpTemplateName As String
  148. End Type
  149.  
  150. Dim Carrier As Long                               'DC for Carrier sprite strip
  151. Dim Arbiter As Long                               'DC for Arbiter sprite strip
  152. Dim CarrierPos As POINTAPI                        'Center point for Carrier
  153. Dim ArbiterPos As POINTAPI                        'Center point for Arbiter
  154. Dim cHeight As Single, cWidth As Single           'Dimensions of Carrier after scaling
  155. Dim aHeight As Single, aWidth As Single           'Dimensions of Arbiter after scaling
  156. Dim oldX As Single, oldY As Single                'Previous mouse positions
  157.  
  158.  
  159. Private Const Carrier_Height = 97                 'Default Carrier height
  160. Private Const Carrier_Width = 124                 'Default Carrier width
  161. Private Const Arbiter_Height = 63                 'Default Arbiter height
  162. Private Const Arbiter_Width = 74                  'Default Arbiter width
  163. Private Const PI = 3.14159265238                  'PI
  164.  
  165. 'Loads an image on your hard drive to DC memory
  166. Public Function LoadPic(DestID As Long, Path As String) As Long
  167.   DestID = CreateCompatibleDC(GetDC(0))
  168.   LoadPic = SelectObject(DestID, LoadPicture(Path))
  169. End Function
  170.  
  171. 'Unloads an image stored in DC memory to prevent memory leaks
  172. Public Function UnloadPic(SrcID As Long) As Long
  173.   UnloadPic = DeleteDC(SrcID)
  174. End Function
  175.  
  176. 'Distance Formula - Gets the distance between two points...(x1,y1) & (x2,y2)
  177. Public Function GetDistance(ByVal X1 As Single, ByVal Y1 As Single, ByVal X2 As Single, ByVal Y2 As Single) As Single
  178.   GetDistance = Sqr(((X2 - X1) ^ 2) + ((Y2 - Y1) ^ 2))
  179. End Function
  180.  
  181. 'Returns the angle between 3 points
  182. Public Function GetAngle(ByVal Ax As Single, ByVal Ay As Single, ByVal Bx As Single, ByVal By As Single, ByVal Cx As Single, ByVal Cy As Single, Optional ByVal FullRotate As Boolean = True) As Single
  183.   Dim PointGrid(1) As Single
  184.   Dim AngleOut As Single
  185.   
  186.   'First we have to get the dot product of our 3 points
  187.   PointGrid(0) = (Ax - Bx) * (Cx - Bx) + (Ay - By) * (Cy - By) 'Dot Product
  188.   'Now we get the cross product of our 3 points
  189.   PointGrid(1) = (Ax - Bx) * (Cy - By) - (Ay - By) * (Cx - Bx) 'Cross Product
  190.   
  191.   'Next we need to get the ArcTangent of our points
  192.   If Abs(PointGrid(0)) < 0.0001 Then
  193.     AngleOut = PI / 2
  194.   Else
  195.     AngleOut = Abs(Atn(PointGrid(1) / PointGrid(0)))
  196.   End If
  197.   
  198.   'If our angle is between 0 and 180
  199.   If PointGrid(0) < 0 Then
  200.     AngleOut = PI - AngleOut
  201.   End If
  202.   
  203.   'If our angle if between 180 and 360
  204.   'Returns a negative angle if above 180....e.g. 300 degrees = -60 degrees
  205.   If PointGrid(1) < 0 Then
  206.     AngleOut = -AngleOut
  207.   End If
  208.   
  209.   'Get the whole numbers of the angle
  210.   AngleOut = Format$(AngleOut / PI * 180, "0")
  211.   
  212.   'If you want the full 360 degree without using the negative angles
  213.   'we have to subtract the negative angle from 360
  214.   If FullRotate And AngleOut < 0 Then
  215.     AngleOut = 360 + AngleOut
  216.   End If
  217.   
  218.   GetAngle = AngleOut
  219. End Function
  220.  
  221. 'For setting the background color of our play field
  222. Private Sub Command1_Click()
  223.   Dim NewColor As Long
  224.   
  225.   NewColor = ShowColor 'calls the ShowColor() function
  226.   
  227.   If NewColor <> -1 Then 'if a color was chosen
  228.     picField.BackColor = NewColor 'set backcolor
  229.     Call picField_MouseMove(0, 0, oldX, oldY) 'Refresh our sprites
  230.   End If
  231. End Sub
  232.  
  233. 'Opens a Choose Color dialog box
  234. Private Function ShowColor() As Long
  235.     Dim cc As CHOOSECOLOR
  236.     Dim Custcolor(16) As Long
  237.     Dim lReturn As Long
  238.  
  239.     'set the structure size
  240.     cc.lStructSize = Len(cc)
  241.     'Set the owner
  242.     cc.hwndOwner = Me.hwnd
  243.     'set the application's instance
  244.     cc.hInstance = App.hInstance
  245.     'set the custom colors (converted to Unicode)
  246.     cc.lpCustColors = StrConv(CustomColors, vbUnicode)
  247.     'no extra flags
  248.     cc.flags = 0
  249.  
  250.     'Show the 'Select Color'-dialog
  251.     If CHOOSECOLOR(cc) <> 0 Then
  252.         ShowColor = cc.rgbResult
  253.         CustomColors = StrConv(cc.lpCustColors, vbFromUnicode)
  254.     Else
  255.         ShowColor = -1
  256.     End If
  257. End Function
  258.  
  259. Private Sub Form_Load()
  260.   lbDesc.Caption = Replace(lbDesc.Caption, "#", vbCrLf)
  261.   LoadPic Carrier, App.Path & "\Protoss - Carrier.bmp" 'Load the Carrier sprite strip into memory
  262.   LoadPic Arbiter, App.Path & "\Protoss - Arbiter.bmp" 'Load the Arbiter sprite strip into memory
  263.   CarrierPos.X = (picField.ScaleWidth / 3) 'Sets the Carrier x position to 1/3rd into the workspace
  264.   CarrierPos.Y = (picField.ScaleHeight / 2) - (Carrier_Height / 2) 'Sets the Carrier y position to the center
  265.   ArbiterPos.X = ((picField.ScaleWidth / 3) * 2) 'Set the Arbiter x position to 2/3rds into the workspace
  266.   ArbiterPos.Y = (picField.ScaleHeight / 2) - (Arbiter_Height / 2) 'Sets the Arbiter y position to the center
  267.   cWidth = Carrier_Width: cHeight = Carrier_Height 'Sets Carrier to default width/height
  268.   aWidth = Arbiter_Width: aHeight = Arbiter_Height 'Sets Arbiter to default width/height
  269.   Call picField_MouseMove(0, 0, 0, 0) 'Draw our sprites
  270. End Sub
  271.  
  272. 'unloads our sprite strips from memory
  273. Private Sub Form_Terminate()
  274.   UnloadPic Carrier
  275.   UnloadPic Arbiter
  276. End Sub
  277.  
  278. 'same as above
  279. Private Sub Form_Unload(Cancel As Integer)
  280.   UnloadPic Carrier
  281.   UnloadPic Arbiter
  282. End Sub
  283.  
  284. 'This is where our main coding is done...You can do this in a separate sub if needed...This is just an example
  285. Private Sub picField_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  286.   Dim cAngle As Single, cOffset As Single
  287.   Dim aAngle As Single, aOffset As Single
  288.   Dim sTime As Long, eTime As Long
  289.   
  290.   oldX = X: oldY = Y 'set old x,y positions
  291.   
  292.   'determine the angle of the carrier ship from it's center to the mouse cursor
  293.   cAngle = GetAngle(CarrierPos.X + 1, CarrierPos.Y, CarrierPos.X, CarrierPos.Y, X, Y) 'gets the angle
  294.   cOffset = Round(cAngle / 11.25) '360 degrees DIVIDED BY 32 Frames in our sprite strip = 11.25 degrees in between frames
  295.   If cOffset = 32 Then cOffset = 0 'Error correction
  296.   
  297.   'same as above except for the arbiter ship
  298.   aAngle = GetAngle(ArbiterPos.X + 1, ArbiterPos.Y, ArbiterPos.X, ArbiterPos.Y, X, Y)
  299.   aOffset = Round(aAngle / 11.25)
  300.   If aOffset = 32 Then aOffset = 0
  301.   
  302.   picField.Cls 'clear our workspace
  303.   
  304.   'Transparent Blit both the ships at their positions
  305.   'We take from the sprite strip loaded into memory starting at the position determined above.
  306.   'e.g. offset * default_width
  307.   TransparentBlt picField.hdc, CarrierPos.X - (cWidth / 2), CarrierPos.Y - (cHeight / 2), cWidth, cHeight, Carrier, (cOffset * Carrier_Width), 0, Carrier_Width, Carrier_Height, vbCyan
  308.   TransparentBlt picField.hdc, ArbiterPos.X - (aWidth / 2), ArbiterPos.Y - (aHeight / 2), aWidth, aHeight, Arbiter, (aOffset * Arbiter_Width), 0, Arbiter_Width, Arbiter_Height, vbCyan
  309.   
  310.   picField.Refresh 'refresh our workspace
  311.   
  312.   'This is just debugging information for the purpose of this tutorial
  313.   picInfoC.Cls
  314.   picInfoA.Cls
  315.   
  316.   picInfoC.FontBold = True
  317.   picInfoC.Print "Carrier Information"
  318.   picInfoC.FontBold = False
  319.   picInfoC.Print "Mouse Angle......" & cAngle
  320.   picInfoC.Print "Mouse Distance..." & Round(GetDistance(CarrierPos.X, CarrierPos.Y, X, Y))
  321.   picInfoC.Print "Sprite Frame....." & cOffset + 1
  322.   picInfoC.Print "Width............" & cWidth
  323.   picInfoC.Print "Height..........." & cHeight
  324.   picInfoC.Print "Position (x,y)..." & CarrierPos.X & "," & CarrierPos.Y
  325.   picInfoC.Print "Scale............" & sScaleCarrier.Value & "%"
  326.   
  327.   picInfoA.FontBold = True
  328.   picInfoA.Print "Arbiter Information"
  329.   picInfoA.FontBold = False
  330.   picInfoA.Print "Mouse Angle......" & aAngle
  331.   picInfoA.Print "Mouse Distance..." & Round(GetDistance(ArbiterPos.X, ArbiterPos.Y, X, Y))
  332.   picInfoA.Print "Sprite Frame....." & aOffset + 1
  333.   picInfoA.Print "Width............" & aWidth
  334.   picInfoA.Print "Height..........." & aHeight
  335.   picInfoA.Print "Position (x,y)..." & ArbiterPos.X & "," & ArbiterPos.Y
  336.   picInfoA.Print "Scale............" & sScaleArbiter.Value & "%"
  337.   
  338.   picInfoC.Refresh
  339.   picInfoA.Refresh
  340. End Sub
  341.  
  342. 'Sets the (x,y) coordinates of our 2 ships
  343. Private Sub picField_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
  344.   If Button = vbLeftButton Then
  345.     CarrierPos.X = X: CarrierPos.Y = Y 'if the left button is clicked move the Carrier to that position
  346.   ElseIf Button = vbRightButton Then
  347.     ArbiterPos.X = X: ArbiterPos.Y = Y 'if the right button is clicked move the Arbiter to that position
  348.   End If
  349. End Sub
  350.  
  351. 'This is to set the scale our ships are painted at
  352. '(Ship's Width) * (ScalePercent / 100)
  353. Private Sub sScaleCarrier_Change()
  354.   cWidth = Round(Carrier_Width * (sScaleCarrier.Value / 100))
  355.   cHeight = Round(Carrier_Height * (sScaleCarrier.Value / 100))
  356.   Call picField_MouseMove(0, 0, oldX, oldY)
  357. End Sub
  358.  
  359. Private Sub sScaleCarrier_Scroll()
  360.   cWidth = Round(Carrier_Width * (sScaleCarrier.Value / 100))
  361.   cHeight = Round(Carrier_Height * (sScaleCarrier.Value / 100))
  362.   Call picField_MouseMove(0, 0, oldX, oldY)
  363. End Sub
  364.  
  365. Private Sub sScaleArbiter_Change()
  366.   aWidth = Round(Arbiter_Width * (sScaleArbiter.Value / 100))
  367.   aHeight = Round(Arbiter_Height * (sScaleArbiter.Value / 100))
  368.   Call picField_MouseMove(0, 0, oldX, oldY)
  369. End Sub
  370.  
  371. Private Sub sScaleArbiter_Scroll()
  372.   aWidth = Round(Arbiter_Width * (sScaleArbiter.Value / 100))
  373.   aHeight = Round(Arbiter_Height * (sScaleArbiter.Value / 100))
  374.   Call picField_MouseMove(0, 0, oldX, oldY)
  375. End Sub
  376.