Add uOLED-96-G1 C Sample Code.
[frickel.git] / projects / geeknamensschilder_c / hardware / libraries / Sprite / Sprite.cpp
blob6055876102d8ad4178a41cdfdffd11e660db64cf
1 /*
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
20 #include <stdlib.h>
21 #include <stdarg.h>
22 //#include <stdio.h>
24 #include "Sprite.h"
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)
38 init(width, height);
41 Sprite::Sprite(uint8_t width, uint8_t height, uint8_t row, ...)
43 init(width, height);
45 if (!_buffer) return;
47 va_list ap;
48 va_start(ap, row);
50 int y = 0;
52 for (y = 0; ; y++) {
53 for (int x = 0; x < width && x < 8; x++)
54 write(x, y, (row >> (width - x - 1)) & 0x01);
56 if (y == height - 1)
57 break;
59 row = va_arg(ap, int); // using '...' promotes uint8_t to int
62 va_end(ap);
65 uint8_t Sprite::width() const
67 return _width;
70 uint8_t Sprite::height() const
72 return _height;
75 void Sprite::write(uint8_t x, uint8_t y, uint8_t value)
77 if (!_buffer) return;
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;