Table of contents
1.
Introduction
2.
Options in Pyglet
2.1.
1. audio
2.2.
2. shadow_window
2.3.
3. debug_gl
2.4.
4. vsync
2.5.
5. debug_lib
2.6.
6. xsync
2.7.
7. darwin_cocoa
2.8.
8. search_local_libs
3.
Frequently Asked Questions
3.1.
How Image Viewer of Pyglet works
3.2.
How would disable the error check that is enabled by itself after every function call?
3.3.
Do you have the option of multiple windows in Pyglet?
3.4.
Say something about the OpenGL interface.
4.
Conclusion
Last Updated: Mar 27, 2024

Pyglet Options

Author Rupal Saluja
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.

Frequently Asked Questions

How Image Viewer of Pyglet works

We will understand this with the help of an example. In this example, we will load an image from the application’s directory and display it within the window.

import pyglet
window = pyglet.window.Window()
image = pyglet.resource.image('kitten.jpg')

@window.event
def on_draw():
      window.clear()
      image.blit(0,0)
pyglet.app.run()

 

How would disable the error check that is enabled by itself after every function call?

To disable the error check, set the following option before importing pyglet.gl or pyglet.window.

pyglet.options['debug_gl'] = False
from pyglet.gl import

 

This will increase the performance further.

Do you have the option of multiple windows in Pyglet?

Yes, Pyglet allows you to create and display any number of windows required simultaneously. Each window will be created with its own OpenGL context, however, all contexts will share the same texture objects, display lists, shader programs, and so on, by default.

Say something about the OpenGL interface.

OpenGL is an interface between OpenGL and GLU. The interface is used by all of the pyglet’s higher-level APIs. This makes sure that rendering is done by the graphics card, rather than the operating system. 

Conclusion

In this article, we have extensively discussed various Pyglet options, their environment variable, their default values, in detail.

We hope that this blog helped you increment your knowledge regarding certain Pyglet options. Click on the links to know more about PygletPygameBasics of Python. For more such topics, visit Coding Ninjas

For peeps out there who want to enhance their knowledge about Data Structures, Algorithms, Power programming, JavaScript, or any other upskilling, please refer to guided paths on Coding Ninjas Studio. Enroll in our courses, go for mock tests and solve problems and interview puzzles. Also, you can put your attention towards interview stuff- interview experiences and an interview bundle for placement preparations. 

Do upvote our blog to help other ninjas grow.

Happy Coding!

Live masterclass