// GLUT key handling code

#include "key.h"
#include "view.h"
#include "draw.h"
#include "motion.h"
#include "pngtex.h"

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

// called on any keypress
// 
// We don't use x and y, but they're the mouse location when the key
// was pressed.
extern "C" void
key(unsigned char k, int /*x*/, int /*y*/)
{
    char *image;
    FILE *ifile;

    switch (k) {
    case 't': case 'T': {	// t: switch to teapot
	drawStyle = DRAW_TEAPOT;
	glutPostRedisplay();
	break;
    }
    case 's': case 'S': {	// s: switch to sphere
	drawStyle = DRAW_SPHERE;
	glutPostRedisplay();
	break;
    }
    case 'd': case 'D': {	// d: switch to torus (donut)
	drawStyle = DRAW_DONUT;
	glutPostRedisplay();
	break;
    }
    case 'p': case 'P': {	// p: switch to plane
	drawStyle = DRAW_PLANE;
	glutPostRedisplay();
	break;
    }
    case 'i': case 'I': {	// i: dump image
	dumpPNG("dump.png",0,0,winWidth,winHeight);
	break;
    }
    case '0': case '1': case '2': case '3': case '4':
    case '5': case '6': case '7': case '8': case '9': {
	// numbers adjust frame counters (see draw.h for mapping)
	updateFrame(k-'0');
	break;
    }

    case '=': {		       // unshifted +: increase frame performance
	adjustFrameRate(1);
	break;
    }
    case '-': {		       // unshifted -: decrease frame performance
	adjustFrameRate(-1);
	break;
    }
    case '+': {		       // shifted +: increase latency
	adjustLatency(10);
	break;
    }
    case '_': {		       // shifted -: decrease latency
	adjustLatency(-10);
	break;
    }

    case 27: {			// Escape: exit
	exit(0);
    }
    case '?': {
	printf("Keyboard commands:\n\
what to draw\n\
  t: draw teapot\n\
  s: draw sphere\n\
  d: draw donut (torus)\n\
  p: draw plane\n\
animated color (can be used to animate shaders)\n\
  1/2: decrease/increase rate of change in red\n\
  3/4: decrease/increase rate of change in green\n\
  5/6: decrease/increase rate of change in blue\n\
  7/8: decrease/increase rate of change in alpha\n\
  0: stop changes in all color channels\n\
  9: reset all color channels to 0\n\
demo changes in frame rate or latency\n\
  -/=: increase or decrease frame rate (using a delay in draw)\n\
  _/+: increase or decrease latency (delay response to mouse motion)\n\
other:\n\
  i:   dump image to 'dump.png'\n\
  ESC: quit\n");
	break;
    }
    }
}
