#include<stdio.h>
#include<X11/Intrinsic.h>
#include<X11/StringDefs.h>
#include<X11/xpm.h>

#include"koken.h"
#include"block.xpm"
#include"back.xpm"

static Pixmap Block_p,Block_m;
static Pixmap Back_p;

static Window Scr_w;
static Pixmap Scr_p;
static Window Next_w;

static Display *Disp;
static GC GC_nomask,GC_mask;

static int ScreenNo;
static int ScrDepth;

extern int Map[8][13];
extern int Next_color[3];
extern int Next_dir;
extern int Dir_x[4];
extern int Dir_y[4];

void init_graph( Widget scr,Widget next )
{
  int x,y;
  int i;
  Pixmap back;

  Disp = XtDisplay( scr );
  Scr_w = XtWindow( scr );
  Next_w = XtWindow( next );

  ScreenNo = DefaultScreen( Disp );
  ScrDepth = DefaultDepth( Disp,ScreenNo );

  XpmCreatePixmapFromData( Disp,Scr_w,
			  block_xpm,&Block_p,&Block_m,NULL );

  XpmCreatePixmapFromData( Disp,Scr_w,
			  back_xpm,&back,NULL,NULL );

  XtVaSetValues(scr,XtNbackgroundPixmap,back,NULL );
  XFreePixmap( Disp,back );

  GC_nomask = DefaultGC( Disp,ScreenNo );
  GC_mask = XCreateGC( Disp,Scr_w,0,NULL );
  XSetClipMask( Disp,GC_mask,Block_m );
}


void draw_map( int x, int y )
{
  int xx,yy;
  int sx;

  xx = x*32;
  yy = y*32;
  XClearArea( Disp,Scr_w,xx,yy,32,32,False );
  if( (Map[x][y]& 15) != B_NONE ){
    sx = (Map[x][y]&15)*32;
    XSetClipOrigin( Disp,GC_mask,xx-sx,yy );
    XCopyArea( Disp,Block_p,Scr_w,GC_mask,sx,0,32,32,xx,yy );
  }
}


void draw_next( void )
{
  int sx;
  int x0,x1;

  XClearWindow( Disp,Next_w );

  if( Next_dir == 0 )
    return;

  if( Next_dir == -1 ){
    x0 = 32;
    x1 = 0;
  }else{
    x0 = 0;
    x1 = 32;
  }
  sx = Next_color[0] * 32;
  XSetClipOrigin( Disp,GC_mask,x0-sx,32 );
  XCopyArea( Disp,Block_p,Next_w,GC_mask,sx,0,32,32,x0,32 );

  sx = Next_color[1] * 32;
  XSetClipOrigin( Disp,GC_mask,x0-sx,0 );
  XCopyArea( Disp,Block_p,Next_w,GC_mask,sx,0,32,32,x0,0 );

  sx = Next_color[2] * 32;
  XSetClipOrigin( Disp,GC_mask,x1-sx,0 );
  XCopyArea( Disp,Block_p,Next_w,GC_mask,sx,0,32,32,x1,0 );
}

