OpenGL Context

class arcade.context.ArcadeContext(window: BaseWindow, gc_mode: str = 'context_gc', gl_api: str = 'gl')[source]

Bases: Context

An OpenGL context implementation for Arcade with added custom features. This context is normally accessed through arcade.Window.ctx.

Pyglet users can use the base Context class and extend that as they please.

This is part of the low level rendering API in arcade and is mainly for more advanced usage

  • window – The pyglet window

  • gc_mode – The garbage collection mode for opengl objects. auto is just what we would expect in python while context_gc (default) requires you to call Context.gc(). The latter can be useful when using multiple threads when it’s not clear what thread will gc the object.

bind_window_block() None[source]

Binds the projection and view uniform buffer object. This should always be bound to index 0 so all shaders have access to them.

get_framebuffer_image(fbo: Framebuffer, components: int = 4, flip: bool = True) Image[source]

Shortcut method for reading data from a framebuffer and converting it to a PIL image.

  • fbo – Framebuffer to get image from

  • components – Number of components to read

  • flip – Flip the image upside down

load_compute_shader(path: str | Path, common: Iterable[str | Path] = ()) ComputeShader[source]

Loads a compute shader from file. This methods supports resource handles.


  • path – Path to texture

  • common – Common source injected into compute shader

load_program(*, vertex_shader: str | Path, fragment_shader: str | Path | None = None, geometry_shader: str | Path | None = None, tess_control_shader: str | Path | None = None, tess_evaluation_shader: str | Path | None = None, common: Iterable[str | Path] = (), defines: Dict[str, Any] | None = None, varyings: Sequence[str] | None = None, varyings_capture_mode: str = 'interleaved') Program[source]

Create a new program given a file names that contain the vertex shader and fragment shader. Note that fragment and geometry shader are optional for when transform shaders are loaded.

This method also supports the resource handles.


# The most common use case if having a vertex and fragment shader
program = window.ctx.load_program(
  • vertex_shader – path to vertex shader

  • fragment_shader – path to fragment shader (optional)

  • geometry_shader – path to geometry shader (optional)

  • tess_control_shader – Tessellation Control Shader

  • tess_evaluation_shader – Tessellation Evaluation Shader

  • common – Common files to be included in all shaders

  • defines – Substitute #define values in the source

  • varyings – The name of the out attributes in a transform shader. This is normally not necessary since we auto detect them, but some more complex out structures we can’t detect.

  • varyings_capture_mode – The capture mode for transforms. "interleaved" means all out attribute will be written to a single buffer. "separate" means each out attribute will be written separate buffers. Based on these settings the transform() method will accept a single buffer or a list of buffer.

load_texture(path: str | Path, *, flip: bool = True, build_mipmaps: bool = False, internal_format: int | None = None, compressed: bool = False) Texture2D[source]

Loads and creates an OpenGL 2D texture. Currently, all textures are converted to RGBA for simplicity.


# Load a texture in current working directory
texture = window.ctx.load_texture("background.png")

# Load a texture using Arcade resource handle
texture = window.ctx.load_texture(":textures:background.png")

# Load and compress a texture
texture = window.ctx.load_texture(
  • path – Path to texture

  • flip – Flips the image upside down

  • build_mipmaps – Build mipmaps for the texture

  • internal_format – The internal format of the texture. This can be used to override the default internal format when using sRGBA or compressed textures.

  • compressed – If the internal format is a compressed format meaning your texture will be compressed by the GPU.


Context manager for doing rendering with pyglet ensuring context states are reverted. This affects things like blending.

reset() None[source]

Reset context flags and other states. This is mostly used in unit testing.

shader_inc(source: str) str[source]

Parse a shader source looking for #include directives and replace them with the contents of the included file.

The #include directive must be on its own line and the file and the path should use a resource handle.


#include :my_shader:lib/common.glsl

source – Shader

atlas_size: Tuple[int, int] = (512, 512)

The default texture atlas. This is created when arcade is initialized. All sprite lists will use use this atlas unless a different atlas is passed in the arcade.SpriteList constructor.




Get the current projection matrix. This 4x4 float32 matrix is calculated by cameras.

This property simply gets and sets pyglet’s projection matrix.




Get the current view matrix. This 4x4 float32 matrix is calculated when setting view_matrix_2d.

This property simply gets and sets pyglet’s view matrix.




Get or set the viewport for the currently active framebuffer. The viewport simply describes what pixels of the screen OpenGL should render to. Normally it would be the size of the window’s framebuffer:

# 4:3 screen
ctx.viewport = 0, 0, 800, 600
# 1080p
ctx.viewport = 0, 0, 1920, 1080
# Using the current framebuffer size
ctx.viewport = 0, 0, *ctx.screen.size

tuple (x, y, width, height)