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

void show_message(char *str)
{
  unsigned l = strlen(str);
  unsigned dialog_top;
  unsigned y;
  unsigned x;
  char *ws;
  unsigned dialog_left;
  unsigned text_left, text_right;
  unsigned dialog_right;
  unsigned char bgcolor;
  unsigned text_color;

  if(graphics)
  {
    if(setup.enable_clippy)
    {
      dialog_left = 30;
      dialog_right = 203;
      dialog_top = 90;
      bgcolor = 0xEF;
      text_color = 0x00;
      draw_bitmap(dialog_right, dialog_top+5, &cliparrw);
    }
    else
    {
      dialog_top = 30;
      dialog_left = 40;
      dialog_right = 280;
      bgcolor = 0x04;
      text_color = 0xFF;
    }
    y = dialog_top + 10;
    text_left = dialog_left + 10;
    text_right = dialog_right - 18;

    draw_rectangle(dialog_left, dialog_top, dialog_right-dialog_left, 10, bgcolor);
    do
    {
      draw_rectangle(dialog_left, y, dialog_right-dialog_left, 20, bgcolor);
      for(x=text_left;*str;x+=9,str++)
      {
        ws = strchr(str+1, ' ');
        if(!ws) ws = strchr(str+1, 0);
        if(*str == ' ')
        {
          if(x == text_left)
            str++;
          else if(ws && (x+(ws - str)*9) >= text_right)
            break;
        }

        draw_chr(x, y, text_color, *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);
  if(dontstop)
    return 13;
  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;
}

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;
}

struct screen_coord piece_coords(unsigned char row, unsigned char col, unsigned char piece);

unsigned char clp_background[4*((50+8)>>3)*100];
unsigned char pc_background[4*((24+8)>>3)*24];

void store_clippyspr(void)
{
  if(setup.enable_clippy)
    get_scrarea(220, 90, 50, 100, clp_background);
}

void restore_clippyspr(void)
{
    if(setup.enable_clippy)
      set_scrarea(220, 90, 50, 100, clp_background);
}

void slide_piece(struct movecoords *move)
{
  struct screen_coord px_coords1;
  struct screen_coord px_coords2;
  struct screen_coord px_coords3;
  unsigned x;
  unsigned y;
  unsigned char *ptr;
  unsigned char piece;
  unsigned char piece2;
  struct bitmap *bitmap;
  struct bitmap *captured_bitmap;
  struct board *old_board;
//  unsigned char pc_background[4*((24+8)>>3)*24];
//  unsigned char clp_background[4*((70+8)>>3)*100];

  if(!setup.slide_pieces || !graphics) 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;

  px_coords1 = piece_coords(move->src.row, move->src.col, piece);
  px_coords2 = piece_coords(move->dst.row, move->dst.col, piece);

  x = px_coords1.x;
  y = px_coords1.y;

  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;

  px_coords3 = piece_coords(move->dst.row, move->dst.col, piece2);

  if(captured_bitmap)
    push_scrbitmap(px_coords3.x, px_coords3.y, captured_bitmap, 1);
  draw_board(&board, 0, 0, 0, 0);
  store_clippyspr();
  while(x != px_coords2.x || y != px_coords2.y)
  {
    get_scrarea(x, y, 24, 24, pc_background);
    push_scrbitmap(x, y, bitmap, 0);
    draw_clipeyes(230, 90, -3+y/24, -2+x/96);
    draw_screen_sprites();
    update_screenr(x-1, y-1, 24, 24, 1);
    if(setup.enable_clippy)
      update_screenr(220, 113, 40, 17, 0);
    set_scrarea(x, y, 24, 24, pc_background);
    restore_clippyspr();
    if(px_coords2.x < x) x--;
    else if(px_coords2.x > x) x++;
    if(px_coords2.y < y) y--;
    else if(px_coords2.y > y) y++;
  }

  *ptr = piece;
  draw_board(&board, 0, 0, 0, 0);
}
#endif
