XOR bitmap to background?

Hi everyone, I’m trying to XOR my sprite to the background, but I can’t find a pre-existing function to do that.

I tried editing the slowBitmap function from the lib, but my sprite is rotated 90 degrees, am I missing something here?

void drawXORBitmap(int16_t x, int16_t y, const uint8_t *bitmap, uint8_t w, uint8_t h, uint8_t color){
  // no need to dar at all of we're offscreen
  if (x+w < 0 || x > WIDTH-1 || y+h < 0 || y > HEIGHT-1)

  int16_t xi, yi, byteWidth = (w + 7) / 8;
  for(yi = 0; yi < h; yi++) {
    for(xi = 0; xi < w; xi++ ) {
      if(pgm_read_byte(bitmap + yi * byteWidth + xi / 8) & (128 >> (xi & 7))) {
        bool col = arduboy.getPixel(x + xi, y + yi);
        arduboy.drawPixel(x + xi, y + yi, color^col);

Sorry for not looking at your exact code, but I’ve worked in another environment where it was best to XOR your 1-bit images onto the background in order to draw things properly. It was a pain in the butt, but I finished my project, eventually.

With that being said, why would you want to do something like this with the existing library. Are you looking to speed up image processing?

speed up? no…
I just like the effect and think it will work well with my current game.

Haha. Oh, my bad! I was over-thinking it, then.

The drawBitmap() function can do an XOR by using colour INVERT instead of WHITE or BLACK. It’s actually the only function that allows the use of colour INVERT.


Just what I was looking for! Thanks.

1 Like

Just a F.Y.I.: I made an attempt to make all the drawing functions in the Arduboy2 library support the INVERT colour. Unfortunately, many of the functions actually draw some of the pixels two (and possibly more) times. The first time, the pixel is XOR’d correctly, flipping its state, but the second time the XOR changes it back to its original state, so the result is “missing” pixels in the object that was drawn. Because of the time it would take me to fix and test the many places that this occurred, I decided to abandon my efforts.

Note: I don’t want this thread to go off topic, so if anyone wants to continue discussing this, please start a new linked topic. (Click on the posting time at the top right of this message and choose “+ New Topic”.