| 1 |
.. _programming-guide-eventloop: |
| 2 |
|
| 3 |
The application event loop |
| 4 |
========================== |
| 5 |
|
| 6 |
In order to let pyglet process operating system events such as mouse and |
| 7 |
keyboard events, applications need to enter an application event loop. The |
| 8 |
event loop watches for new events, dispatches those events, and sleeps until |
| 9 |
something else needs to be done. It also handles any functions that you |
| 10 |
have scheduled on the clock (see :ref:`guide_calling-functions-periodically`). |
| 11 |
pyglet ensures that the loop iterates only as often as necessary to fulfill all |
| 12 |
scheduled functions and user input. It is well tuned for performance and low |
| 13 |
power usage on Windows, Linux and macOS. |
| 14 |
|
| 15 |
After creating Windows and attaching event handlers, most applications need |
| 16 |
only call:: |
| 17 |
|
| 18 |
pyglet.app.run() |
| 19 |
|
| 20 |
The :py:func:`~pyglet.app.run` function does not return until all open windows |
| 21 |
have been closed, or until :py:func:`pyglet.app.exit()` is called. |
| 22 |
|
| 23 |
Once you have entered the event loop it dispatches window events (such as for |
| 24 |
keyboard input or mouse movement), events from Controllers or Joysticks, |
| 25 |
and any other events as they occur. By default, the application event loop will |
| 26 |
also refresh all Windows and dispatch the :py:meth:`~pyglet.window.Window.on_draw` |
| 27 |
event at a rate of 60Hz (60 times per second). You can customize this by |
| 28 |
passing the desired interval in seconds to :py:func:`~pyglet.app.run`:: |
| 29 |
|
| 30 |
pyglet.app.run(1/30) # 30Hz |
| 31 |
# or |
| 32 |
pyglet.app.run(1/120) # 120Hz |
| 33 |
# or for benchmarking, redraw as fast as possible: |
| 34 |
pyglet.app.run(0) |
| 35 |
|
| 36 |
|
| 37 |
Passing `None` to :py:func:`~pyglet.app.run` is a special case. It will enter |
| 38 |
the event loop as usual, but it will not dispatch the Window events. This can |
| 39 |
be desired if you wish to have different refresh rates for different Windows, |
| 40 |
or even change the refresh rate while the application is running. |
| 41 |
|
| 42 |
Customising the event loop |
| 43 |
-------------------------- |
| 44 |
|
| 45 |
The pyglet event loop is encapsulated in the |
| 46 |
:py:class:`~pyglet.app.EventLoop` class, which provides |
| 47 |
several hooks that can be overridden for customising its behaviour. This is |
| 48 |
recommended only for advanced users -- typical applications and games are |
| 49 |
unlikely to require this functionality. |
| 50 |
|
| 51 |
To use the :py:class:`~pyglet.app.EventLoop` class directly, instantiate it and call `run`:: |
| 52 |
|
| 53 |
event_loop = pyglet.app.EventLoop() |
| 54 |
event_loop.run() |
| 55 |
|
| 56 |
Only one :py:class:`~pyglet.app.EventLoop` can be running at a time; when the |
| 57 |
:py:meth:`~pyglet.app.EventLoop.run` method is called |
| 58 |
the module variable :py:attr:`pyglet.app.event_loop` is set to the running |
| 59 |
instance. Other pyglet modules such as :py:mod:`pyglet.window` depend on this. |
| 60 |
|
| 61 |
Event loop events |
| 62 |
^^^^^^^^^^^^^^^^^ |
| 63 |
|
| 64 |
You can listen for several events on the event loop instance. A useful one |
| 65 |
of these is :py:meth:`~pyglet.app.EventLoop.on_window_close`, which is |
| 66 |
dispatched whenever a window is closed. The default handler for this event |
| 67 |
exits the event loop if there are no more windows. The following example |
| 68 |
overrides this behaviour to exit the application whenever any window is |
| 69 |
closed:: |
| 70 |
|
| 71 |
event_loop = pyglet.app.EventLoop() |
| 72 |
|
| 73 |
@event_loop.event |
| 74 |
def on_window_close(window): |
| 75 |
event_loop.exit() |
| 76 |
return pyglet.event.EVENT_HANDLED |
| 77 |
|
| 78 |
event_loop.run() |
| 79 |
|
| 80 |
Overriding the default idle policy |
| 81 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 82 |
|
| 83 |
The :py:meth:`pyglet.app.EventLoop.idle` method is called every iteration of |
| 84 |
the event loop. It is responsible for calling scheduled clock functions, |
| 85 |
and deciding how idle the application is. You can override |
| 86 |
this method if you have specific requirements for tuning the performance |
| 87 |
of your application; especially if it uses many windows. |
| 88 |
|
| 89 |
The default implementation has the following algorithm: |
| 90 |
|
| 91 |
1. Call :py:func:`pyglet.clock.tick` with ``poll=True`` to call any scheduled |
| 92 |
functions. |
| 93 |
2. Return the value of :py:func:`pyglet.clock.get_sleep_time`. |
| 94 |
|
| 95 |
The return value of the :py:meth:`~pyglet.clock.get_sleep_time` method is |
| 96 |
the number of seconds until the event loop needs to iterate again (unless |
| 97 |
there is an earlier user-input event); or ``None`` if the loop can wait |
| 98 |
for input indefinitely. |
| 99 |
|
| 100 |
|
| 101 |
Creating a Custom Event Loop |
| 102 |
---------------------------- |
| 103 |
|
| 104 |
Many windowing toolkits require the application developer to write their own |
| 105 |
event loop. This is also possible in pyglet, but is usually just an inconvenience |
| 106 |
compared to :py:func:`pyglet.app.run`. It can be necessary in some situations, |
| 107 |
such as when combining pyglet with other toolkits, but is strongly discouraged |
| 108 |
for the following reasons: |
| 109 |
|
| 110 |
* Keeping track of delta times between frames, and maintaining a stable frame |
| 111 |
rate can be challenging. It is difficult to write a manual event loop that does |
| 112 |
not waste CPU cycles and is still responsive to user input. |
| 113 |
* The :py:class:`~pyglet.app.EventLoop` class provides plenty of hooks for most |
| 114 |
toolkits to be integrated without needing to resort to a manual event loop. |
| 115 |
* Because :py:class:`~pyglet.app.EventLoop` is tuned for specific operating |
| 116 |
systems, it is more responsive to user events, and continues calling clock |
| 117 |
functions while windows are being resized, and (on macOS) the menu bar is |
| 118 |
being tracked. |
| 119 |
|
| 120 |
With that out of the way, a manual event loop usually has the following form:: |
| 121 |
|
| 122 |
while True: |
| 123 |
pyglet.clock.tick() |
| 124 |
pyglet.app.platform_event_loop.step(timeout) |
| 125 |
|
| 126 |
for window in pyglet.app.windows: |
| 127 |
window.switch_to() |
| 128 |
window.dispatch_events() |
| 129 |
window.dispatch_event('on_draw') |
| 130 |
window.flip() |
| 131 |
|
| 132 |
The call to :py:func:`pyglet.clock.tick` is required for ensuring scheduled |
| 133 |
functions are called, including the internal data pump functions for playing |
| 134 |
sounds, animations, and video. |
| 135 |
|
| 136 |
The :py:meth:`~pyglet.window.Window.dispatch_events` method checks the window's |
| 137 |
operating system event queue for user input and dispatches any events found. |
| 138 |
The method does not wait for input -- if there are no events pending, control is |
| 139 |
returned to the program immediately. |
| 140 |
|
| 141 |
The :py:meth:`~pyglet.window.Window.dispatch_event('on_draw')` method is optional |
| 142 |
if you are catching this Window event. If you are not using this event, your |
| 143 |
draw calls (`Batch.draw()`) should go here instead. |