Speed optimizations for Hi-Res 2 bpp mode


the performance of full screen drawing in Hi-Res mode (220x176, 4 colors) is not enough for most games. To optimize drawing to the screen buffer, I added Display::setClipRect() method. That can be used e.g. to draw only the “dirty rect” area of the background bitmap to the screen buffer. I have made also another speed optimization which is drawing only the “updated rect” from the screen buffer to the LCD in the Hi-Res 2 bpp refresh functions. Updating always the whole screen to LCD easily kills performance.

How to use:

If you want to redraw only a part of a bitmap to the screen buffer you can now do as follows:

  1. First, set the clipping rect for bitmap drawing:
    mygame.display.setClipRect(50, 50, 10, 10);

  2. Now, when you draw the backgound bitmap like this:
    mygame.display.drawBitmap(0, 0, pokitto_bmp);
    It will only draw an area of the bitmap, where the top left corner is at (50,50) and the size of the area is 10 x 10 pixels, to the screen buffer.
    Remember to reset the clip rect to the full screen when you do not need it any more.

If you want to update only a part of the screen buffer to LCD you can now do as follows:

  • mygame.update(false, 50, 50, 10, 10)
    This will update only a part of the screen buffer to LCD. Note that for speed optimization reasons the actual area is a bit bigger. The top left corner coordinates and the width are automatically changed to be the values that are dividable by 4. The height is expanded to be dividable by 8. Still, this is terribly faster than drawing the whole screen to LCD (!)

I added a small test program, which gradually reveals the picture by drawing vertical stripes. The speed without the update rect was about 13 fps. After the update rect is used, the speed is about 50 fps :slight_smile: (On HW of course). The program is found in: “Examples\UpdateAndClipRect”

I added comparable methods also to MicroPython API.

Note: Currently the “update rect” optimization affects only Hi-Res 2 bpp mode. However, nothing is stopping you from implementing it to other screen modes. Look at the function Pokitto::lcdRefreshMode1() as an example. Also the clip rect is only implemented in basic bitmap drawing in Hi-Res 2 bpp mode.


Good work @Hanski! Is this in the PokittoLib repo now?

1 Like

Thanks! I have made a PR to Pokittolib.

1 Like



Did the changes you made recently get implemented on GitHub before the merge? I know you said yesterday you planned to update the GitHub lib.

1 Like

No. But I will do it right now. The changes were related to sound & EEPROM.

1 Like

Everything is under control now. Project targets for compiling your demos @Hanski were added and tested.

@epicdude312 take a look at how the 4 different target projects are set up under EmBitz, you can see how Project properties -> target .elf , search directory and My_settings.h, and pre/post build steps together describe a target. By comparing the targets you will notice where changes are for each target. Its a little confusing at first but you will get the hang of it.