/***********************************************************************
 * 
 *  G F O ! 
 *  by Chee Chew
 *      and 
 *     Ken Duda 
 *
 *  (c) 1990
 *
 *  This software is free for distribution.  This notice, however, may 
 *  not be altered.
 *  
 *  Enjoy! (and punt... )
 *
 ***********************************************************************/


#include "gfo.h"
#include <string.h>
/* make the TitleScreen pixmap have at least 1*INCREMENT 
   blankspace on either side */

#define INCREMENT 3
#define OFFSET 30

Pixmap animal[10];
int title_end_y;

#define MESSAGE1 "ken and chee's limey" 
#define MESSAGE2 "tom palka's limey horse"
 
animate(XC *xc)
{
  Pixmap pixmap;
  unsigned int w, h;
  int i, error; 

  if ((error = XReadBitmapFile(xc->dsp, xc->mwin, TitleScreen, &w, &h, &pixmap, NULL, NULL))
      != BitmapSuccess) {
    fprintf(stderr,"cant open bitmap, code %d\n", error);
    exit(1);
  }
  BringInTheTitle(xc, pixmap, w,h);
  BringInSpunky(xc, (xc->width-w)/2 - OFFSET, pixmap, w,h); 
  AccreditIt(xc);
  QuoteMeBaby(xc);
  WaitForSpace(xc,pixmap );
}

BringInTheTitle(XC *xc, Pixmap pixmap, int w, int h)
{
  int i; 

  for (i = 0; i < (xc->height-h)/2; i+= INCREMENT ) { 
    MoveTitle(xc, pixmap, w, h, i);
  }
  for (i -= INCREMENT ; i < (xc->height-h)/2; i += 1 ) { 
    MoveTitle(xc, pixmap, w, h, i);
  }
  title_end_y = i - 1;
} 

MoveTitle(XC *xc, Pixmap pixmap,int w, int h, int i)
{
  XCopyPlane(xc->dsp, pixmap, xc->mwin, xc->copy_gc, 0, 0, w, h/2, 
	     (xc->width-w)/2 - OFFSET, i, 1) ; 

  XCopyPlane(xc->dsp, pixmap, xc->mwin, xc->copy_gc, 0, h/2, w, h, 
	     (xc->width-w)/2 - 1, xc->height - i - h/2 - 2, 1) ; 
  XSync(xc->dsp, 0);
}


WaitForSpace(XC *xc, Pixmap pixmap) 
{
  XEvent e;
  int waiting = 1; 
  char buffer[2];

  XCopyArea(xc->dsp, xc->wellpixmap, xc->mwin, xc->copy_gc, 
	    0 ,0, xc->width, xc->height, 0 ,0);
  XSync(xc->dsp, True);
  do {
    XNextEvent (xc->dsp, &e);
    switch ( e.type ) {
    case KeyPress: 
      (void) XLookupString(&e.xkey, buffer, 2, NULL,NULL) ;
      waiting = 0; /*buffer[0] != 32;*/
      break; 
    case Expose: 
      XCopyArea(xc->dsp, xc->wellpixmap, xc->mwin, xc->copy_gc, 
		0 ,0, xc->width, xc->height, 0 ,0);
      break;
    }
  } while (waiting);
  ClearMainWin(xc);
}


BringInSpunky(XC *xc, int x, Pixmap pixmap,unsigned int w, unsigned int h)
{
  unsigned int aw, ah;
  char *animal_name[10] = { 
    Animal1, 
    Animal2,
    Animal3,
    Animal4,
    Animal5,
    Animal6,
    Animal7,
    Animal8,
    Animal9,
    Animal0 
};
  int i;
  for(i = 0; i < 10; i++)
    XReadBitmapFile(xc->dsp, xc->mwin, animal_name[i], &aw, &ah, &animal[i], NULL, NULL);

  RunSpunkyRun(xc, x,pixmap, aw, ah, w, h );
}


RunSpunkyRun(XC *xc, int x, Pixmap pixmap, unsigned int aw, unsigned int ah, unsigned int w, unsigned int h)
{
  int i, j;

  XSynchronize(xc->dsp, True); 
  for(i = 0, j=0; i < (x-aw); j++, i+= INCREMENT) {
    XClearArea(xc->dsp, xc->mwin, i-INCREMENT, (xc->height-ah)/2, INCREMENT, ah, False);
    XCopyPlane(xc->dsp, animal[j % 9], xc->mwin, xc->copy_gc, 0, 0,
	       aw , ah ,  i, (xc->height - ah)/2, 1);
    
  }

  XSynchronize(xc->dsp, False); 

#undef INCREMENT 
#define INCREMENT 1

  for(; i < (x-aw + OFFSET) ; i += INCREMENT, j++) {
    XClearArea(xc->dsp, xc->mwin, i-INCREMENT, (xc->height-ah)/2, 
	       INCREMENT, ah, False);
    XCopyPlane(xc->dsp, animal[j % 9], xc->mwin, xc->copy_gc, 0, 0,
	       aw + 1, ah + 1,  i, (xc->height - ah)/2, 1);

    XCopyPlane(xc->dsp, pixmap, xc->mwin, xc->copy_gc, 0, 0, w, h/2, 
	       i + aw,title_end_y, 1) ; 
    XSync(xc->dsp, 0);
  } 

  /* save it! */ 
  i -= INCREMENT; j--;
  clear_well_pixmap(xc);
  XCopyPlane(xc->dsp, animal[j % 9], xc->wellpixmap, xc->copy_gc, 0, 0,
	     aw + 1, ah + 1,  i, (xc->height - ah)/2, 1);
  XCopyPlane(xc->dsp, pixmap, xc->wellpixmap, xc->copy_gc, 0, 0, w, h, 
	     i + aw,title_end_y, 1) ; 
}

AccreditIt(XC *xc) 
{
  Pixmap newpix;
  
  newpix = XCreatePixmap(xc->dsp, xc->mwin, 300, 100, xc->planes);
  XFillRectangle(xc->dsp, newpix, xc->wellclear, 0, 0, 301, 101);
  DrawRString(xc,newpix,  MESSAGE1, xc->height/4); 

  XFillRectangle(xc->dsp, newpix, xc->wellclear, 0, 0, 301, 101);
  DrawRString(xc,newpix,  MESSAGE2, 2*xc->height/3); 
  XFreePixmap(xc->dsp, newpix);
}

DrawRString(XC *xc, Pixmap newpix, char *msg, int y)
{
  int w, i; 
  XDrawString(xc->dsp, newpix, xc->sdraw_gc, 0, 50, msg, strlen(msg));
  w = XTextWidth(xc->scorefont, msg, strlen(msg)); 
  for(i = 0; i < w; i++)  {
    XCopyArea(xc->dsp, newpix, xc->wellpixmap, xc->copy_gc, i, 0 , 1 , 75, 
	      (xc->width + w)/2 - i, y);
  }
		 
}

#define BUFSIZE 4096
QuoteMeBaby(XC *xc)
{
  int line = 1;
  char buf[BUFSIZE],*temp;
  if(GetQuote(buf)) return;
  temp = buf;
  temp = strtok(buf, "\r\n");
  do {
    XDrawString(xc->dsp, xc->wellpixmap, xc->sdraw_gc, 100, 
		(2*xc->height/3 + 50) + (line * 35),temp, strlen(temp));
    line++;
  }  while(temp = strtok(NULL,"\r\n"));
}

#include<stdio.h>



GetQuote (char *retstr)
{
  FILE *fptr;
  long filesize,pos;
  char buf[BUFSIZE],c;
  int ptr=0,nl=0,displ=0;

  if((fptr=fopen("/afs/sipb/project/gfo/lib/quotes","r"))== NULL)
    {
      perror (" unable to open quotes file");
      exit(-1);
    }
  srandom(time(0));
  fseek(fptr,0L,2);
  filesize = ftell(fptr)-20;
  pos = random() % filesize;
  fseek(fptr,pos,0);
  fread(buf,1,BUFSIZE,fptr);
  while(displ < 2)
    {
      c = buf[ptr++];
      if(displ==1 && c!='\r' && nl==0) {
	*retstr++ = c;
      }
      if(c=='\n') 
	{
	  if(++nl == 2)
	    {
	      nl = 0;
	      ++displ;
	    }
	}
      else 
	if(c!='\r') 
	  nl=0;
      if(ptr>=BUFSIZE) return -1;
    }
  fclose(fptr);
  return 0;
}


  
  
  
  
