Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

setContext overload

edited September 2015 in Code Sharing Posts: 11

Hi.
There is my version of setContext function overload.
It have stack-like structure(in the bottom it have context of main screen in top - current context).
It suported time-access priority(the one who locked access earlier have larger priority) with kye sharing system.

For what reason it can be used:

Sometimes in my project i use containers that includes objects that i want to draw on container buffer image, but some of it can use constructions like this:

setContext(buff)
 ...some stuff... 
setContext()

multiple amount of times in it draw function if it uses its own buffer to draw or draws on mesh texture.
But every typical call of not overloaded setContext will broke the context of my container buffer image and all next drawings will occur in main context(context of main screen). And it is bad.
So i write some kind of system that will automatically sets previous context on setContext() call(with no parameters), and if you lock some layer of context - will not allow you to pass dipper to context stack bottom.
So now if you want that setContext() sets some context(but not main) as default just set it with setContext(myContext) and than lock it with kye = context_stack.lockContext() and unlock with context_stack.unlockContext(kye) and no matter how many times you will call setContext() it will always set locked context, of course after pop all your previous contexts. So you need to control amount of your current context layers, but you can not be worried about locked context.
It uses only one global object context_stack to save all self variables and functions. So if you need it, than please don't use name context_stack.
Old version of setContext function stored in context_stack.sContext.
I would be very grateful if you can help me with finding bugs in this system.
Thanks for reading. Have a good day)

_G["context_stack"]={esp=0,ebp=0,kye=1,stack={},state={},sContext=_G["setContext"]}

function setContext(context)
    if context then
        context_stack.pushContext(context)  
    else
        context_stack.popContext()
    end
end

function context_stack.pushContext(context)
    context_stack.esp = context_stack.esp + 1
    table.insert(context_stack.stack, 0)
    context_stack.stack[#context_stack.stack] = context
    context_stack.sContext(context_stack.stack[context_stack.esp+context_stack.ebp])
end

function context_stack.popContext()
    if context_stack.esp < 2 and context_stack.ebp == 0 and #context_stack.stack < 2 then
        context_stack.esp = 0
        context_stack.stack = {}
        context_stack.state = {}
        context_stack.sContext()
    elseif context_stack.esp > 0 and #context_stack.stack > context_stack.ebp then 
        context_stack.esp = context_stack.esp - 1
        context_stack.sContext()
        context_stack.sContext(context_stack.stack[context_stack.esp+context_stack.ebp])
        table.remove(context_stack.stack)
    end
end

function context_stack.lockContext()
    table.insert(context_stack.state, context_stack:newkye())
    table.insert(context_stack.state, context_stack.ebp)
    table.insert(context_stack.state, context_stack.esp)
    context_stack.ebp = context_stack.esp
    context_stack.esp = 0
    context_stack.newkye()
    return context_stack.state[#context_stack.state-2]
end

function context_stack.unlockContext(kye)
    for i,v in ipairs(context_stack.state) do
        if v == kye then
            context_stack.ebp = context_stack.state[i+1]
            context_stack.esp = context_stack.state[i+2]
            for j = context_stack.ebp+context_stack.esp+1, #context_stack.stack do
                context_stack.stack[j]=nil
            end
            for j = i, #context_stack.state do
                context_stack.state[j]=nil
            end
            return true
        end
    end
    return false
end

function context_stack.newkye()
    context_stack.kye = math.fmod(math.sqrt(os.time()), context_stack.kye+1)
    return context_stack.kye+0
end

Comments

Sign In or Register to comment.