py-sdl2 / doc / tutorial / pygamers.rst

PySDL2 for Pygamers

Care to move to a newer SDL with your Pygame knowledge? Then you should know one thing or two about PySDL2 before hacking code, since it is completely different from Pygame. Do not let that fact scare you away, the basics with graphics and sound are still the same (as they are fundamental), but you will not find many similarities to the Pygame API within PySDL2.

Technical differences

Pygame is implemented as a mixture of Python, C and Assembler code, wrapping 3rd party libraries with CPython API interfaces. PySDL2 in contrast is written in pure Python, using :mod:`ctypes` to interface with the C interfaces of 3rd party libraries.

API differences


pygame sdl2
init() :func:`sdl2.SDL_init()` where appropriate
quit() :func:`sdl2.SDL_quit()` where appropriate
error No equivalent
get_error() :func:`sdl2.SDL_GetError()`
set_error() :func:`sdl2.SDL_SetError()`
get_sdl_version() :func:`sdl2.SDL_GetVersion()`
get_sdl_byteorder() :data:`sdl2.SDL_BYTEORDER`
register_quit() No equivalent planned
encode_string() No equivalent planned
encode_file_path() No equivalent planned


PySDL2 does not feature any CD-ROM related interfaces. They were removed in SDL2 and PySDL2 does not provide its own facilities.


You can find a similar class in :class:`sdl2.ext.Color`. It does not feature a set_length() or correct_gamma() method, though.


PySDL2 does not feature any pre-defined cursor settings at the moment.


Drawing primitives can be accessed through either the sdl2.SDL_RenderDraw*() and sdl2.SDL_RenderFill*() functions or the more powerful :mod:`sdl2.sdlgfx` module,


pygame.font sdl2
init() :func:`sdl2.sdlttf.TTF_Init()`
quit() :func:`sdl2.sdlttf.TTF_Quit()`
get_init() :func:`sdl2.sdlttf.TTF_WasInit()`
get_default_font() No equivalent planned [1]
get_fonts() No equivalent planned [1]
match_font() No equivalent planned [1]
SysFont No equivalent planned [1]
Font No equivalent planned [1]


PySDL2 does not feature direct FreeType support.


PySDL2 offers SDL_gfx support through the :mod:`sdl2.sdlgfx` module.


pygame.image sdl2
load() :func:`sdl2.sdlimage.IMG_Load()`, :func:`sdl2.ext.load_image()`
save() :func:`sdl2.surface.SDL_SaveBMP()`, :func:`sdl2.sdlimage.IMG_SavePNG()`
get_extended() :func:`sdl2.sdlimage.IMG_isBMP()` et al.
tostring() No equivalent yet
fromstring() No equivalent yet
frombuffer() No equivalent yet


pygame.joystick sdl2
init() :func:`sdl2.SDL_Init()`
quit() :func:`sdl2.SDL_Quit()`
get_init() :func:`sdl2.SDL_WasInit()`
get_count() :func:`sdl2.joystick.SDL_NumJoysticks()`
Joystick() :class:`sdl2.joystick.SDL_Joystick` and related functions


pygame.key sdl2
get_focused() :func:`sdl2.keyboard.SDL_GetKeyboardFocus()`
get_pressed() :func:`sdl2.keyboard.SDL_GetKeyboardState()`
get_mods() :func:`sdl2.keyboard.SDL_GetModState()`
set_mods() :func:`sdl2.keyboard.SDL_SetModState()`
set_repeat() Based on the OS/WM settings, no equivalent
get_repeat() Based on the OS/WM settings, no equivalent
name() :func:`sdl2.keyboard.SDL_GetKeyName()`


Constants in PySDL2 are spread across the different packages and modules, depending on where they originate from.

See pygame.mixer.

No such module is planned for PySDL2.


You can work with YUV overlays by using the :mod:`sdl2.render` module with :class:`sdl2.render.SDL_Texture` objects.


You can access pixel data of sprites and surfaces directly via the :class:`sdl2.ext.PixelView` class. It does not feature comparison or extractions methods.


No such functionality is available for PySDL2. Rectangles are represented via :class:`sdl2.rect.SDL_Rect` for low-level SDL2 wrappers or 4-value tuples.


PySDL2 offers basic text-based clipboard access via the :mod:`sdl2.clipboard` module. A feature-rich clipboard API as for Pygame does not exist yet.


No such module is available for PySDL2 yet.


PySDL2 uses a different approach of rendering and managing sprite objects via a component-based system and the :class:`sdl2.ext.Sprite` class. A sprite module as for Pygame is not planned.


pygame.Surface sdl2
blit() :meth:`sdl2.surface.SDL_BlitSurface()`, :class:`sdl2.ext.SpriteRenderer`
convert() :func:`sdl2.surface.SDL_ConvertSurface()`
convert_alpha() :func:`sdl2.surface.SDL_ConvertSurface()`
copy() :func:`sdl2.surface.SDL_ConvertSurface()`
fill() :func:`sdl2.surface.SDL_FillRect()`, :func:`sdl2.surface.SDL_FillRects()`, :func:`sdl2.ext.fill()`
scroll() No equivalent planned
set_colorkey() :func:`sdl2.surface.SDL_SetColorKey()`
get_colorkey() :func:`sdl2.surface.SDL_GetColorKey()`
set_alpha() :func:`sdl2.surface.SDL_SetSurfaceAlphaMod()`
get_alpha() :func:`sdl2.surface.SDL_GetSurfaceAlphaMod()`
lock() :func:`sdl2.surface.SDL_LockSurface()`
unlock() :func:`sdl2.surface.SDL_UnlockSurface()`
mustlock() :func:`sdl2.surface.SDL_MUSTLOCK()`
get_locked() :attr:`sdl2.surface.SDL_Surface.locked`
get_locks() No equivalent planned
get_at() Direct access to the pixels for surfaces can be achieved via the :class:`sdl2.ext.PixelView` class
set_at() Direct access to the pixels for surfaces can be achieved via the :class:`sdl2.ext.PixelView` class
get_at_mapped() No equivalent planned
get_palette() via :attr:`sdl2.surface.SDL_Surface.format` and the :attr:`sdl2.pixels.SDL_PixelFormat.palette` attribute
get_palette_at() sdl2.pixels.SDL_Palette.colors[offset]
set_palette() :func:`sdl2.surface.SDL_SetSurfacePalette()`
set_palette_at() sdl2.pixels.SDL_Palette.colors[offset]
map_rgb() :func:`sdl2.pixels.SDL_MapRGB()`
unmap_rgb() :func:`sdl2.pixels.SDL_GetRGB()`
set_clip() :func:`sdl2.surface.SDL_SetClipRect()`
get_clip() :func:`sdl2.surface.SDL_GetClipRect()`
subsurface No equivalent yet
get_parent() As for subsurface
get_abs_parent() As for subsurface
get_offset() As for subsurface
get_abs_offset() As for subsurface
get_size() :attr:`sdl2.ext.Sprite.size`, :attr:`sdl2.surface.SDL_Surface.w`, :attr:`sdl2.surface.SDL_Surface.h`
get_width() sdl2.ext.Sprite.size[0], :attr:`sdl2.surface.SDL_Surface.w`,
get_height() sdl2.ext.Sprite.size[1], :attr:`sdl2.surface.SDL_Surface.h`
get_rect() No equivalent planned
get_bitsize() :attr:`sdl2.pixels.SDL_PixelFormat.BitsPerPixel`
get_bytesize() :attr:`sdl2.pixels.SDL_PixelFormat.BytesPerPixel`
get_flags() :attr:`sdl2.surface.SDL_Surface.flags`
get_pitch() :attr:`sdl2.surface.SDL_Surface.pitch`
get_masks() :attr:`sdl2.pixels.SDL_PixelFormat.Rmask`, ...
get_shifts() :attr:`sdl2.pixels.SDL_PixelFormat.Rshift`, ...
get_losses() :attr:`sdl2.pixels.SDL_PixelFormat.Rloss`, ...
get_bounding_rect() No equivalent planned
get_view() :class:`sdl2.ext.PixelView`
get_buffer() :class:`sdl2.ext.PixelView` or :attr:`sdl2.surface.SDL_Surface.pixels`


2D and 3D pixel access can be achieved via the :class:`sdl2.ext.PixelView` class in environments without numpy. Simplified numpy-array creation with direct pixel access (similar to pygame.surfarray.pixels2d() and pygame.surfarray.pixels3d()) is available via :func:`sdl2.ext.pixels2d()` and :func:`sdl2.ext.pixels3d()`.


pygame.time sdl2
get_ticks() :func:`sdl2.timer.SDL_GetTicks()`
wait() :func:`sdl2.timer.SDL_Delay()`
delay() :func:`sdl2.timer.SDL_Delay()`
Clock No equivalent planned


The are no transformation helpers in PySDL2 at moment. Those might be implemented later on via numpy helpers, the Python Imaging Library or other 3rd party packages.


pygame.version sdl2
ver :attr:`sdl2.__version__`
vernum :attr:`sdl2.version_info`


[1](1, 2, 3, 4, 5) Check for an easy to use system font detection module