Using Pixmaps in Libgdx

Pixel based shooter image

Sharing is caring!

Pixmaps, as the name implies is a map of pixels, each pixel containing a level for red, green and blue and sometimes alpha. They can be used to store image data and can be generated on the fly in your application. Pixmaps reside in native heap memory and should be disposed when they are no longer needed. Pixmaps can be created in libgdx using a variety of formats such as RGBA8888, RGBA4444, RGB888 and RGB565. These formats simply tell the pixmap how to store the colour and alpha values. RGBA8888 for example stores each value, R,G,B and alpha as 8-bit values, 000000FF as black and FFFFFFFF as white.

Why use pixmaps instead of saved images?

Well, if you have a single image with lots of variation such as a ball that is sometimes drawn with spikes, sometimes with a glow, sometimes with an aura etc. Then you can use pixmaps to combine these images into a single texture at runtime. Another example would be a character creation screen, here we use lots of separate images together to create a character. A pixmap could be used here to store the finished character. Another reason is loading an image from a disk is a lot slower than creating the image on the fly, so there’s a speed improvement when using pixmaps. This, however, isn’t much of an issue if you’re using an asset manager. (which I highly suggest you do)

How to use a pixmap

First we create a pixmap with a width of 16, a height of 16, and format of RGBA8888.

Then we can start drawing our pixels to it. Here we will fill the pixmap with white and draw a small red circle in the center.

Now we add 4 dark blue pixels, one in each corner.

You should now have an image like this 

Now let’s make another pixmap this time 32 x 32.

And draw our smaller pixmap to it 3 times twice normally and once stretched on the right.

You should now have an image like this 

Now you can create a new texture with the image and dispose your no longer required pixmaps.

 

Further Reading:

Pixmap Guide

 

Sharing is caring!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.