Colorize these bitmap functions


(Jeremy Wood) #1

I was hoping ni could get some help colorizing these bitmap functions in which I mean adding color arguments to fuctions .cpp counter part. ive been trying for a while but cant seem to nail it.


		void drawBitmapA(int8_t x, int8_t y, const uint8_t *bitmap);
		void drawBitmapB(int8_t x, int8_t y, int8_t w, int8_t h, const uint8_t *bitmap);
		void drawBitmapC(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 Display::drawBitmapA(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::drawBitmapB(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);
	  }
   }
}
	void Display::drawBitmapC(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::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);
			}
		}
	}
}

(Scott) #2

Respectfully, I’d say you’re putting the cart before the horse. Shouldn’t there be some indication that there will ever be an Arduboy with a color display, and have some confidence of what the dimensions and capabilities of that display are, before working on software enhancements for color handling?


(Gavin Atkin) #3

thje color variable is uint16_t color

This would mean for a display of 128x64 pixels (same as current) but with 16 bits per pixel, the video buffer would be 16,384 bytes. The ram in the current Arduboy is 2,500 Bytes. This would require a significant increase in ram, flash, and processor.

If it’s something you really want to make, go for it. But the odds you will ever actually get to use it on anything other than hardware you create yourself are pretty small unfortunately.

Once the next unit is officially announced there will be a lot of interest in creating a new graphics library to support it.

Good luck!


(Jeremy Wood) #4

I will be using my own hardware, a teensy 3.2 with 256k flash and a 32-bit processor but oh well I’ll just stick with the arduino forum.


(spinal) #5

One way to keep the video buffer small, would be to make sure the main buffer stays 1bpp, but colorize 8x8 blocks in single colours, aligning them to an 8x8 grid, like the zx spectrum. which would only require a 16x8 array, maybe even 8x8 if you stick to 16 colours, or even less.


(Kevin) #6

The scope is outside of Arduboy for now but we do have a general section, if people have ideas feel free to add them! Never know when something might be useful! :slight_smile: