#if defined(SDLFUNCS) || defined(DOS)

void select_tile(struct coord *legal_moves, unsigned char legal_move_count, unsigned char *row, unsigned char *col)
{
#ifdef SDLFUNCS
  SDL_Scancode i;
#endif
#ifdef DOS
  int i;
#endif
  static char old_row = 7;
  static char old_col = 7;

  *row = old_row;
  *col = old_col;

  while(1)
  {
    i = poll_keypress();
#ifdef SDLFUNCS
    if(i == SDL_SCANCODE_UNKNOWN) continue;
#endif
    if(i == KEY_ENTER) break;
    switch (i)
    {
      case KEY_UP:
        if(*row) --*row;
        break;
      case KEY_DOWN:
        if(*row < 7) ++*row;
        break;
      case KEY_LEFT:
        if(*col) --*col;
        break;
      case KEY_RIGHT:
        if(*col < 7) ++*col;
        break;
      case KEY_ESC: show_menu(mainmenu);
    }
    draw_board(&board, legal_moves, legal_move_count, *row, *col);
  }

  old_col = *col;
  old_row = *row;
}
#endif

void init_board(struct board *brd)
{
  memset(brd, 0, sizeof(struct board));
  brd->tiles[0][0] = PC_ROOK | PC_BLACK;
  brd->tiles[0][1] = PC_KNIGHT | PC_BLACK;
  brd->tiles[0][2] = PC_BISHOP | PC_BLACK;
  brd->tiles[0][3] = PC_QUEEN | PC_BLACK;
  brd->tiles[0][4] = PC_KING | PC_BLACK;
  brd->tiles[0][5] = PC_BISHOP | PC_BLACK;
  brd->tiles[0][6] = PC_KNIGHT | PC_BLACK;
  brd->tiles[0][7] = PC_ROOK | PC_BLACK;
  memset(brd->tiles[1], PC_PAWN_UNMOVED | PC_BLACK, 8);

  memset(brd->tiles[6], PC_PAWN_UNMOVED | PC_WHITE, 8);
  brd->tiles[7][0] = PC_ROOK | PC_WHITE;
  brd->tiles[7][1] = PC_KNIGHT | PC_WHITE;
  brd->tiles[7][2] = PC_BISHOP | PC_WHITE;
  brd->tiles[7][3] = PC_QUEEN | PC_WHITE;
  brd->tiles[7][4] = PC_KING | PC_WHITE;
  brd->tiles[7][5] = PC_BISHOP | PC_WHITE;
  brd->tiles[7][6] = PC_KNIGHT | PC_WHITE;
  brd->tiles[7][7] = PC_ROOK | PC_WHITE;

}

void draw_board(struct board *brd, struct coord *legal_moves, unsigned char legal_move_count, unsigned selrow, unsigned selcol)
{
  unsigned char row, col;
  char chr;
  unsigned char piece;

#if defined(SDLFUNCS) || defined(DOS)
  if(graphics)
  {
    fill_screen(1);
    for(col=0;col<8;col++)
      draw_chr(16+col*24, 192, 0xF, 'A' + col);
    for(row=0;row<8;row++)
    {
      draw_chr(0, 9+row*24, 0xF, '1' + (7-row));

      for(col=0;col<8;col++)
      {
        draw_rectangle(8+col*24, row*24, 24, 24, (row & 1) ^ (col & 1) ? 6 : 0xF);
        if(legal_move_count && move_in_list(legal_moves, legal_move_count, row, col))
          draw_rectangle(14+col*24, 6+row*24, 12, 12, 2);
        if(selrow == row && selcol == col)
          draw_rectangle(10+col*24, 2+row*24, 20, 20, 9);
      }
    }
    print_text(220, 20, 0xF, "Black:");
    print_text(220, 30, 0xF, game_status.last_moves.last_moves_black[0]);
    print_text(220, 40, 0xF, game_status.last_moves.last_moves_black[1]);

    print_text(220, 60, 0xF, "White:");
    print_text(220, 70, 0xF, game_status.last_moves.last_moves_white[0]);
    print_text(220, 80, 0xF, game_status.last_moves.last_moves_white[1]);

  }
#endif

  if(!graphics)
  {
    for(col=0;col<8;col++)
      printf("  %c  ", 'a' + col);
    printf("\n");
  }
  for(row=0;row<8;row++)
  {
    if(!graphics) printf("%c ", '8' - row);
    for(col=0;col<8;col++)
    {
      piece = piece_in_tile(brd, row, col);
#if defined(SDLFUNCS) || defined(DOS)
      if(graphics)
      {
        if(piece)
          push_scrbitmap(10+col*24, (((piece & 7) < PC_ROOK) ? 2 : 1) + row*24, piece_bitmap(piece), 0);
      }
#endif
      if(!graphics)
      {
        switch(piece & 0x7)
        {
          case 0:
            chr = ' ';
            break;
          case PC_PAWN_UNMOVED: case PC_PAWN_MOVED:
            chr = 'P';
            break;
          case PC_ROOK:
            chr = 'R';
            break;
          case PC_KNIGHT:
            chr = 'H';
            break;
          case PC_BISHOP:
            chr = 'B';
          break;
          case PC_QUEEN:
            chr = 'Q';
            break;
          case PC_KING:
            chr = 'K';
            break;
        }
        if(piece & PC_BLACK)
          chr += 0x20;

        if((selrow == row && selcol == col)
           || (legal_move_count && move_in_list(legal_moves, legal_move_count, row, col)))
        {
          printf("[[%c]]", chr);
        }
        else if((row & 1) ^ (col & 1))
          printf("[ %c ]", chr);
        else printf("] %c [", chr);
      }
    }
    if(!graphics)
      printf("\n");
  }
#if defined(SDLFUNCS) || defined(DOS)
  if(graphics)
  {
    draw_screen_sprites();
    update_screen();
  }
#endif

}
