News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

Execution Stack Example

Started by kevin, March 25, 2015, 12:30:31 PM

Previous topic - Next topic

kevin

  Execution Stack Example

   This is more a conceptual demonstration than a shining example.   The primary idea here is that we can create a stack of execution events and then call them at run time.   Since the order is created at run time and not at compile time, we can insert / change the order as the program is executing.  

   In the following example source code bellow, we're drawing a scene made up of only two functions for the sake of simplicity.  When the program starts, we adds our two render functions to the stack that we want to be called during each Render() call.     The Render() Function runs through our array of stacked events calls the function(s) if they exist.  

 
PlayBASIC Code: [Select]
/*
define the order that Render() should darw the screen.
render will call each function in the execution stack from first
to last.
*/


AddToExectionStack("ClearScreen")
AddToExectionStack("DrawMessages")


// --------------------------------------------------------------------
// MAIN LOOP
// --------------------------------------------------------------------
Do
render()
loop

end



// Render Functions

Function CLearScreen()
cls $102030
EndFunction



Function DrawMessages()
For ylp =0 to getsurfaceheight() step GetTextHeight("|")
text 0,ylp,"Hello World........."
next
EndFunction






/*
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
---[ EXECUTION STACK ]------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------

This is bare bones version to introduce you to the concept. SO we don't
have any fancy depth sorting or management functiosn that could be attached
to each exectuion event in the stack.

*/



Type tExecutionStack
NameOfFunction$
IndexOfFunction
EndType

Dim ExeStack(0) as tExecutionStack


Function AddToExectionStack(Name$)

if getarrayStatus(ExeStack())=false
Dim ExeStack(0) as tExecutionStack
endif

index=getfreecell(ExeStack())

ExeStack(index)=new tExecutionStack
ExeStack(index).NameOfFunction=Name$
ExeStack(index).IndexOfFunction=FunctionIndex(name$)

EndFunction Index



Function Render()

if getarrayStatus(ExeStack())
// find the depth of the stackl
for lp=1 to getarrayelements(ExeStack())

if ExeStack(lp)

Index=ExeStack(lp).IndexOfFunction
if Index=0
index=FunctionIndex(ExeStack(lp).NameOfFunction$)
ExeStack(lp).IndexOfFunction=Index
endif

if Index>0
CallFunction Index
endif


endif
next
endif

sync
EndFunction






    Related Examples:

         * Action GUI
         * Abstraction Layer