But I'm still confused about some of the variables here, I was able to remove some code from your program and it still works perfectly like (Oldsurface).
Just in case you're not sure, The point of the asking PB what the current surface is, then restoring it at the end of the function. Is cleanliness. If your writing a function that alters the current surface, ink colour, current font or whatever. Then it's best to preseve these before you change them, so the exit state can be restored upon exit. So when you call your function from other parts of your program, the function doesn't unexpectedly change the state in some way.
Also when it comes to the "FindDistancetoImpact" function, I can't seem to figure out what a few of the codes is doing, when I saw NullPixel=(0,0) and removed it, the ball would suddenly start flying up pass the top of the screen, so I would like to know what is this controlling???
Bellow is taken from the manual,
* Note: Before you can use PixelRunLength safely. You must lock the draw surface, then it's highly recommended you read a pixel from this surface using Point(). This will ensure the address of the surface are seeded correctly internally for PixelRunLength
When we're drawing or reading from a graphics surface (the screen or an Image), the Pb gfx engine has certain fixed overheads that come doing any operation. Primarly it has to work out the correct rendering method for the surface your drawing to, then prepare it self for this operation. The preparation is seeding the internally buffer pointer and pixel format. You can think of this overhead as nanny code. So the nanny code protects you from the real world underneath!
While most built in commands in PB have nanny code in them, some don't.
PixelRunLength is one of them.
FastPoint &
FastDot are some others. These commands have no nanny code protection, given they're going to be called in high frequency. As while the nanny code might save you from poking your eye out, it can add considerable overhead to every operation.
function FindDistancetoImpact(img,Xpos,Ypos)
if Xpos>-1 and Xpos<getimagewidth(img) //If Xpos is more than -1 and less than imagewidth()"800x600" do this...
if Ypos<0 then Ypos=0
OldSurface=getsurface()
rendertoimage img
lockbuffer
NullPixel = Point(0,0)
Runs=PixelRunLength(Xpos,Ypos,0,1,RGB(0,0,0))-1
unlockbuffer
rendertoimage oldsurface
endif
endfunction Runs
In the function above, what i'm doing here is first preserving the the current surface the GFX engine is rendering to, then I'm changing to the surface in question then locking it. The thing is, since we haven't drawn anything to this surface, we can't be sure if the internal frame buffer pointers and pixel formats have been initialized. But we can do this via using POINT(). When we call point, the nanny code within it (the core GFX engine) will work out the type of the surface and rendering routines we've selected , then initialize the pointers internally. Now given that PixelRunLength() has no nanny code in it, it simply uses gfx engines the current settings.
And the rendertoimage img and rendertoimage oldsurface, usually I thought I was suppose to always type "rendertoscreen" when I am done, so how does this work?
No.