#define VGA ((unsigned char __far*)0xA0000000L)

void outofmemory(void);

unsigned char __far *dbuff;

void init_dbuff(void)
{
  if(!(dbuff = _fmalloc(64000)))
    outofmemory();
}

void put_pixel(unsigned x, unsigned y, unsigned char color)
{
  dbuff[(y<<6)+(y<<8)+x] = color;
}

void fill_screen(unsigned char color)
{
  _fmemset(dbuff, color, 64000);
}

void draw_rectangle(signed x, signed y, unsigned w, unsigned h,
                    unsigned char color)
{
  unsigned n;

  while(h--)
    for(n=0;n<w;n++)
      put_pixel(x+n, y+h, color);
}

void update_screen(void)
{
  _fmemcpy(VGA, dbuff, 64000);
}

void restore_display_mode(void)
{
  __asm
  {
    mov ax, 0x0003
    int 0x10
  }
}

#define KEY_ENTER 0x0D
#define KEY_ESC 27
#define KEY_UP 72
#define KEY_DOWN 80
#define KEY_LEFT 75
#define KEY_RIGHT 77

int poll_keycode(void)
{
  int c = getch();
    if(!c)
      return getch();
  return c;
}

#define poll_keypress() poll_keycode()
