Sprite

Sprites are optimized for fast drawing on the screen, but slow for direct pixel access. They are best for drawing a relatively unchanging image repeatedly on the screen.

More...

Constructors

this
this(SimpleWindow win, Image i)

Makes a sprite based on the image with the initial contents from the Image

Destructor

A destructor is present on this object, but not explicitly documented in the source.

Members

Functions

dispose
void dispose()

Call this when you're ready to get rid of it

draw
ScreenPainter draw()
drawAt
void drawAt(ScreenPainter painter, Point where, Point imageUpperLeft, Size sliceSize)

Draws the image on the specified painter at the specified point. The point is the upper-left point where the image will be drawn.

takeScreenshot
TrueColorImage takeScreenshot()

Copies the sprite's current state into a TrueColorImage.

Properties

height
int height [@property getter]
width
int width [@property getter]

Static functions

fromMemoryImage
Sprite fromMemoryImage(SimpleWindow win, MemoryImage img, bool enableAlpha)

Inherited Members

From CapableOfBeingDrawnUpon

draw
ScreenPainter draw()
width
int width()
height
int height()
takeScreenshot
TrueColorImage takeScreenshot()

Be warned: this can be a very slow operation

Detailed Description

On X11, this corresponds to an XPixmap. On Windows, it still uses a bitmap, though I'm not sure that's ideal and the implementation might change.

You create one by giving a window and an image. It optimizes for that window, and copies the image into it to use as the initial picture. Creating a sprite can be quite slow (especially over a network connection) so you should do it as little as possible and just hold on to your sprite handles after making them. simpledisplay does try to do its best though, using the XSHM extension if available, but you should still write your code as if it will always be slow.

Then you can use sprite.drawAt(painter, point); to draw it, which should be a fast operation - much faster than drawing the Image itself every time.

Sprite represents a scarce resource which should be freed when you are done with it. Use the dispose method to do this. Do not use a Sprite after it has been disposed. If you are unsure about this, don't take chances, just let the garbage collector do it for you. But ideally, you can manage its lifetime more efficiently.

Sprite, like the rest of simpledisplay's ScreenPainter, does not support alpha blending in its drawing at this time. That might change in the future, but if you need alpha blending right now, use OpenGL instead. See gamehelpers.d for a similar class to Sprite that uses OpenGL: OpenGlTexture.

Update: on April 23, 2021, I finally added alpha blending support. You must opt in by setting the enableAlpha = true in the constructor.

Suggestion Box / Bug Report