Hi dobro,
the Proc "GrabArrayFromSprite()" in the following code grabs a sprite into an Array, as you've requested.
Because it's quite slippy, it might be of some use for you:

(On my WinXP-System with a GForce 8600GTS I get around 330 FPS (including the BGR->RGB conversion (which is needed on my system) and without plotting the grabbed Data to the demo-image).)
Code: Select all
EnableExplicit
#SprWidth = 320 ; <= Just change the Sprite-Dimensions
#SprHeight = 240 ; to the size you want.
Dim myPixel.l(0,0)
InitSprite()
Define x, y, StartTime, Frames, Event
Procedure ConvertArrayPixelFormat(Array Points.l(2))
Protected Width = ArraySize(Points(),2)
Protected Height = ArraySize(Points(),1)
Protected x,y, col
For x = 0 To Width
For y = 0 To Height
col = Points(y,x)
Points(y,x) = (col & $FF00FF00) | ((col & $FF) << 16) | ((col & $FF0000) >> 16)
Next
Next
EndProcedure
Procedure GrabArrayFromSprite(Sprite, Array Points.l(2), TargetFormat = #PB_PixelFormat_32Bits_RGB)
Protected ReturnValue = #False
Protected PixelFormat
If IsSprite(Sprite)
If StartDrawing(SpriteOutput(Sprite))
PixelFormat = DrawingBufferPixelFormat()
If PixelFormat = #PB_PixelFormat_32Bits_BGR Or PixelFormat = #PB_PixelFormat_32Bits_RGB
Protected Width = SpriteWidth(Sprite)
Protected Height = SpriteHeight(Sprite)
Protected dbWidth = DrawingBufferPitch() >> 2
If ArraySize(Points(),1) <> Height-1 Or ArraySize(Points(),2) <> dbWidth-1
Dim Points(Height-1,dbWidth-1)
EndIf
CopyMemory(DrawingBuffer(), @Points(0,0), (dbWidth << 2) * Height)
If TargetFormat <> PixelFormat
ConvertArrayPixelFormat(Points())
EndIf
ReturnValue = #True
Else
Debug "Unsupported Pixelformat"
EndIf
StopDrawing()
EndIf
Else
Debug "Sprite Nr. " + Str(Sprite) + " does not exist."
EndIf
ProcedureReturn ReturnValue
EndProcedure
If OpenWindow(0,0,0,#SprWidth*2+10,#SprHeight + 30,"", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
If OpenWindowedScreen(WindowID(0), 0, 0, #SprWidth, #SprHeight, 0, 0, 0 , #PB_Screen_NoSynchronization)
If CreateImage(0, #SprWidth, #SprHeight) And CreateSprite(0,#SprWidth, #SprHeight); , #PB_Sprite_Memory)
ImageGadget(0,#SprWidth + 10, 0, #SprWidth, #SprHeight, ImageID(0))
CheckBoxGadget(1, #SprWidth - 70, #SprHeight + 5, 150, 20, "plot grabbed Array to Image")
SetGadgetState(1,#True)
StartTime = ElapsedMilliseconds()-1
Repeat
; ----- 1st, draw circles into the Demo-Sprite
If StartDrawing(SpriteOutput(0))
For x = 1 To 30
Circle(Random(#SprWidth), Random(#SprHeight), Random(100)+10, Random($bbbbbb)+$222222)
Next
StopDrawing()
EndIf
; ----- 2nd, Grab the Sprite to the Array
If Not GrabArrayFromSprite(0, myPixel())
Debug "Cannot grab Array from Sprite."
End
EndIf
; ----- 3rd, plot grabbed Data onto the Demo-Image, if CheckBox is checked
If GetGadgetState(1)
If StartDrawing(ImageOutput(0))
For x = 0 To #SprWidth-1
For y = 0 To #SprHeight-1
Plot(x,y, myPixel(y,x))
Next
Next
StopDrawing()
EndIf
SetGadgetState(0, ImageID(0))
EndIf
; ----- 4th, display FPS-Info in Window-Title
Frames + 1
SetWindowTitle(0, "GrabArrayFromSprite() Demo - "+Str(Frames * 1000 / (ElapsedMilliseconds() - StartTime))+" Frames/Sec")
; ----- 5th, display Sprite on Screen to compare Original and Copy
DisplaySprite(0,0,0)
FlipBuffers()
Repeat
Event = WindowEvent()
If Event = #PB_Event_Gadget And EventGadget() = 1 : StartTime = ElapsedMilliseconds()-1 : Frames = 0 : EndIf
If Event = #PB_Event_CloseWindow : Break 2 : EndIf
Until Not Event
ForEver
EndIf
EndIf
EndIf
The only Drawback you have to deal with is, that the Array-Coordinates are Pixel(
y,
x) instead of Pixel(
x,
y).
I hope you can use it anyway.
And don't forget to turn of the debugger if you test it.
Greetz, PL.