# Simple context configuration

A context can only be created from a config that was provided by the system. Enumerating and comparing the attributes of all the possible configs is a complicated process, so pyglet provides a simpler interface based on "template" configs.

To get the config with the attributes you need, construct a Config and set only the attributes you are interested in. You can then supply this config to the Window constructor to create the context.

For example, to create a window with an alpha channel:

    config = pyglet.gl.Config(alpha_size=8)
    window = pyglet.window.Window(config=config)

It is sometimes necessary to create the context yourself, rather than letting the Window constructor do this for you. In this case use Screen.get_best_config to obtain a "complete" config, which you can then use to create the context:

    platform = pyglet.window.get_platform()
    display = platform.get_default_display()
    screen = display.get_default_screen()

    template = pyglet.gl.Config(alpha_size=8)
    config = screen.get_best_config(template)
    context = config.create_context(None)
    window = pyglet.window.Window(context=context)

Note that you cannot create a context directly from a template (any Config you constructed yourself). The Window constructor performs a similar process to the above to create the context if a template config is given.

Not all configs will be possible on all machines. The call to get_best_config will raise NoSuchConfigException if the hardware does not support the requested attributes. It will never return a config that does not meet or exceed the attributes you specify in the template.

You can use this to support newer hardware features where available, but also accept a lesser config if necessary. For example, the following code creates a window with multisampling if possible, otherwise leaves multisampling off:

    template = gl.Config(sample_buffers=1, samples=4)
    try:
        config = screen.get_best_config(template)
    except pyglet.window.NoSuchConfigException:
        template = gl.Config()
        config = screen.get_best_config(template)
    window = pyglet.window.Window(config=config)