sdl2.ext.common - Frequently Used SDL2 Functions

This module wraps various common SDL2 functions, including methods for initializing and quitting SDL2 and its various subsystems and for retrieving events from the SDL2 event queue.

sdl2.ext.common.init(video=True, audio=False, timer=False, joystick=False, controller=False, haptic=False, sensor=False, events=True)[source]

Initializes SDL and its optional subsystems.

By default, only the video and events subsystems are initialized. Note that the sensor subsystem requires SDL 2.0.9 or later.

Parameters:
  • video (bool, optional) – Whether to initialize the SDL video subsystem. If True, the events subsystem will also be initialized. Defaults to True.
  • audio (bool, optional) – Whether to initialize the SDL audio subsystem. Defaults to False.
  • timer (bool, optional) – Whether to initialize the SDL timer subsystem. Defaults to False.
  • joystick (bool, optional) – Whether to initialize the SDL joystick subsystem. If True, the events subsystem will also be initialized. Defaults to False.
  • controller (bool, optional) – Whether to initialize the SDL gamecontroller subsystem. If True, the joystick subsystem will also be initialized. Defaults to False.
  • haptic (bool, optional) – Whether to initialize the SDL haptic (force feedback) subsystem. Defaults to False.
  • sensor (bool, optional) – Whether to initialize the SDL sensor subsystem. Defaults to False.
  • events (bool, optional) – Whether to initialize the SDL events subsystem. Will automatically be initialized if the video, joystick, or gamecontroller subsystems are enabled. Defaults to False.

See pygame for a comparison between this function and pygame.init().

Raises:SDLError – If a requested SDL subsystem cannot be initialized.
sdl2.ext.common.quit()[source]

Quits the SDL2 video subysystem.

If no other subsystems are active, this will also call sdl2.SDL_Quit(), sdlttf.TTF_Quit() and sdlimage.IMG_Quit().

sdl2.ext.common.get_events()[source]

Gets all SDL events that are currently on the event queue.

Returns:A list of all SDL_Event objects currently in the event queue.
Return type:List
sdl2.ext.common.quit_requested(events)[source]

Checks for quit requests in a given event queue.

Quit requests occur when an SDL recieves a system-level quit signal (e.g clicking the ‘close’ button on an SDL window, pressing Command-Q on macOS). This function makes it easier to handle these events in your code:

running = True
while running:
    events = sdl2.ext.get_events()
    if quit_requested(events):
        running = False
Parameters:events (list of sdl2.SDL_Event) – A list of SDL events to check for quit requests.
Returns:True if a quit request has occurred, otherwise False.
Return type:bool
class sdl2.ext.common.TestEventProcessor[source]

A simple event processor for testing purposes.

run(window)[source]

Starts an event loop without actually processing any event.

This method will run endlessly until an SDL_QUIT event occurs.

Parameters:window (sdl2.ext.Window) – The window within which to run the test event loop.