I'm playing around with pointers, lists, and "constructing" procedures. Here is my test code:
Code: Select all
Structure WALKER
x.f
y.f
EndStructure
Procedure Walker_new(x.f, y.f)
*w.WALKER = AllocateMemory(SizeOf(WALKER))
*w\x = x
*w\y = y
ProcedureReturn *w
EndProcedure
Procedure Walker_move(*w.WALKER)
Select Random(3)
Case 0
*w\x + 1
Case 1
*w\x - 1
Case 2
*w\y + 1
Case 3
*w\y - 1
EndSelect
EndProcedure
Procedure Walker_draw(*w.WALKER)
FrontColor(RGB(255,255,255))
Plot(*w\x, *w\y)
EndProcedure
; -----------------------------------------------------------------------
;- Open window
If Not InitSprite() | InitKeyboard()
MessageRequester("Error", "Initialization error.", #PB_MessageRequester_Ok)
End
EndIf
If Not OpenWindow(0, 100, 100, 640, 340, "Random Walker", #PB_Window_ScreenCentered|#PB_Window_TitleBar|#PB_Window_SystemMenu)
MessageRequester("Error", "Error opening window.", #PB_MessageRequester_Ok)
End
EndIf
If Not OpenWindowedScreen(WindowID(0), 0, 0, WindowWidth(0), WindowHeight(0))
MessageRequester("Error", "Error opening window.", #PB_MessageRequester_Ok)
End
EndIf
NewList *ws.WALKER()
AddElement(*ws())
*ws() = Walker_new(ScreenWidth()/2,ScreenHeight()/2)
AddElement(*ws())
*ws() = Walker_new(ScreenWidth()/3,ScreenHeight()/2)
AddElement(*ws())
*ws() = Walker_new(ScreenWidth()*2/3,ScreenHeight()/2)
w.WALKER
w\x = 5
w\y = 10
Walker_move(@w)
;- Start loop
ClearScreen(RGB(255,255,255))
CreateSprite(0,ScreenWidth(),ScreenHeight())
Repeat
; Process window events
Repeat
Event = WindowEvent()
Select (Event)
Case #PB_Event_CloseWindow
End
EndSelect
Until Event = 0
; Process keyboard
ExamineKeyboard()
If KeyboardReleased(#PB_Key_Escape)
PostEvent(#PB_Event_CloseWindow)
EndIf
; Simulation
ForEach *ws()
Walker_move(*ws())
Next
; Drawing to sprite before display
StartDrawing(SpriteOutput(0))
ForEach *ws()
Walker_draw(*ws())
Next
StopDrawing()
DisplaySprite(0,0,0)
FlipBuffers()
ForEver
2) Is that a good way to make a list of pointers (*ws())? Does this code make sense?
3) I am drawing to a sprite and then drawing this to the screen. Is this an efficient way to use the 2D Drawing commands on Mac? I seemed to get "interesting" results drawing to the screen (ScreenOutput()) directly, without clearing the backbuffer using ClearScreen(). Is this a good way of doing 2D drawing, not using sprites?
(And yes, I realize I am needlessly looping through my list twice...

Thanks!
