#include <SPI.h>
#include <GD.h>

#define GRAY(c)  RGB(c,c,c)

void setup()
{
  Serial.begin(1000000);    // JCB
  GD.begin();

  // Load the font16 into sprite RAM_SPRIMG
  Asset a;
  a.open("fonts", "font16", NULL);
  a.load(RAM_SPRIMG);
  // JCB-B
  GD.wr16(PALETTE4A    , GRAY(255));
  GD.wr16(PALETTE4A + 2, GRAY(128));
  GD.wr16(PALETTE4A + 4, GRAY( 64));
  GD.wr16(PALETTE4A + 6, TRANSPARENT);
  // JCB-B

  GD.wr16(RAM_PAL, RGB(0, 64, 32)); // teal background
}

// Write string s at pixel (x,y) using sprites starting at sprnum
static void text(int sprnum, int x, int y, char *s)
{
  while (*s) {
    byte c = *s - ' ';              // font characters start at ' '
    byte image = c >> 2;            // four characters per image
    byte pal = 8 | ((c & 3) << 1);  // 4-color palette select
    GD.sprite(sprnum, x, y, image, pal, 0);
    x += 11;
    sprnum++;
    s++;
  }
}

void loop()
{
  text(0, 10, 100, "This is font16");
  text(20, 10, 120, "loaded from flash");
  GD.waitvblank();
  // GD.screenshot(0); for(;;);  // JCB
}
