/*  card_gl.c  uses gifread.c to input cards, needs gif.h */
/*             just a demo, displays, shuffles            */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <GL/glut.h>

static char *card_files[54] = {
  "ad.gif","2d.gif","3d.gif","4d.gif","5d.gif",
  "6d.gif","7d.gif","8d.gif","9d.gif","td.gif",  
  "jd.gif","qd.gif","kd.gif",
  "ac.gif","2c.gif","3c.gif","4c.gif","5c.gif",
  "6c.gif","7c.gif","8c.gif","9c.gif","tc.gif",
  "jc.gif","qc.gif","kc.gif",
  "ah.gif","2h.gif","3h.gif","4h.gif","5h.gif",
  "6h.gif","7h.gif","8h.gif","9h.gif","th.gif",
  "jh.gif","qh.gif","kh.gif",
  "as.gif","2s.gif","3s.gif","4s.gif","5s.gif",
  "6s.gif","7s.gif","8s.gif","9s.gif","ts.gif",
  "js.gif","qs.gif","ks.gif",
  "back.gif","j.gif"};

static GLubyte rgbpix[54][4*73*97]; /* size for cards*/
static width;                       /* read in */
static height; 
static alpha;
static int shuf[52]; /* 0, 1, 2 is natural order */
static int irnd[52]; /* used to shuffle shuf */

static void printstring(float x, float y, char *string)
{
   int len, i;

   glRasterPos2f(x, y);
   len = (int) strlen(string);
   for (i = 0; i < len; i++)
      glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, string[i]);
} /* end printstring */

void display(void)
{
  int i, j;
  
  /* clear window */
  glClear(GL_COLOR_BUFFER_BIT); 
  glLoadIdentity ();
  glColor3f(0.0, 0.0, 0.0);

  /* draw .gif files */  
  glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  for(j=0; j<4; j++)
  {
    for(i=0; i<13; i++)
    {
      glRasterPos2f(1.0+74.0*i, 402.0-100.0*j);
      glDrawPixels(width, height, GL_RGBA, GL_UNSIGNED_BYTE, rgbpix[shuf[13*j+i]]);
    }
  }
  glRasterPos2f(1.0, 2.0);
  glDrawPixels(width, height, GL_RGBA, GL_UNSIGNED_BYTE, rgbpix[52]); /*back*/
  glRasterPos2f(75.0, 2.0);
  glDrawPixels(width, height, GL_RGBA, GL_UNSIGNED_BYTE, rgbpix[53]); /*joker*/

  printstring(500.0, 10.0, "left mouse click to shuffle");
  glFlush(); 
}

static void mouse(int btn, int state, int x, int y)
{
  int i, j, k;
  
  /* mouse callback, selects an axis about which to rotate */
  if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN)
  {
    for(i=0; i<52; i++) irnd[i] = rand();
    for(i=0; i<51; i++)
    {
      for(j=i+1; j<52; j++)
      {
        if(irnd[i]>irnd[j])
        {
          k=irnd[i];
          irnd[i]=irnd[j];
          irnd[j]=k;
          k=shuf[i];
          shuf[i]=shuf[j];
          shuf[j]=k;
        }
      }
    }
  }
  glutPostRedisplay();
} /* end mouse */

void myreshape(int w, int h)
{
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluOrtho2D(0.0, 964.0, 0.0, 500.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glViewport(0,0,w,h);
}

void init()
{
  int status, i;
  char filename[64];
  for(i=0; i<54; i++)
  {
    shuf[i] = i; /* for shuffling later */
    strcpy(filename, card_files[i]);
    printf("test_gifread calling gifread on file %s\n", filename);
    status = gifread(filename, alpha, &width, &height, rgbpix[i]);
    printf("gifread returned status=%d, width=%d, height=%d, alpha=%d \n",
           status, width, height, alpha);
  }
  /* set clear color to green */
  glClearColor (0.0, 1.0, 0.0, 0.0);
  /* set fill  color to green */
}

int main(int argc, char* argv[])
{
  glutInit(&argc,argv);
  glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);  
  glutInitWindowSize(964,500);
  glutInitWindowPosition(50,50); 
  glutCreateWindow(argv[0]);
  glutReshapeFunc(myreshape);
  glutDisplayFunc(display);
  glutMouseFunc(mouse);
  init();
  glutMainLoop();
  return 0;
}
