Skip to content

sdldisplay

displaysys.sdldisplay

displaysys.sdldisplay

Classes

SDLDisplay

SDLDisplay(width=320, height=240, rotation=0, color_depth=16, title='SDL2 Display', scale=1.0, window_flags=SDL_WINDOW_SHOWN, render_flags=SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC, x=SDL_WINDOWPOS_CENTERED, y=SDL_WINDOWPOS_CENTERED)

A class to emulate an LCD using SDL2. Provides scrolling and rotation functions similar to an LCD. The .texture object functions as the LCD's internal memory.

Parameters:

Name Type Description Default
width int

The width of the display. Defaults to 320.

320
height int

The height of the display. Defaults to 240.

240
rotation int

The rotation of the display. Defaults to 0.

0
color_depth int

The color depth of the display. Defaults to 16.

16
title str

The title of the display window. Defaults to "SDL2 Display".

'SDL2 Display'
scale float

The scale of the display. Defaults to 1.0.

1.0
window_flags int

The flags for creating the display window. Defaults to SDL_WINDOW_SHOWN.

SDL_WINDOW_SHOWN
render_flags int

The flags for creating the renderer. Defaults to SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC.

SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC
x int

The x-coordinate of the display window's position. Defaults to SDL_WINDOWPOS_CENTERED.

SDL_WINDOWPOS_CENTERED
y int

The y-coordinate of the display window's position. Defaults to SDL_WINDOWPOS_CENTERED.

SDL_WINDOWPOS_CENTERED
Attributes
width property
width

The width of the display in pixels.

height property
height

The height of the display in pixels.

rotation property writable
rotation

The rotation of the display.

touch_device property writable
touch_device

The touch device.

requires_byteswap property
requires_byteswap

Whether the display requires byte swapping.

vscroll property writable
vscroll

The vertical scroll position relative to the top fixed area.

Returns:

Type Description
int

The vertical scroll position.

tfa property
tfa

The top fixed area set by set_vscroll or vscrdef.

Returns:

Type Description
int

The top fixed area.

vsa property
vsa

The vertical scroll area set by set_vscroll or vscrdef.

Returns:

Type Description
int

The vertical scroll area.

bfa property
bfa

The bottom fixed area set by set_vscroll or vscrdef.

Returns:

Type Description
int

The bottom fixed area.

tfa_area property
tfa_area

Top fixed area for vertical scrolling.

Returns:

Type Description

tuple[int, int, int, int]: (x, y, width, height) of the top fixed band.

vsa_area property
vsa_area

The vertical scroll area as an Area object.

Returns:

Type Description
tuple

The vertical scroll area.

bfa_area property
bfa_area

The bottom fixed area as an Area object.

Returns:

Type Description
tuple

The bottom fixed area.

power property writable
power

The power state of the display.

brightness property writable
brightness

The brightness of the display.

Methods:
init
init()

Initializes the display instance. Called by init and rotation setter.

blit_rect
blit_rect(buffer, x, y, w, h)

Blits a buffer to the display.

Parameters:

Name Type Description Default
buffer memoryview

The buffer to blit.

required
x int

The x-coordinate of the buffer.

required
y int

The y-coordinate of the buffer.

required
w int

The width to blit.

required
h int

The height to blit.

required

Returns:

Type Description
tuple

A tuple containing the x, y, w, h values.

fill_rect
fill_rect(x, y, w, h, c)

Fill a rectangle with a color.

Renders to the texture instead of directly to the window to facilitate scrolling and scaling.

Parameters:

Name Type Description Default
x int

The x-coordinate of the rectangle.

required
y int

The y-coordinate of the rectangle.

required
w int

The width of the rectangle.

required
h int

The height of the rectangle.

required
c int

The color of the rectangle.

required

Returns:

Type Description
tuple

A tuple containing the x, y, w, h values

pixel
pixel(x, y, c)

Set a pixel on the display.

Parameters:

Name Type Description Default
x int

The x-coordinate of the pixel.

required
y int

The y-coordinate of the pixel.

required
c int

The color of the pixel.

required

Returns:

Type Description
tuple

A tuple containing the x, y values.

vscrdef
vscrdef(tfa, vsa, bfa)

Set the vertical scroll definition.

Parameters:

Name Type Description Default
tfa int

The top fixed area.

required
vsa int

The vertical scroll area.

required
bfa int

The bottom fixed area.

required
vscsad
vscsad(vssa=None)

Set or get the vertical scroll start address.

Parameters:

Name Type Description Default
vssa int

The vertical scroll start address. Defaults to None.

None

Returns:

Name Type Description
int int

The vertical scroll start address.

render
render(renderRect=None)

Render the display. Automatically called after blitting or filling the display.

Parameters:

Name Type Description Default
renderRect Optional[SDL_Rect]

The rectangle to render. Defaults to None.

None
show
show()

Show the display.

deinit
deinit()

Deinitializes the SDLDisplay instance. Idempotent and safe to call from the quit path.

quit
quit(code=0)

Release SDL resources and terminate the process.

fill
fill(color)

Fill the display with a color.

Parameters:

Name Type Description Default
color int

The color to fill the display with.

required
scroll
scroll(dx, dy)

Scroll the display.

Parameters:

Name Type Description Default
dx int

The number of pixels to scroll horizontally.

required
dy int

The number of pixels to scroll vertically.

required
disable_auto_byteswap
disable_auto_byteswap(value)

Disable byte swapping in the display driver.

If self.requires_byteswap and the guest application is capable of byte swapping color data check to see if byte swapping can be disabled. If so, disable it.

Usage
# If byte swapping is required and the display driver is capable of having byte swapping disabled,
# disable it and set a flag so we can swap the color bytes as they are created.
if display_drv.requires_byteswap:
    needs_swap = display_drv.disable_auto_byteswap(True)
else:
    needs_swap = False

Parameters:

Name Type Description Default
value bool

Whether to disable byte swapping.

required

Returns:

Type Description
bool

Whether byte swapping was disabled successfully.

blit_transparent
blit_transparent(buf, x, y, w, h, key)

Blit a buffer with transparency.

Parameters:

Name Type Description Default
buf memoryview

The buffer to blit.

required
x int

The x coordinate to blit to.

required
y int

The y coordinate to blit to.

required
w int

The width to blit.

required
h int

The height to blit.

required
key int

The color key to use for transparency.

required

Returns:

Type Description
tuple

The x, y, w, h coordinates of the blitted area.

set_vscroll
set_vscroll(tfa=0, bfa=0)

Set the vertical scroll definition and move the vertical scroll to the top.

Parameters:

Name Type Description Default
tfa int

The top fixed area.

0
bfa int

The bottom fixed area.

0
translate_point
translate_point(point)

Translate a point from real coordinates to scrolled coordinates.

Useful for touch events.

Parameters:

Name Type Description Default
point tuple

The x and y coordinates to translate.

required

Returns:

Type Description
tuple

The translated x and y coordinates.

invert_colors
invert_colors(value)

Invert the colors of the display. Should be overridden by the subclass.

Parameters:

Name Type Description Default
value bool

True to invert the colors, False to restore the colors.

required
reset
reset()

Perform a reset of the display. Should be overridden by the subclass.

hard_reset
hard_reset()

Perform a hardware reset of the display. Should be overridden by the subclass.

soft_reset
soft_reset()

Perform a software reset of the display. Should be overridden by the subclass.

sleep_mode
sleep_mode(value)

Set the sleep mode of the display. Should be overridden by the subclass.

Parameters:

Name Type Description Default
value bool

True to enter sleep mode, False to exit sleep mode.

required

Functions:

poll

poll()

Polls for an event and returns the event type and data.

Returns:

Type Description

Optional[events]: The event type and data.

get

get()

Gets all events from the event queue.

Returns:

Type Description
[events]

[events]: A list of events.