struct font
{
  struct fontchar
  {
    unsigned char bitmap[8];
  } chr[256];
} font;

void draw_chr(unsigned x, unsigned y, unsigned char color, unsigned char chr)
{
  unsigned char *chrptr = font.chr[chr].bitmap;
  unsigned char n;
  unsigned char w;

  for(n=0;n<8;n++)
  {
    for(w=0;w<8;w++)
    {
      if((*chrptr >> w) & 1)
        put_pixel(x+w, y, color);
    }
    chrptr++;
    y++;
  }
}

void print_text(unsigned x, unsigned y, unsigned char color, char *str)
{
  unsigned n = 0;

  while(*str)
  {
    if(*str == '\n')
    {
      y += 9;
      n = 0;
    }
    else
    {
      draw_chr(x+n, y, color, *str);
      n += 9;
    }
    str++;
  }
}

void teletype_print(unsigned x, unsigned y, unsigned char color, char *str)
{
  static unsigned mx, my;
  static unsigned n = 0;

  if(x != -1)
  {
    mx = x;
    n = 0;
  }
  if(y != -1) my = y;

  while(*str)
  {
    if(*str == '\n')
    {
      newline:
      my += 9;
      n = 0;
    }
    else
    {
      draw_chr(mx+n, my, color, *str);
      n += 9;
      if(mx+n > 310)
        goto newline;
    }
    str++;

  }
}

int load_font(char *filename)
{
  FILE *fp = fopen(filename, "rb");
  if(!fp) return -1;

  if(!fread(&font, sizeof(struct font), 1, fp))
    return -1;

  fclose(fp);

  return 0;
}
