base.window.WindowConfig

moderngl_window.context.base.window.WindowConfig[source]

Creating a WindowConfig instance is the simplest interface this library provides to open and window, handle inputs and provide simple shortcut method for loading basic resources. It’s appropriate for projects with basic needs.

Example:

class MyConfig(mglw.WindowConfig):
    gl_version = (3, 3)
    window_size = (1920, 1080)
    aspect_ratio = 16 / 9
    title = "My Config"
    resizable = False
    samples = 8

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        # Do other initialization here

    def render(self, time: float, frametime: float):
        # Render stuff here with ModernGL

    def resize(self, width: int, height: int):
        print("Window was resized. buffer size is {} x {}".format(width, height))

    def mouse_position_event(self, x, y):
        print("Mouse position:", x, y)

    def mouse_press_event(self, x, y, button):
        print("Mouse button {} pressed at {}, {}".format(button, x, y))

    def mouse_release_event(self, x: int, y: int, button: int):
        print("Mouse button {} released at {}, {}".format(button, x, y))

    def key_event(self, key, action, modifiers):
        print(key, action, modifiers)

Methods

WindowConfig.__init__(ctx: moderngl.context.Context = None, wnd: moderngl_window.context.base.window.BaseWindow = None, timer: moderngl_window.timers.base.BaseTimer = None, **kwargs)[source]

Initialize the window config

Keyword Arguments
  • ctx (moderngl.Context) – The moderngl context

  • wnd – The window instance

  • timer – The timer instance

WindowConfig.render(time: float, frame_time: float)[source]

Renders the assigned effect

Parameters
  • time (float) – Current time in seconds

  • frame_time (float) – Delta time from last frame in seconds

WindowConfig.resize(width: int, height: int)[source]

Called every time the window is resized in case the we need to do internal adjustments.

Parameters
  • width (int) – width in buffer size (not window size)

  • height (int) – height in buffer size (not window size)

WindowConfig.key_event(key: Any, action: Any, modifiers: moderngl_window.context.base.keys.KeyModifiers)[source]

Called for every key press and release. Depending on the library used, key events may trigger repeating events during the pressed duration based on the configured key repeat on the users operating system.

Parameters
  • key – The key that was press. Compare with self.wnd.keys.

  • action – self.wnd.keys.ACTION_PRESS or ACTION_RELEASE

  • modifiers – Modifier state for shift and ctrl

WindowConfig.mouse_position_event(x: int, y: int)[source]

Reports the current mouse cursor position in the window

Parameters
  • x (int) – X postion of the mouse cursor

  • Iint) (y) – Y position of the mouse cursor

WindowConfig.mouse_press_event(x: int, y: int, button: int)[source]

Called when a mouse button in pressed

Parameters
  • x (int) – X position the press occured

  • y (int) – Y position the press occured

  • button (int) – 1 = Left button, 2 = right button

WindowConfig.mouse_release_event(x: int, y: int, button: int)[source]

Called when a mouse button in released

Parameters
  • x (int) – X position the release occured

  • y (int) – Y position the release occured

  • button (int) – 1 = Left button, 2 = right button

WindowConfig.mouse_drag_event(x: int, y: int)[source]

Called when the mouse is moved while a button is pressed.

Parameters
  • x (int) – X postion of the mouse cursor

  • Iint) (y) – Y position of the mouse cursor

WindowConfig.mouse_scroll_event(x_offset: float, y_offset: float)[source]

Called when the mouse wheel is scrolled.

Some input devices also support horisontal scrolling, but vertical scrolling is fairly universal.

Parameters
  • x_offset (int) – X scroll offset

  • Iint) (y_offset) – Y scroll offset

WindowConfig.unicode_char_entered(char: str)[source]

Called when the user entered a unicode character.

Parameters

char (str) – The character entered

WindowConfig.load_texture_2d(path: str, flip=True, mipmap=False, mipmap_levels: Tuple[int, int] = None, anisotropy=1.0, **kwargs) → moderngl.texture.Texture[source]

Loads a 2D texture

Parameters

path (str) – Path to the texture relative to search directories

Keyword Arguments
  • flip (boolean) – Flip the image horisontally

  • mipmap (bool) – Generate mipmaps. Will generate max possible levels unless mipmap_levels is defined.

  • mipmap_levels (tuple) – (base, max_level) controlling mipmap generation. When defined the mipmap parameter is automatically True

  • anisotropy (float) – Number of samples for anisotropic filtering

  • **kwargs – Additonal parameters to TextureDescription

Returns

Texture instance

Return type

moderngl.Texture

WindowConfig.load_texture_array(path: str, layers: int = 0, flip=True, mipmap=False, mipmap_levels: Tuple[int, int] = None, anisotropy=1.0, **kwargs) → moderngl.texture_array.TextureArray[source]

Loads a texture array.

Parameters

path (str) – Path to the texture relative to search directories

Keyword Arguments
  • layers (int) – How many layers to split the texture into vertically

  • flip (boolean) – Flip the image horisontally

  • mipmap (bool) – Generate mipmaps. Will generate max possible levels unless mipmap_levels is defined.

  • mipmap_levels (tuple) – (base, max_level) controlling mipmap generation. When defined the mipmap parameter is automatically True

  • anisotropy (float) – Number of samples for anisotropic filtering

  • **kwargs – Additonal parameters to TextureDescription

Returns

The texture instance

Return type

moderngl.TextureArray

WindowConfig.load_program(path=None, vertex_shader=None, geometry_shader=None, fragment_shader=None, tess_control_shader=None, tess_evaluation_shader=None) → moderngl.program.Program[source]

Loads a shader program.

Note that path should only be used if all shaders are defined in the same glsl file separated by defines.

Keyword Arguments
  • path (str) – Path to a single glsl file

  • vertex_shader (str) – Path to vertex shader

  • geometry_shader (str) – Path to geometry shader

  • fragment_shader (str) – Path to fragment shader

  • tess_control_shader (str) – Path to tessellation control shader

  • tess_evaluation_shader (str) – Path to tessellation eval shader

Returns

The program instance

Return type

moderngl.Program

WindowConfig.load_text(path: str, **kwargs) → str[source]

Load a text file.

Parameters
  • path (str) – Path to the file relative to search directories

  • **kwargs – Additional parameters to DataDescription

Returns

Contents of the text file

Return type

str

WindowConfig.load_json(path: str, **kwargs) → dict[source]

Load a json file

Parameters
  • path (str) – Path to the file relative to search directories

  • **kwargs – Additional parameters to DataDescription

Returns

Contents of the json file

Return type

dict

WindowConfig.load_binary(path: str, **kwargs) → bytes[source]

Load a file in binary mode.

Parameters
  • path (str) – Path to the file relative to search directories

  • **kwargs – Additional parameters to DataDescription

Returns

The byte data of the file

Return type

bytes

WindowConfig.load_scene(path: str, cache=False, attr_names=<class 'moderngl_window.geometry.attributes.AttributeNames'>, kind=None, **kwargs) → moderngl_window.scene.scene.Scene[source]

Loads a scene.

Keyword Arguments
  • path (str) – Path to the file relative to search directories

  • cache (str) – Use the loader caching system if present

  • attr_names (AttributeNames) – Attrib name config

  • kind (str) – Override loader kind

  • **kwargs – Additional parameters to SceneDescription

Returns

The scene instance

Return type

Scene

Attributes

WindowConfig.window_size

Size of the window.

# Default value
window_size = (1280, 720)
WindowConfig.resizable

Determines of the window should be resizable

# Default value
resizable = True
WindowConfig.gl_version

The minimum required OpenGL version required

# Default value
gl_version = (3, 3)
WindowConfig.title

Title of the window

# Default value
title = "Example"
WindowConfig.aspect_ratio

The endorced aspect ratio of the viewport. When specified back borders will be calulcated both vertically and horizontally if needed.

This property can be set to None to disable the fixed viewport system.

# Default value
aspect_ratio = 16 / 9
WindowConfig.cursor

Determines if the mouse cursor should be visible inside the window. If enabled on some platforms

# Default value
cursor = True
WindowConfig.samples

Number of samples to use in multisampling.

# Default value
samples = 4
WindowConfig.resource_dir

Absolute path to your resource directory containing textures, scenes, shaders/programs or data files. The load_ methods in this class will look for resources in this path. This attribute can be a str or a pathlib.Path.

# Default value
resource_dir = None
WindowConfig.log_level

Sets the log level for this library using the standard logging module.

# Default value
log_level = logging.INFO