Sprite 06 - Array of Static Images (Icons), Single Frame

Category: Sprite Tutor Series

Date: 02-16-2022

Return to Index


 
'Compilable Example:  (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
#Resource "gbsnippets.pbr"
 
Type SpriteInfo
   w As Long
   h As Long
   x As Long
   y As Long
   dx As Long
   dy As Long
   sx As Long
   sy As Long
   hBMP as Long
End Type
 
   Global hDlg As DWord, sprite() As SpriteInfo, iCount as Long
   Global hBMP as DWordhLst as DWord
   Global canvasW as Long, canvasH as Long
   %ID_Graphic = 300 : %ID_Timer = 400
   %Delta = 2 : %sMax = 100
 
Function PBMain() As Long
   Dim sprite(%sMax)
   canvasW = 900 : canvasH = 600
   Dialog New Pixels, 0, "Graphic Control Test",300,300,canvasW,canvasH, %WS_SysMenu, 0 To hDlg
   Control Add Graphic, hDlg, %ID_Graphic,"", 0,0,canvasW,canvasH, %WS_Visible Or %SS_Sunken
   Graphic Attach hDlg, %ID_Graphic, Redraw
   Graphic Color %Black,%White
   Graphic Clear
   Dialog Show Modal hDlg Call DlgProc
End Function
 
CallBack Function DlgProc() As Long
   Select Case CB.Msg
      Case %WM_InitDialog
         DefineSprites
         SetTimer(CB.Hndl, %ID_Timer, 20, ByVal %NULL)   'uses callback messages
      Case %WM_Timer
         Incr iCount : Dialog Set Text hDlg, Str$(iCount)
         MoveSprites
         DrawSprites
         Graphic Redraw
      Case %WM_Destroy
         KillTimer CB.Hndl, %ID_Timer
      Case %WM_Command
         If CB.Ctl = %ID_Graphic Then
 
         End If
   End Select
End Function
 
Sub DrawSprites
   Local i as Long
   Graphic Clear
   For i = 0 to %sMax
      Graphic Copy sprite(i).hBMP, 0 To (sprite(i).x, sprite(i).y)
   Next i
End Sub
 
Sub MoveSprites
   Local i As Long
   For i = 0 To %sMax
      sprite(i).x = sprite(i).x + sprite(i).dx
      sprite(i).y = sprite(i).y + sprite(i).dy
 
      If sprite(i).x < 0 Then
         sprite(i).x = 0
         sprite(i).dx = sprite(i).sx
      ElseIf sprite(i).x + sprite(i).w > canvasW Then
         sprite(i).x = canvasW - sprite(i).w
         sprite(i).dx = - sprite(i).sx
      End If
 
      If sprite(i).y < 0 Then
         sprite(i).y = 0
         sprite(i).dy = sprite(i).sy
      ElseIf sprite(i).y + sprite(i).h > canvasH Then
         sprite(i).y = canvasH - sprite(i).h
         sprite(i).dy = - sprite(i).sy
      End If
   Next i
End Sub
 
Sub DefineSprites
   Local i As Long
   Randomize
 
   'create imagelist  w,h,depth,size
   ImageList New Icon 32,32,24,100 To hLst
 
   For i = 0 To %sMax
      sprite(i).w = 32
      sprite(i).h = 32
      sprite(i).x = Rnd(10,canvasW)
      sprite(i).y = Rnd(10,canvasH)
      sprite(i).sx = Rnd(2,4)
      sprite(i).sy = Rnd(2,4)
      sprite(i).dx = sprite(i).sx
      sprite(i).dy = sprite(i).sy
      Graphic Bitmap New 32,32 to hBMP
      sprite(i).hBMP = hBMP
 
      Graphic Attach hBMP,0
      ImageList Add Icon hLst, "mos" + Format$(i,"000")
      Graphic ImageList (0,0), hLst, i,0,%ILD_Normal
 
      Graphic Attach hDlg, %ID_Graphic, Redraw
   Next i
End Sub
 
'gbs_00455
'Date: 03-10-2012


created by gbSnippets
http://www.garybeene.com/sw/gbsnippets.htm