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

void show_message(char *str)
{
#define DIALOG_LEFT 40
#define DIALOG_RIGHT 280

  unsigned l = strlen(str);
  unsigned y = 40;
  unsigned x;
  char *ws;

  if(graphics)
  {
    draw_rectangle(DIALOG_LEFT, 30, DIALOG_RIGHT-DIALOG_LEFT, 10, 4);
    do
    {
      draw_rectangle(DIALOG_LEFT, y, DIALOG_RIGHT-DIALOG_LEFT, 20, 4);
      for(x=50;*str;x+=9,str++)
      {
        ws = strchr(str+1, ' ');
        if(!ws) ws = strchr(str+1, 0);
        if(*str == ' ' && ws && x+((ws - str)*9) >= DIALOG_RIGHT-9)
          break;

        draw_chr(x, y, 0xF, *str);
      }
      y += 10;
    }
    while(*str);
  }
  else
    puts(str);
  update_screen();
}

#endif

char show_dialog(char *str)
{
  char buff[16];
#if defined(SDLFUNCS) || defined(DOS)
  if(graphics)
  {
    show_message(str);
    if(dontstop)
    {
      sleep(1);
      return 13;
    }
    return poll_keycode();
  }
#endif
  puts(str);
  fgets(buff, sizeof(buff), stdin);
  return buff[0];
}

void show_dialog_nokey(char *str)
{
  while(!show_dialog(str));
}

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

struct bitmap bitmap_white_pawn;
struct bitmap bitmap_white_bishop;
struct bitmap bitmap_white_rook;
struct bitmap bitmap_white_knight;
struct bitmap bitmap_white_queen;
struct bitmap bitmap_white_king;

struct bitmap bitmap_black_pawn;
struct bitmap bitmap_black_bishop;
struct bitmap bitmap_black_rook;
struct bitmap bitmap_black_knight;
struct bitmap bitmap_black_queen;
struct bitmap bitmap_black_king;

int load_piece_bitmap(char *filename, struct bitmap *bitmapstruct, unsigned char black)
{
  unsigned char width, height;
  unsigned char n;
  unsigned char color;
  unsigned char a = 0;

  if(load_bitmap(filename, bitmapstruct, &width, &height))
    return 1;

  if(black)
  {
    while(height--)
      for(n=0;n<width;n++)
      {
        switch(recall_pixel(bitmapstruct, n, height))
        {
          case 0xF:
            if(a++ & 1)
              color = 0x7;
            else
              color = 0x8;
            break;
          case 0x7:
              color = 0x0;
            break;
          case 0x8:
            color = 0;
            break;
          default:
            continue;;
        }
        store_pixel(bitmapstruct, n, height, color);
      }
  }

  return 0;
}


int load_sprites(void)
{
  int rc;

  rc = 0;
  rc |= load_piece_bitmap("pawn.spr", &bitmap_white_pawn, 0);
  rc |= load_piece_bitmap("bishop.spr", &bitmap_white_bishop, 0);
  rc |= load_piece_bitmap("rook.spr", &bitmap_white_rook, 0);
  rc |= load_piece_bitmap("knight.spr", &bitmap_white_knight, 0);
  rc |= load_piece_bitmap("queen.spr", &bitmap_white_queen, 0);
  rc |= load_piece_bitmap("king.spr", &bitmap_white_king, 0);

  rc |= load_piece_bitmap("pawn.spr", &bitmap_black_pawn, 1);
  rc |= load_piece_bitmap("bishop.spr", &bitmap_black_bishop, 1);
  rc |= load_piece_bitmap("rook.spr", &bitmap_black_rook, 1);
  rc |= load_piece_bitmap("knight.spr", &bitmap_black_knight, 1);
  rc |= load_piece_bitmap("queen.spr", &bitmap_black_queen, 1);
  rc |= load_piece_bitmap("king.spr", &bitmap_black_king, 1);

  if(rc || load_font("font.fnt"))
    return 1;

  return 0;
}

unsigned char slide_pieces = 1;

struct bitmap *piece_bitmap(unsigned char piece)
{
  if((piece & 7) < PC_ROOK)
    return (piece & PC_BLACK) ? &bitmap_black_pawn : &bitmap_white_pawn;
  else if((piece & 7) == PC_ROOK)
    return (piece & PC_BLACK) ? &bitmap_black_rook : &bitmap_white_rook;
  else if((piece & 7) == PC_BISHOP)
    return (piece & PC_BLACK) ? &bitmap_black_bishop : &bitmap_white_bishop;
  else if((piece & 7) == PC_KNIGHT)
    return (piece & PC_BLACK) ? &bitmap_black_knight : &bitmap_white_knight;
  else if((piece & 7) == PC_QUEEN)
    return (piece & PC_BLACK) ? &bitmap_black_queen : &bitmap_white_queen;
  else if((piece & 7) == PC_KING)
    return (piece & PC_BLACK) ? &bitmap_black_king : &bitmap_white_king;
}

void slide_piece(struct move *move)
{
  unsigned x1 = 10+move->src.col*24;
  unsigned x2 = 10+move->dst.col*24;
  unsigned y1 = 1+move->src.row*24;
  unsigned y2 = 1+move->dst.row*24;
  unsigned x = x1;
  unsigned y = y1;
  unsigned char *ptr;
  unsigned char piece;
  unsigned char piece2;
  struct bitmap *bitmap;
  struct bitmap *captured_bitmap;
  struct board *old_board;

  if(!slide_pieces) return;

  ptr = ptr_to_tile(&board, move->dst.row, move->dst.col);
  piece = *ptr;
  *ptr = 0;
  old_board = &game_status.last_moves.position[(game_status.last_moves.index-1) & LAST_MOVES_MASK].board;

  bitmap = piece_bitmap(piece);
  piece2 = piece_in_tile(old_board, move->dst.row, move->dst.col);
  if(piece2)
    captured_bitmap = piece_bitmap(piece2);
  else
    captured_bitmap = 0;

  while(x != x2 || y != y2)
  {
    if(captured_bitmap)
      push_scrbitmap(x2, y2, captured_bitmap, 1);
    push_scrbitmap(x, y, bitmap, 0);
    draw_board(&board, 0, 0, -1, -1);
    if(x2 < x) x--;
    else if(x2 > x) x++;
    if(y2 < y) y--;
    else if(y2 > y) y++;
  }

  *ptr = piece;
}
#endif
