-
Notifications
You must be signed in to change notification settings - Fork 20
5.Defining Sprite Image Data
On the GitHub I have now provided the 'LEDSpriteDataGenerator.exe', it is a windows program that can load a variety of standard graphic formats and can then produce the formatted sprite data source code ready for insertion into your projects. You must already have the image set to the right size and colour range. Images with more than 256 colours including black, will not be converted.
To make hand coding sprite image data easier I have provided several macros than can be used to define 1, 3, 7, 15, 31 & 255 colour sprites easily in your source code. The macros bit pack the data per line and as such are quite memory efficient.
It is also good practice to declare the image data as const
outside of any functions or classes as this will ensure the data is placed in program memory space rather than ram as most micros have far more program memory than ram.
The macros are B8_1BIT()
single colour, B8_2BIT()
3 colours, B8_3BIT()
7 colours, B8_4BIT()
15 colours & B8_5BIT()
31 colours.
The data in the '()' should always be 8 digits to ensure correct conversion to image data.
B8_1BIT(11110000) is correct whereas B8_1BIT(1111) is incorrect.
For B8_1BIT
only 0 & 1 are valid.
For B8_2BIT
0-3 is valid.
For B8_3BIT
0-7 is valid.
For B8_4BIT
0-9 & A-F are valid where 'A' is 10 and 'F' is 15.
For B8_5BIT
0-9 & A-V are valid where 'A' is 10 and 'V' is 31.
For _8BIT data you just simply declare the data as you would any uint8_t, e.g.
const uint8_t Sprite256Data[] = { 0x1F,0x27,0x35,0x35,0x86,0x74,0xD5,0xD5,0xBF.... };
So each B8_xBIT
macro is capable of defining 8 leds/pixels of data, using multiple macros on a line allows larger sprites to be defined e.g.
#define RAINBOW_WIDTH 13
#define RAINBOW_HEIGHT 7
const uint8_t RainbowData[] =
{
B8_3BIT(00000010),B8_3BIT(00000000),
B8_3BIT(00000121),B8_3BIT(00000000),
B8_3BIT(00001232),B8_3BIT(10000000),
B8_3BIT(00012343),B8_3BIT(21000000),
B8_3BIT(00123454),B8_3BIT(32100000),
B8_3BIT(01234565),B8_3BIT(43210000),
B8_3BIT(12345676),B8_3BIT(54321000),
};
Any '0' in the image data is treated as transparent, if you require a blank edge around your sprite you should define one of the colour array to be Black and use this colour index where you require the blank edge.
The Mask image data for a sprite can be defined using the B8_1BIT
macro and should have a '1' in any led/pixel position that you want to be used for collision detection. If your sprite is only a single colour sprite defined with B8_1BIT
then you can use the same array for both Data & Mask.