/*  cards_gl.c  uses xbmread.c to input cards, needs xbm.h */
/*             just a demo, displays, shuffles            */

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

static char *card_files[55] = {
  "d01.xbm","d02.xbm","d03.xbm","d04.xbm","d05.xbm",
  "d06.xbm","d07.xbm","d08.xbm","d09.xbm","d10.xbm",  
  "d11.xbm","d12.xbm","d13.xbm",
  "c01.xbm","c02.xbm","c03.xbm","c04.xbm","c05.xbm",
  "c06.xbm","c07.xbm","c08.xbm","c09.xbm","c10.xbm",
  "c11.xbm","c12.xbm","c13.xbm",
  "h01.xbm","h02.xbm","h03.xbm","h04.xbm","h05.xbm",
  "h06.xbm","h07.xbm","h08.xbm","h09.xbm","h10.xbm",
  "h11.xbm","h12.xbm","h13.xbm",
  "s01.xbm","s02.xbm","s03.xbm","s04.xbm","s05.xbm",
  "s06.xbm","s07.xbm","s08.xbm","s09.xbm","s10.xbm",
  "s11.xbm","s12.xbm","s13.xbm",
  "back.xbm","back1.xbm","joker.xbm"};

static GLubyte rgbpix[55][4*73*97]; /* size for cards*/
static width;                       /* read in */
static height; 
static alpha;
static int RGBA = 0xFF000000;        /* check if black */
static int RGBA_back = 0xFFFFFFFF;   /* white background */
static int RGBA2 = 0xFF0000FF;       /* check if red */
static int RGBA2_back = 0xFFFFFFFF;  /* white background */
static int RGBA3 = 0xFFFF0000;       /* check if blue */
static int RGBA3_back = 0xFFFFFFFF;  /* white background */
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 .xbm 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]); /*back1*/
  glRasterPos2f(150.0, 2.0);
  glDrawPixels(width, height, GL_RGBA, GL_UNSIGNED_BYTE, rgbpix[54]); /*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<55; i++)
  {
    shuf[i] = i; /* for shuffling later */
    strcpy(filename, card_files[i]);
    if(i<13 || (i>25 && i<39))
    {
      printf("test_xbmread calling xbmread on file %s\n", filename);
      status = xbmread(filename, RGBA2, RGBA2_back, &width, &height, rgbpix[i]);
      printf("xbmread returned status=%d, width=%d, height=%d, RGBA2=%X, RGBA2_back=%X \n",
             status, width, height, RGBA2, RGBA2_back);
    }
    else if(i<52)
    {
      printf("test_xbmread calling xbmread on file %s\n", filename);
      status = xbmread(filename, RGBA, RGBA_back, &width, &height, rgbpix[i]);
      printf("xbmread returned status=%d, width=%d, height=%d, RGBA=%X, RGBA_back=%X \n",
             status, width, height, RGBA, RGBA_back);
    }
    else
    {
      printf("test_xbmread calling xbmread on file %s\n", filename);
      status = xbmread(filename, RGBA3, RGBA3_back, &width, &height, rgbpix[i]);
      printf("xbmread returned status=%d, width=%d, height=%d, RGBA3=%X, RGBA3_back=%X \n",
             status, width, height, RGBA3, RGBA3_back);
    }
  }
  /* 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;
}

