here is the original from summoner123's library
.cpp
- Code: Select all
void Display::drawBitmap(int8_t x, int8_t y, const uint8_t *bitmap) {
int8_t w = pgm_read_byte(bitmap);
int8_t h = pgm_read_byte(bitmap + 1);
bitmap = bitmap + 2; //add an offset to the pointer to start after the width and height
drawBitmap(x,y,w,h,bitmap);
}
void Display::drawBitmap(int8_t x, int8_t y, int8_t w, int8_t h , const uint8_t *bitmap) {
#if (ENABLE_BITMAPS > 0)
/* original code
int8_t i, j, byteWidth = (w + 7) / 8;
for (j = 0; j < h; j++) {
for (i = 0; i < w; i++) {
if (pgm_read_byte(bitmap + j * byteWidth + i / 8) & (B10000000 >> (i % 8))) {
drawPixel(x + i, y + j);
}
}
}
*/
uint8_t * buffer = getBuffer();
const uint8_t col = color;
const uint8_t bw = (w+7) / 8;
// clip
if (x >= LCDWIDTH)
return;
if (x + w <= 0)
return;
if (y >= LCDHEIGHT)
return;
if (y + h <= 0)
return;
if (y < 0)
h += y, bitmap -= bw * y, y = 0;
if (y + h > LCDHEIGHT)
h = LCDHEIGHT - y;
uint8_t x1 = max(0, x);
uint8_t x2 = min(LCDWIDTH, x + w);
#ifdef ENABLE_GRAYSCALE
uint8_t g = y ^ frameCount;
#endif
// draw
uint8_t first_bitmap_mask = 0x80 >> ((x1 - x) & 7);
const uint8_t * bitmap_line = bitmap + (x1 - x) / 8;
uint8_t screen_mask = 0x01 << (y % 8);
uint8_t * screen_row = buffer + (y / 8) * LCDWIDTH + x1;
for (uint8_t dy=0; dy<h; dy++, bitmap_line+=bw)
{
const uint8_t * bitmap_ptr = bitmap_line;
uint8_t bitmap_mask = first_bitmap_mask;
uint8_t pixels = pgm_read_byte(bitmap_ptr);
uint8_t * dst = screen_row;
if (col == BLACK)
for (uint8_t sx=x1; sx<x2; sx++, dst++)
{
if (pixels & bitmap_mask)
*dst |= screen_mask;
bitmap_mask >>= 1;
if (!bitmap_mask)
{
bitmap_mask = 0x80;
pixels = pgm_read_byte(++bitmap_ptr);
}
}
else if (col == WHITE)
{
uint8_t inv_screen_mask = ~screen_mask;
for (uint8_t sx=x1; sx<x2; sx++, dst++)
{
if (pixels & bitmap_mask)
*dst &= inv_screen_mask;
bitmap_mask >>= 1;
if (!bitmap_mask)
{
bitmap_mask = 0x80;
pixels = pgm_read_byte(++bitmap_ptr);
}
}
}
#ifdef ENABLE_GRAYSCALE
else if (col == GRAY)
{
uint8_t inv_screen_mask = ~screen_mask;
for (uint8_t sx=x1; sx<x2; sx++, dst++)
{
if (pixels & bitmap_mask)
{
if ((sx^g) & 1)
*dst |= screen_mask;
else
*dst &= inv_screen_mask;
}
bitmap_mask >>= 1;
if (!bitmap_mask)
{
bitmap_mask = 0x80;
pixels = pgm_read_byte(++bitmap_ptr);
}
}
g ^= 1;
}
#endif
else // invert
for (uint8_t sx=x1; sx<x2; sx++, dst++)
{
if (pixels & bitmap_mask)
*dst ^= screen_mask;
bitmap_mask >>= 1;
if (!bitmap_mask)
{
bitmap_mask = 0x80;
pixels = pgm_read_byte(++bitmap_ptr);
}
}
screen_mask <<= 1;
if (!screen_mask)
{
screen_mask = 1;
screen_row += LCDWIDTH;
}
}
#else
drawRect(x, y, w, h);
#endif
}
void Display::drawBitmap(int8_t x, int8_t y, int8_t w,int8_t h, const uint8_t *bitmap, uint8_t dx, uint8_t dy, uint8_t dw, uint8_t dh) {
int8_t i, j, byteWidth = (w + 7) / 8;
dw += dx;
dh += dy;
int8_t largest = 0;
int8_t largesty = 0;
for (j = 0; j < h; j++) {
for (i = 0; i < w; i++) {
if (pgm_read_byte(bitmap + j * byteWidth + i / 8) & (B10000000 >> (i % 8))) {
int8_t drawX = x + i;
int8_t drawY = y + j;
if(drawX >= dx && drawX < dw && drawY >= dy && drawY < dh){
drawPixel(drawX, drawY);
}
}
}
}
}
boolean Display::getBitmapPixel(const uint8_t* bitmap, uint8_t x, uint8_t y){
return pgm_read_byte(bitmap+2 + y * ((pgm_read_byte(bitmap)+7)/8) + (x >> 3)) & (B10000000 >> (x % 8));
}
void Display::drawBitmap(int8_t x, int8_t y, const uint8_t *bitmap,
uint8_t rotation, uint8_t flip) {
if((rotation == NOROT) && (flip == NOFLIP)){
drawBitmap(x,y,bitmap); //use the faster algorithm
return;
}
uint8_t w = pgm_read_byte(bitmap);
uint8_t h = pgm_read_byte(bitmap + 1);
bitmap = bitmap + 2; //add an offset to the pointer to start after the width and height
#if (ENABLE_BITMAPS > 0)
int8_t i, j, //coordinates in the raw bitmap
k, l, //coordinates in the rotated/flipped bitmap
byteNum, bitNum, byteWidth = (w + 7) >> 3;
rotation %= 4;
for (i = 0; i < w; i++) {
byteNum = i / 8;
bitNum = i % 8;
for (j = 0; j < h; j++) {
if (pgm_read_byte(bitmap + j * byteWidth + byteNum) & (B10000000 >> bitNum)) {
switch (rotation) {
case NOROT: //no rotation
k = i;
l = j;
break;
case ROTCCW: //90° counter-clockwise
k = j;
l = w - i - 1;
break;
case ROT180: //180°
k = w - i - 1;
l = h - j - 1;
break;
case ROTCW: //90° clockwise
k = h - j - 1;
l = i;
break;
}
if (flip) {
flip %= 4;
if (flip & B00000001) { //horizontal flip
k = w - k - 1;
}
if (flip & B00000010) { //vertical flip
l = h - l;
}
}
k += x; //place the bitmap on the screen
l += y;
drawPixel(k, l);
}
}
}
#else
drawRect(x, y, w, h);
#endif
}
void Display::drawTilemap(int x, int y, const uint8_t *tilemap, const uint8_t **spritesheet){
drawTilemap(x,y,tilemap,spritesheet,0,0,LCDWIDTH,LCDHEIGHT);
}
void Display::drawTilemap(int x, int y, const uint8_t *tilemap, const uint8_t **spritesheet,uint8_t dx,uint8_t dy,uint8_t dw,uint8_t dh){
uint8_t tilemap_width = pgm_read_byte(tilemap);
uint8_t tilemap_height = pgm_read_byte(tilemap + 1);
uint8_t tile_width = pgm_read_byte(tilemap + 2);
uint8_t tile_height = pgm_read_byte(tilemap + 3);
tilemap += 4; // now the first tiyleis at tilemap
uint8_t ddw = dw + dx;
uint8_t ddh = dh + dy;
uint8_t maxDdx = (dw - x + tile_width - 1) / tile_width;
uint8_t maxDdy = (dh - y + tile_height - 1) / tile_height;
if(tilemap_width < maxDdx){
maxDdx = tilemap_width;
}
if(tilemap_height < maxDdy){
maxDdy = tilemap_height;
}
int8_t startDdx = (-x) / tile_width;
int8_t startDdy = (-y) / tile_height;
if(startDdx < 0){
startDdx = 0;
}
if(startDdy < 0){
startDdy = 0;
}
if(flagcollision)numcolision = 0; //Line 735 - clear numcolision - ADD by Summoner123
for(uint8_t ddy = startDdy;ddy < maxDdy;ddy++){
for(uint8_t ddx = startDdx;ddx < maxDdx;ddx++){
int8_t drawX = ddx*tile_width + x + dx;
int8_t drawY = ddy*tile_height + y + dy;
uint8_t tile = pgm_read_byte(tilemap + ddy*tilemap_width + ddx);
if(drawX >= dx && drawY >= dy && drawX <= (ddw-tile_width) && drawY <= (ddh-tile_height)){
drawBitmap(drawX,drawY,tile_width,tile_height,spritesheet[tile]);
if(flagcollision){
solid[numcolision].x = drawX; //Save X coordinate - ADD by Summoner123
solid[numcolision].y = drawY; //Save Y coordinate - ADD by Summoner123
solid[numcolision].spritecol = spritesheet[tile]; //Save Sprite of tile - ADD by Summoner123
numcolision++; //Increment numcolision - ADD by Summoner123
}
}else{ // we need to draw a partial bitmap
drawBitmap(drawX,drawY,tile_width,tile_height,spritesheet[tile],dx,dy,dw,dh);
}
}
}
}
.h
- Code: Select all
void drawBitmap(int8_t x, int8_t y, const uint8_t *bitmap);
void drawBitmap(int8_t x, int8_t y, int8_t w, int8_t h , const uint8_t *bitmap);
void drawBitmap(int8_t x, int8_t y, const uint8_t *bitmap, uint8_t rotation, uint8_t flip);
void drawBitmap(int8_t x, int8_t y, int8_t w, int8_t h, const uint8_t *bitmap, uint8_t dx, uint8_t dy, uint8_t dw, uint8_t dh);
boolean getBitmapPixel(const uint8_t* bitmap, uint8_t x, uint8_t y);
void drawTilemap(int x, int y, const uint8_t *tilemap, const uint8_t **spritesheet);
void drawTilemap(int x, int y, const uint8_t *tilemap, const uint8_t **spritesheet,uint8_t dx,uint8_t dy,uint8_t dw,uint8_t dh);
heres mine
- Code: Select all
//adafruit_gfx
void drawBitmap1(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color);
void drawBitmap2(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color, uint16_t bg);
void drawBitmap3(int16_t x, int16_t y, uint8_t *bitmap, int16_t w, int16_t h, uint16_t color);
void drawBitmap4(int16_t x, int16_t y, uint8_t *bitmap, int16_t w, int16_t h, uint16_t color, uint16_t bg);
void drawXBitmap(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color);
//GAMEBUINO----SUMMONER123------for tilemap--Duhjoker
void drawBitmap(int8_t x, int8_t y, int8_t w, int8_t h, const uint8_t *bitmap, uint8_t dx, uint8_t dy, uint8_t dw, uint8_t dh, uint16_t color);
boolean getBitmapPixel(const uint8_t* bitmap, uint8_t x, uint8_t y);
//draw from SD mouguino updated by Duhjoker
//Draw bitmap from SD at position x0, y0
//void drawBitmapFromSdA(byte x, byte y, char *bitmap, uint16_t color);
//void drawBitmapFromSdB(byte x, byte y, char *bitmap, uint8_t dx, uint8_t dy, uint8_t dw, uint8_t dh, uint16_t color);
//int drawBitmapFromSd(byte x0, byte y0, char *bitmap, uint16_t color);
//void updateSlide();
//GAMEBUINO----SUMMONER123
void drawTilemap(int x, int y, const uint8_t *tilemap, const uint8_t **spritesheet, uint16_t color);
void drawTilemap(int x, int y, const uint8_t *tilemap, const uint8_t **spritesheet, uint8_t dx, uint8_t dy, uint8_t dw, uint8_t dh, uint16_t color);
[/code}
.cpp
[code]
//Adafruit GFX
// Draw a 1-bit image (bitmap) at the specified (x,y) position from the
// provided bitmap buffer (must be PROGMEM memory) using the specified
// foreground color (unset bits are transparent).
void Display::drawBitmap1(int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color) {
int16_t i, j, byteWidth = (w + 7) / 8;
uint8_t byte;
for (j = 0; j<h; j++) {
for (i = 0; i<w; i++) {
if (i & 7) byte <<= 1;
else byte = pgm_read_byte(bitmap + j * byteWidth + i / 8);
if (byte & 0x80) drawPixel(x + i, y + j, color);
}
}
}
//Adafruit GFX
// Draw a 1-bit image (bitmap) at the specified (x,y) position from the
// provided bitmap buffer (must be PROGMEM memory) using the specified
// foreground (for set bits) and background (for clear bits) colors.
void Display::drawBitmap2(int16_t x, int16_t y,
const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color, uint16_t bg) {
int16_t i, j, byteWidth = (w + 7) / 8;
uint8_t byte;
for (j = 0; j<h; j++) {
for (i = 0; i<w; i++) {
if (i & 7) byte <<= 1;
else byte = pgm_read_byte(bitmap + j * byteWidth + i / 8);
if (byte & 0x80) drawPixel(x + i, y + j, color);
else drawPixel(x + i, y + j, bg);
}
}
}
//Adafruit GFX
// drawBitmap() variant for RAM-resident (not PROGMEM) bitmaps.
void Display::drawBitmap3(int16_t x, int16_t y,
uint8_t *bitmap, int16_t w, int16_t h, uint16_t color) {
int16_t i, j, byteWidth = (w + 7) / 8;
uint8_t byte;
for (j = 0; j<h; j++) {
for (i = 0; i<w; i++) {
if (i & 7) byte <<= 1;
else byte = bitmap[j * byteWidth + i / 8];
if (byte & 0x80) drawPixel(x + i, y + j, color);
}
}
}
//Adafruit GFX
// drawBitmap() variant w/background for RAM-resident (not PROGMEM) bitmaps.
void Display::drawBitmap4(int16_t x, int16_t y,
uint8_t *bitmap, int16_t w, int16_t h, uint16_t color, uint16_t bg) {
int16_t i, j, byteWidth = (w + 7) / 8;
uint8_t byte;
for (j = 0; j<h; j++) {
for (i = 0; i<w; i++) {
if (i & 7) byte <<= 1;
else byte = bitmap[j * byteWidth + i / 8];
if (byte & 0x80) drawPixel(x + i, y + j, color);
else drawPixel(x + i, y + j, bg);
}
}
}
void Display::drawBitmap(int8_t x, int8_t y, int8_t w, int8_t h, const uint8_t *bitmap, uint8_t dx, uint8_t dy, uint8_t dw, uint8_t dh, uint16_t color) {
int8_t i, j, byteWidth = (w + 7) / 8;
dw += dx;
dh += dy;
int8_t largest = 0;
int8_t largesty = 0;
for (j = 0; j < h; j++) {
for (i = 0; i < w; i++) {
if (pgm_read_byte(bitmap + j * byteWidth + i / 8) & (B10000000 >> (i % 8))) {
int8_t drawX = x + i;
int8_t drawY = y + j;
if (drawX >= dx && drawX < dw && drawY >= dy && drawY < dh) {
drawPixel(x, y, color);
}
}
}
}
}
void Display::drawTilemap(int x, int y, const uint8_t *tilemap, const uint8_t **spritesheet, uint16_t color) {
drawTilemap(x, y, tilemap, spritesheet, 0, 0, TFT_ILI93XX_TFTWIDTH, TFT_ILI93XX_TFTHEIGHT, color);
}
void Display::drawTilemap(int x, int y, const uint8_t *tilemap, const uint8_t **spritesheet, uint8_t dx, uint8_t dy, uint8_t dw, uint8_t dh, uint16_t color) {
uint8_t tilemap_width = pgm_read_byte(tilemap);
uint8_t tilemap_height = pgm_read_byte(tilemap + 1);
uint8_t tile_width = pgm_read_byte(tilemap + 2);
uint8_t tile_height = pgm_read_byte(tilemap + 3);
tilemap += 4; // now the first tiyleis at tilemap
uint8_t ddw = dw + dx;
uint8_t ddh = dh + dy;
uint8_t maxDdx = (dw - x + tile_width - 1) / tile_width;
uint8_t maxDdy = (dh - y + tile_height - 1) / tile_height;
if (tilemap_width < maxDdx) {
maxDdx = tilemap_width;
}
if (tilemap_height < maxDdy) {
maxDdy = tilemap_height;
}
int8_t startDdx = (-x) / tile_width;
int8_t startDdy = (-y) / tile_height;
if (startDdx < 0) {
startDdx = 0;
}
if (startDdy < 0) {
startDdy = 0;
}
if (flagcollision)numcolision = 0; //Line 735 - clear numcolision - ADD by Summoner123
for (uint8_t ddy = startDdy; ddy < maxDdy; ddy++) {
for (uint8_t ddx = startDdx; ddx < maxDdx; ddx++) {
int8_t drawX = ddx*tile_width + x + dx;
int8_t drawY = ddy*tile_height + y + dy;
uint8_t tile = pgm_read_byte(tilemap + ddy*tilemap_width + ddx);
if (drawX >= dx && drawY >= dy && drawX <= (ddw - tile_width) && drawY <= (ddh - tile_height)) {
drawBitmap1(drawX, drawY, spritesheet[tile], tile_width, tile_height, color);
if (flagcollision) {
solid[numcolision].x = drawX; //Save X coordinate - ADD by Summoner123
solid[numcolision].y = drawY; //Save Y coordinate - ADD by Summoner123
solid[numcolision].spritecol = spritesheet[tile]; //Save Sprite of tile - ADD by Summoner123
numcolision++; //Increment numcolision - ADD by Summoner123
}
}
else { // we need to draw a partial bitmap
drawBitmap(drawX, drawY, tile_width, tile_height, spritesheet[tile], dx, dy, dw, dh, color);
}
}
}
}