going backwards thru event handler stack

13 views
Skip to first unread message

Joseph Clark

unread,
Mar 26, 2013, 5:55:34 PM3/26/13
to pyglet...@googlegroups.com
Hi all,
I've been using push_handlers() and pop_handlers() to change 'game modes' in the game I'm programming.  My code allows for multiple modes to be alive at once, in a stack.  For example, a mini-map may be active as a "pop up" over the main game display.  The event handler stack is great for this: the mini-map's event handlers are on top, so they get first crack at the keyboard events, then whatever isn't captured falls to the next mode in the stack.
 
My problem is this: with the on_draw() events, I want them to go the other way.  They should start at the *bottom* of the stack and then go up, so that the mini-map will be drawn on top of the regular display, and not vice-versa.  Could any of you tell me the best way to make this happen for the on_draw events?  I can't say that I fully understand how the event handler stack works.
Joe

Winston Wolff

unread,
Mar 26, 2013, 5:58:32 PM3/26/13
to pyglet...@googlegroups.com
Is the thing drawn on top determined by when on_draw is called, or by the Z value? I can't remember but I'd guess the Z value.

-Winston
> --
> You received this message because you are subscribed to the Google Groups "pyglet-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to pyglet-users...@googlegroups.com.
> To post to this group, send email to pyglet...@googlegroups.com.
> Visit this group at http://groups.google.com/group/pyglet-users?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Adam Bark

unread,
Mar 26, 2013, 6:35:04 PM3/26/13
to pyglet...@googlegroups.com
On 26/03/13 21:58, Winston Wolff wrote:
> Is the thing drawn on top determined by when on_draw is called, or by the Z value? I can't remember but I'd guess the Z value.
>
> -Winston
>
> On Mar 26, 2013, at 2:55 PM, Joseph Clark <joecl...@gmail.com> wrote:
>
>> Hi all,
>> I've been using push_handlers() and pop_handlers() to change 'game modes' in the game I'm programming. My code allows for multiple modes to be alive at once, in a stack. For example, a mini-map may be active as a "pop up" over the main game display. The event handler stack is great for this: the mini-map's event handlers are on top, so they get first crack at the keyboard events, then whatever isn't captured falls to the next mode in the stack.
>>
>> My problem is this: with the on_draw() events, I want them to go the other way. They should start at the *bottom* of the stack and then go up, so that the mini-map will be drawn on top of the regular display, and not vice-versa. Could any of you tell me the best way to make this happen for the on_draw events? I can't say that I fully understand how the event handler stack works.
>> Joe
>>
>>
You'll probably need to set the depth buffer on if you want to draw
based on the Z-value ie put
glEnable(GL_DEPTH_TEST) and glDisable(GL_DEPTH_TEST) around your drawing
calls for your mini-map and make sure it is positioned at the front of
the viewport.
If that isn't useful you could have a single on_draw active that calls
the drawing procedures in order.
HTH

Joseph Clark

unread,
Mar 27, 2013, 2:16:33 PM3/27/13
to pyglet...@googlegroups.com
Here's what I ended up doing:
 
I extended pyglet.window.Window so it contains a list of "GameModes", starting with the initial one I specify:
 
class GameWindow( pyglet.window.Window ):
    def __init__( self, gamemode ):
        self._modes = [gamemode] 
        pyglet.window.Window.__init__( self )
I override the GameWindow's on_draw handler so that it clears the screen only once, goes from the "bottom" (left) of the stack to the top (right), and calls a draw() method from each game mode.
 
    def on_draw( self ):
        self.clear()
        for m in self._modes:
            m.draw()
 
The GameMode objects are derived from this one.  Any graphics that are created by the mode are added to a pyglet.graphics.Batch so they can be drawn all at once, and the draw() method can be altered to do updating, etc.
 
class GameMode():
    def __init__(self):
        self.batch = pyglet.graphics.Batch()
    def draw(self):
        self.batch.draw()
Specific GameModes are also defined by other handlers (e.g. on_key_press(), on_text() etc) and these are added to the GameWindow using push_handlers() so they run from top to bottom unlike the draw() methods.
 
It's not quite as elegant as finding a way to reverse the built-in event handler stack, but it works.
Reply all
Reply to author
Forward
0 new messages