1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-11-30 11:24:12 +01:00
Arduino/libraries/Sprite/Sprite.cpp

96 lines
2.4 KiB
C++
Raw Normal View History

2006-02-23 13:01:57 +01:00
/*
Sprite.cpp - 2D sprite buffer library for Arduino & Wiring
Copyright (c) 2006 David A. Mellis. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <stdarg.h>
//#include <stdio.h>
#include "Sprite.h"
void Sprite::init(uint8_t width, uint8_t height)
2006-02-23 13:01:57 +01:00
{
_width = width >= 8 ? 8 : width;
_height = height >= 8 ? 8 : height;
2006-02-23 13:01:57 +01:00
// for now, do nothing if this allocation fails. methods that require it
// should silently fail if _buffer is null.
_buffer = (uint8_t *) calloc(_height, 1);
2006-02-23 13:01:57 +01:00
}
Sprite::Sprite(uint8_t width, uint8_t height)
2006-02-23 13:01:57 +01:00
{
init(width, height);
2006-02-23 13:01:57 +01:00
}
Sprite::Sprite(uint8_t width, uint8_t height, uint8_t row, ...)
2006-02-23 13:01:57 +01:00
{
init(width, height);
2006-02-23 13:01:57 +01:00
if (!_buffer) return;
va_list ap;
va_start(ap, row);
int y = 0;
for (y = 0; ; y++) {
2006-02-23 13:01:57 +01:00
for (int x = 0; x < width && x < 8; x++)
write(x, y, (row >> (width - x - 1)) & 0x01);
if (y == height - 1)
break;
row = va_arg(ap, int); // using '...' promotes uint8_t to int
}
va_end(ap);
}
uint8_t Sprite::width() const
{
return _width;
}
uint8_t Sprite::height() const
{
return _height;
}
void Sprite::write(uint8_t x, uint8_t y, uint8_t value)
{
if (!_buffer) return;
// uint8_t's can't be negative, so don't test for negative x and y.
if (x >= _width || y >= _height) return;
2006-02-23 13:01:57 +01:00
// we need to bitwise-or the value of the other pixels in the byte with
// the new value, masked and shifted into the proper bits.
_buffer[y] = (_buffer[y] & ~(0x01 << x)) | ((value & 0x01) << x);
2006-02-23 13:01:57 +01:00
}
uint8_t Sprite::read(uint8_t x, uint8_t y) const
{
if (!_buffer) return 0;
// uint8_t's can't be negative, so don't test for negative x and y.
if (x >= _width || y >= _height) return 0;
2006-02-23 13:01:57 +01:00
return (_buffer[y] >> x) & 0x01;
}