Introduction
In this blog, we will learn about Pyglet and its options. Pyglet is a cross-platform and multimedia library for Python. It is easy to use, yet its powerful Python library is used for game development and for some other visually-rich applications on Windows, Mac OS X, and Linux. It is provided for free under the BSD license. BSD is an open-source license that is of low restriction type and does not put requirements on distribution. Pyglet allows you to use it for both commercial and other open-source projects with few restrictions.
Pyglet does not need to be installed. It uses no external libraries or compiled binaries, you can run it in place. You can distribute the pyglet source code or runtime eggs alongside your application code. Now, let us learn about different options in pyglet.
Options in Pyglet
There exists a dictionary of pyglet options which are accepted globally. A brief description of each of them is given below.
1. audio
In order of preference, a sequence of the name of audio modules which attempt to load are mentioned below in the table. Valid Driver names are also presented along with it.
AUDIO MODULES |
DRIVER NAMES |
APPLICABLE TO |
Windows Xaudio2 audio module | xaudio2 | Windows |
Windows DirectSound audio module | directsound | Windows |
PulseAudio module | pulse | Linux |
OpenAL audio module | openal | Everywhere |
No audio | silent | Everywhere |
2. shadow_window
This pyglet option has been available to us since version 1.1. A hidden window, namely a shadow window, is created with a GL context whenever ‘pyglet.gl’ is imported, by default. If you disable this option by setting this option to false, this hidden window will not be created. With this, you will lose all the advantages that come with this option.
‘shadow_window’ not only allows the resources to be loaded before the application window is created but also permits GL objects to be shared between windows even after they have been closed. There are some OpenGL driver implementations that may not support shared OpenGL contexts. This will require you to disable the shadow window. In that case, all the resources that were supposed to be loaded before the application window was created, will be loaded after the window using them is created.
Due to all such complications, the option ‘shadow_window’ is recommended for advanced developers only.
3. debug_gl
This severely affects performance, yet provides very useful exceptions at the time of failure. All calls to OpenGL functions will be checked for errors using ‘g1GetError’, if true. This option is enabled by default if ‘_debug_’ is enabled. When the pyglet option is frozen within a py2exe or py2app library archive, this default option is disabled by itself.
4. vsync
You need to set or unset this option as per the requirement of the situation. The property ‘pyglet.window.Window.vsync’ can be ignored, if set. This option overrides that property to either force vsync on or off. The ‘pyglet.window.Window.vsync’ property behaves as documented, if unset or set to none.
5. debug_lib
debug_lib is a boolean type environment variable. It is one such Pyglet option that prints the path of each dynamic library, if true.
6. xsync
This pyglet option has been available to us since version 1.1. Here again, you need to set or unset this option as per the requirement of the situation. The default option is set. The drawing of double-buffered windows and border updates of the X11 window manager needs to be synchronized to cause improvisation. This improvisation is done in the appearance of the window during resize operations by Pyglet if xsync is set. But also, it affects the double-buffered windows on X11 servers, which are supporting ‘xsync’ extension with a window manager implementing ‘_NET_WM_SYNC_REQUEST’ protocol.
7. darwin_cocoa
This pyglet option has been available to us since version 1.2. This option facilitates cocoa-based pyglet implementation. It is used in opposition to the 32-bit Carbon implementation, if true. This 32-bit carbon implementation was done earlier when darwin_cocoa did not exist. By default, this option is set to true, if python is running in 64-bit mode, on Mac OS X or later. Otherwise, it is advised to use the Carbon implementation.
8. search_local_libs
This option has been available to us since pyglet version 1.2. This option is set to true by default. Pyglet is not obliged to search for libraries in the script directory and its lib subdirectory. Instead of the system-installed version of the library, it is useful to load the local library.
Every option described above has been set to some default value already. They have defaults set through the operating system’s environment variable. The following table shows which environment variable is used for each option.
ENVIRONMENT VARIABLE | Pyglet.options key | TYPE | DEFAULT VALUE |
PYGLET_AUDIO | audio | List of Strings | directsound, openal, alsa, silent |
PYGLET_DEBUG_GL | debug_gl | Boolean | 1 |
To change an option from its default value, make sure to import ‘pyglet’ before any sub-packages.
import pyglet
The default options are overridden by prefacing each option key with ‘PYGLET_’
For Example,
PYGLET_DEBUG_LIB= True; export PYGLET_DEBUG_LIB
Usually, even the developers prefer the default value. In case some changes are needed the above piece of code can be used accordingly.