On 07/19/2011 12:59 AM, Taco wrote:
Thank you both, I will go for the threaded approach!


Just beware that *all* your graphics/pyglet calls must take place in one thread. Bad things eventually happen if you get this wrong. Other threads can easily communicate with the graphics thread to affect various graphical operations, but they can't do it themselves.

Enjoy


On Jul 19, 4:43 am, Gary Herron<[email protected]>  wrote:
On 07/18/2011 06:32 AM, Taco wrote:

Thanks for your reply Mike,
I'm on Ubuntu linux. The reason I don't want to start the pyglet event
loop is because I want to be able to interact with the program through
the interpreter while it is running, but interpreter input is blocked
while the event loop is running.
Is it possible to handle the events triggered by draw() et al. just
once after calling them and then returning to the interpreter again?
Should I just start the normal event loop for one frame, or will this
result in a large overhead for starting/stopping?
Better than that (perhaps) is this:  You can start the pyglet event loop
and all its graphics operations in a separate thread, and leave the main
thread running the interpreter:

This tiny example starts the pyglet Hello World app in a thread and
keeps the interactive interpreter running in the main thread.  Both are
active and neither interferes with the other.   The down side, of
course, is that any communication between the two threads (say in
response to your interactive input) needs to be done in a thread safe
manner.  (locks,  semaphores, queues, ...)

To run, store in a file, run the interpreter, import that file and type
go().

def App():
      import pyglet
      window = pyglet.window.Window()
      label = pyglet.text.Label('Hello, world',
                                font_name='Times New Roman', font_size=36,
                                x=window.width//2, y=window.height//2,
                                anchor_x='center', anchor_y='center')
      @window.event
      def on_draw():
          window.clear()
          label.draw()
      pyglet.app.run()

import thread
def go():
      thread.start_new_thread(App, ())

Gary Herron







Also, any idea why the call to pg.app.run wont work when I do it in
the interpreter as opposed to in the __main__ block?
On Jul 18, 2:44 pm, Mike Redhorse<[email protected]>    wrote:
The first example won't work because the draw(), clear(), etc
functions, are triggering events. With no running event loop, nothing
will happen. I don't understand why you're not calling app.run()
though. A running event loop shouldn't hurt you at all, surely?
Also, what OS are you on? Event loops differ based on OS.
You could also make your own event loop, and ignore any events you
aren't looking for.
On Jul 18, 1:17 pm, Taco<[email protected]>    wrote:
Hi there,
I've been playing around with pyglet in order to get some offscreen
rendering to work. I want to render simple shapes and convert them to
numpy arrays. Following the advice given 
inhttp://groups.google.com/group/pyglet-users/browse_thread/thread/71cb...
, I created an invisible window and didn't call app.run(). For now,
I'm just rendering on screen. My code looks something like this:
window = pg.window.Window(visible = False)
window.set_size(10,10)
window.clear()
drawTriangle( )
window.flip()
x = screenAsArray() # Does a screencapture and converts to numpy array
This doesn't work, so I thought that maybe I could just start and stop
the event loop for one frame whenever needed. So I tried something
like this:
if __name__ == "__main__":
      window = pg.window.Window()#visible = False)
      window.set_size(10,10)
      @window.event
      def on_draw():
          window.clear()
          drawTriangle()
          x = screenAsArray()
          pg.app.exit()
      pg.app.run()
This works, but ONLY if I make the pg.app.run call where it is in the
above example. When I place it in another function or enter it in the
interpreter, it doesn't work. It does work when I leave the run call
where it is, so that it renders one frame, and then call pg.app.run
again in the interpreter later.. Does anyone know what's going on
here?
I would preferably get the first method to work. It seems like app.run
does some initialization work that I'm not doing in the first code
snippet above.
Any help is appreciated.

--
You received this message because you are subscribed to the Google Groups 
"pyglet-users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/pyglet-users?hl=en.

Reply via email to