2 Sprite.cpp - 2D sprite buffer library for Arduino & Wiring
3 Copyright (c) 2006 David A. Mellis. All right reserved.
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 void Sprite::init(uint8_t width
, uint8_t height
)
28 _width
= width
>= 8 ? 8 : width
;
29 _height
= height
>= 8 ? 8 : height
;
31 // for now, do nothing if this allocation fails. methods that require it
32 // should silently fail if _buffer is null.
33 _buffer
= (uint8_t *) calloc(_height
, 1);
36 Sprite::Sprite(uint8_t width
, uint8_t height
)
41 Sprite::Sprite(uint8_t width
, uint8_t height
, uint8_t row
, ...)
53 for (int x
= 0; x
< width
&& x
< 8; x
++)
54 write(x
, y
, (row
>> (width
- x
- 1)) & 0x01);
59 row
= va_arg(ap
, int); // using '...' promotes uint8_t to int
65 uint8_t Sprite::width() const
70 uint8_t Sprite::height() const
75 void Sprite::write(uint8_t x
, uint8_t y
, uint8_t value
)
79 // uint8_t's can't be negative, so don't test for negative x and y.
80 if (x
>= _width
|| y
>= _height
) return;
82 // we need to bitwise-or the value of the other pixels in the byte with
83 // the new value, masked and shifted into the proper bits.
84 _buffer
[y
] = (_buffer
[y
] & ~(0x01 << x
)) | ((value
& 0x01) << x
);
87 uint8_t Sprite::read(uint8_t x
, uint8_t y
) const
89 if (!_buffer
) return 0;
91 // uint8_t's can't be negative, so don't test for negative x and y.
92 if (x
>= _width
|| y
>= _height
) return 0;
94 return (_buffer
[y
] >> x
) & 0x01;