Date: 02-16-2022
Return to Index
created by gbSnippets
'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 DWord, hLst as DWord
Global canvasW as Long, canvasH as Long
%ID_Graphic = 300 : %ID_Timer = 400
%Delta = 2 : %sMax = 1000
Function PBMain() As Long
Dim sprite(%sMax)
canvasW = 900 : canvasH = 600
Dialog New Pixels, 0, "Graphic Control Test",150,150,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, 40, ByVal %NULL) 'uses callback messages
Case %WM_Timer
Local qFreq, qOverhead, qElapsed as Quad
QueryPerformanceFrequency qFreq 'clock frequency
Tix qOverHead : Tix qOverHead : Tix End qOverHead 'Tix overhead (done twice per Intel)
Tix qElapsed 'start ticks
MoveSprites : DrawSprites : Graphic Redraw
Tix End qElapsed 'end ticks
Dialog Set Text hDlg, Format$((qElapsed-qOverHead)*1000/qFreq, "####.#") 'display ms for the loop
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 100
ImageList Add Icon hLst, "mos" + Format$(i,"000")
Next i
For i = 0 To %sMax
sprite(i).w = 32
sprite(i).h = 32
sprite(i).x = Rnd(0,canvasW-sprite(i).w)
sprite(i).y = Rnd(0,canvasH-sprite(i).h)
sprite(i).sx = Rnd(2,4)
sprite(i).sy = Rnd(2,4)
sprite(i).dx = sprite(i).sx * Sgn(Rnd(-10,10))
sprite(i).dy = sprite(i).sy * Sgn(Rnd(-10,10))
Graphic Bitmap New 32,32 To hBMP
sprite(i).hBMP = hBMP
Graphic Attach hBMP,0
Graphic ImageList (0,0), hLst, i Mod 100 ,0,%ILD_Normal
Graphic Attach hDlg, %ID_Graphic, Redraw
Next i
End Sub
'gbs_00456
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm