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
sx As Long
sy As Long
hBMP as Long
End Type
Global hDlg As DWord, sprite() As SpriteInfo, iCount as Long
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",300,300,400,400, %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
DefineBalls
SetTimer(CB.Hndl, %ID_Timer, 20, ByVal %NULL) 'uses callback messages
Case %WM_Timer
Incr iCount : Dialog Set Text hDlg, Str$(iCount)
MoveBalls
DrawBalls
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 DrawBalls
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 MoveBalls
Local i As Long
For i = 0 To %sMax
sprite(i).x = sprite(i).x + sprite(i).sx
sprite(i).y = sprite(i).y + sprite(i).sy
If sprite(i).x < 0 Then
sprite(i).x = 0
sprite(i).sx = sprite(i).sx
ElseIf sprite(i).x + sprite(i).w > canvasW Then
sprite(i).x = canvasW - sprite(i).w
sprite(i).sx = - sprite(i).sx
End If
If sprite(i).y < 0 Then
sprite(i).y = 0
sprite(i).sy = sprite(i).sy
ElseIf sprite(i).y + sprite(i).h > canvasH Then
sprite(i).y = canvasH - sprite(i).h
sprite(i).sy = - sprite(i).sy
End If
Next i
End Sub
Sub DefineBalls
Local i As Long
Randomize
For i = 0 To %sMax
sprite(i).w = Rnd(5,30)
sprite(i).h = Rnd(5,30)
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).sx = sprite(i).sx '* Sgn(Rnd(-1,+1))
sprite(i).sy = sprite(i).sy '* Sgn(Rnd(-1,+1))
Graphic Bitmap Load "cowgirl", 32, 32 To sprite(i).hBMP
Next i
End Sub
'gbs_00454
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm