Functions to ChangeSurface and Restore the current surface settings

Started by kevin, February 06, 2012, 11:50:52 AM

Previous topic - Next topic

kevin

Functions to ChangeSurface and Restore the current surface settings


  This example includes a couple of function that will preserve and restore the current state (Surface, Ink Colour, Draw mode, Pen Position etc). These can be used when writing functions that are going to be altering the current state inside them.  So what you do is call the ChangeSurface() function at the start of the your function,  do whatever you need do, then call the RestoreSurface() function upon close.  


PlayBASIC Code: [Select]
   Type tRememberState
Status
CurrentSurfaceIndex
CurrentFont
CurrentInkMode
CurrentInkColour
PenX
PenY
EndType
Dim RememberStateStack(0) as tRememberState


Psub ChangeSurface(ThisIMage)
Index=GetFreeCell(RememberStateStack())

RememberStateStack(Index)= new tRememberState
RememberStateStack(Index).Status=true
RememberStateStack(Index).CurrentSurfaceIndex=GetSurface()
RememberStateStack(Index).CurrentFont =GetCurrentFont()
RememberStateStack(Index).CurrentInkColour =GetInk()
RememberStateStack(Index).CurrentInkMode =GetInkMode()
RememberStateStack(Index).PenX =GetCursorX()
RememberStateStack(Index).PenY =GetCursorY()

rendertoimage ThisIMage

EndPsub


Psub RestoreSurface()
Index=0
For lp =GetArrayElements(RememberStateStack()) to 1 step -1
if RememberStateStack(lp).Status=true
Index=lp
exitfor
endif
next
if Index>0
Rendertoimage RememberStateStack(Index).CurrentSurfaceIndex
setfont RememberStateStack(Index).CurrentFont
ink RememberStateStack(Index).CurrentInkColour
inkmode RememberStateStack(Index).CurrentInkMode
SetCursor RememberStateStack(Index).PenX,RememberStateStack(Index).PenY
RememberStateStack(Index) = null
endif

EndPsub


; -------------------------------------------------------------------------------------------
; USAGE EXAMPLE BELLOW HERE
; -------------------------------------------------------------------------------------------

Function MakeIMage(Width,Height)

ThisIMage=NewImage(Width,Height)
ChangeSurface(ThisIMage)

For lp =0 to 10
Ink rndrgb()
setcursor rnd(width),rnd(height)
print "STUFF"
next


RestoreSurface()

EndFunction ThisIMage




Image=MakeIMage(256,256)


cls 255
print IMage
drawimage Image,200,200,false


Sync
Waitkey







LemonWizard

Wow kevin this is brilliant.

I really wish I could have your understanding of programming and your intuitive skills.
I think I'm getting closer though hehe  ;)

Maybe in another century =p

But yeah this is awesome, I'll have to try it out later.

btw I still has no internet at home. it is teh suck.